Paul Calnan's Blog
Published May 10, 2020

I'm working on a side project that has required some on-device troubleshooting. I find myself loading a build on my phone and using it for several days. There's been a lot of trial-and-error debugging in this project, so I often run code from an experimental branch for extended periods of time. It's easy to lose track of what source configuration I have loaded on my phone at any given time.

I wanted to get a way to get the current Git revision and branch into the app. I figured a good start would be the following:

I wanted to get this information into each build, not just release builds. Getting this information from a shell script into a build is tricky. One of the problems is that you can't modify the working copy. Doing so would change the "is clean" flag. The build would always show dirty and a commit would be needed to clean it, changing the commit hash.

The approach I settled on was to write an untracked Swift file as part of the build process.

First, add generate-git-revision.sh (see below) to your project's bin directory (or wherever you keep your project's scripts).

#!/usr/bin/env bash

set -euo pipefail

cat <<SWIFT
//
//  GitRevision.swift
//
//  This file was automatically generated. Do not edit manually. It will be overwritten on every build.
//
//  IMPORTANT: This file must be added to .gitignore or it will dirty the working tree every time there is a build.
//

/// Information about the current Git revision.
struct GitRevision {

    /// The name of the current branch.
    static let branch = "$(git rev-parse --abbrev-ref HEAD)"

    /// The abbreviated hash of the current commit.
    static let commit = "$(git rev-parse --short HEAD)"

    /// Whether the working tree is clean. If false, indicates that there are uncommitted or untracked files in the working tree.
    static let clean = $([[ -z $(git status -s) ]] && echo 'true' || echo 'false')

    /// Build timestamp. Set when this file was generated at the beginning of the build process.
    static let timestamp = "$(date +%Y-%m-%dT%H:%M:%S)"

    /// A string summarizing the current Git revision.
    static var description: String {
        return "\(commit)\(clean ? "" : "-dirty") \(branch) \(timestamp)"
    }
}
SWIFT

Next, in Xcode add a new Run Script build phase at the top of the list of Build Phases (directly under Dependencies). Call the new build phase Generate GitRevision.swift. Make the body of the build phase script:

"${PROJECT_DIR}/path/to/generate-git-revision.sh" > "${PROJECT_DIR}/path/to/GitRevision.swift"

Build the project. This causes the script to run and generate GitRevision.swift.

Now, add the new GitRevision.swift file to the project. Also add GitRevision.swift to the project's .gitignore file.

That's it. The generated GitRevision.swift file will look something like this:

//
//  GitRevision.swift
//
//  This file was automatically generated. Do not edit manually. It will be overwritten on every build.
//
//  IMPORTANT: This file must be added to .gitignore or it will dirty the working tree every time there is a build.
//

/// Information about the current Git revision.
struct GitRevision {

    /// The name of the current branch.
    static let branch = "development"

    /// The abbreviated hash of the current commit.
    static let commit = "3c747d5"

    /// Whether the working tree is clean. If false, indicates that there are uncommitted or untracked files in the working tree.
    static let clean = false

    /// Build timestamp. Set when this file was generated at the beginning of the build process.
    static let timestamp = "2020-05-10T23:03:15"

    /// A string summarizing the current Git revision.
    static var description: String {
        return "\(commit)\(clean ? "" : "-dirty") \(branch) \(timestamp)"
    }
}

If you leave that file open in Xcode when you build, you can see the property values change as the file gets regenerated.

You can now reference GitRevision.description from your app's code to show the commit, branch, and build time.

Published May 10, 2020

I can never remember the commands to delete a Git tag. It's one of those things I don't do frequently enough to remember — I always have to look it up.

To delete a local tag called TAGNAME:

$ git tag --delete TAGNAME

To delete a remote tag called TAGNAME:

$ git push --delete origin TAGNAME

But rather than have to enter in two commands, I wrote a script that combines them:

#!/usr/bin/env bash

set -euo pipefail

E_BADARGS=85
if [ $# -ne 1 ]; then
    echo "Usage: $(basename $0) tag-name"
    exit $E_BADARGS
fi

tagname="$1"

# Delete the local tag
echo "Deleting local tag $tagname"
git tag --delete $tagname

# Delete the remote tag
echo "Deleting remote tag $tagname"
git push --delete origin $tagname

That script is saved in my ~/bin directory under the name git-rmtag. Using that naming convention, you can run it like this:

$ git rmtag TAGNAME

and it will delete the local and remote tags.