From 0bd6180a0b2cb6d5059b5c3283eace43d376fdf1 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 19 Feb 2015 05:43:03 +0100 Subject: [PATCH 1/7] Prevent word splitting. Re-use previous git configurations and hooks rather than overwriting them. --- scripts/init-git-post-commit | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index 88a0875..ebf3d9e 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -1,21 +1,32 @@ #!/bin/sh -git_templates_dir="$HOME/.git-templates" -git_hooks_dir="$git_templates_dir/hooks" -post_commit_path="$git_hooks_dir/post-commit" +git_templates_dir=$(git config --global --get init.templatedir); +if [ $? -ne 0 ]; then + # Create a new global templatedir if there are none + git_templates_dir="${HOME}/.git-templates" + git config --global init.templatedir "$git_templates_dir" +fi +git_hooks_dir="${git_templates_dir}/hooks" +post_commit_path="${git_hooks_dir}/post-commit" -git config --global init.templatedir $git_templates_dir -mkdir -p $git_hooks_dir +mkdir -p "$git_hooks_dir" -cat << EOF > $post_commit_path -#!/bin/sh +hook=$(cat < "$post_commit_path" + chmod +x "$post_commit_path" +else + printf "\n%s\n" "$hook" >> "$post_commit_path" +fi From dea9adc440b0c014a0727e803b3a8663e1929a6c Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 19 Feb 2015 05:54:50 +0100 Subject: [PATCH 2/7] Add feedback output to git hook init script. --- package.json | 3 ++- scripts/init-git-post-commit | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index eb54986..bb35516 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ }, "author": "Ionică Bizău ", "contributors": [ - "Gnab " + "Gnab ", + "William Boman " ], "license": "MIT", "devDependencies": {}, diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index ebf3d9e..6c3b416 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -1,5 +1,7 @@ #!/bin/sh +echo "Setting up git-stats hooks."; + git_templates_dir=$(git config --global --get init.templatedir); if [ $? -ne 0 ]; then # Create a new global templatedir if there are none @@ -25,8 +27,15 @@ EOF ); if [ ! -f "$post_commit_path" ]; then - printf "#!/bin/sh\n%s" "$hook" > "$post_commit_path" - chmod +x "$post_commit_path" + 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 else - printf "\n%s\n" "$hook" >> "$post_commit_path" + printf "\n%s\n" "$hook" >> "$post_commit_path" \ + && echo "Successfully set up git-stats hook at ${post_commit_path}." \ + && exit 0 fi + +echo "Couldn't set up git-stats hook." +exit 1 From 93be56019c8f86052ca64fae5db2bf5ca6c067e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Thu, 19 Feb 2015 15:33:29 +0200 Subject: [PATCH 3/7] Added comments --- scripts/init-git-post-commit | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index 6c3b416..512a797 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -2,17 +2,21 @@ echo "Setting up git-stats hooks."; +# Create a new global templatedir if there are none git_templates_dir=$(git config --global --get init.templatedir); if [ $? -ne 0 ]; then - # Create a new global templatedir if there are none - git_templates_dir="${HOME}/.git-templates" - git config --global init.templatedir "$git_templates_dir" + git_templates_dir="${HOME}/.git-templates" + git config --global init.templatedir "$git_templates_dir" fi + +# Prepare some path variables git_hooks_dir="${git_templates_dir}/hooks" post_commit_path="${git_hooks_dir}/post-commit" +# Create the hook directory mkdir -p "$git_hooks_dir" +# Create the post-commit file content hook=$(cat < "$post_commit_path" \ - && chmod +x "$post_commit_path" \ - && echo "Successfully set up git-stats hook at ${post_commit_path}." \ - && exit 0 + 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 else - printf "\n%s\n" "$hook" >> "$post_commit_path" \ - && echo "Successfully set up git-stats hook at ${post_commit_path}." \ - && exit 0 + printf "\n%s\n" "$hook" >> "$post_commit_path" \ + && echo "Successfully set up git-stats hook at ${post_commit_path}." \ + && exit 0 fi echo "Couldn't set up git-stats hook." From 84626341beec9c2b4407ca87929181b25b7e7a65 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 19 Feb 2015 05:54:50 +0100 Subject: [PATCH 4/7] Add feedback output to git hook init script. --- package.json | 3 ++- scripts/init-git-post-commit | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index eb54986..bb35516 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ }, "author": "Ionică Bizău ", "contributors": [ - "Gnab " + "Gnab ", + "William Boman " ], "license": "MIT", "devDependencies": {}, diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index ebf3d9e..428cd2c 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -1,10 +1,12 @@ #!/bin/sh +echo "Setting up git-stats hooks."; + git_templates_dir=$(git config --global --get init.templatedir); if [ $? -ne 0 ]; then # Create a new global templatedir if there are none git_templates_dir="${HOME}/.git-templates" - git config --global init.templatedir "$git_templates_dir" + git config --global init.templatedir "$git_templates_dir" && echo "Set new global git template dir at ${git_templates_dir}" fi git_hooks_dir="${git_templates_dir}/hooks" post_commit_path="${git_hooks_dir}/post-commit" @@ -25,8 +27,15 @@ EOF ); if [ ! -f "$post_commit_path" ]; then - printf "#!/bin/sh\n%s" "$hook" > "$post_commit_path" - chmod +x "$post_commit_path" + 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 else - printf "\n%s\n" "$hook" >> "$post_commit_path" + printf "\n%s\n" "$hook" >> "$post_commit_path" \ + && echo "Successfully set up git-stats hook at ${post_commit_path}." \ + && exit 0 fi + +echo "Couldn't set up git-stats hook." +exit 1 From 3800e2487063f9e4f53282eaa4f80ada8787e257 Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 20 Feb 2015 00:11:36 +0100 Subject: [PATCH 5/7] Don't use backticks. --- scripts/init-git-post-commit | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index 428cd2c..a174581 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -17,9 +17,9 @@ hook=$(cat < Date: Fri, 20 Feb 2015 00:12:55 +0100 Subject: [PATCH 6/7] Now ensures the user has required binaries before trying to set git hook. --- scripts/init-git-post-commit | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index a174581..8345dec 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -1,5 +1,13 @@ #!/bin/sh +check_command() { + command -v "$1" > /dev/null 2>&1 || { echo "git-stats hook script requires the \`${1}\` binary in order to run." >&2; exit 1; } +} + +check_command "perl" +check_command "printf" +check_command "git" + echo "Setting up git-stats hooks."; git_templates_dir=$(git config --global --get init.templatedir); From bd763d8fe832771ab225653255e93008d5e78097 Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 20 Feb 2015 00:57:46 +0100 Subject: [PATCH 7/7] Remove any previous git-stats code blocks in pre-existing git hook files. --- scripts/init-git-post-commit | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/init-git-post-commit b/scripts/init-git-post-commit index 8345dec..a21d6c2 100755 --- a/scripts/init-git-post-commit +++ b/scripts/init-git-post-commit @@ -22,14 +22,13 @@ post_commit_path="${git_hooks_dir}/post-commit" mkdir -p "$git_hooks_dir" hook=$(cat <> "$post_commit_path" \ && echo "Successfully set up git-stats hook at ${post_commit_path}." \ && exit 0