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?
We can obtain the hash using rev-parse . We can add the --verify flag to rev-parse to ensure that the specified object is a valid git object. It's especially help to use it --verify with a variable branch name. To obtain the shortened version of the hash, we can use the --short flag.
Git relies on the collision-resistance of SHA-1. (In practice, if you encounter such a collision, which I believe has never occurred outside of intentional attempts to create collisions), you can just add a space to the end of the commit message, which will generate a new hash.
2.3 Commit Hashes In computer science, a hash function is a many-to-one function that takes lengthy input, massages it, and produces vastly shorter output of a fixed length, called the hash . While the output looks random, the function has no randomness, and the actual function is well-known to everyone.
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!
This wasn't working for me on Xcode 12. I placed the following in the Build Phases step as mentioned above after putting GIT_COMMIT_HASH
in the Info.plist
file:
COMMIT_HASH=`git rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set :GIT_COMMIT_HASH $COMMIT_HASH" "${INFOPLIST_FILE}"
Then you can access it in Swift with:
Bundle.main.object(forInfoDictionaryKey: "GIT_COMMIT_HASH")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With