From 4861179c6fd89eb5c9553d2c4b303e21fb8c018e Mon Sep 17 00:00:00 2001 From: SBPro Date: Tue, 17 Mar 2026 14:49:10 +0300 Subject: [PATCH] Fix linearize fallback for already-applied merge commits When cherry-pick -m 1 fails because the merge's changes were already applied by a prior cherry-pick (empty result, clean working tree), skip the commit instead of trying git-apply which also fails. Only use the diff-from-first-parent fallback for real conflicts. Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/sync.sh | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/sync.sh b/lib/sync.sh index 5c80de2..4fceae3 100644 --- a/lib/sync.sh +++ b/lib/sync.sh @@ -366,16 +366,17 @@ reverse_sync() { if git cherry-pick -m 1 "$sha" >/dev/null 2>&1; then log "INFO" " squash ${sha:0:7}: ${short_msg}" else - git cherry-pick --abort 2>/dev/null - # Diff from first parent — what the merge actually introduced. - # NOT from HEAD (which would capture all tree differences, not - # just this merge's changes). - if git diff "${sha}^1" "$sha" --quiet 2>/dev/null; then - # Empty diff from first parent — the merge introduced no new - # changes (e.g., a fast-forward merge or changes already applied - # by a prior cherry-pick). Skip silently. - log "INFO" " skip ${sha:0:7}: ${short_msg} (empty — already applied)" + # Cherry-pick failed — distinguish empty result (changes already + # applied by a prior cherry-pick) from a real conflict. + if git diff --cached --quiet 2>/dev/null && git diff --quiet 2>/dev/null; then + # Working tree clean — the merge changes are already present + git cherry-pick --abort 2>/dev/null || git reset --hard HEAD >/dev/null 2>&1 + log "INFO" " skip ${sha:0:7}: ${short_msg} (already applied)" else + # Real conflict — apply diff from first parent (what the merge + # actually introduced, NOT from HEAD which would capture all + # tree differences). + git cherry-pick --abort 2>/dev/null git diff "${sha}^1" "$sha" | git apply --index >/dev/null 2>&1 \ || die "Failed to apply merge diff for ${sha:0:7}" GIT_AUTHOR_NAME="$author_name" GIT_AUTHOR_EMAIL="$author_email" \