Skip to content

Instantly share code, notes, and snippets.

@karlvr
Forked from nickshanks/xcode-git-version.sh
Last active October 15, 2022 17:36
Show Gist options
  • Select an option

  • Save karlvr/c93a98d7000ecb163895 to your computer and use it in GitHub Desktop.

Select an option

Save karlvr/c93a98d7000ecb163895 to your computer and use it in GitHub Desktop.

Revisions

  1. karlvr revised this gist May 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -72,6 +72,6 @@ echo "MASTER_COMMIT_COUNT: $MASTER_COMMIT_COUNT"
    echo "BRANCH_COMMIT_COUNT: $BRANCH_COMMIT_COUNT"
    echo "BUNDLE_VERSION: $BUNDLE_VERSION"

    /usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFO_PLIST" || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $BUILD_VERSION" "$INFO_PLIST"
    /usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFO_PLIST" 2>/dev/null || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $BUILD_VERSION" "$INFO_PLIST"
    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "$INFO_PLIST"
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUNDLE_VERSION" "$INFO_PLIST"
  2. karlvr revised this gist May 8, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -72,6 +72,6 @@ echo "MASTER_COMMIT_COUNT: $MASTER_COMMIT_COUNT"
    echo "BRANCH_COMMIT_COUNT: $BRANCH_COMMIT_COUNT"
    echo "BUNDLE_VERSION: $BUNDLE_VERSION"

    /usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFOPLIST_FILE" || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $BUILD_VERSION" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUNDLE_VERSION" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFO_PLIST" || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $BUILD_VERSION" "$INFO_PLIST"
    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "$INFO_PLIST"
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUNDLE_VERSION" "$INFO_PLIST"
  3. karlvr revised this gist May 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ BUILD_VERSION=$(git describe --tags --always --dirty=+)

    # Use the latest tag for short version (expected tag format "vn[.n[.n]]")
    # or if there are no tags, we make up version 0.0.<commit count>
    LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null) || LATEST_TAG="HEAD"
    LATEST_TAG=$(git describe --tags --match 'v*' --abbrev=0 2>/dev/null) || LATEST_TAG="HEAD"
    if [ $LATEST_TAG = "HEAD" ]
    then COMMIT_COUNT=$(git rev-list --count HEAD)
    LATEST_TAG="0.0.$COMMIT_COUNT"
  4. karlvr revised this gist May 8, 2014. 1 changed file with 40 additions and 35 deletions.
    75 changes: 40 additions & 35 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -22,51 +22,56 @@ INFO_PLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
    # Build version (closest-tag-or-branch "-" commits-since-tag "-" short-hash dirty-flag)
    BUILD_VERSION=$(git describe --tags --always --dirty=+)

    # Use the latest tag for short version (expected tag format "n[.n[.n]]")
    LATEST_TAG=$(git describe --tags --abbrev=0)
    COMMIT_COUNT_SINCE_TAG=$(git rev-list --count ${LATEST_TAG}..)
    if [ $LATEST_TAG = "start" ]
    then LATEST_TAG=0
    # Use the latest tag for short version (expected tag format "vn[.n[.n]]")
    # or if there are no tags, we make up version 0.0.<commit count>
    LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null) || LATEST_TAG="HEAD"
    if [ $LATEST_TAG = "HEAD" ]
    then COMMIT_COUNT=$(git rev-list --count HEAD)
    LATEST_TAG="0.0.$COMMIT_COUNT"
    COMMIT_COUNT_SINCE_TAG=0
    else
    COMMIT_COUNT_SINCE_TAG=$(git rev-list --count ${LATEST_TAG}..)
    LATEST_TAG=${LATEST_TAG##v} # Remove the "v" from the front of the tag
    fi
    if [ $COMMIT_COUNT_SINCE_TAG = 0 ]; then
    SHORT_VERSION="$LATEST_TAG"
    SHORT_VERSION="$LATEST_TAG"
    else
    # increment final digit of tag and append "d" + commit-count-since-tag
    # e.g. commit after 1.0 is 1.1d1, commit after 1.0.0 is 1.0.1d1
    # this is the bit that requires /bin/bash
    OLD_IFS=$IFS
    IFS="."
    VERSION_PARTS=($LATEST_TAG)
    LAST_PART=$((${#VERSION_PARTS[@]}-1))
    VERSION_PARTS[$LAST_PART]=$((${VERSION_PARTS[${LAST_PART}]}+1))
    SHORT_VERSION="${VERSION_PARTS[*]}d${COMMIT_COUNT_SINCE_TAG}"
    IFS=$OLD_IFS
    # increment final digit of tag and append "d" + commit-count-since-tag
    # e.g. commit after 1.0 is 1.1d1, commit after 1.0.0 is 1.0.1d1
    # this is the bit that requires /bin/bash
    OLD_IFS=$IFS
    IFS="."
    VERSION_PARTS=($LATEST_TAG)
    LAST_PART=$((${#VERSION_PARTS[@]}-1))
    VERSION_PARTS[$LAST_PART]=$((${VERSION_PARTS[${LAST_PART}]}+1))
    SHORT_VERSION="${VERSION_PARTS[*]}d${COMMIT_COUNT_SINCE_TAG}"
    IFS=$OLD_IFS
    fi

    # Bundle version (commits-on-master[-until-branch "." commits-on-branch])
    # Assumes that two release branches will not diverge from the same commit on master.
    if [ $(git rev-parse --abbrev-ref HEAD) = "master" ]; then
    MASTER_COMMIT_COUNT=$(git rev-list --count HEAD)
    BRANCH_COMMIT_COUNT=0
    BUNDLE_VERSION="$MASTER_COMMIT_COUNT"
    MASTER_COMMIT_COUNT=$(git rev-list --count HEAD)
    BRANCH_COMMIT_COUNT=0
    BUNDLE_VERSION="$MASTER_COMMIT_COUNT"
    else
    MASTER_COMMIT_COUNT=$(git rev-list --count $(git rev-list master.. | tail -n 1)^)
    BRANCH_COMMIT_COUNT=$(git rev-list --count master..)
    if [ $BRANCH_COMMIT_COUNT = 0 ]
    MASTER_COMMIT_COUNT=$(git rev-list --count $(git rev-list master.. | tail -n 1)^)
    BRANCH_COMMIT_COUNT=$(git rev-list --count master..)
    if [ $BRANCH_COMMIT_COUNT = 0 ]
    then BUNDLE_VERSION="$MASTER_COMMIT_COUNT"
    else BUNDLE_VERSION="${MASTER_COMMIT_COUNT}.${BRANCH_COMMIT_COUNT}"
    fi
    fi
    fi

    defaults write "$INFO_PLIST" CFBundleBuildVersion "$BUILD_VERSION"
    defaults write "$INFO_PLIST" CFBundleShortVersionString "$SHORT_VERSION"
    defaults write "$INFO_PLIST" CFBundleVersion "$BUNDLE_VERSION"


    # For debugging:
    #echo "BUILD VERSION: $BUILD_VERSION"
    #echo "LATEST_TAG: $LATEST_TAG"
    #echo "COMMIT_COUNT_SINCE_TAG: $COMMIT_COUNT_SINCE_TAG"
    #echo "SHORT VERSION: $SHORT_VERSION"
    #echo "MASTER_COMMIT_COUNT: $MASTER_COMMIT_COUNT"
    #echo "BRANCH_COMMIT_COUNT: $BRANCH_COMMIT_COUNT"
    #echo "BUNDLE_VERSION: $BUNDLE_VERSION"
    echo "BUILD VERSION: $BUILD_VERSION"
    echo "LATEST_TAG: $LATEST_TAG"
    echo "COMMIT_COUNT_SINCE_TAG: $COMMIT_COUNT_SINCE_TAG"
    echo "SHORT VERSION: $SHORT_VERSION"
    echo "MASTER_COMMIT_COUNT: $MASTER_COMMIT_COUNT"
    echo "BRANCH_COMMIT_COUNT: $BRANCH_COMMIT_COUNT"
    echo "BUNDLE_VERSION: $BUNDLE_VERSION"

    /usr/libexec/PlistBuddy -c "Add :CFBundleBuildVersion string $BUILD_VERSION" "$INFOPLIST_FILE" || /usr/libexec/PlistBuddy -c "Set :CFBundleBuildVersion $BUILD_VERSION" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $SHORT_VERSION" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUNDLE_VERSION" "$INFOPLIST_FILE"
  5. @nickshanks nickshanks revised this gist Feb 19, 2014. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/sh
    #!/bin/bash

    # This script automatically sets the version and short version string of
    # an Xcode project from the Git repository containing the project.
    @@ -28,12 +28,22 @@ COMMIT_COUNT_SINCE_TAG=$(git rev-list --count ${LATEST_TAG}..)
    if [ $LATEST_TAG = "start" ]
    then LATEST_TAG=0
    fi
    if [ $COMMIT_COUNT_SINCE_TAG = 0 ]
    then SHORT_VERSION="$LATEST_TAG"
    else SHORT_VERSION="${LATEST_TAG}d${COMMIT_COUNT_SINCE_TAG}"
    if [ $COMMIT_COUNT_SINCE_TAG = 0 ]; then
    SHORT_VERSION="$LATEST_TAG"
    else
    # increment final digit of tag and append "d" + commit-count-since-tag
    # e.g. commit after 1.0 is 1.1d1, commit after 1.0.0 is 1.0.1d1
    # this is the bit that requires /bin/bash
    OLD_IFS=$IFS
    IFS="."
    VERSION_PARTS=($LATEST_TAG)
    LAST_PART=$((${#VERSION_PARTS[@]}-1))
    VERSION_PARTS[$LAST_PART]=$((${VERSION_PARTS[${LAST_PART}]}+1))
    SHORT_VERSION="${VERSION_PARTS[*]}d${COMMIT_COUNT_SINCE_TAG}"
    IFS=$OLD_IFS
    fi

    # Bundle version (commits-on-master-until-branch "." commits-on-branch)
    # Bundle version (commits-on-master[-until-branch "." commits-on-branch])
    # Assumes that two release branches will not diverge from the same commit on master.
    if [ $(git rev-parse --abbrev-ref HEAD) = "master" ]; then
    MASTER_COMMIT_COUNT=$(git rev-list --count HEAD)
  6. @nickshanks nickshanks revised this gist Feb 17, 2014. 1 changed file with 49 additions and 15 deletions.
    64 changes: 49 additions & 15 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,62 @@
    #!/bin/bash
    #!/bin/sh

    # 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 4, add the contents to a "Run Script" build
    # To use this script in Xcode, add the script's path to a "Run Script" build
    # phase for your application target.

    set -o errexit
    set -o nounset

    INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
    # First, check for git in $PATH
    hash git 2>/dev/null || { echo >&2 "Git required, not installed. Aborting build number update script."; exit 0; }

    # Use the latest version tag for CFBundleShortVersionString. I tag releases
    # in Git using the format v0.0.0; this assumes you're doing the same.
    SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" describe --dirty | sed -e 's/^v//' -e 's/g//')
    # Alternatively, we could use Xcode's copy of the Git binary,
    # but old Xcodes don't have this.
    #GIT=$(xcrun -find git)

    # 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)
    # Run Script build phases that operate on product files of the target that defines them should use the value of this build setting [TARGET_BUILD_DIR]. But Run Script build phases that operate on product files of other targets should use “BUILT_PRODUCTS_DIR” instead.
    INFO_PLIST="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"

    # 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)
    # Build version (closest-tag-or-branch "-" commits-since-tag "-" short-hash dirty-flag)
    BUILD_VERSION=$(git describe --tags --always --dirty=+)

    defaults write $INFO_PLIST CFBundleShortVersionString $SHORT_VERSION
    defaults write $INFO_PLIST CFBundleVersion $VERSION
    # Use the latest tag for short version (expected tag format "n[.n[.n]]")
    LATEST_TAG=$(git describe --tags --abbrev=0)
    COMMIT_COUNT_SINCE_TAG=$(git rev-list --count ${LATEST_TAG}..)
    if [ $LATEST_TAG = "start" ]
    then LATEST_TAG=0
    fi
    if [ $COMMIT_COUNT_SINCE_TAG = 0 ]
    then SHORT_VERSION="$LATEST_TAG"
    else SHORT_VERSION="${LATEST_TAG}d${COMMIT_COUNT_SINCE_TAG}"
    fi

    # Bundle version (commits-on-master-until-branch "." commits-on-branch)
    # Assumes that two release branches will not diverge from the same commit on master.
    if [ $(git rev-parse --abbrev-ref HEAD) = "master" ]; then
    MASTER_COMMIT_COUNT=$(git rev-list --count HEAD)
    BRANCH_COMMIT_COUNT=0
    BUNDLE_VERSION="$MASTER_COMMIT_COUNT"
    else
    MASTER_COMMIT_COUNT=$(git rev-list --count $(git rev-list master.. | tail -n 1)^)
    BRANCH_COMMIT_COUNT=$(git rev-list --count master..)
    if [ $BRANCH_COMMIT_COUNT = 0 ]
    then BUNDLE_VERSION="$MASTER_COMMIT_COUNT"
    else BUNDLE_VERSION="${MASTER_COMMIT_COUNT}.${BRANCH_COMMIT_COUNT}"
    fi
    fi

    defaults write "$INFO_PLIST" CFBundleBuildVersion "$BUILD_VERSION"
    defaults write "$INFO_PLIST" CFBundleShortVersionString "$SHORT_VERSION"
    defaults write "$INFO_PLIST" CFBundleVersion "$BUNDLE_VERSION"

    # For debugging:
    #echo "BUILD VERSION: $BUILD_VERSION"
    #echo "LATEST_TAG: $LATEST_TAG"
    #echo "COMMIT_COUNT_SINCE_TAG: $COMMIT_COUNT_SINCE_TAG"
    #echo "SHORT VERSION: $SHORT_VERSION"
    #echo "MASTER_COMMIT_COUNT: $MASTER_COMMIT_COUNT"
    #echo "BRANCH_COMMIT_COUNT: $BRANCH_COMMIT_COUNT"
    #echo "BUNDLE_VERSION: $BUNDLE_VERSION"
  7. @jpwatts jpwatts revised this gist May 13, 2011. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,26 @@
    # This script automatically sets the version and short version string of
    # an Xcode project from the Git repository containing the project.
    #
    # I tag releases in Git using the format v0.0[.0] and this script assumes
    # you're doing the same. If you're using a different tag format, you'll
    # need to adjust how $SHORT_VERSION is created.
    #
    # To use this script in Xcode 4, add the contents to a "Run Script" build
    # phase for your application target.

    set -o errexit
    set -o nounset

    INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"

    # Use the latest version tag for CFBundleShortVersionString. I tag releases
    # in Git using the format v0.0.0; this assumes you're doing the same.
    SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" describe --dirty | sed -e 's/^v//' -e 's/g//')
    VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)

    # 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
  8. @jpwatts jpwatts created this gist May 11, 2011.
    21 changes: 21 additions & 0 deletions xcode-git-version.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/bin/bash

    # This script automatically sets the version and short version string of
    # an Xcode project from the Git repository containing the project.
    #
    # I tag releases in Git using the format v0.0[.0] and this script assumes
    # you're doing the same. If you're using a different tag format, you'll
    # need to adjust how $SHORT_VERSION is created.
    #
    # To use this script in Xcode 4, add the contents to a "Run Script" build
    # phase for your application target.

    set -o errexit
    set -o nounset

    INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
    SHORT_VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" describe --dirty | sed -e 's/^v//' -e 's/g//')
    VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}" rev-parse --short HEAD)

    defaults write $INFO_PLIST CFBundleShortVersionString $SHORT_VERSION
    defaults write $INFO_PLIST CFBundleVersion $VERSION