mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
[git-utils] gate merge drivers on three-way fallback
This commit is contained in:
@@ -57,23 +57,8 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result<ApplyGitResult> {
|
||||
ensure_no_selected_executable_git_filters(&git_root, &patch_paths, &cfg_parts)?;
|
||||
ensure_paths_do_not_enter_submodules(&git_root, &patch_paths)?;
|
||||
|
||||
if !req.preflight {
|
||||
ensure_no_selected_merge_drivers(&git_root, &patch_paths, &cfg_parts)?;
|
||||
}
|
||||
|
||||
if req.revert && !req.preflight {
|
||||
// Stage WT paths first to avoid index mismatch on revert.
|
||||
stage_effective_paths(&git_root, &patch_paths)?;
|
||||
}
|
||||
// Build git args
|
||||
let mut args: Vec<String> = vec!["apply".into(), "--3way".into()];
|
||||
if req.revert {
|
||||
args.push("-R".into());
|
||||
}
|
||||
|
||||
cfg_parts.extend(safe_git_config_parts());
|
||||
|
||||
args.push(patch_path.to_string_lossy().to_string());
|
||||
let patch_arg = patch_path.to_string_lossy().to_string();
|
||||
|
||||
// Optional preflight: dry-run only; do not modify working tree
|
||||
if req.preflight {
|
||||
@@ -81,7 +66,7 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result<ApplyGitResult> {
|
||||
if req.revert {
|
||||
check_args.push("-R".to_string());
|
||||
}
|
||||
check_args.push(patch_path.to_string_lossy().to_string());
|
||||
check_args.push(patch_arg);
|
||||
let rendered = render_command_for_log(&git_root, &cfg_parts, &check_args);
|
||||
let (c_code, c_out, c_err) = run_git(&git_root, &cfg_parts, &check_args)?;
|
||||
let (mut applied_paths, mut skipped_paths, mut conflicted_paths) =
|
||||
@@ -103,6 +88,35 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result<ApplyGitResult> {
|
||||
});
|
||||
}
|
||||
|
||||
// Avoid three-way machinery entirely when the patch applies cleanly.
|
||||
// A selected merge driver is relevant only to the three-way fallback.
|
||||
let mut plain_check_args = vec![
|
||||
"apply".to_string(),
|
||||
"--check".to_string(),
|
||||
"--index".to_string(),
|
||||
];
|
||||
if req.revert {
|
||||
plain_check_args.push("-R".to_string());
|
||||
}
|
||||
plain_check_args.push(patch_arg.clone());
|
||||
let (plain_check_code, _, _) = run_git(&git_root, &cfg_parts, &plain_check_args)?;
|
||||
|
||||
let mut args = vec!["apply".to_string()];
|
||||
if plain_check_code != 0 {
|
||||
ensure_no_selected_merge_drivers(&git_root, &patch_paths, &cfg_parts)?;
|
||||
if req.revert {
|
||||
// Stage WT paths first to avoid index mismatch on three-way revert.
|
||||
stage_effective_paths(&git_root, &patch_paths)?;
|
||||
}
|
||||
args.push("--3way".to_string());
|
||||
} else {
|
||||
args.push("--index".to_string());
|
||||
}
|
||||
if req.revert {
|
||||
args.push("-R".to_string());
|
||||
}
|
||||
args.push(patch_arg);
|
||||
|
||||
let cmd_for_log = render_command_for_log(&git_root, &cfg_parts, &args);
|
||||
let (code, stdout, stderr) = run_git(&git_root, &cfg_parts, &args)?;
|
||||
|
||||
|
||||
@@ -50,6 +50,113 @@ fn apply_allows_unused_global_merge_driver() {
|
||||
assert_ne!(marker_code, 0, "unused merge driver must not run");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_allows_clean_patch_with_selected_merge_driver() {
|
||||
let repo = init_repo();
|
||||
let root = repo.path();
|
||||
std::fs::write(root.join(".gitattributes"), "file.txt merge=codex-test\n")
|
||||
.expect("write attributes");
|
||||
std::fs::write(root.join("file.txt"), "old\n").expect("write file");
|
||||
let (add_code, _, add_err) = run(root, &["git", "add", "."]);
|
||||
assert_eq!(add_code, 0, "add fixture: {add_err}");
|
||||
let (commit_code, _, commit_err) = run(root, &["git", "commit", "-m", "fixture"]);
|
||||
assert_eq!(commit_code, 0, "commit fixture: {commit_err}");
|
||||
let (config_code, _, config_err) = run(
|
||||
root,
|
||||
&[
|
||||
"git",
|
||||
"config",
|
||||
"merge.codex-test.driver",
|
||||
"git config codex.mergeran true && false",
|
||||
],
|
||||
);
|
||||
assert_eq!(config_code, 0, "configure merge driver: {config_err}");
|
||||
|
||||
let result = apply_git_patch(&ApplyGitRequest {
|
||||
cwd: root.to_path_buf(),
|
||||
diff: "diff --git a/file.txt b/file.txt\n--- a/file.txt\n+++ b/file.txt\n@@ -1 +1 @@\n-old\n+new\n".to_string(),
|
||||
revert: false,
|
||||
preflight: false,
|
||||
})
|
||||
.expect("allow clean patch with selected merge driver");
|
||||
|
||||
assert_eq!(result.exit_code, 0);
|
||||
assert!(!result.cmd_for_log.contains("--3way"));
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("file.txt")).expect("read file"),
|
||||
"new\n"
|
||||
);
|
||||
let (marker_code, _, _) = run(root, &["git", "config", "--get", "codex.mergeran"]);
|
||||
assert_ne!(marker_code, 0, "merge driver must not run");
|
||||
let (status_code, status, status_err) = run(root, &["git", "status", "--porcelain"]);
|
||||
assert_eq!(status_code, 0, "status: {status_err}");
|
||||
assert_eq!(
|
||||
status.trim(),
|
||||
"M file.txt",
|
||||
"patch should update the index"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_apply_allows_clean_patch_with_selected_merge_driver() {
|
||||
let repo = init_repo();
|
||||
let root = repo.path();
|
||||
std::fs::write(root.join(".gitattributes"), "file.txt merge=codex-test\n")
|
||||
.expect("write attributes");
|
||||
std::fs::write(root.join("file.txt"), "old\n").expect("write file");
|
||||
let (add_code, _, add_err) = run(root, &["git", "add", "."]);
|
||||
assert_eq!(add_code, 0, "add fixture: {add_err}");
|
||||
let (commit_code, _, commit_err) = run(root, &["git", "commit", "-m", "fixture"]);
|
||||
assert_eq!(commit_code, 0, "commit fixture: {commit_err}");
|
||||
let (config_code, _, config_err) = run(
|
||||
root,
|
||||
&[
|
||||
"git",
|
||||
"config",
|
||||
"merge.codex-test.driver",
|
||||
"git config codex.mergeran true && false",
|
||||
],
|
||||
);
|
||||
assert_eq!(config_code, 0, "configure merge driver: {config_err}");
|
||||
let diff = "diff --git a/file.txt b/file.txt\n--- a/file.txt\n+++ b/file.txt\n@@ -1 +1 @@\n-old\n+new\n";
|
||||
let patch_dir = tempfile::tempdir().expect("patch tempdir");
|
||||
let patch = patch_dir.path().join("change.diff");
|
||||
std::fs::write(&patch, diff).expect("write patch");
|
||||
let (apply_code, _, apply_err) = run(
|
||||
root,
|
||||
&[
|
||||
"git",
|
||||
"apply",
|
||||
"--index",
|
||||
patch.to_str().expect("patch path"),
|
||||
],
|
||||
);
|
||||
assert_eq!(apply_code, 0, "prepare forward apply: {apply_err}");
|
||||
|
||||
let result = apply_git_patch(&ApplyGitRequest {
|
||||
cwd: root.to_path_buf(),
|
||||
diff: diff.to_string(),
|
||||
revert: true,
|
||||
preflight: false,
|
||||
})
|
||||
.expect("allow clean reverse patch with selected merge driver");
|
||||
|
||||
assert_eq!(result.exit_code, 0);
|
||||
assert!(!result.cmd_for_log.contains("--3way"));
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("file.txt")).expect("read file"),
|
||||
"old\n"
|
||||
);
|
||||
let (marker_code, _, _) = run(root, &["git", "config", "--get", "codex.mergeran"]);
|
||||
assert_ne!(marker_code, 0, "merge driver must not run");
|
||||
let (status_code, status, status_err) = run(root, &["git", "status", "--porcelain"]);
|
||||
assert_eq!(status_code, 0, "status: {status_err}");
|
||||
assert!(
|
||||
status.is_empty(),
|
||||
"reverse apply should restore HEAD: {status}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preflight_does_not_probe_or_run_selected_merge_driver() {
|
||||
let repo = init_repo();
|
||||
|
||||
Reference in New Issue
Block a user