Skip to content

Commit a400255

Browse files
committed
feat(installer): add github_clone installer for repository-based tools
Clones or updates GitHub repositories to specified paths. Used by ruby-build (rbenv plugin) and similar tools. Features: - Clone to custom path (with tilde expansion) - Update existing clones (git fetch + reset) - Track commits (before/after) - Configurable branch - Refresh snapshot after install
1 parent 136bab1 commit a400255

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

scripts/installers/github_clone.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env bash
2+
# Generic installer for GitHub repository clones
3+
set -euo pipefail
4+
5+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6+
. "$DIR/lib/install_strategy.sh"
7+
8+
TOOL="${1:-}"
9+
if [ -z "$TOOL" ]; then
10+
echo "Usage: $0 TOOL_NAME [ACTION]" >&2
11+
exit 1
12+
fi
13+
14+
ACTION="${2:-install}"
15+
16+
CATALOG_FILE="$DIR/../catalog/$TOOL.json"
17+
if [ ! -f "$CATALOG_FILE" ]; then
18+
echo "[$TOOL] Error: Catalog file not found: $CATALOG_FILE" >&2
19+
exit 1
20+
fi
21+
22+
# Parse catalog
23+
GITHUB_REPO="$(jq -r '.github_repo // ""' "$CATALOG_FILE")"
24+
CLONE_PATH="$(jq -r '.clone_path // ""' "$CATALOG_FILE")"
25+
BRANCH="$(jq -r '.branch // "master"' "$CATALOG_FILE")"
26+
27+
if [ -z "$GITHUB_REPO" ]; then
28+
echo "[$TOOL] Error: github_repo not specified in catalog" >&2
29+
exit 1
30+
fi
31+
32+
if [ -z "$CLONE_PATH" ]; then
33+
echo "[$TOOL] Error: clone_path not specified in catalog" >&2
34+
exit 1
35+
fi
36+
37+
# Expand tilde in clone path
38+
CLONE_PATH="${CLONE_PATH/#\~/$HOME}"
39+
40+
# Ensure git is available
41+
if ! command -v git >/dev/null 2>&1; then
42+
echo "[$TOOL] Error: git not found. Please install git first." >&2
43+
exit 1
44+
fi
45+
46+
# Get current version/commit if already cloned
47+
before=""
48+
if [ -d "$CLONE_PATH/.git" ]; then
49+
cd "$CLONE_PATH"
50+
before="$(git rev-parse --short HEAD 2>/dev/null || echo "<unknown>")"
51+
cd - >/dev/null
52+
fi
53+
54+
# Clone or update
55+
if [ ! -d "$CLONE_PATH" ]; then
56+
echo "[$TOOL] Cloning from https://github.com/$GITHUB_REPO" >&2
57+
mkdir -p "$(dirname "$CLONE_PATH")"
58+
git clone --depth=1 --branch="$BRANCH" "https://github.com/$GITHUB_REPO.git" "$CLONE_PATH" || {
59+
echo "[$TOOL] Error: git clone failed" >&2
60+
exit 1
61+
}
62+
else
63+
echo "[$TOOL] Updating repository at $CLONE_PATH" >&2
64+
cd "$CLONE_PATH"
65+
git fetch origin "$BRANCH" --depth=1 || {
66+
echo "[$TOOL] Error: git fetch failed" >&2
67+
exit 1
68+
}
69+
git reset --hard "origin/$BRANCH" || {
70+
echo "[$TOOL] Error: git reset failed" >&2
71+
exit 1
72+
}
73+
cd - >/dev/null
74+
fi
75+
76+
# Get new version/commit
77+
after=""
78+
if [ -d "$CLONE_PATH/.git" ]; then
79+
cd "$CLONE_PATH"
80+
after="$(git rev-parse --short HEAD 2>/dev/null || echo "<unknown>")"
81+
cd - >/dev/null
82+
fi
83+
84+
# Report
85+
printf "[%s] before: %s\n" "$TOOL" "${before:-<none>}"
86+
printf "[%s] after: %s\n" "$TOOL" "${after:-<unknown>}"
87+
printf "[%s] path: %s\n" "$TOOL" "$CLONE_PATH"
88+
89+
# Refresh snapshot after successful installation
90+
refresh_snapshot "$TOOL"

0 commit comments

Comments
 (0)