DEV Community

Cover image for Fetch and display the latest git commit hash in your iOS apps
Tarik Dahic
Tarik Dahic

Posted on • Originally published at tarikdahic.com on

Fetch and display the latest git commit hash in your iOS apps

If you want to know from what exact commit your app is built you can include the latest git commit hash to the place where you display the app version. I like to call that generated hash “revision” or in short “rev”.

Displaying this can be useful when you are distributing internal versions of your apps to QA engineers and you want to know on what app version they are testing. While the changes are coming in on development branches we don’t increase version or build numbers, we follow the revision value and always know what version is in use.

In this article I will explain how to fetch the latest git commit hash in Xcode and display it in the iOS app.

Fetching the latest git commit hash

The procedure I will describe adds a new row to your Info.plist file with specified key and sets the value to the short git commit hash.

The first step is to go to your target’s Build phases setting and add a new run script phase. The “Run script” action will be on the bottom and you need to move it above the “Copy bundle resources” action.

When you move it, you can paste this code for the script:

GIT=`xcrun -find git`
GIT_REV=`${GIT} rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set :REVISION ${GIT_REV}" "${SRCROOT}/{{path-to-your-info.plist-file}}"
Enter fullscreen mode Exit fullscreen mode

In the script above we rely on PlistBuddy to set the key REVISION and the value of the latest git commit hash to the app’s Info.plist file. PlistBuddy is a macOS utility that helps us to manage plist files. You can read more about it here or you can type /usr/libexec/PlistBuddy -h in your terminal to find out more of what it can do.

Now, when you have run the build process, the script that we added should’ve inserted a new entry in your Info.plist file and we can proceed to displaying it.

Displaying the revision

To display the revision we just need to fetch our Info.plist and provide a key to read the value from it:

let revision = Bundle.main.object(forInfoDictionaryKey: "REVISION") as? String 
Enter fullscreen mode Exit fullscreen mode

In Objective-C we would do it like this:

NSString *revision = [NSBundle.mainBundle objectForInfoDictionaryKey:@"REVISION"];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)