-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1058 from US-Trustee-Program/CAMS-461-cleanup-hel…
…per-script CAMS-461 cleanup helper script
- Loading branch information
Showing
2 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env bash | ||
|
||
# A utility script useful for identifying deployed branch environments | ||
|
||
# Usage | ||
# From the root directory, run the following command: | ||
# ./ops/scripts/utility/check-env-hashes.sh [-l|h|r|e {hash}] | ||
|
||
############################################################ | ||
# Help # | ||
############################################################ | ||
Help() | ||
{ | ||
# Display Help | ||
echo "This script prints branch names and their short hashes or checks a" | ||
echo "known short hash against existing branches. Default lists remote" | ||
echo "branches and their short hashes." | ||
echo | ||
echo "Syntax: ./ops/scripts/utility/check-env-hashes.sh [-l|h|r|e {hash}]" | ||
echo "options:" | ||
echo "l Check local branches." | ||
echo "h Print this Help and exit." | ||
echo "r Check remote branches." | ||
echo "e Check against a specific known short hash." | ||
echo " Example usage: -e 0a3de4" | ||
echo | ||
} | ||
|
||
############################################################ | ||
############################################################ | ||
# Main program # | ||
############################################################ | ||
############################################################ | ||
LOCAL=true | ||
REMOTE=true | ||
while getopts ":hlre:" option; do | ||
case $option in | ||
h) # display help | ||
Help | ||
exit;; | ||
l) # Local branches | ||
LOCAL=true | ||
REMOTE=false | ||
;; | ||
r) # Remote branches | ||
REMOTE=true | ||
;; | ||
e) # Existing environment | ||
inputHash=${OPTARG} | ||
;; | ||
\?) # Invalid option | ||
echo "Run with the '-h' option to see valid usage." | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
branches=() | ||
if [[ "$LOCAL" = "true" ]]; then | ||
mapfile -t localBranches < <(git for-each-ref refs/heads --format='%(refname:short)') | ||
branches+=("${localBranches[@]}") | ||
fi | ||
if [[ "$REMOTE" = "true" ]]; then | ||
mapfile -t remoteBranches < <(git for-each-ref refs/remotes --format='%(refname:short)' | sed 's|^origin/||') | ||
branches+=("${remoteBranches[@]}") | ||
fi | ||
|
||
found=false | ||
for branch in "${branches[@]}"; do | ||
# Generate the SHA256 hash | ||
hash=$(echo -n "$branch" | openssl sha256 | awk '{print $2}') | ||
# Extract the first 6 characters of the hash | ||
shortHash="${hash:0:6}" | ||
if [[ -z "$inputHash" ]]; then | ||
echo "Branch: \"$branch\", Short Hash: \"$shortHash\"" | ||
else | ||
if [ "$shortHash" == "$inputHash" ]; then | ||
echo "Matching branch found: $branch" | ||
found=true | ||
break | ||
fi | ||
fi | ||
done | ||
|
||
# If -e was used but no branch was found | ||
if [[ -n "$inputHash" && "$found" = "false" ]]; then | ||
echo "No branch found with the short hash: $inputHash" | ||
exit 0 | ||
fi |