Skip to content

Commit 204ef93

Browse files
committed
Merge branch 'patch-1' of https://github.com/williamboman/git-stats into williamboman-patch-1
Conflicts: scripts/init-git-post-commit
2 parents 93be560 + bd763d8 commit 204ef93

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

scripts/init-git-post-commit

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
#!/bin/sh
22

3+
# Check dependencies
4+
check_command() {
5+
command -v "$1" > /dev/null 2>&1 || { echo "git-stats hook script requires the \`${1}\` binary in order to run." >&2; exit 1; }
6+
}
7+
8+
check_command "perl"
9+
check_command "printf"
10+
check_command "git"
11+
312
echo "Setting up git-stats hooks.";
413

514
# Create a new global templatedir if there are none
615
git_templates_dir=$(git config --global --get init.templatedir);
716
if [ $? -ne 0 ]; then
17+
# Create a new global templatedir if there are none
818
git_templates_dir="${HOME}/.git-templates"
9-
git config --global init.templatedir "$git_templates_dir"
19+
git config --global init.templatedir "$git_templates_dir" && echo "Set new global git template dir at ${git_templates_dir}"
1020
fi
11-
12-
# Prepare some path variables
1321
git_hooks_dir="${git_templates_dir}/hooks"
1422
post_commit_path="${git_hooks_dir}/post-commit"
1523

16-
# Create the hook directory
1724
mkdir -p "$git_hooks_dir"
1825

1926
# Create the post-commit file content
2027
hook=$(cat <<EOF
21-
2228
### git-stats hook (begin) ###
2329
# Copy last commit hash to clipboard on commit
24-
commit_hash=\`git rev-parse HEAD\`
25-
repo_url=\`git config --get remote.origin.url\`
26-
commit_date=\`git log -1 --format=%cd\`
30+
commit_hash=\$(git rev-parse HEAD)
31+
repo_url=\$(git config --get remote.origin.url)
32+
commit_date=\$(git log -1 --format=%cd)
2733
commit_data="\"{ \"date\": \"\$commit_date\", \"url\": \"\$repo_url\", \"hash\": \"\$commit_hash\" }\""
28-
git-stats --record "\$commit_data"
34+
git-stats --record "\${commit_data}"
2935
### git-stats hook (end) ###
3036
EOF
3137
);
3238

33-
# Does post-commit file exist?
3439
if [ ! -f "$post_commit_path" ]; then
3540
printf "#!/bin/sh\n%s" "$hook" > "$post_commit_path" \
3641
&& chmod +x "$post_commit_path" \
3742
&& echo "Successfully set up git-stats hook at ${post_commit_path}." \
3843
&& exit 0
3944
else
40-
printf "\n%s\n" "$hook" >> "$post_commit_path" \
45+
# Remove any previous git-stats hook code blocks
46+
perl -i -0pe 's/(([ \t]*# Copy last commit hash to clipboard on commit\s*(\n.*){5}\s*git-stats --record "\$commit_data"\s*)|([ \t]*### git-stats hook \(begin\) ###\s*(\n.*){7}\s*### git-stats hook \(end\) ###\s*))//g' "$post_commit_path"
47+
printf "%s\n" "$hook" >> "$post_commit_path" \
4148
&& echo "Successfully set up git-stats hook at ${post_commit_path}." \
4249
&& exit 0
4350
fi

0 commit comments

Comments
 (0)