Clicky

iOS Dev Nugget 282 Update to My Git no-commit Pre-commit Hook

.

Need to run a code review on your codebase? Hire me

Wanted to share the updated version of my typical pre-commit hook since 2 years ago. It's better at only picking up additions rather than diffs (additions + removals).

I've also included checks for prints and NSLogs.

Sometimes I work on projects in a team and want to make sure that I don't accidentally check in changes to .xcodeproj, so I have a check for that too.

My commit workflow then includes commenting and uncommenting out section of this pre-commit file while I hit warnings and comparing it with the changes I have in mind. Finally I'll just do a git commit -m "some message" -n (with the additional -n switch) to commit, bypassing the precommit hook.

#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1; then  
  against=HEAD
else  
  # Initial commit: diff against an empty tree object
  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

exec 1>&2

#Workaround for filenames with spaces in them (because for loop breaks use space by default)
SAVEIFS=$IFS
IFS=$(echo "\n\b")

for ENTRY in `git diff-index --cached --name-status $against -- | cut -c1,3-`; do  
  CHANGE=`echo "$ENTRY" | cut -c1`
  FILE=`echo "$ENTRY" | cut -c2-`
  if [ $CHANGE = "A" ] || [ $CHANGE = "M" ]; then
      if [ ${FILE: -26} == ".xcodeproj/project.pbxproj" ]; then
          echo "$FILE" 'contains .xcodeproj/project.pbxproj! Are you sure?'
          IFS=$SAVEIFS
          exit 1
      fi
      if [ ${FILE: -6} == ".swift" ] || [ ${FILE: -2} == ".m" ] || [ ${FILE: -2} == ".h" ]; then
          if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q '\t'; then
              echo "$FILE" 'contains tabs!'
              IFS=$SAVEIFS
              exit 1
          fi
      fi
      if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q 'kkk'; then
          echo "$FILE" 'contains kkk!'
          IFS=$SAVEIFS
          exit 1
      fi
      if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q ' NSLog'; then
          echo "$FILE" 'contains NSLog!'
          IFS=$SAVEIFS
          exit 1
      fi
      if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q ' print'; then
          echo "$FILE" 'contains print!'
          IFS=$SAVEIFS
          exit 1
      fi
      if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q ' dump'; then
          echo "$FILE" 'contains dump!'
          IFS=$SAVEIFS
          exit 1
      fi
      if git diff --cached --color=always "$FILE" | perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' | grep -q '007'; then
          echo "$FILE" 'contains 007!'
          IFS=$SAVEIFS
          exit 1
      fi
  fi
done

IFS=$SAVEIFS
exit 0

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues