mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
git-utils: fix direct reverse apply reporting
This commit is contained in:
@@ -15,7 +15,7 @@ use crate::apply_output::parse_git_apply_output;
|
||||
use crate::git_command::GitRunner;
|
||||
use crate::merge_driver::ensure_no_selected_merge_drivers;
|
||||
use crate::patch_paths::extract_effective_paths_from_patch;
|
||||
use crate::patch_paths::stage_effective_paths;
|
||||
use crate::patch_paths::stage_effective_paths_for_reverse;
|
||||
use crate::safe_git::DISABLED_HOOKS_PATH;
|
||||
use crate::safe_git::ensure_no_selected_executable_git_filters;
|
||||
#[cfg(test)]
|
||||
@@ -114,12 +114,17 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result<ApplyGitResult> {
|
||||
guard
|
||||
} else {
|
||||
args.push("--index".to_string());
|
||||
// Direct applications otherwise succeed silently, which leaves the
|
||||
// caller unable to report which files were changed. `--verbose`
|
||||
// emits the same parser-compatible "Applied patch ... cleanly"
|
||||
// records that the three-way path already provides.
|
||||
args.push("--verbose".to_string());
|
||||
None
|
||||
};
|
||||
if req.revert {
|
||||
// Stage only after the worktree-only applicability decision. The
|
||||
// guarded config is shared with the final Git command.
|
||||
stage_effective_paths(&git, &git_root, &patch_paths, &cfg_parts)?;
|
||||
stage_effective_paths_for_reverse(&git, &git_root, &patch_paths, &cfg_parts)?;
|
||||
}
|
||||
if req.revert {
|
||||
args.push("-R".to_string());
|
||||
@@ -435,6 +440,7 @@ mod tests {
|
||||
};
|
||||
let r = apply_git_patch(&req).expect("run apply");
|
||||
assert_eq!(r.exit_code, 0, "exit code 0");
|
||||
assert_eq!(r.applied_paths, vec!["hello.txt"]);
|
||||
// File exists now
|
||||
assert!(root.join("hello.txt").exists());
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use std::time::Instant;
|
||||
|
||||
const OLD_DRIVER_COMMAND: &str = "git config codex.oldmergeran true && false";
|
||||
const NEW_DRIVER_COMMAND: &str = "git config codex.newmergeran true && false";
|
||||
const TRACE_MUTATION_MARKER: &str = "CODEX_MERGE_DRIVER_TEST_MUTATION";
|
||||
|
||||
fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) {
|
||||
let mut command = std::process::Command::new("git");
|
||||
@@ -139,6 +140,29 @@ fn wait_for_merge_attribute_probe(trace: &Path) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn record_trace_mutation(trace: &Path) {
|
||||
let mut trace = OpenOptions::new()
|
||||
.append(true)
|
||||
.open(trace)
|
||||
.expect("open trace for mutation marker");
|
||||
writeln!(trace, "{TRACE_MUTATION_MARKER}").expect("record mutation marker");
|
||||
}
|
||||
|
||||
fn assert_mutation_happened_between_probe_and_three_way(trace: &Path) {
|
||||
let contents = std::fs::read_to_string(trace).expect("read completed trace");
|
||||
let probe_start = contents.find("check-attr").expect("merge attribute probe");
|
||||
let probe_exit = probe_start
|
||||
+ contents[probe_start..]
|
||||
.find("\"event\":\"exit\"")
|
||||
.expect("merge attribute probe exit");
|
||||
let mutation = contents
|
||||
.find(TRACE_MUTATION_MARKER)
|
||||
.expect("mutation trace marker");
|
||||
let three_way = contents.find("--3way").expect("final three-way apply");
|
||||
assert!(probe_exit < mutation, "mutation preceded probe exit");
|
||||
assert!(mutation < three_way, "final Git started before mutation");
|
||||
}
|
||||
|
||||
fn append_driver_config(root: &Path, driver: &str, command: &str) {
|
||||
let mut config = OpenOptions::new()
|
||||
.append(true)
|
||||
@@ -164,6 +188,7 @@ fn clean_reverse_checks_worktree_before_staging_and_skips_merge_driver() {
|
||||
.expect("clean reverse apply");
|
||||
|
||||
assert_eq!(result.exit_code, 0, "{}", result.stderr);
|
||||
assert_eq!(result.applied_paths, vec!["file.txt"]);
|
||||
assert!(result.cmd_for_log.contains("--index"));
|
||||
assert!(!result.cmd_for_log.contains("--3way"));
|
||||
assert_eq!(
|
||||
@@ -174,6 +199,65 @@ fn clean_reverse_checks_worktree_before_staging_and_skips_merge_driver() {
|
||||
assert!(run_success(root, &["status", "--porcelain"]).is_empty());
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum ReverseTopology {
|
||||
Delete,
|
||||
Rename,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_reverse_stages_missing_delete_and_rename_endpoints() {
|
||||
for topology in [ReverseTopology::Delete, ReverseTopology::Rename] {
|
||||
let repo = init_repo();
|
||||
let root = repo.path();
|
||||
std::fs::write(root.join(".gitattributes"), "*.txt merge=demo\n")
|
||||
.expect("write attributes");
|
||||
std::fs::write(root.join("old.txt"), "old\n").expect("write old file");
|
||||
run_success(root, &["add", ".gitattributes", "old.txt"]);
|
||||
run_success(root, &["commit", "-m", "base"]);
|
||||
configure_driver(root, "demo", OLD_DRIVER_COMMAND);
|
||||
|
||||
match topology {
|
||||
ReverseTopology::Delete => {
|
||||
run_success(root, &["rm", "old.txt"]);
|
||||
}
|
||||
ReverseTopology::Rename => {
|
||||
run_success(root, &["mv", "old.txt", "new.txt"]);
|
||||
}
|
||||
}
|
||||
let patch = run_success(root, &["diff", "--cached", "--full-index", "--binary"]);
|
||||
run_success(root, &["reset", "--hard", "HEAD"]);
|
||||
match topology {
|
||||
ReverseTopology::Delete => {
|
||||
std::fs::remove_file(root.join("old.txt")).expect("delete worktree file");
|
||||
}
|
||||
ReverseTopology::Rename => {
|
||||
std::fs::rename(root.join("old.txt"), root.join("new.txt"))
|
||||
.expect("rename worktree file");
|
||||
}
|
||||
}
|
||||
|
||||
let result = apply_git_patch(&request(root, patch, /*revert*/ true))
|
||||
.expect("clean topology reverse");
|
||||
|
||||
assert_eq!(result.exit_code, 0, "{topology:?}: {}", result.stderr);
|
||||
assert_eq!(result.applied_paths.len(), 1, "{topology:?}");
|
||||
assert!(result.cmd_for_log.contains("--index"), "{topology:?}");
|
||||
assert!(!result.cmd_for_log.contains("--3way"), "{topology:?}");
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("old.txt")).expect("restored old file"),
|
||||
"old\n",
|
||||
"{topology:?}"
|
||||
);
|
||||
assert!(!root.join("new.txt").exists(), "{topology:?}");
|
||||
assert!(!configured_marker_exists(root, "codex.oldmergeran"));
|
||||
assert!(
|
||||
run_success(root, &["status", "--porcelain"]).is_empty(),
|
||||
"{topology:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_empty_and_equals_named_drivers_reject_without_marker_or_index_mutation() {
|
||||
for (driver, attribute) in [
|
||||
@@ -218,12 +302,14 @@ fn post_probe_attribute_change_cannot_run_empty_named_driver() {
|
||||
configure_driver(root, "", OLD_DRIVER_COMMAND);
|
||||
let trace = trace_path();
|
||||
std::fs::write(&trace, "").expect("clear fixture trace");
|
||||
let watcher_trace = trace.clone();
|
||||
let attributes = root.join(".gitattributes");
|
||||
let watcher = thread::spawn(move || {
|
||||
let observed = wait_for_merge_attribute_probe(&trace);
|
||||
let observed = wait_for_merge_attribute_probe(&watcher_trace);
|
||||
if observed {
|
||||
std::fs::write(attributes, "target.txt merge=\n")
|
||||
.expect("select empty-name merge driver");
|
||||
record_trace_mutation(&watcher_trace);
|
||||
}
|
||||
observed
|
||||
});
|
||||
@@ -232,6 +318,7 @@ fn post_probe_attribute_change_cannot_run_empty_named_driver() {
|
||||
.expect("neutralized three-way apply");
|
||||
assert!(watcher.join().expect("attribute watcher"));
|
||||
|
||||
assert_mutation_happened_between_probe_and_three_way(&trace);
|
||||
assert_ne!(result.exit_code, 0);
|
||||
assert!(!configured_marker_exists(root, "codex.oldmergeran"));
|
||||
}
|
||||
@@ -251,9 +338,10 @@ fn post_probe_attribute_and_same_driver_command_change_stays_neutralized() {
|
||||
configure_driver(root, "x=y", OLD_DRIVER_COMMAND);
|
||||
let trace = trace_path();
|
||||
std::fs::write(&trace, "").expect("clear fixture trace");
|
||||
let watcher_trace = trace.clone();
|
||||
let watcher_root = root.to_path_buf();
|
||||
let watcher = thread::spawn(move || {
|
||||
let observed = wait_for_merge_attribute_probe(&trace);
|
||||
let observed = wait_for_merge_attribute_probe(&watcher_trace);
|
||||
if observed {
|
||||
append_driver_config(&watcher_root, "x=y", NEW_DRIVER_COMMAND);
|
||||
std::fs::write(
|
||||
@@ -261,6 +349,7 @@ fn post_probe_attribute_and_same_driver_command_change_stays_neutralized() {
|
||||
"target.txt merge=x=y\n",
|
||||
)
|
||||
.expect("select replacement merge driver");
|
||||
record_trace_mutation(&watcher_trace);
|
||||
}
|
||||
observed
|
||||
});
|
||||
@@ -269,6 +358,7 @@ fn post_probe_attribute_and_same_driver_command_change_stays_neutralized() {
|
||||
.expect("neutralized three-way apply");
|
||||
assert!(watcher.join().expect("config watcher"));
|
||||
|
||||
assert_mutation_happened_between_probe_and_three_way(&trace);
|
||||
assert_ne!(result.exit_code, 0);
|
||||
assert!(!configured_marker_exists(root, "codex.oldmergeran"));
|
||||
assert!(!configured_marker_exists(root, "codex.newmergeran"));
|
||||
|
||||
@@ -239,26 +239,75 @@ pub(crate) fn stage_effective_paths(
|
||||
git_root: &Path,
|
||||
paths: &[String],
|
||||
git_config_args: &[String],
|
||||
) -> io::Result<()> {
|
||||
stage_effective_paths_with_missing(
|
||||
git,
|
||||
git_root,
|
||||
paths,
|
||||
git_config_args,
|
||||
MissingPathPolicy::Ignore,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn stage_effective_paths_for_reverse(
|
||||
git: &GitRunner,
|
||||
git_root: &Path,
|
||||
paths: &[String],
|
||||
git_config_args: &[String],
|
||||
) -> io::Result<()> {
|
||||
stage_effective_paths_with_missing(
|
||||
git,
|
||||
git_root,
|
||||
paths,
|
||||
git_config_args,
|
||||
MissingPathPolicy::StageTrackedDeletion,
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
enum MissingPathPolicy {
|
||||
Ignore,
|
||||
StageTrackedDeletion,
|
||||
}
|
||||
|
||||
fn stage_effective_paths_with_missing(
|
||||
git: &GitRunner,
|
||||
git_root: &Path,
|
||||
paths: &[String],
|
||||
git_config_args: &[String],
|
||||
missing_path_policy: MissingPathPolicy,
|
||||
) -> io::Result<()> {
|
||||
let confined = confine_patch_paths(git, git_root, paths)?;
|
||||
let mut existing = Vec::new();
|
||||
let mut staged_paths = Vec::new();
|
||||
let mut content_filter_paths = Vec::new();
|
||||
for path in confined.into_exact_leaves()? {
|
||||
let joined = git_root.join(&path);
|
||||
if let Ok(metadata) = std::fs::symlink_metadata(&joined) {
|
||||
let file_type = metadata.file_type();
|
||||
if leaf_is_traversable_directory(file_type) {
|
||||
return Err(containment_error(
|
||||
"refusing to recursively stage a directory patch path",
|
||||
));
|
||||
match std::fs::symlink_metadata(&joined) {
|
||||
Ok(metadata) => {
|
||||
let file_type = metadata.file_type();
|
||||
if leaf_is_traversable_directory(file_type) {
|
||||
return Err(containment_error(
|
||||
"refusing to recursively stage a directory patch path",
|
||||
));
|
||||
}
|
||||
if leaf_may_run_git_content_filter(file_type) {
|
||||
content_filter_paths.push(path.clone());
|
||||
}
|
||||
staged_paths.push(path);
|
||||
}
|
||||
if leaf_may_run_git_content_filter(file_type) {
|
||||
content_filter_paths.push(path.clone());
|
||||
Err(error)
|
||||
if error.kind() == io::ErrorKind::NotFound
|
||||
&& missing_path_policy == MissingPathPolicy::StageTrackedDeletion =>
|
||||
{
|
||||
// A clean reverse of a deletion or rename needs the missing
|
||||
// old endpoint staged as a deletion before direct `git apply
|
||||
// --index -R`. Missing leaves cannot run a content filter.
|
||||
staged_paths.push(path);
|
||||
}
|
||||
existing.push(path);
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
if existing.is_empty() {
|
||||
if staged_paths.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let filter_guard =
|
||||
@@ -268,7 +317,7 @@ pub(crate) fn stage_effective_paths(
|
||||
"add".to_string(),
|
||||
"--".to_string(),
|
||||
];
|
||||
args.extend(existing);
|
||||
args.extend(staged_paths);
|
||||
let mut guarded_config = git_config_args.to_vec();
|
||||
guarded_config.extend_from_slice(filter_guard.git_config_args());
|
||||
let (_code, _, _) = run_git(git, git_root, &guarded_config, &args)?;
|
||||
|
||||
Reference in New Issue
Block a user