Skip to content

Commit a8bea17

Browse files
authored
Merge branch 'master' into fix-env
2 parents 060d525 + 60cc0d7 commit a8bea17

40 files changed

+2396
-321
lines changed

.ci/resolve_git_ref

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 https://github.com/eic/epic.git 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, either:
25+
- org/repo format (e.g., eic/edm4eic)
26+
- Full URL (e.g., https://github.com/eic/edm4eic.git)
27+
ref Git reference: branch name, tag name, or commit SHA
28+
29+
Examples:
30+
# Resolve branch to SHA (org/repo format)
31+
resolve_git_ref eic/edm4eic main
32+
33+
# Resolve tag to SHA (full URL)
34+
resolve_git_ref https://github.com/eic/epic.git v24.11.0
35+
36+
# Return commit SHA as-is
37+
resolve_git_ref eic/eicrecon abc123def456...
38+
39+
Exit codes:
40+
0 - Success (SHA printed to stdout)
41+
1 - Error (message printed to stderr)
42+
USAGE
43+
exit 1
44+
fi
45+
46+
# Require exactly 2 arguments
47+
if [ $# -ne 2 ]; then
48+
echo "Error: Expected 2 arguments, got $#" >&2
49+
echo "Run 'resolve_git_ref --help' for usage" >&2
50+
exit 1
51+
fi
52+
53+
case "${1}" in
54+
http*)
55+
repo_url="${1}"
56+
;;
57+
*)
58+
repo_url="https://github.com/${1}.git"
59+
;;
60+
esac
61+
ref="${2}"
62+
63+
# Handle empty ref
64+
if [ -z "${ref}" ]; then
65+
echo "Error: ref argument is empty" >&2
66+
exit 1
67+
fi
68+
69+
# If it's already a commit SHA (7-40 hex characters), return as-is
70+
# Full SHAs are 40 chars, but short SHAs (7+) are also valid
71+
# POSIX-compliant check: use case statement for pattern matching
72+
case "${ref}" in
73+
*[!0-9a-f]*)
74+
# Contains non-hex characters, not a SHA
75+
;;
76+
*)
77+
# All hex characters, check length
78+
ref_len=${#ref}
79+
if [ "${ref_len}" -ge 7 ] && [ "${ref_len}" -le 40 ]; then
80+
echo "${ref}"
81+
exit 0
82+
fi
83+
;;
84+
esac
85+
86+
# Try to resolve as branch
87+
sha=$(git ls-remote "${repo_url}" "refs/heads/${ref}" 2>/dev/null | cut -f1)
88+
if [ -n "${sha}" ]; then
89+
echo "${sha}"
90+
exit 0
91+
fi
92+
93+
# Try to resolve as tag
94+
sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}" 2>/dev/null | cut -f1)
95+
if [ -n "${sha}" ]; then
96+
echo "${sha}"
97+
exit 0
98+
fi
99+
100+
# Try to resolve as annotated tag (^{} dereferences to commit)
101+
sha=$(git ls-remote "${repo_url}" "refs/tags/${ref}^{}" 2>/dev/null | cut -f1)
102+
if [ -n "${sha}" ]; then
103+
echo "${sha}"
104+
exit 0
105+
fi
106+
107+
# Unable to resolve
108+
echo "Error: Unable to resolve ref '${ref}' in repository '${repo_url}'" >&2
109+
echo "Ref must be a valid branch name, tag name, or commit SHA" >&2
110+
exit 1

0 commit comments

Comments
 (0)