How to get SHA of commit for Xcode Bot "Run Script" Trigger? Updating status of tests on Github How to get SHA of commit for Xcode Bot "Run Script" Trigger? Updating status of tests on Github git git

How to get SHA of commit for Xcode Bot "Run Script" Trigger? Updating status of tests on Github


I am using this code in my Xcode Bot triggers to get the SHA of the commit:

git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse HEAD

And this to get the branch name:

git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse --abbrev-ref HEAD

This executes a git command in the source directory, replace "name_of_your_git_repo" by the name of your repository on git


XCS_OUTPUT_DIR has a file called sourceControl.log. This file has logs like the following:

"DVTSourceControlLocationRevisionKey" : "3787c0d9e5107861a8b8d4c7300b2d414ad41dbb",

You can parse that log to find the SHA.

Perhaps more practically, CaveJohnson can pull the SHA:

PATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATHSHA=`cavejohnson getSha`

Or it can just go ahead and set the GitHub status as a one-liner:

#!/bin/bashPATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATHcavejohnson setGithubStatus

Notably, there are more statuses than just success and failure, there are at least 6 that I'm aware of. You can read more about them in my Xcode 6 CI Missing Manual.


Using the cavejohnson code in the other answer, which gets the hash from certain keys an Xcode log, I ran into an issue where the returned hash was an outdated one from the last build.

I'm now instead using git rev-parse HEAD to get the hash of the commit that was actually used in the CI build. I've submitted this as a revision to cavejohnson.

Use get_sha() to retrieve the SHA-1 hash:

def get_sha():    return get_repo_sha(get_git_directory())def get_git_directory():    for subdir in os.listdir('.'):        if is_git_directory(subdir):            return subdir    assert Falsedef is_git_directory(path = '.'):    return subprocess.call(['git', '-C', path, 'status'], stderr=subprocess.STDOUT, stdout = open(os.devnull, 'w')) == 0    def get_repo_sha(repo):    sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=repo).decode('ascii').strip()    return sha