diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index f0f7c27..9ea513f 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -9,7 +9,6 @@ env: PATH: ./src:./bin:./vendor/bin:/usr/local/bin:/usr/bin:/bin jobs: - run-with-summary: name: run-with-summary script runs-on: ubuntu-latest @@ -66,3 +65,43 @@ jobs: TIMEOUT: 5 run: | run-with-summary wait-for sleep 2 + + git-ref: + name: git-ref script + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Check SHA + run: | + git-ref + git-ref --type + REF=$(git-ref) + TYPE=$(git-ref --type) + echo "Current ref: $REF $TYPE" >> $GITHUB_STEP_SUMMARY + if [[ "$REF" != "${{ github.sha }}" ]]; then + echo "Output of git-ref script ($REF) does not match github.sha (${{ github.sha }})" + exit 1; + fi + if [[ "$TYPE" != "sha" ]]; then + echo "git-ref did not return 'sha'." + fi + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Check branch + run: | + git-ref + git-ref --type + REF=$(git-ref) + TYPE=$(git-ref --type) + echo "Current ref: $REF $TYPE" >> $GITHUB_STEP_SUMMARY + if [[ "$REF" != "${{ github.head_ref }}" ]]; then + echo "Output of git-ref script ($REF) does not match github.head_ref (${{ github.head_ref }})" + exit 1; + fi + if [[ "$TYPE" != "branch" ]]; then + echo "git-ref did not return 'branch'." + fi + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index ac09df2..6b43f60 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ jobs: ## Scripts - `run-with-summary`: Runs a command and generates a markdown summary with output and execution details. - `wait-for`: Runs a command repeatedly until it passes. +- `git-ref`: Prints the branch or tag name of a git clone. ## Run With Summary @@ -96,6 +97,21 @@ wait-for curl https://deploy-url/ready # SILENT: Set to 1 to not print any output except the timeout exceeded error. Useful when running from other scripts. See wait-mysql ``` +## `git-ref` + +``` +git-ref [OPTIONS] + +Print the branch or tag of the current git repository. + +Options: + + -t, --type Print the git reference type ('branch' or 'tag'). + + If PWD is tracking a branch, print 'branch'. + If PWD is in a DETACHED HEAD state, and there is a tag for the SHA, print 'tag'. + If the PWD is in a DETACHED HEAD state, and there is no tag, print 'sha'. +``` ## History diff --git a/src/git-ref b/src/git-ref new file mode 100755 index 0000000..10f9e18 --- /dev/null +++ b/src/git-ref @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +usage() { + echo -n " +git-ref [OPTIONS] + +Print the branch or tag of the current git repository. + +Options: + + -t, --type Print the git reference type ('branch' or 'tag'). + + If PWD is tracking a branch, print 'branch'. + If PWD is in a DETACHED HEAD state, and there is a tag for the SHA, print 'tag'. + If the PWD is in a DETACHED HEAD state, and there is no tag, print 'sha'. +" +} + +BRANCH=`git symbolic-ref --quiet --short HEAD 2> /dev/null` +TAG=`git describe --tags --exact-match 2> /dev/null` +SHA=`git rev-parse HEAD 2> /dev/null` + +# Set up options for --type and --help. +while [ $# -gt 0 ]; do + case "$1" in + -t|--type) + if [[ -n $BRANCH ]]; then + echo "branch" + elif [[ -n $TAG ]]; then + echo "tag" + elif [[ -n $SHA ]]; then + echo "sha" + else + echo "Unable to detect branch, tag, or sha." + exit 1 + fi + + exit 0 + ;; + -h|--help) usage >&2; exit 0;; + *) + echo "Invalid option: $1" + exit 1 + esac + shift +done + +# Print the branch or tag name +if [[ -n $BRANCH ]]; then + echo $BRANCH +elif [[ -n $TAG ]]; then + echo $TAG +elif [[ -n $SHA ]]; then + echo $SHA +else + echo "No git tag, branch or SHA detected." + exit 1 +fi