From e9fec1112f3fa88a9d3efa6dcb15d413b0713a6e Mon Sep 17 00:00:00 2001 From: "posthog[bot]" <206114724+posthog[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:25:30 +0000 Subject: [PATCH 1/3] fix(daemon): map a single-commit cherry-pick by content when the reflog entry has no cherry-pick action The cherry-pick side effect rebuilt the pick from HEAD reflog entries that contain "cherry-pick". When the entry for the new head has a different action (an inherited GIT_REFLOG_ACTION, or a hook that amends the pick), the side effect failed and the authorship note was not rewritten. For a single-commit pick, accept the reflog path from the parent of the new head to the new head when the new commit has the same stable patch id as the source commit. Log the reflog actions (not the commit subjects) as a warning, so the next hit shows the trigger. Co-Authored-By: Claude Opus 5.5 Generated-By: PostHog Desktop Task-Id: 530e5807-a8fc-4702-82b4-b02677f92a61 --- src/ci/ci_context.rs | 5 ++- src/daemon.rs | 59 ++++++++++++++++++++++++++- src/git/repo_state.rs | 68 ++++++++++++++++++++++++++++++++ tests/integration/cherry_pick.rs | 42 ++++++++++++++++++++ 4 files changed, 172 insertions(+), 2 deletions(-) diff --git a/src/ci/ci_context.rs b/src/ci/ci_context.rs index 66d2399..64911f9 100644 --- a/src/ci/ci_context.rs +++ b/src/ci/ci_context.rs @@ -975,7 +975,10 @@ fn sync_fetch_remote_supports_lazy_blobs( } } -fn stable_patch_id_for_commit(repo: &Repository, commit_sha: &str) -> Result { +pub(crate) fn stable_patch_id_for_commit( + repo: &Repository, + commit_sha: &str, +) -> Result { let mut show_args = repo.global_args_for_exec(); show_args.extend( [ diff --git a/src/daemon.rs b/src/daemon.rs index 389d561..ddb520a 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,3 +1,4 @@ +use crate::ci::ci_context::stable_patch_id_for_commit; use crate::config; use crate::daemon::domain::RepoContext; use crate::daemon::git_backend::GitBackend; @@ -9,7 +10,8 @@ use crate::git::cli_parser::{ use crate::git::find_repository_in_path; use crate::git::repo_state::{ HeadState, common_dir_for_git_dir, common_dir_for_worktree, git_dir_for_worktree, - latest_reflog_old_oid_for_worktree, read_head_state_for_worktree, read_ref_oid_for_worktree, + head_reflog_path_messages_for_worktree, latest_reflog_old_oid_for_worktree, + read_head_state_for_worktree, read_ref_oid_for_worktree, resolve_linear_head_commit_chain_for_worktree, resolve_rebase_segment_for_worktree, resolve_reflog_old_oid_for_ref_new_oid_in_worktree, resolve_squash_source_head_for_worktree, resolve_stash_target_oid_for_worktree, resolve_worktree_head_reflog_old_oid_for_new_head, @@ -3443,6 +3445,12 @@ fn strict_cherry_pick_mappings_from_command( Err(err) => last_err = err.to_string(), } } + if let [source_commit] = source_commits.as_slice() + && let Some(original_head) = + recover_single_cherry_pick_by_content(worktree, source_commit, new_head, context) + { + return Ok((original_head, source_commits, vec![new_head.to_string()])); + } Err(AutterError::Generic(format!( "{} failed to reconstruct cherry-pick commits new={} expected_count={}: {}", context, @@ -3452,6 +3460,55 @@ fn strict_cherry_pick_mappings_from_command( ))) } +/// Recover a single-commit pick whose HEAD reflog entry does not say +/// "cherry-pick", for example when a hook amends the picked commit or an +/// inherited `GIT_REFLOG_ACTION` renames the entry. Accept it when the reflog +/// moved HEAD from the parent of `new_head` to `new_head` and the new commit +/// applies the same patch as the source commit. Returns the original head. +fn recover_single_cherry_pick_by_content( + worktree: &Path, + source_commit: &str, + new_head: &str, + context: &str, +) -> Option { + let repo = find_repository_in_path(&worktree.to_string_lossy()).ok()?; + let original_head = repo + .find_commit(new_head.to_string()) + .ok()? + .parent(0) + .ok()? + .id(); + let messages = + head_reflog_path_messages_for_worktree(worktree, &original_head, new_head).ok()??; + let content_matches = matches!( + ( + stable_patch_id_for_commit(&repo, source_commit), + stable_patch_id_for_commit(&repo, new_head), + ), + (Ok(source), Ok(new)) if source == new + ); + // Only the action part of each reflog message: the rest is the commit subject. + let reflog_actions = messages + .iter() + .map(|message| { + message + .split_once(": ") + .map_or(message.as_str(), |(action, _)| action) + }) + .collect::>(); + crate::observability::log_message( + "cherry-pick HEAD reflog entry without cherry-pick action", + "warn", + Some(serde_json::json!({ + "component": "daemon", + "phase": context, + "reflog_actions": reflog_actions, + "content_matches": content_matches, + })), + ); + content_matches.then_some(original_head) +} + /// Match source commits to new commits by commit subject (first line of message). /// /// Cherry-pick preserves commit messages, so we can align source commits with new commits diff --git a/src/git/repo_state.rs b/src/git/repo_state.rs index 733cdc0..e012589 100644 --- a/src/git/repo_state.rs +++ b/src/git/repo_state.rs @@ -535,6 +535,36 @@ pub fn resolve_linear_head_commit_chain_for_worktree( } } +/// Walk the HEAD reflog back from the newest entry that moved HEAD to +/// `new_head` until an entry that moved HEAD away from `original_head`. +/// Returns the messages on that path, newest first, or `None` when the +/// reflog has no such path. +pub fn head_reflog_path_messages_for_worktree( + worktree: &Path, + original_head: &str, + new_head: &str, +) -> Result>, AutterError> { + let transitions = read_head_reflog_transitions_for_worktree(worktree)?; + let Some(mut cursor) = transitions.iter().rposition(|t| t.new == new_head) else { + return Ok(None); + }; + let mut messages = Vec::new(); + loop { + let current = &transitions[cursor]; + messages.push(current.message.clone()); + if current.old == original_head { + return Ok(Some(messages)); + } + let Some(previous) = transitions[..cursor] + .iter() + .rposition(|t| t.new == current.old) + else { + return Ok(None); + }; + cursor = previous; + } +} + #[cfg(test)] mod tests { use super::*; @@ -813,4 +843,42 @@ mod tests { assert_eq!(resolved_original, original); assert_eq!(commits, vec![commit.to_string()]); } + + #[test] + fn head_reflog_path_messages_for_worktree_follows_amend_after_pick() { + let temp = tempfile::tempdir().unwrap(); + let worktree = temp.path(); + let git_dir = worktree.join(".git"); + let original = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + let picked = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + let amended = "cccccccccccccccccccccccccccccccccccccccc"; + write_file(&git_dir.join("HEAD"), "ref: refs/heads/main\n"); + write_file( + &git_dir.join("logs/HEAD"), + &format!( + concat!( + "{original} {picked} Test 0 -0000\tcherry-pick: feature\n", + "{picked} {amended} Test 0 -0000\tcommit (amend): feature\n", + ), + original = original, + picked = picked, + amended = amended + ), + ); + + let messages = head_reflog_path_messages_for_worktree(worktree, original, amended) + .unwrap() + .unwrap(); + assert_eq!( + messages, + vec![ + "commit (amend): feature".to_string(), + "cherry-pick: feature".to_string() + ] + ); + assert_eq!( + head_reflog_path_messages_for_worktree(worktree, picked, original).unwrap(), + None + ); + } } diff --git a/tests/integration/cherry_pick.rs b/tests/integration/cherry_pick.rs index 6c90bb4..79a1cf0 100644 --- a/tests/integration/cherry_pick.rs +++ b/tests/integration/cherry_pick.rs @@ -75,6 +75,48 @@ fn test_single_commit_cherry_pick() { } } +/// An inherited `GIT_REFLOG_ACTION` replaces "cherry-pick" in the HEAD reflog +/// entry. The daemon must still map the picked commit by its content. +#[test] +fn test_single_commit_cherry_pick_with_custom_reflog_action() { + let repo = TestRepo::new(); + + let mut file = repo.filename("file.txt"); + file.set_contents(crate::lines!["Initial content"]); + repo.stage_all_and_commit("Initial commit").unwrap(); + file.assert_committed_lines(crate::lines!["Initial content".human()]); + + let main_branch = repo.current_branch(); + + repo.git(&["checkout", "-b", "feature"]).unwrap(); + file.insert_at(1, crate::lines!["AI feature line".ai()]); + repo.stage_all_and_commit("Add AI feature").unwrap(); + file.assert_committed_lines(crate::lines![ + "Initial content".human(), + "AI feature line".ai(), + ]); + let feature_commit = repo.git(&["rev-parse", "HEAD"]).unwrap().trim().to_string(); + + repo.git(&["checkout", &main_branch]).unwrap(); + repo.git_with_env( + &["cherry-pick", &feature_commit], + &[("GIT_REFLOG_ACTION", "custom-tool")], + None, + ) + .unwrap(); + + let reflog_subject = repo.git(&["reflog", "-1", "--format=%gs", "HEAD"]).unwrap(); + assert!( + !reflog_subject.contains("cherry-pick"), + "reflog entry should not name cherry-pick: {reflog_subject}" + ); + + file.assert_committed_lines(crate::lines![ + "Initial content".human(), + "AI feature line".ai(), + ]); +} + #[test] fn test_cherry_pick_preserves_human_only_commit_note_metadata() { let repo = TestRepo::new(); From 289f83ffba5246705336b8e7c4bc5f561f9e6403 Mon Sep 17 00:00:00 2001 From: "posthog[bot]" <206114724+posthog[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:28:18 +0000 Subject: [PATCH 2/3] refactor(daemon): reuse commit_parent_head_for_capture in the cherry-pick content fallback Co-Authored-By: Claude Opus 5.5 Generated-By: PostHog Desktop Task-Id: 530e5807-a8fc-4702-82b4-b02677f92a61 --- src/daemon.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index ddb520a..c64c9ca 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -3472,12 +3472,7 @@ fn recover_single_cherry_pick_by_content( context: &str, ) -> Option { let repo = find_repository_in_path(&worktree.to_string_lossy()).ok()?; - let original_head = repo - .find_commit(new_head.to_string()) - .ok()? - .parent(0) - .ok()? - .id(); + let original_head = commit_parent_head_for_capture(&repo, new_head)?; let messages = head_reflog_path_messages_for_worktree(worktree, &original_head, new_head).ok()??; let content_matches = matches!( From 2c610e116ccc6bf43b005f3097d570e9b767a63e Mon Sep 17 00:00:00 2001 From: Sagnik Ghosh Date: Fri, 13 Nov 2026 21:55:20 +0530 Subject: [PATCH 3/3] fix(daemon): only map a single cherry-pick by content when the pick wrote the landing reflog entry The content fallback accepted any commit whose parent-to-commit reflog path existed and whose patch id matched the source. A stale ref state could then name a commit that was already on the branch (a plain `commit:` entry with the same patch) and rewrite its note with the source's attribution. The fallback now requires the HEAD reflog entry that landed on the new head to pass head_reflog_entry_written_by("cherry-pick", ...) (which also accepts a renamed GIT_REFLOG_ACTION entry) or to be `commit (amend)`. The warn log records whether the landing entry was accepted. Co-Authored-By: Claude Opus 5.5 --- src/daemon.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 104 insertions(+), 14 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index b54e30c..b3fd3c1 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -12,11 +12,10 @@ use crate::git::repo_state::{ HeadState, common_dir_for_git_dir, common_dir_for_worktree, git_dir_for_worktree, head_reflog_entry_written_by, head_reflog_path_messages_for_worktree, is_valid_git_oid, latest_worktree_head_reflog_move, read_head_state_for_worktree, read_ref_oid_for_worktree, - read_stash_stack_for_worktree, - resolve_linear_head_commit_chain_for_worktree, resolve_rebase_segment_for_worktree, - resolve_reflog_old_oid_for_ref_new_oid_in_worktree, resolve_squash_source_head_for_worktree, - resolve_stash_target_oid_for_worktree, resolve_worktree_head_reflog_old_oid_for_new_head, - worktree_root_for_path, + read_stash_stack_for_worktree, resolve_linear_head_commit_chain_for_worktree, + resolve_rebase_segment_for_worktree, resolve_reflog_old_oid_for_ref_new_oid_in_worktree, + resolve_squash_source_head_for_worktree, resolve_stash_target_oid_for_worktree, + resolve_worktree_head_reflog_old_oid_for_new_head, worktree_root_for_path, }; use crate::git::repository::{ Repository, discover_repository_in_path_no_git_exec, exec_git, @@ -3723,11 +3722,22 @@ fn strict_cherry_pick_mappings_from_command( ))) } +/// Whether the HEAD reflog entry that landed on the new head can come from the +/// pick itself: an entry `git cherry-pick` writes (including a renamed one from +/// an inherited `GIT_REFLOG_ACTION`), or a hook amending the picked commit. +/// Anything else (`commit:`, `reset:`, `checkout:` …) means `new_head` is a +/// commit that existed before the pick, which must not be mapped by content. +fn cherry_pick_content_fallback_allowed(landing_message: &str) -> bool { + head_reflog_entry_written_by("cherry-pick", landing_message) + || landing_message.starts_with("commit (amend)") +} + /// Recover a single-commit pick whose HEAD reflog entry does not say /// "cherry-pick", for example when a hook amends the picked commit or an /// inherited `GIT_REFLOG_ACTION` renames the entry. Accept it when the reflog -/// moved HEAD from the parent of `new_head` to `new_head` and the new commit -/// applies the same patch as the source commit. Returns the original head. +/// moved HEAD from the parent of `new_head` to `new_head`, the entry landing on +/// `new_head` passes [`cherry_pick_content_fallback_allowed`], and the new +/// commit applies the same patch as the source commit. Returns the original head. fn recover_single_cherry_pick_by_content( worktree: &Path, source_commit: &str, @@ -3738,13 +3748,18 @@ fn recover_single_cherry_pick_by_content( let original_head = commit_parent_head_for_capture(&repo, new_head)?; let messages = head_reflog_path_messages_for_worktree(worktree, &original_head, new_head).ok()??; - let content_matches = matches!( - ( - stable_patch_id_for_commit(&repo, source_commit), - stable_patch_id_for_commit(&repo, new_head), - ), - (Ok(source), Ok(new)) if source == new - ); + // The walk starts at the entry that landed on `new_head`. + let landing_allowed = messages + .first() + .is_some_and(|message| cherry_pick_content_fallback_allowed(message)); + let content_matches = landing_allowed + && matches!( + ( + stable_patch_id_for_commit(&repo, source_commit), + stable_patch_id_for_commit(&repo, new_head), + ), + (Ok(source), Ok(new)) if source == new + ); // Only the action part of each reflog message: the rest is the commit subject. let reflog_actions = messages .iter() @@ -3761,6 +3776,7 @@ fn recover_single_cherry_pick_by_content( "component": "daemon", "phase": context, "reflog_actions": reflog_actions, + "landing_allowed": landing_allowed, "content_matches": content_matches, })), ); @@ -13609,6 +13625,80 @@ mod tests { coord.request_shutdown(); } + /// A base commit on `main`, then a `feature` branch with one commit that + /// adds a line. Returns (repo, base, feature commit), back on `main`. + fn repo_with_feature_commit() -> (crate::git::test_utils::TmpRepo, String, String) { + let tmp = crate::git::test_utils::TmpRepo::new().unwrap(); + tmp.write_file("file.txt", "base\n", true).unwrap(); + let base = tmp.commit_all("base").unwrap(); + tmp.git_command(&["switch", "-c", "feature"]).unwrap(); + tmp.write_file("file.txt", "base\nfeature line\n", true) + .unwrap(); + let feature = tmp.commit_all("add feature line").unwrap(); + tmp.switch_branch("main").unwrap(); + (tmp, base, feature) + } + + #[test] + fn cherry_pick_content_fallback_accepts_pick_then_amend() { + let (tmp, base, feature) = repo_with_feature_commit(); + tmp.git_command(&["cherry-pick", &feature]).unwrap(); + tmp.git_command(&["commit", "--amend", "--no-edit"]) + .unwrap(); + let new_head = tmp + .git_command(&["rev-parse", "HEAD"]) + .unwrap() + .trim() + .to_string(); + + assert_eq!( + recover_single_cherry_pick_by_content(tmp.path(), &feature, &new_head, "test"), + Some(base) + ); + } + + // A stale ref state can name a commit that was on the branch before the + // pick and happens to have the source's patch id. Its reflog entry is a + // plain `commit:`, so the fallback must not map it to the source. + #[test] + fn cherry_pick_content_fallback_rejects_existing_commit_with_same_patch() { + let (tmp, _base, feature) = repo_with_feature_commit(); + tmp.write_file("file.txt", "base\nfeature line\n", true) + .unwrap(); + let manual = tmp.commit_all("add feature line by hand").unwrap(); + + assert_eq!( + recover_single_cherry_pick_by_content(tmp.path(), &feature, &manual, "test"), + None + ); + } + + #[test] + fn cherry_pick_content_fallback_landing_actions() { + for accepted in [ + "cherry-pick: add feature line", + "commit (cherry-pick): add feature line", + "commit (amend): add feature line", + "custom-tool: add feature line", + ] { + assert!(cherry_pick_content_fallback_allowed(accepted), "{accepted}"); + } + for rejected in [ + "commit: add feature line", + "commit (initial): add feature line", + "commit (merge): add feature line", + "reset: moving to HEAD~1", + "checkout: moving from feature to main", + "rebase (pick): add feature line", + "merge feature: Fast-forward", + ] { + assert!( + !cherry_pick_content_fallback_allowed(rejected), + "{rejected}" + ); + } + } + fn init_repo_with_stash_create_event(stash_sha: &str) -> crate::git::test_utils::TmpRepo { let tmp = crate::git::test_utils::TmpRepo::new().unwrap(); tmp.autter_repo()