Skip to content

Commit 9740a57

Browse files
committed
Fix Python sync --onto bug: capture pre-rebase SHAs upfront
The --onto fallback was capturing old_sha inside the loop after the parent was already rebased, making old_sha == new_parent_sha for branches 3+ in a chain. Now captures all SHAs before any rebasing begins, matching the Go version. Also: replace ugly __import__ with normal import for get_git_version.
1 parent cb0d8d9 commit 9740a57

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

src/gx/commands/sync.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
GitError,
2222
ensure_git_repo,
2323
get_current_branch,
24+
get_git_version,
2425
is_clean_working_tree,
2526
run_git,
2627
supports_update_refs,
@@ -68,16 +69,18 @@ def _sync_with_onto(chain: list[str]) -> bool:
6869
"""Sync using --onto iteration (Git < 2.38 fallback). Returns True on success."""
6970
console.print(" Rebasing stack (using --onto fallback)...")
7071

72+
# Capture all pre-rebase SHAs before any rebasing begins
73+
pre_rebase_sha: dict[str, str] = {}
74+
for b in chain:
75+
try:
76+
pre_rebase_sha[b] = run_git(["rev-parse", b])
77+
except GitError:
78+
pre_rebase_sha[b] = ""
79+
7180
for i in range(1, len(chain)):
7281
parent = chain[i - 1]
7382
branch = chain[i]
7483

75-
# Record old SHA before rebase
76-
try:
77-
old_sha = run_git(["rev-parse", parent])
78-
except GitError:
79-
old_sha = ""
80-
8184
if i == 1:
8285
# First branch: simple rebase
8386
try:
@@ -96,7 +99,7 @@ def _sync_with_onto(chain: list[str]) -> bool:
9699
print_error(f"Rebase of {branch} timed out.")
97100
return False
98101
else:
99-
# Subsequent branches: use --onto with old/new parent SHAs
102+
# Subsequent branches: use --onto with pre-rebase SHA as old base
100103
try:
101104
new_parent_sha = run_git(["rev-parse", parent])
102105
except GitError as e:
@@ -105,7 +108,7 @@ def _sync_with_onto(chain: list[str]) -> bool:
105108

106109
try:
107110
result = subprocess.run(
108-
["git", "rebase", "--onto", new_parent_sha, old_sha, branch],
111+
["git", "rebase", "--onto", new_parent_sha, pre_rebase_sha[parent], branch],
109112
capture_output=True,
110113
text=True,
111114
encoding="utf-8",
@@ -212,7 +215,7 @@ def sync(
212215

213216
if dry_run:
214217
strategy = "--update-refs" if use_update_refs else "--onto (fallback)"
215-
major, minor, _ = __import__("gx.utils.git", fromlist=["get_git_version"]).get_git_version()
218+
major, minor, _ = get_git_version()
216219
actions = [
217220
f"Would sync stack: {' -> '.join(chain)}",
218221
"",

0 commit comments

Comments
 (0)