iosxcode

How can I include the git commit hash in Xcode?


I have an application, which people can report bugs directly from it, but I'd like for the user to be able to submit what git hash the application was built on. Does Xcode expose a #define which will include this information or would I have to include it in some custom build script?


Solution

  • I've written an implementation based on the answer cited by gagarwal. I added this build script to my build phases before the compilation phase:

    /usr/libexec/PlistBuddy -c "Set :GIT_COMMIT_HASH `git rev-parse HEAD`" "${TARGET_BUILD_DIR}"/"${INFOPLIST_PATH}"
    

    In my code I reference it by calling:

    [[NSBundle mainBundle] infoDictionary][@"GIT_COMMIT_HASH"];
    

    And voila, your last commit hash value is available at run-time!