Skip to content

Instantly share code, notes, and snippets.

@ishaq
Forked from jpwatts/xcode-git-version.sh
Last active October 11, 2019 22:15
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ishaq/4003642 to your computer and use it in GitHub Desktop.
Save ishaq/4003642 to your computer and use it in GitHub Desktop.
This Xcode build phase script automatically sets the version and short version string of an application bundle based on information from the containing Git repository. To Use it, you 1) add the contents of `xcode-git-version.sh` to a "Run Script" build phase for your application target. 2) add a new value to your info plist called `FullVersion` …
#import <Foundation/Foundation.h>
@interface KUtils : NSObject
+ (NSString *)getVersionInfo;
@end
#import "KUtils.h"
@implementation KUtils
+ (NSString *)getVersionInfo
{
// NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
// NSString *shortVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *fullVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"FullVersion"];
NSString *productName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *appVersionInfo = [NSString stringWithFormat:@"%@ version: %@", productName, fullVersion];
return appVersionInfo; // app version would contain something like this "AppName 0.0.0-8-90ee99d-dirty"
}
@end
#!/bin/bash
# This script automatically sets the version and short version string of
# an Xcode project from the Git repository containing the project.
#
# To use this script in Xcode, add the contents to a "Run Script" build
# phase for your application target.
# NOTE: make sure you add a new value to your Info plist called "FullVersion"
#
# NOTE: if you receive error saying 'fatal: Not a git responsitory', make sure |--git-dir| points
# to the root dir of your repo
#
# NOTE: if your git repo has no tags, this would fail, so make sure you create at least one tag
# (I usually create a tag '0.0.0' at the very first commit, you can do the same).
set -o errexit
set -o nounset
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
# Get git tag and hash in the FullVersion
FULL_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" describe --dirty | sed -e 's/^v//' -e 's/g//')
# Use the latest tag for short version (You'll have to make sure that all your tags are of the format 0.0.0,
# this is to satisfy Apple's rule that short version be three integers separated by dots)
# using git tag for version also encourages you to create tags that match your releases
SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" describe --abbrev=0)
# I'd like to use the Git commit hash for CFBundleVersion.
# VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)
# But Apple wants this value to be a monotonically increasing integer, so
# instead use the number of commits on the master branch. If you like to
# play fast and loose with your Git history, this may cause you problems.
# Thanks to @amrox for pointing out the issue and fix.
VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" rev-list master | wc -l)
defaults write "$INFO_PLIST" CFBundleShortVersionString $SHORT_VERSION
defaults write "$INFO_PLIST" CFBundleVersion $VERSION
defaults write "$INFO_PLIST" FullVersion $FULL_VERSION
echo "VERSION: ${VERSION}"
echo "SHORT VERSION: ${SHORT_VERSION}"
echo "FULL VERSION: ${FULL_VERSION}"
@twam
Copy link

twam commented Apr 6, 2013

You'l' never define $KAHAF_VERSION! I guess this should be $FULL_VERSION.

Also you should escape $INFO_PLIST with " as outerwise this will not work with project names which include whitespaces.

@ishaq
Copy link
Author

ishaq commented Nov 20, 2013

@twam, you are right about $KAHAF_VERSION, and thank you for pointing out the issue with $INFO_PLIST, I have fixed it.

(sorry for missing your comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment