Add git commit SHA to iOS application Add git commit SHA to iOS application objective-c objective-c

Add git commit SHA to iOS application


Version 2.17. Build a85b242.

If you want to add a pretty versioning like this above, just follow these steps:

  1. Open Build Phases in Xcode
  2. Press Add Build Phase
  3. Press Add Run Script Build Phase. You can find this in the top menu Editor. Drag script-line to the position after Target Dependencies.
  4. Set Shell line to /bin/sh
  5. Set the script below to the Script field. Don't forget to change Sources to your path-to-file, where GitVersion.h should be. For example:

    version=$(git rev-parse --verify HEAD | cut -c 1-7)curdate=$(date +"%d.%m.%y")filesource="//\n//  GitVersion.h\n//\n//  Created by sig on $curdate.\n//\n\n#ifndef GitVersion_h\n#define GitVersion_h\n\n#define GIT_SHA_VERSION @\"$version\"\n\n#endif"cd ${SOURCE_ROOT}/${PROJECT_NAME}echo -e "$filesource" > Sources/GitVersion.htouch Sources/GitVersion.h
  6. Import GitVersion.h file into Xcode project

  7. Paste these lines:

    NSDictionary *info = [[NSBundle mainBundle] infoDictionary];NSString *version = [info objectForKey:@"CFBundleShortVersionString"];NSString *app_version = [NSString stringWithFormat:@"Version %@. Build %@.", version, GIT_SHA_VERSION];NSLog(@"app_version : %@", app_version);

Fully documented answer with images and described advantages could be found here.


You can do it in Schemes. Open your scheme (edit), expand Build in your scheme, click on Pre-Actions, click on + button, select New Run Script Action and write some script which gets SHA and modifies some header file where you can put SHA (easiest way is #define GIT_SHA @"...") and use GIT_SHA in your app in a place where you can display it.


For Swift

Git log format link

Other Swift Flagsenter image description here

Run Script

version=$(git rev-parse --verify HEAD | cut -c 1-10)commitDate=$(git log -n 1 HEAD --pretty=format:"%h - %cd" | cut -c 12-)filesource="//\n//  GitVersion.swift\n//\n//  Commit Date:$commitDate\n//\n\n#if DEBUG\nlet gitVersion = \"$version\"\n#endif"cd ${SOURCE_ROOT}/${PROJECT_NAME}echo -e "$filesource" > GitVersion.swifttouch GitVersion.swift

GitVersion.swift

////  GitVersion.swift////  Commit Date: Tue Jun 19 11:09:55 2018 +0900//#if DEBUGlet gitVersion = "2fd2f0315d"#endif