From 07bbf8a7837df12cf51f68836f2bfa9acc713878 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:31:34 -0500 Subject: [PATCH 01/19] Add git-ref script to print current git reference This script determines the current git reference type and prints the branch or tag name based on the repository state. description from copilot --- src/git-ref | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/git-ref diff --git a/src/git-ref b/src/git-ref new file mode 100644 index 0000000..6e8b4c4 --- /dev/null +++ b/src/git-ref @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +usage() { + echo -n "git-ref [OPTION]... [FILE]... + +Prints the branch or tag of the current git repository. + + -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, exit with an error. +" +} + +BRANCH=`git symbolic-ref --quiet --short HEAD 2> /dev/null` +TAG=`git describe --tags --exact-match 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" + else + 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 +else + exit 1 +fi From 7a4e53fb661c8b63d1a21c45f952a3ac9ad615cf Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:34:34 -0500 Subject: [PATCH 02/19] Executable. --- src/git-ref | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 src/git-ref diff --git a/src/git-ref b/src/git-ref old mode 100644 new mode 100755 From 1997b32d6af186a4e515a38ea03eee0a5ff35324 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:37:07 -0500 Subject: [PATCH 03/19] Add to CI --- .github/workflows/scripts.tests.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index f0f7c27..72c9c92 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -66,3 +66,17 @@ 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: get current reference + run: | + REF=$(git-ref) + echo "Current ref: $REF" >> $GITHUB_STEP_SUMMARY + if [[ "$REF" != "${{ github.head_ref }}" ]]; then + exit 1; + fi From 666f48ab11335a605e6a484d156417b0808652b8 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:38:32 -0500 Subject: [PATCH 04/19] Run to see what it is --- .github/workflows/scripts.tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 72c9c92..cece30c 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -75,6 +75,7 @@ jobs: - name: get current reference run: | + git-ref REF=$(git-ref) echo "Current ref: $REF" >> $GITHUB_STEP_SUMMARY if [[ "$REF" != "${{ github.head_ref }}" ]]; then From e8ea01c2165e3ae9debfced0b08634108a399398 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:41:42 -0500 Subject: [PATCH 05/19] Print something if failed. --- src/git-ref | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/git-ref b/src/git-ref index 6e8b4c4..cb0bbf9 100755 --- a/src/git-ref +++ b/src/git-ref @@ -43,5 +43,7 @@ if [[ -n $BRANCH ]]; then elif [[ -n $TAG ]]; then echo $TAG else + echo "No git tag or branch is checked out. git status:" + git status exit 1 fi From 855768b360eebd959eaa9711ccb2180d555a9e7f Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Wed, 12 Nov 2025 08:45:46 -0500 Subject: [PATCH 06/19] Enhance git-ref to include SHA output Added SHA detection to git-ref script for better output. --- src/git-ref | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/git-ref b/src/git-ref index cb0bbf9..b6358d6 100755 --- a/src/git-ref +++ b/src/git-ref @@ -14,6 +14,7 @@ If the PWD is in a DETACHED HEAD state, and there is no tag, exit with an error. 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 @@ -42,8 +43,9 @@ if [[ -n $BRANCH ]]; then echo $BRANCH elif [[ -n $TAG ]]; then echo $TAG +elif [[ -n $SHA ]]; then + echo $SHA else - echo "No git tag or branch is checked out. git status:" - git status + echo "No git tag, branch or SHA detected." exit 1 fi From 527d3abe0897d32aeecf4363c0094ec2e1f3432e Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Thu, 13 Nov 2025 21:39:46 -0500 Subject: [PATCH 07/19] Add error message for git-ref script mismatch --- .github/workflows/scripts.tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index cece30c..eaaa959 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -79,5 +79,6 @@ jobs: REF=$(git-ref) echo "Current ref: $REF" >> $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 From 7865a7559c399ba6d222bcf445988e837b690d13 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Thu, 13 Nov 2025 21:41:34 -0500 Subject: [PATCH 08/19] Fix echo statement for git-ref script output check --- .github/workflows/scripts.tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index eaaa959..f963097 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -79,6 +79,6 @@ jobs: REF=$(git-ref) echo "Current ref: $REF" >> $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 }}) + echo "Output of git-ref script ($REF) does not match github.head_ref (${{ github.head_ref }})" exit 1; fi From 5fadc2657e6c9745c8cc84565c801d8cb259f6c0 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Thu, 13 Nov 2025 21:46:21 -0500 Subject: [PATCH 09/19] Add run step to git-ref job in workflow --- .github/workflows/scripts.tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index f963097..1d11a0a 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 @@ -72,6 +71,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - run: run-with-summary echo ${{ toJSON(github) }} - name: get current reference run: | From 9eed304099c3488df620830b6e43b72bf81a6dcd Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Thu, 13 Nov 2025 21:48:02 -0500 Subject: [PATCH 10/19] Update condition to compare with github.sha --- .github/workflows/scripts.tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 1d11a0a..90fdcc9 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -78,7 +78,7 @@ jobs: git-ref REF=$(git-ref) echo "Current ref: $REF" >> $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 }})" + if [[ "$REF" != "${{ github.sha }}" ]]; then + echo "Output of git-ref script ($REF) does not match github.sha (${{ github.sha }})" exit 1; fi From 1ac079b176635b8202f2bda46d827f795a05580d Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Thu, 13 Nov 2025 21:53:14 -0500 Subject: [PATCH 11/19] Remove echo of GitHub context in git-ref script Removed the run command that echoed GitHub context as JSON. --- .github/workflows/scripts.tests.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 90fdcc9..515d1e7 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -71,7 +71,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - run: run-with-summary echo ${{ toJSON(github) }} - name: get current reference run: | From 4b0ed4022916edcb1ebb92ce415feab076037f0e Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:06:00 -0500 Subject: [PATCH 12/19] ignore .idea --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea From a574a2b35d4fba4135d5df17939ebcab2cf60ea9 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:08:41 -0500 Subject: [PATCH 13/19] Document 'git-ref' script in README Added documentation for the 'git-ref' script, including usage and options. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index ac09df2..709a767 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,23 @@ 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` + +> Print the branch or tag name of the current directory of a cloned git repo. Use `--type` to show if it is a branch or tag. + +``` +git-ref [OPTIONS] + +Prints 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, exit with an error. +``` ## History From f6075e04ba3a4ec8c75b6b9e9f5cff079a1915fe Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:26:19 -0500 Subject: [PATCH 14/19] Run git-ref with --type option --- .github/workflows/scripts.tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 515d1e7..f46145e 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -76,7 +76,8 @@ jobs: run: | git-ref REF=$(git-ref) - echo "Current ref: $REF" >> $GITHUB_STEP_SUMMARY + 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; From 00c8080a40516bdf692c8da0c2d22f8fc6908f67 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:28:38 -0500 Subject: [PATCH 15/19] call git-ref --type --- .github/workflows/scripts.tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index f46145e..0f82809 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -75,8 +75,8 @@ jobs: - name: get current reference 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 }})" From e2dacaa378b255f52ae289f3b83749b8ec3124f8 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:30:04 -0500 Subject: [PATCH 16/19] Forgot sha type --- src/git-ref | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/git-ref b/src/git-ref index b6358d6..af62464 100755 --- a/src/git-ref +++ b/src/git-ref @@ -24,7 +24,10 @@ while [ $# -gt 0 ]; do 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 From 34774dfc96c2179ba3331b08b55400b5bc8b81a4 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:33:49 -0500 Subject: [PATCH 17/19] check branch --- .github/workflows/scripts.tests.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 0f82809..4e5b407 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -77,8 +77,31 @@ jobs: 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: get current reference + 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" != "branch" ]]; then + echo "git-ref did not return 'branch'." + fi + From ec90d36a356dd6fadaaa7689e0883eb9b91687ed Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:35:13 -0500 Subject: [PATCH 18/19] Fix tests, check for head ref. --- .github/workflows/scripts.tests.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 4e5b407..9ea513f 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -72,7 +72,7 @@ jobs: steps: - uses: actions/checkout@v4 - - name: get current reference + - name: Check SHA run: | git-ref git-ref --type @@ -90,15 +90,15 @@ jobs: with: ref: ${{ github.head_ref }} - - name: get current reference + - 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.sha }}" ]]; then - echo "Output of git-ref script ($REF) does not match github.sha (${{ github.sha }})" + 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 From 84199d1dfc1e948f8a316743169e60797c28ded6 Mon Sep 17 00:00:00 2001 From: Jon Pugh Date: Sat, 13 Dec 2025 08:39:05 -0500 Subject: [PATCH 19/19] Fix usage --- README.md | 10 ++++------ src/git-ref | 13 ++++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 709a767..6b43f60 100644 --- a/README.md +++ b/README.md @@ -99,20 +99,18 @@ wait-for curl https://deploy-url/ready ## `git-ref` -> Print the branch or tag name of the current directory of a cloned git repo. Use `--type` to show if it is a branch or tag. - ``` git-ref [OPTIONS] -Prints the branch or tag of the current git repository. +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, exit with an error. + 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 index af62464..10f9e18 100755 --- a/src/git-ref +++ b/src/git-ref @@ -1,14 +1,17 @@ #!/usr/bin/env bash usage() { - echo -n "git-ref [OPTION]... [FILE]... + echo -n " +git-ref [OPTIONS] -Prints the branch or tag of the current git repository. +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, exit with an error. + 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'. " }