Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-12 08:24:59

0001 #!/bin/sh
0002 # Resolve git refs (branches, tags, or commit SHAs) to full commit SHAs
0003 #
0004 # Usage:
0005 #   resolve_git_ref <repo> <ref>
0006 #   resolve_git_ref eic/edm4eic main
0007 #   resolve_git_ref https://github.com/eic/epic.git v24.11.0
0008 #   resolve_git_ref eic/eicrecon abc123def456...
0009 #
0010 # Exit codes:
0011 #   0 - Success (ref resolved or already a SHA)
0012 #   1 - Invalid arguments or ref not found
0013 
0014 set -e
0015 
0016 # Show usage if no arguments or --help
0017 if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
0018   cat >&2 << 'USAGE'
0019 Usage: resolve_git_ref <repo> <ref>
0020 
0021 Resolves a git reference (branch, tag, or commit SHA) to a full commit SHA.
0022 
0023 Arguments:
0024   repo        Git repository, either:
0025               - org/repo format (e.g., eic/edm4eic)
0026               - Full URL (e.g., https://github.com/eic/edm4eic.git)
0027   ref         Git reference: branch name, tag name, or commit SHA
0028 
0029 Examples:
0030   # Resolve branch to SHA (org/repo format)
0031   resolve_git_ref eic/edm4eic main
0032 
0033   # Resolve tag to SHA (full URL)
0034   resolve_git_ref https://github.com/eic/epic.git v24.11.0
0035 
0036   # Return commit SHA as-is
0037   resolve_git_ref eic/eicrecon abc123def456...
0038 
0039 Exit codes:
0040   0 - Success (SHA printed to stdout)
0041   1 - Error (message printed to stderr)
0042 USAGE
0043   exit 1
0044 fi
0045 
0046 # Require exactly 2 arguments
0047 if [ $# -ne 2 ]; then
0048   echo "Error: Expected 2 arguments, got $#" >&2
0049   echo "Run 'resolve_git_ref --help' for usage" >&2
0050   exit 1
0051 fi
0052 
0053 case "${1}" in
0054   http*)
0055     repo_url="${1}"
0056     ;;
0057   *)
0058     repo_url="https://github.com/${1}.git"
0059     ;;
0060 esac
0061 ref="${2}"
0062 
0063 # Handle empty ref
0064 if [ -z "${ref}" ]; then
0065   echo "Error: ref argument is empty" >&2
0066   exit 1
0067 fi
0068 
0069 # If it's already a commit SHA (7-40 hex characters), return as-is
0070 # Full SHAs are 40 chars, but short SHAs (7+) are also valid
0071 # POSIX-compliant check: use case statement for pattern matching
0072 case "${ref}" in
0073   *[!0-9a-f]*)
0074     # Contains non-hex characters, not a SHA
0075     ;;
0076   *)
0077     # All hex characters, check length
0078     ref_len=${#ref}
0079     if [ "${ref_len}" -ge 7 ] && [ "${ref_len}" -le 40 ]; then
0080       echo "${ref}"
0081       exit 0
0082     fi
0083     ;;
0084 esac
0085 
0086 # Try to resolve as branch
0087 sha=$(git ls-remote "${repo_url}" "refs/heads/${ref}" 2>/dev/null | cut -f1)
0088 if [ -n "${sha}" ]; then
0089   echo "${sha}"
0090   exit 0
0091 fi
0092 
0093 # Try to resolve as tag
0094 sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}" 2>/dev/null | cut -f1)
0095 if [ -n "${sha}" ]; then
0096   echo "${sha}"
0097   exit 0
0098 fi
0099 
0100 # Try to resolve as annotated tag (^{} dereferences to commit)
0101 sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}^{}" 2>/dev/null | cut -f1)
0102 if [ -n "${sha}" ]; then
0103   echo "${sha}"
0104   exit 0
0105 fi
0106 
0107 # Unable to resolve
0108 echo "Error: Unable to resolve ref '${ref}' in repository '${repo_url}'" >&2
0109 echo "Ref must be a valid branch name, tag name, or commit SHA" >&2
0110 exit 1