Show progress indicator for /diff command (#2245)
## Summary - Show a temporary Working on diff state in the bottom pan - Add `DiffResult` app event and dispatch git diff asynchronously ## Testing - `just fmt` - `just fix` *(fails: `let` expressions in this position are unstable)* - `cargo test --all-features` *(fails: `let` expressions in this position are unstable)* ------ https://chatgpt.com/codex/tasks/task_i_689a839f32b88321840a893551d5fbef
This commit is contained in:
@@ -7,24 +7,26 @@
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::process::Stdio;
|
||||
use tokio::process::Command;
|
||||
|
||||
/// Return value of [`get_git_diff`].
|
||||
///
|
||||
/// * `bool` – Whether the current working directory is inside a Git repo.
|
||||
/// * `String` – The concatenated diff (may be empty).
|
||||
pub(crate) fn get_git_diff() -> io::Result<(bool, String)> {
|
||||
pub(crate) async fn get_git_diff() -> io::Result<(bool, String)> {
|
||||
// First check if we are inside a Git repository.
|
||||
if !inside_git_repo()? {
|
||||
if !inside_git_repo().await? {
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
|
||||
// 1. Diff for tracked files.
|
||||
let tracked_diff = run_git_capture_diff(&["diff", "--color"])?;
|
||||
|
||||
// 2. Determine untracked files.
|
||||
let untracked_output = run_git_capture_stdout(&["ls-files", "--others", "--exclude-standard"])?;
|
||||
// Run tracked diff and untracked file listing in parallel.
|
||||
let (tracked_diff_res, untracked_output_res) = tokio::join!(
|
||||
run_git_capture_diff(&["diff", "--color"]),
|
||||
run_git_capture_stdout(&["ls-files", "--others", "--exclude-standard"]),
|
||||
);
|
||||
let tracked_diff = tracked_diff_res?;
|
||||
let untracked_output = untracked_output_res?;
|
||||
|
||||
let mut untracked_diff = String::new();
|
||||
let null_device: &Path = if cfg!(windows) {
|
||||
@@ -33,26 +35,26 @@ pub(crate) fn get_git_diff() -> io::Result<(bool, String)> {
|
||||
Path::new("/dev/null")
|
||||
};
|
||||
|
||||
let null_path = null_device.to_str().unwrap_or("/dev/null").to_string();
|
||||
let mut join_set: tokio::task::JoinSet<io::Result<String>> = tokio::task::JoinSet::new();
|
||||
for file in untracked_output
|
||||
.split('\n')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
// Use `git diff --no-index` to generate a diff against the null device.
|
||||
let args = [
|
||||
"diff",
|
||||
"--color",
|
||||
"--no-index",
|
||||
"--",
|
||||
null_device.to_str().unwrap_or("/dev/null"),
|
||||
file,
|
||||
];
|
||||
|
||||
match run_git_capture_diff(&args) {
|
||||
Ok(diff) => untracked_diff.push_str(&diff),
|
||||
// If the file disappeared between ls-files and diff we ignore the error.
|
||||
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
|
||||
Err(err) => return Err(err),
|
||||
let null_path = null_path.clone();
|
||||
let file = file.to_string();
|
||||
join_set.spawn(async move {
|
||||
let args = ["diff", "--color", "--no-index", "--", &null_path, &file];
|
||||
run_git_capture_diff(&args).await
|
||||
});
|
||||
}
|
||||
while let Some(res) = join_set.join_next().await {
|
||||
match res {
|
||||
Ok(Ok(diff)) => untracked_diff.push_str(&diff),
|
||||
Ok(Err(err)) if err.kind() == io::ErrorKind::NotFound => {}
|
||||
Ok(Err(err)) => return Err(err),
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,12 +63,13 @@ pub(crate) fn get_git_diff() -> io::Result<(bool, String)> {
|
||||
|
||||
/// Helper that executes `git` with the given `args` and returns `stdout` as a
|
||||
/// UTF-8 string. Any non-zero exit status is considered an *error*.
|
||||
fn run_git_capture_stdout(args: &[&str]) -> io::Result<String> {
|
||||
async fn run_git_capture_stdout(args: &[&str]) -> io::Result<String> {
|
||||
let output = Command::new("git")
|
||||
.args(args)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.output()?;
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if output.status.success() {
|
||||
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
|
||||
@@ -80,12 +83,13 @@ fn run_git_capture_stdout(args: &[&str]) -> io::Result<String> {
|
||||
|
||||
/// Like [`run_git_capture_stdout`] but treats exit status 1 as success and
|
||||
/// returns stdout. Git returns 1 for diffs when differences are present.
|
||||
fn run_git_capture_diff(args: &[&str]) -> io::Result<String> {
|
||||
async fn run_git_capture_diff(args: &[&str]) -> io::Result<String> {
|
||||
let output = Command::new("git")
|
||||
.args(args)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.output()?;
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if output.status.success() || output.status.code() == Some(1) {
|
||||
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
|
||||
@@ -98,12 +102,13 @@ fn run_git_capture_diff(args: &[&str]) -> io::Result<String> {
|
||||
}
|
||||
|
||||
/// Determine if the current directory is inside a Git repository.
|
||||
fn inside_git_repo() -> io::Result<bool> {
|
||||
async fn inside_git_repo() -> io::Result<bool> {
|
||||
let status = Command::new("git")
|
||||
.args(["rev-parse", "--is-inside-work-tree"])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status();
|
||||
.status()
|
||||
.await;
|
||||
|
||||
match status {
|
||||
Ok(s) if s.success() => Ok(true),
|
||||
|
||||
Reference in New Issue
Block a user