mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
Fix untracked file handling in the TUI Git diff (#64616)
## Why The TUI can silently omit untracked files when Git quotes their names, when their names have leading or trailing whitespace, or when they disappear between listing and comparison. ## What changed Read NUL-delimited paths from `git ls-files` without trimming them, skip directory entries for untracked nested repositories, and use Git's `/dev/null` handling across local and remote workspaces. Warn when an untracked file can't be compared while still showing the available patches. ## Testing Exercise a clean repository and an untracked file named ` café.txt` beside a nested repository. Verify that a missing file produces a warning while tracked and other untracked patches remain visible, including a patch that Git emits alongside a line-ending warning. GitOrigin-RevId: b92922ea49928381504dc11b71d599c60dbb7068
This commit is contained in:
@@ -112,25 +112,18 @@ pub(crate) async fn get_git_diff(
|
||||
run_git_capture_stdout(
|
||||
&commands,
|
||||
fsmonitor,
|
||||
&["ls-files", "--others", "--exclude-standard"]
|
||||
&["ls-files", "--others", "--exclude-standard", "-z"]
|
||||
),
|
||||
);
|
||||
let tracked_diff = tracked_diff_res?;
|
||||
let mut diff = tracked_diff_res?.stdout;
|
||||
let untracked_output = untracked_output_res?;
|
||||
|
||||
let mut untracked_diff = String::new();
|
||||
let null_device: &Path = if cfg!(windows) {
|
||||
Path::new("NUL")
|
||||
} else {
|
||||
Path::new("/dev/null")
|
||||
};
|
||||
|
||||
let null_path = null_device.to_str().unwrap_or("/dev/null");
|
||||
for file in untracked_output
|
||||
.split('\n')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
let mut warnings = String::new();
|
||||
for file in untracked_output.split_terminator('\0') {
|
||||
// Git lists untracked nested repositories as directories.
|
||||
if file.ends_with('/') {
|
||||
continue;
|
||||
}
|
||||
let args = [
|
||||
"diff",
|
||||
"--no-textconv",
|
||||
@@ -140,15 +133,23 @@ pub(crate) async fn get_git_diff(
|
||||
"--color",
|
||||
"--no-index",
|
||||
"--",
|
||||
null_path,
|
||||
// Git recognizes /dev/null even on Windows. The workspace may be remote.
|
||||
"/dev/null",
|
||||
file,
|
||||
];
|
||||
let diff =
|
||||
let output =
|
||||
run_git_capture_diff(&commands, fsmonitor, &diff_config_overrides, &args).await?;
|
||||
untracked_diff.push_str(&diff);
|
||||
// An input can disappear between ls-files and diff. Keep the other patches visible.
|
||||
if output.exit_code == 1 && output.stdout.is_empty() && !output.stderr.is_empty() {
|
||||
warnings.push_str(&format!(
|
||||
"Warning: incomplete diff. Could not compare {file:?}.\n"
|
||||
));
|
||||
}
|
||||
diff.push_str(&output.stdout);
|
||||
}
|
||||
|
||||
Ok((true, format!("{tracked_diff}{untracked_diff}")))
|
||||
diff.insert_str(0, &warnings);
|
||||
Ok((true, diff))
|
||||
}
|
||||
|
||||
/// Helper that executes `git` with the given `args` and returns `stdout` as a
|
||||
@@ -169,17 +170,16 @@ async fn run_git_capture_stdout(
|
||||
}
|
||||
}
|
||||
|
||||
/// Like [`run_git_capture_stdout`] but treats exit status 1 as success and
|
||||
/// returns stdout. Git returns 1 for diffs when differences are present.
|
||||
/// Git returns 1 for differences, but also for unreadable --no-index inputs.
|
||||
async fn run_git_capture_diff(
|
||||
commands: &DiffCommands<'_>,
|
||||
fsmonitor: FsmonitorOverride,
|
||||
config_overrides: &[(String, String)],
|
||||
args: &[&str],
|
||||
) -> Result<String, String> {
|
||||
) -> Result<WorkspaceCommandOutput, String> {
|
||||
let output = run_git_command(commands, fsmonitor, config_overrides, args).await?;
|
||||
if output.success() || output.exit_code == 1 {
|
||||
Ok(output.stdout)
|
||||
Ok(output)
|
||||
} else {
|
||||
Err(format!(
|
||||
"git {:?} failed with status {}",
|
||||
@@ -483,10 +483,10 @@ mod tests {
|
||||
response(
|
||||
git_command(
|
||||
FsmonitorOverride::Disabled,
|
||||
&["ls-files", "--others", "--exclude-standard"],
|
||||
&["ls-files", "--others", "--exclude-standard", "-z"],
|
||||
),
|
||||
/*exit_code*/ 0,
|
||||
"new.txt\n",
|
||||
"new.txt\0",
|
||||
),
|
||||
response(
|
||||
git_command(
|
||||
@@ -500,7 +500,7 @@ mod tests {
|
||||
"--color",
|
||||
"--no-index",
|
||||
"--",
|
||||
null_device(),
|
||||
"/dev/null",
|
||||
"new.txt",
|
||||
],
|
||||
),
|
||||
@@ -526,6 +526,27 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn get_git_diff_preserves_builtin_fsmonitor_for_diff_workflow() {
|
||||
let cwd = PathBuf::from("/workspace");
|
||||
let untracked = |file: &str| {
|
||||
git_command(
|
||||
FsmonitorOverride::BuiltIn,
|
||||
&[
|
||||
"diff",
|
||||
"--no-textconv",
|
||||
"--no-ext-diff",
|
||||
"--submodule=short",
|
||||
"--ignore-submodules=dirty",
|
||||
"--color",
|
||||
"--no-index",
|
||||
"--",
|
||||
"/dev/null",
|
||||
file,
|
||||
],
|
||||
)
|
||||
};
|
||||
let mut missing = response(untracked("gone.txt"), /*exit_code*/ 1, "");
|
||||
missing.output.stderr = "error: Could not access 'gone.txt'\n".into();
|
||||
let mut patch = response(untracked("new.txt"), /*exit_code*/ 1, "untracked\n");
|
||||
patch.output.stderr = "warning: LF will be replaced by CRLF\n".into();
|
||||
let runner = FakeRunner::new(vec![
|
||||
response(
|
||||
git_command(
|
||||
@@ -577,35 +598,24 @@ mod tests {
|
||||
response(
|
||||
git_command(
|
||||
FsmonitorOverride::BuiltIn,
|
||||
&["ls-files", "--others", "--exclude-standard"],
|
||||
&["ls-files", "--others", "--exclude-standard", "-z"],
|
||||
),
|
||||
/*exit_code*/ 0,
|
||||
"new.txt\n",
|
||||
),
|
||||
response(
|
||||
git_command(
|
||||
FsmonitorOverride::BuiltIn,
|
||||
&[
|
||||
"diff",
|
||||
"--no-textconv",
|
||||
"--no-ext-diff",
|
||||
"--submodule=short",
|
||||
"--ignore-submodules=dirty",
|
||||
"--color",
|
||||
"--no-index",
|
||||
"--",
|
||||
null_device(),
|
||||
"new.txt",
|
||||
],
|
||||
),
|
||||
/*exit_code*/ 1,
|
||||
"untracked\n",
|
||||
"gone.txt\0new.txt\0",
|
||||
),
|
||||
missing,
|
||||
patch,
|
||||
]);
|
||||
|
||||
let result = get_git_diff(&runner, &cwd, /*permission_profile*/ None).await;
|
||||
|
||||
assert_eq!(result, Ok((true, "tracked\nuntracked\n".to_string())));
|
||||
let (is_repo, diff) = get_git_diff(&runner, &cwd, /*permission_profile*/ None)
|
||||
.await
|
||||
.expect("show remaining patches");
|
||||
assert!(is_repo);
|
||||
insta::assert_snapshot!(diff, @r#"
|
||||
Warning: incomplete diff. Could not compare "gone.txt".
|
||||
tracked
|
||||
untracked
|
||||
"#);
|
||||
assert_command_metadata(&runner.commands(), &cwd);
|
||||
}
|
||||
|
||||
@@ -658,7 +668,7 @@ mod tests {
|
||||
response(
|
||||
git_command(
|
||||
FsmonitorOverride::Disabled,
|
||||
&["ls-files", "--others", "--exclude-standard"],
|
||||
&["ls-files", "--others", "--exclude-standard", "-z"],
|
||||
),
|
||||
/*exit_code*/ 0,
|
||||
"",
|
||||
@@ -720,7 +730,7 @@ mod tests {
|
||||
response(
|
||||
git_command(
|
||||
FsmonitorOverride::Disabled,
|
||||
&["ls-files", "--others", "--exclude-standard"],
|
||||
&["ls-files", "--others", "--exclude-standard", "-z"],
|
||||
),
|
||||
/*exit_code*/ 0,
|
||||
"",
|
||||
@@ -782,6 +792,11 @@ mod tests {
|
||||
);
|
||||
run_git_setup(&repo, &["commit", "-qm", "initial"]);
|
||||
|
||||
assert_eq!(
|
||||
get_git_diff(&LocalRunner, &repo, /*permission_profile*/ None).await,
|
||||
Ok((true, String::new()))
|
||||
);
|
||||
|
||||
let filter_helper = tempdir.path().join("filter-helper.sh");
|
||||
let fsmonitor_helper = tempdir.path().join("fsmonitor-helper.sh");
|
||||
let hooks_dir = tempdir.path().join("hooks");
|
||||
@@ -826,6 +841,8 @@ mod tests {
|
||||
std::thread::sleep(Duration::from_secs(/*secs*/ 1));
|
||||
fs::write(repo.join("unchanged.txt"), "unchanged\n").expect("refresh unchanged file");
|
||||
fs::write(repo.join("tracked.txt"), "after\n").expect("modify tracked file");
|
||||
fs::write(repo.join(" café.txt"), "new work\n").expect("create untracked file");
|
||||
run_git_setup(&repo, &["init", "-q", "nested"]);
|
||||
|
||||
let result = get_git_diff(&LocalRunner, &repo, /*permission_profile*/ None)
|
||||
.await
|
||||
@@ -835,11 +852,12 @@ mod tests {
|
||||
(
|
||||
result.1.contains("before"),
|
||||
result.1.contains("after"),
|
||||
result.1.contains("new work"),
|
||||
filter_helper.with_extension("sh.ran").exists(),
|
||||
fsmonitor_helper.with_extension("sh.ran").exists(),
|
||||
hook_helper.with_extension("sh.ran").exists(),
|
||||
),
|
||||
(true, true, false, false, false),
|
||||
(true, true, true, false, false, false),
|
||||
"diff:\n{}",
|
||||
result.1
|
||||
);
|
||||
@@ -962,10 +980,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn null_device() -> &'static str {
|
||||
if cfg!(windows) { "NUL" } else { "/dev/null" }
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn run_git_setup(cwd: &Path, args: &[&str]) {
|
||||
let output = ProcessCommand::new("git")
|
||||
|
||||
Reference in New Issue
Block a user