Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion .github/workflows/scripts.tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
57 changes: 57 additions & 0 deletions src/git-ref
Original file line number Diff line number Diff line change
@@ -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
Loading