Xcode and git – Another build script

stompy » 14 August 2008 » In Mac OS X »

Update: 17Oct09

Since the CFBundleVersion is now presented within brackets, I’ve changed this again.

You can copy and paste the version below, or you can get it directly from Github where it is gist 208825

Update 25Nov08: The script is also available from Codecollector.net here

I’ve rewritten the script from Cocoa Is My Girlfriend in ruby to append the git commit hash into an application’s Info.plist file.

This way, it keeps the version string that is added in Xcode, so the default About panel looks like this:


Here is the script

#!/usr/bin/env ruby

# Xcode auto-versioning script for Subversion by Axel Andersson
# Updated for git by Marcus S. Zarra and Matt Long
# Converted to ruby by Abizer Nasir
# Appends the git sha to the version number set in Xcode.
# see
http://www.stompy.org/2008/08/14/xcode-and-git-another-build-script/ for more details

# These are the common places where git is installed.
# Change this if your path isn’t here
common_git_paths = %w[/usr/local/bin/git /usr/local/git/bin/git /opt/local/bin/git]
git_path = ""

common_git_paths.each do |p|
  if File.exist?(p)
    git_path = p

if git_path == ""
  puts "Path to git not found"

command_line = git_path + " rev-parse –short HEAD"
sha = `#{command_line}`.chomp
puts sha


f = File.open(info_file, "r").read
re = /([\t ]+<key>CFBundleVersion<\/key>\n[\t ]+<string>)(.*?)(<\/string>)/
f =~ re

# Get the version info from the source Info.plist file
# If the script has already been run we need to remove the git sha
# from the bundle’s Info.plist.
open = $1
orig_version = $2
close = $3

# If the git hash has not already been injected into the Info.plist, this will set version to nil
version = $2.sub!(/\s*git sha [\w]+/, )
if (!version)
  version = orig_version

# Inject the git hash into the bundle’s Info.plist
sub = "#{open}#{version}git sha #{sha}#{close}"
puts sub
f.gsub!(re, sub)
File.open(info_file, "w") { |file| file.write(f) }

Make sure that the shell is set to /usr/bin/ruby


Tags: , , , ,

Trackback URL

8 Comments on "Xcode and git – Another build script"

  1. stompy
    18/12/2008 at 10:53 am Permalink

    I modified it a bit. Better to use `which` to grab the path to the `git` executable:


  2. stompy
    Shalonda Koth
    16/02/2011 at 10:49 am Permalink

    My husband and i have been cheerful when Edward could complete his investigations via the precious recommendations he was given through the weblog. It’s not at all simplistic just to possibly be freely giving tactics which many others could have been selling. And we also fully understand we now have you to thank because of that. Those explanations you made, the simple website navigation, the friendships your site give support to engender – it is mostly exceptional, and it’s aiding our son and our family reckon that the subject is brilliant, which is tremendously fundamental. Many thanks for the whole thing!

  3. stompy
    fat loss for idiots scam
    27/02/2013 at 5:07 pm Permalink

    Excellent blog here! Also your website loads up fast!

    What host are you using? Can I get your affiliate link
    to your host? I wish my web site loaded up as quickly as yours lol

  4. stompy
    highland adventures
    05/02/2018 at 4:58 pm Permalink

    It’s an awesome piece of writing designed for all the internet visitors; they will get benefit from it I am sure.


  1. [...] | user-saved public links | iLinkShare 1 votesXcode and git - Another build script>> saved by rizvani2828 1 ...

  2. [...] Courtesy of Abizer at Stompy.org http://www.stompy.org/2008/08/14/xcode-and-git-another-build-script/ Tags: bazaar, build numbers, source subversion, version [...]

  3. [...] illustrate Aaron’s idea, you can also append the git commit hash into an application’s “info” file you are distributing ...

  4. [...] illustrate Aaron’s idea, you can also append the git commit hash into an application’s “info” file you are distributing ...

Hi Stranger, leave a comment:


<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments