App developer

Adding the Git Commit Hash to Your App

When I launch my apps into the App Store, I like to include the git commit hash of that build in the application bundle. It makes it easier for tracking crashes as it can be included in crash reports and in in-app feedback.

My method for including the git hash within my apps is simple:

  • Create a new plist file and call it Versions
  • In Versions.plist add a new Stringkey called Commit
  • Create a new Run Script Build Phase and use the following code:
git=`sh /etc/profile; which git`
gitHash=`$git rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set Commit $gitHash" "${TARGET_BUILD_DIR}/${EXECUTABLE_NAME}.app/Versions.plist"
echo "Updated ${TARGET_BUILD_DIR}/Versions.plist";

Then, if you need to reference the commit, you can access it using the following code:

let commitHash = NSDictionary.init(contentsOfFile: NSBundle.mainBundle().pathForResource("Versions", ofType: "plist")!)!["Commit"]!

Easy. (Look at all those exclamation marks!)