|
| 1 | +#!/bin/sh |
| 2 | +# Resolve git refs (branches, tags, or commit SHAs) to full commit SHAs |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# resolve_git_ref <repo> <ref> |
| 6 | +# resolve_git_ref eic/edm4eic main |
| 7 | +# resolve_git_ref eic/epic v24.11.0 |
| 8 | +# resolve_git_ref eic/eicrecon abc123def456... |
| 9 | +# |
| 10 | +# Exit codes: |
| 11 | +# 0 - Success (ref resolved or already a SHA) |
| 12 | +# 1 - Invalid arguments or ref not found |
| 13 | + |
| 14 | +set -e |
| 15 | + |
| 16 | +# Show usage if no arguments or --help |
| 17 | +if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 18 | + cat >&2 << 'USAGE' |
| 19 | +Usage: resolve_git_ref <repo> <ref> |
| 20 | +
|
| 21 | +Resolves a git reference (branch, tag, or commit SHA) to a full commit SHA. |
| 22 | +
|
| 23 | +Arguments: |
| 24 | + repo Git repository, in the format org/repo (e.g., eic/edm4eic) |
| 25 | + ref Git reference: branch name, tag name, or commit SHA |
| 26 | +
|
| 27 | +Examples: |
| 28 | + # Resolve branch to SHA |
| 29 | + resolve_git_ref eic/edm4eic main |
| 30 | +
|
| 31 | + # Resolve tag to SHA |
| 32 | + resolve_git_ref eic/epic v24.11.0 |
| 33 | +
|
| 34 | + # Return commit SHA as-is |
| 35 | + resolve_git_ref eic/eicrecon abc123def456... |
| 36 | +
|
| 37 | +Exit codes: |
| 38 | + 0 - Success (SHA printed to stdout) |
| 39 | + 1 - Error (message printed to stderr) |
| 40 | +USAGE |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +# Require exactly 2 arguments |
| 45 | +if [ $# -ne 2 ]; then |
| 46 | + echo "Error: Expected 2 arguments, got $#" >&2 |
| 47 | + echo "Run 'resolve_git_ref --help' for usage" >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +repo_url="https://github.com/${1}.git" |
| 52 | +ref="${2}" |
| 53 | + |
| 54 | +# Handle empty ref |
| 55 | +if [ -z "${ref}" ]; then |
| 56 | + echo "Error: ref argument is empty" >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +# If it's already a commit SHA (7-40 hex characters), return as-is |
| 61 | +# Full SHAs are 40 chars, but short SHAs (7+) are also valid |
| 62 | +# POSIX-compliant check: use case statement for pattern matching |
| 63 | +case "${ref}" in |
| 64 | + *[!0-9a-f]*) |
| 65 | + # Contains non-hex characters, not a SHA |
| 66 | + ;; |
| 67 | + *) |
| 68 | + # All hex characters, check length |
| 69 | + ref_len=${#ref} |
| 70 | + if [ "${ref_len}" -ge 7 ] && [ "${ref_len}" -le 40 ]; then |
| 71 | + echo "${ref}" |
| 72 | + exit 0 |
| 73 | + fi |
| 74 | + ;; |
| 75 | +esac |
| 76 | + |
| 77 | +# Try to resolve as branch |
| 78 | +sha=$(git ls-remote "${repo_url}" "refs/heads/${ref}" 2>/dev/null | cut -f1) |
| 79 | +if [ -n "${sha}" ]; then |
| 80 | + echo "${sha}" |
| 81 | + exit 0 |
| 82 | +fi |
| 83 | + |
| 84 | +# Try to resolve as tag |
| 85 | +sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}" 2>/dev/null | cut -f1) |
| 86 | +if [ -n "${sha}" ]; then |
| 87 | + echo "${sha}" |
| 88 | + exit 0 |
| 89 | +fi |
| 90 | + |
| 91 | +# Try to resolve as annotated tag (^{} dereferences to commit) |
| 92 | +sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}^{}" 2>/dev/null | cut -f1) |
| 93 | +if [ -n "${sha}" ]; then |
| 94 | + echo "${sha}" |
| 95 | + exit 0 |
| 96 | +fi |
| 97 | + |
| 98 | +# Unable to resolve |
| 99 | +echo "Error: Unable to resolve ref '${ref}' in repository '${repo_url}'" >&2 |
| 100 | +echo "Ref must be a valid branch name, tag name, or commit SHA" >&2 |
| 101 | +exit 1 |
0 commit comments