Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include the git commit hash in Xcode?

Tags:

xcode

ios

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?

like image 477
Ryan Dignard Avatar asked Oct 22 '14 18:10

Ryan Dignard


People also ask

How do I get commit hash?

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.

How does git create commit hash?

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.

What is hash of commit?

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.


2 Answers

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!

like image 92
Ryan Dignard Avatar answered Sep 20 '22 01:09

Ryan Dignard


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")
like image 37
CodingLamb Avatar answered Sep 22 '22 01:09

CodingLamb