Skip to content

Instantly share code, notes, and snippets.

@hhallman
Last active May 24, 2018 08:53
Show Gist options
  • Save hhallman/b2252a8c6c1411d30f8e7c17dbe302e5 to your computer and use it in GitHub Desktop.
Save hhallman/b2252a8c6c1411d30f8e7c17dbe302e5 to your computer and use it in GitHub Desktop.
print-version.sh
#/bin/bash
# Prints the currently checked out version-name
# <number-of-commits-on-master-since-branchout-or-`rebase`>.branch-name.<number-of-commits-since-branchout><flags>
# example: 12342-feature-34
# e.g.: 12334-feature-25cp for Changes-exist and Push-needed
origin="origin" # can be improved
master="master" # can be improved
short=" --short " # make it "" if long hashes are required instead of short
current_commit=$(git rev-parse $short HEAD)
local_commit_count=$(git rev-list --count HEAD ^$master)
total_commit_count=$(git rev-list --count HEAD)
latest_common_ancestor=$(git merge-base "$master" HEAD)
baseline_commit_count=$(git rev-list --count $latest_common_ancestor)
latest_common_ancestor=$(git rev-parse $short $latest_common_ancestor)
current_branch=$(git branch | grep \* | cut -d ' ' -f2)
flag_changes=""
flag_push_needed=""
git diff-index --quiet HEAD -- || flag_changes="c"
git cherry -v "$origin"/$current_branch | grep -q '[a-z]' && {
flag_push_needed="p"
}
flags="${flag_changes}${flag_push_needed}"
[ $current_branch == "$master" ] && {
echo "${total_commit_count}${flags}-${current_commit}"
exit 0
}
echo "$baseline_commit_count/${current_branch}@${local_commit_count}${flags}-${latest_common_ancestor}/${current_commit}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment