diff --git a/codex-rs/tui/src/get_git_diff.rs b/codex-rs/tui/src/get_git_diff.rs index a01f85d5f8..2690e3192c 100644 --- a/codex-rs/tui/src/get_git_diff.rs +++ b/codex-rs/tui/src/get_git_diff.rs @@ -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 { +) -> Result { 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")