2015-02-10 09:19:51 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2015-02-19 05:54:50 +01:00
|
|
|
echo "Setting up git-stats hooks.";
|
|
|
|
|
2015-02-19 14:33:29 +01:00
|
|
|
# Create a new global templatedir if there are none
|
2015-02-19 05:43:03 +01:00
|
|
|
git_templates_dir=$(git config --global --get init.templatedir);
|
|
|
|
if [ $? -ne 0 ]; then
|
2015-02-19 14:33:29 +01:00
|
|
|
git_templates_dir="${HOME}/.git-templates"
|
|
|
|
git config --global init.templatedir "$git_templates_dir"
|
2015-02-19 05:43:03 +01:00
|
|
|
fi
|
2015-02-19 14:33:29 +01:00
|
|
|
|
|
|
|
# Prepare some path variables
|
2015-02-19 05:43:03 +01:00
|
|
|
git_hooks_dir="${git_templates_dir}/hooks"
|
|
|
|
post_commit_path="${git_hooks_dir}/post-commit"
|
2015-02-11 18:33:45 +01:00
|
|
|
|
2015-02-19 14:33:29 +01:00
|
|
|
# Create the hook directory
|
2015-02-19 05:43:03 +01:00
|
|
|
mkdir -p "$git_hooks_dir"
|
2015-02-10 09:19:51 +01:00
|
|
|
|
2015-02-19 14:33:29 +01:00
|
|
|
# Create the post-commit file content
|
2015-02-19 05:43:03 +01:00
|
|
|
hook=$(cat <<EOF
|
2015-02-10 09:19:51 +01:00
|
|
|
|
2015-02-19 05:43:03 +01:00
|
|
|
### git-stats hook (begin) ###
|
2015-02-10 09:19:51 +01:00
|
|
|
# Copy last commit hash to clipboard on commit
|
2015-02-11 18:33:45 +01:00
|
|
|
commit_hash=\`git rev-parse HEAD\`
|
|
|
|
repo_url=\`git config --get remote.origin.url\`
|
|
|
|
commit_date=\`git log -1 --format=%cd\`
|
|
|
|
commit_data="\"{ \"date\": \"\$commit_date\", \"url\": \"\$repo_url\", \"hash\": \"\$commit_hash\" }\""
|
|
|
|
git-stats --record "\$commit_data"
|
2015-02-19 05:43:03 +01:00
|
|
|
### git-stats hook (end) ###
|
2015-02-10 09:19:51 +01:00
|
|
|
EOF
|
2015-02-19 05:43:03 +01:00
|
|
|
);
|
2015-02-11 18:25:44 +01:00
|
|
|
|
2015-02-19 14:33:29 +01:00
|
|
|
# Does post-commit file exist?
|
2015-02-19 05:43:03 +01:00
|
|
|
if [ ! -f "$post_commit_path" ]; then
|
2015-02-19 14:33:29 +01:00
|
|
|
printf "#!/bin/sh\n%s" "$hook" > "$post_commit_path" \
|
|
|
|
&& chmod +x "$post_commit_path" \
|
|
|
|
&& echo "Successfully set up git-stats hook at ${post_commit_path}." \
|
|
|
|
&& exit 0
|
2015-02-19 05:43:03 +01:00
|
|
|
else
|
2015-02-19 14:33:29 +01:00
|
|
|
printf "\n%s\n" "$hook" >> "$post_commit_path" \
|
|
|
|
&& echo "Successfully set up git-stats hook at ${post_commit_path}." \
|
|
|
|
&& exit 0
|
2015-02-19 05:43:03 +01:00
|
|
|
fi
|
2015-02-19 05:54:50 +01:00
|
|
|
|
|
|
|
echo "Couldn't set up git-stats hook."
|
|
|
|
exit 1
|