Skip to content

Commit a939d4b

Browse files
fix(installer): resolve source refs safely (#130907)
Source installs must select the requested release commit and leave existing checkouts recoverable when updates fail. This change qualifies Git refs, carries ref mutability into lockfile selection, restores conflicting rebases, and verifies unchanged state when a pre-rebase hook refuses before rebase metadata exists. Co-authored-by: Jason O'Neal <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 6165efe commit a939d4b

4 files changed

Lines changed: 644 additions & 101 deletions

File tree

‎scripts/install-cli.sh‎

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ JSON=0
9292
RUN_ONBOARD=0
9393
SET_NPM_PREFIX=0
9494
PNPM_CMD=()
95+
GIT_REF_KIND=""
9596
FRESH_GIT_MIN_FREE_KIB=$((6 * 1024 * 1024))
9697

9798
print_usage() {
@@ -1016,57 +1017,93 @@ resolve_git_openclaw_ref() {
10161017
esac
10171018
}
10181019

1020+
verify_git_rebase_recovery() {
1021+
local repo_dir="$1"
1022+
local expected_head="$2"
1023+
local expected_status="$3"
1024+
local git_dir
1025+
1026+
git_dir="$(git -C "$repo_dir" rev-parse --absolute-git-dir)" || return 1
1027+
if [[ -d "$git_dir/rebase-merge" || -d "$git_dir/rebase-apply" ]]; then
1028+
git -C "$repo_dir" rebase --abort >/dev/null 2>&1 || return 1
1029+
fi
1030+
1031+
[[ "$(git -C "$repo_dir" rev-parse --verify HEAD 2>/dev/null)" == "$expected_head" ]] &&
1032+
[[ "$(git -C "$repo_dir" status --porcelain=v1 --untracked-files=all 2>/dev/null)" == "$expected_status" ]] &&
1033+
[[ ! -d "$git_dir/rebase-merge" && ! -d "$git_dir/rebase-apply" ]]
1034+
}
1035+
10191036
checkout_git_openclaw_ref() {
10201037
local repo_dir="$1"
10211038
local ref="$2"
1039+
local original_head=""
1040+
local original_status=""
1041+
local namespaces=(heads tags)
1042+
1043+
GIT_REF_KIND=""
10221044

10231045
if [[ -z "$ref" ]]; then
10241046
return 0
10251047
fi
10261048

10271049
if [[ "$ref" == "main" ]]; then
1028-
git -C "$repo_dir" fetch --no-tags origin main
1050+
git -C "$repo_dir" fetch --no-tags origin "refs/heads/main:refs/remotes/origin/main"
10291051
git -C "$repo_dir" checkout main
10301052
if [[ "$GIT_UPDATE" == "1" ]]; then
1031-
git -C "$repo_dir" pull --rebase --no-tags || true
1032-
fi
1033-
return 0
1034-
fi
1035-
1036-
if git -C "$repo_dir" ls-remote --exit-code --heads origin "$ref" >/dev/null 2>&1; then
1037-
git -C "$repo_dir" fetch --no-tags origin "refs/heads/${ref}:refs/remotes/origin/${ref}"
1038-
git -C "$repo_dir" checkout -B "$ref" "origin/$ref"
1039-
if [[ "$GIT_UPDATE" == "1" ]]; then
1040-
git -C "$repo_dir" pull --rebase --no-tags || true
1053+
if ! original_head="$(git -C "$repo_dir" rev-parse --verify HEAD 2>/dev/null)"; then
1054+
fail "Could not record repository state before updating from origin/main"
1055+
fi
1056+
if ! original_status="$(git -C "$repo_dir" status --porcelain=v1 --untracked-files=all 2>/dev/null)"; then
1057+
fail "Could not record repository state before updating from origin/main"
1058+
fi
1059+
if ! git -C "$repo_dir" rebase origin/main; then
1060+
if verify_git_rebase_recovery "$repo_dir" "$original_head" "$original_status"; then
1061+
fail "Could not update repository from origin/main; the checkout was restored to its pre-update state"
1062+
fi
1063+
fail "Could not update repository from origin/main; checkout recovery was not verified. Run git -C \"$repo_dir\" rebase --abort and inspect the checkout before retrying"
1064+
fi
10411065
fi
1066+
GIT_REF_KIND="moving"
10421067
return 0
10431068
fi
10441069

1045-
git -C "$repo_dir" fetch --tags origin
1046-
1047-
if git -C "$repo_dir" rev-parse --verify --quiet "refs/tags/${ref}^{commit}" >/dev/null; then
1048-
git -C "$repo_dir" checkout --detach "$ref"
1049-
return 0
1070+
# Normalized release selectors prefer immutable tags. A same-name branch
1071+
# remains a fallback for operator-supplied v-prefixed branch names.
1072+
if [[ "$ref" == v[0-9]* ]]; then
1073+
namespaces=(tags heads)
10501074
fi
10511075

1052-
if git -C "$repo_dir" rev-parse --verify --quiet "${ref}^{commit}" >/dev/null; then
1053-
git -C "$repo_dir" checkout --detach "$ref"
1054-
return 0
1055-
fi
1076+
local namespace=""
1077+
local probe_status=0
1078+
for namespace in "${namespaces[@]}"; do
1079+
if git -C "$repo_dir" ls-remote --exit-code origin "refs/${namespace}/${ref}" >/dev/null 2>&1; then
1080+
if [[ "$namespace" == "heads" ]]; then
1081+
git -C "$repo_dir" fetch --no-tags origin "refs/heads/${ref}:refs/remotes/origin/${ref}"
1082+
git -C "$repo_dir" checkout -B "$ref" "origin/$ref"
1083+
GIT_REF_KIND="moving"
1084+
else
1085+
git -C "$repo_dir" fetch --no-tags origin "refs/tags/${ref}:refs/tags/${ref}"
1086+
git -C "$repo_dir" rev-parse --verify --quiet "refs/tags/${ref}^{commit}" >/dev/null ||
1087+
fail "Requested git version is not a commit: ${ref}"
1088+
git -C "$repo_dir" checkout --detach "refs/tags/${ref}"
1089+
GIT_REF_KIND="immutable"
1090+
fi
1091+
return 0
1092+
else
1093+
probe_status=$?
1094+
fi
1095+
(( probe_status == 2 )) || fail "Could not resolve requested git ref: ${ref}"
1096+
done
10561097

10571098
fail "Requested git version not found: ${ref}"
10581099
}
10591100

10601101
git_install_lockfile_flag() {
1061-
local repo_dir="$1"
1062-
local ref="$2"
1063-
1064-
if [[ "$ref" == "main" ]] || git -C "$repo_dir" ls-remote --exit-code --heads origin "$ref" >/dev/null 2>&1; then
1102+
if [[ "$1" == "moving" ]]; then
10651103
echo "--no-frozen-lockfile"
1066-
return 0
1104+
else
1105+
echo "--frozen-lockfile"
10671106
fi
1068-
1069-
echo "--frozen-lockfile"
10701107
}
10711108

10721109
repo_pnpm_spec() {
@@ -1633,6 +1670,11 @@ install_openclaw_from_git() {
16331670
else
16341671
log "Repo is dirty; skipping git checkout/update"
16351672
emit_json step name git-update status warn reason dirty
1673+
if git -C "$repo_dir" symbolic-ref --quiet HEAD >/dev/null; then
1674+
GIT_REF_KIND="moving"
1675+
else
1676+
GIT_REF_KIND="immutable"
1677+
fi
16361678
fi
16371679

16381680
if [[ -n "${REQUIRED_COMPATIBLE_VERSION:-}" ]]; then
@@ -1649,7 +1691,7 @@ install_openclaw_from_git() {
16491691
activate_repo_pnpm_version "$repo_dir"
16501692

16511693
local install_lockfile_flag
1652-
install_lockfile_flag="$(git_install_lockfile_flag "$repo_dir" "$git_ref")"
1694+
install_lockfile_flag="$(git_install_lockfile_flag "$GIT_REF_KIND")"
16531695
emit_json step name dependencies status start
16541696
CI="${CI:-true}" run_pnpm -C "$repo_dir" install "$install_lockfile_flag"
16551697
emit_json step name dependencies status ok

‎scripts/install.sh‎

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ VERBOSE="${OPENCLAW_VERBOSE:-0}"
13971397
VERIFY_INSTALL="${OPENCLAW_VERIFY_INSTALL:-0}"
13981398
OPENCLAW_BIN=""
13991399
PNPM_CMD=()
1400+
GIT_REF_KIND=""
14001401
HELP=0
14011402

14021403
print_usage() {
@@ -2627,48 +2628,105 @@ resolve_git_openclaw_ref() {
26272628
esac
26282629
}
26292630

2631+
verify_git_rebase_recovery() {
2632+
local repo_dir="$1"
2633+
local expected_head="$2"
2634+
local expected_status="$3"
2635+
local git_dir
2636+
2637+
git_dir="$(git -C "$repo_dir" rev-parse --absolute-git-dir)" || return 1
2638+
if [[ -d "$git_dir/rebase-merge" || -d "$git_dir/rebase-apply" ]]; then
2639+
git -C "$repo_dir" rebase --abort >/dev/null 2>&1 || return 1
2640+
fi
2641+
2642+
[[ "$(git -C "$repo_dir" rev-parse --verify HEAD 2>/dev/null)" == "$expected_head" ]] &&
2643+
[[ "$(git -C "$repo_dir" status --porcelain=v1 --untracked-files=all 2>/dev/null)" == "$expected_status" ]] &&
2644+
[[ ! -d "$git_dir/rebase-merge" && ! -d "$git_dir/rebase-apply" ]]
2645+
}
2646+
26302647
checkout_git_openclaw_ref() {
26312648
local repo_dir="$1"
26322649
local ref="$2"
2650+
local original_head=""
2651+
local original_status=""
2652+
local namespaces=(heads tags)
2653+
2654+
GIT_REF_KIND=""
26332655

26342656
if [[ -z "$ref" ]]; then
26352657
return 0
26362658
fi
26372659

26382660
if [[ "$ref" == "main" ]]; then
2639-
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --no-tags origin main
2661+
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --no-tags origin "refs/heads/main:refs/remotes/origin/main"
26402662
run_quiet_step "Checking out main" git -C "$repo_dir" checkout main
26412663
if [[ "$GIT_UPDATE" == "1" ]]; then
2642-
run_quiet_step "Updating repository" git -C "$repo_dir" pull --rebase --no-tags || true
2643-
fi
2644-
return 0
2645-
fi
2646-
2647-
if git -C "$repo_dir" ls-remote --exit-code --heads origin "$ref" >/dev/null 2>&1; then
2648-
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --no-tags origin "refs/heads/${ref}:refs/remotes/origin/${ref}"
2649-
run_quiet_step "Checking out ${ref}" git -C "$repo_dir" checkout -B "$ref" "origin/$ref"
2650-
if [[ "$GIT_UPDATE" == "1" ]]; then
2651-
run_quiet_step "Updating repository" git -C "$repo_dir" pull --rebase --no-tags || true
2664+
if ! original_head="$(git -C "$repo_dir" rev-parse --verify HEAD 2>/dev/null)"; then
2665+
ui_error "Could not record repository state before updating from origin/main"
2666+
return 1
2667+
fi
2668+
if ! original_status="$(git -C "$repo_dir" status --porcelain=v1 --untracked-files=all 2>/dev/null)"; then
2669+
ui_error "Could not record repository state before updating from origin/main"
2670+
return 1
2671+
fi
2672+
if ! run_quiet_step "Updating repository" git -C "$repo_dir" rebase origin/main; then
2673+
if verify_git_rebase_recovery "$repo_dir" "$original_head" "$original_status"; then
2674+
ui_error "Could not update repository from origin/main; the checkout was restored to its pre-update state"
2675+
else
2676+
ui_error "Could not update repository from origin/main; checkout recovery was not verified. Run git -C \"$repo_dir\" rebase --abort and inspect the checkout before retrying"
2677+
fi
2678+
return 1
2679+
fi
26522680
fi
2681+
GIT_REF_KIND="moving"
26532682
return 0
26542683
fi
26552684

2656-
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --tags origin
2657-
2658-
if git -C "$repo_dir" rev-parse --verify --quiet "refs/tags/${ref}^{commit}" >/dev/null; then
2659-
run_quiet_step "Checking out ${ref}" git -C "$repo_dir" checkout --detach "$ref"
2660-
return 0
2685+
# Normalized release selectors prefer immutable tags. A same-name branch
2686+
# remains a fallback for operator-supplied v-prefixed branch names.
2687+
if [[ "$ref" == v[0-9]* ]]; then
2688+
namespaces=(tags heads)
26612689
fi
26622690

2663-
if git -C "$repo_dir" rev-parse --verify --quiet "${ref}^{commit}" >/dev/null; then
2664-
run_quiet_step "Checking out ${ref}" git -C "$repo_dir" checkout --detach "$ref"
2665-
return 0
2666-
fi
2691+
local namespace=""
2692+
local probe_status=0
2693+
for namespace in "${namespaces[@]}"; do
2694+
if git -C "$repo_dir" ls-remote --exit-code origin "refs/${namespace}/${ref}" >/dev/null 2>&1; then
2695+
if [[ "$namespace" == "heads" ]]; then
2696+
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --no-tags origin "refs/heads/${ref}:refs/remotes/origin/${ref}"
2697+
run_quiet_step "Checking out ${ref}" git -C "$repo_dir" checkout -B "$ref" "origin/$ref"
2698+
GIT_REF_KIND="moving"
2699+
else
2700+
run_quiet_step "Fetching requested version" git -C "$repo_dir" fetch --no-tags origin "refs/tags/${ref}:refs/tags/${ref}"
2701+
if ! git -C "$repo_dir" rev-parse --verify --quiet "refs/tags/${ref}^{commit}" >/dev/null; then
2702+
ui_error "Requested git version is not a commit: ${ref}"
2703+
return 1
2704+
fi
2705+
run_quiet_step "Checking out ${ref}" git -C "$repo_dir" checkout --detach "refs/tags/${ref}"
2706+
GIT_REF_KIND="immutable"
2707+
fi
2708+
return 0
2709+
else
2710+
probe_status=$?
2711+
fi
2712+
if (( probe_status != 2 )); then
2713+
ui_error "Could not resolve requested git ref: ${ref}"
2714+
return 1
2715+
fi
2716+
done
26672717

26682718
ui_error "Requested git version not found: ${ref}"
26692719
return 1
26702720
}
26712721

2722+
git_install_lockfile_flag() {
2723+
if [[ "$1" == "moving" ]]; then
2724+
echo "--no-frozen-lockfile"
2725+
else
2726+
echo "--frozen-lockfile"
2727+
fi
2728+
}
2729+
26722730
validate_git_checkout_head() {
26732731
local repo_dir="$1"
26742732

@@ -2766,18 +2824,6 @@ NODE
27662824
fi
27672825
}
27682826

2769-
git_install_lockfile_flag() {
2770-
local repo_dir="$1"
2771-
local ref="$2"
2772-
2773-
if [[ "$ref" == "main" ]] || git -C "$repo_dir" ls-remote --exit-code --heads origin "$ref" >/dev/null 2>&1; then
2774-
echo "--no-frozen-lockfile"
2775-
return 0
2776-
fi
2777-
2778-
echo "--frozen-lockfile"
2779-
}
2780-
27812827
repo_pnpm_spec() {
27822828
local repo_dir="$1"
27832829
local package_json="${repo_dir}/package.json"
@@ -3258,13 +3304,18 @@ install_openclaw_from_git() {
32583304
checkout_git_openclaw_ref "$repo_dir" "$git_ref"
32593305
else
32603306
ui_info "Repo has local changes; skipping git checkout/update"
3307+
if git -C "$repo_dir" symbolic-ref --quiet HEAD >/dev/null; then
3308+
GIT_REF_KIND="moving"
3309+
else
3310+
GIT_REF_KIND="immutable"
3311+
fi
32613312
fi
32623313

32633314
cleanup_legacy_submodules "$repo_dir"
32643315
activate_repo_pnpm_version "$repo_dir"
32653316

32663317
local install_lockfile_flag
3267-
install_lockfile_flag="$(git_install_lockfile_flag "$repo_dir" "$git_ref")"
3318+
install_lockfile_flag="$(git_install_lockfile_flag "$GIT_REF_KIND")"
32683319
CI="${CI:-true}" run_quiet_step "Installing dependencies" run_pnpm -C "$repo_dir" install "$install_lockfile_flag"
32693320

32703321
if ! run_quiet_step "Building UI" run_pnpm -C "$repo_dir" ui:build; then

0 commit comments

Comments
 (0)