Skip to content

Commit 886242f

Browse files
laststylebender14autofix-ci[bot]
authored andcommitted
fix(context-engine): return the actual error for git ls-files command (#2519)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent e3124fd commit 886242f

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

crates/forge_services/src/context_engine.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,13 @@ impl<F: 'static + ProviderRepository + WorkspaceIndexRepository> ForgeWorkspaceS
411411
Ok(best_match.map(|(w, _)| w.clone()))
412412
}
413413
/// Runs `git ls-files` in `dir_path` and returns the tracked files as
414-
/// `WalkedFile` entries. Returns `None` when git is unavailable or the
415-
/// directory is not a git repository.
416-
async fn git_ls_files(&self, dir_path: &Path) -> Option<Vec<WalkedFile>>
414+
/// `WalkedFile` entries.
415+
///
416+
/// # Errors
417+
///
418+
/// Returns an error when the command fails to execute or exits with a
419+
/// non-zero status code (e.g. when the directory is not a git repository).
420+
async fn git_ls_files(&self, dir_path: &Path) -> anyhow::Result<Vec<WalkedFile>>
417421
where
418422
F: CommandInfra,
419423
{
@@ -425,11 +429,14 @@ impl<F: 'static + ProviderRepository + WorkspaceIndexRepository> ForgeWorkspaceS
425429
true,
426430
None,
427431
)
428-
.await
429-
.ok()?;
432+
.await?;
430433

431434
if output.exit_code != Some(0) {
432-
return None;
435+
let err = anyhow::anyhow!(output.stderr);
436+
return Err(match output.exit_code {
437+
Some(code) => err.context(format!("'git ls-files' exited with code {}", code)),
438+
None => err,
439+
});
433440
}
434441

435442
let files = output
@@ -445,7 +452,7 @@ impl<F: 'static + ProviderRepository + WorkspaceIndexRepository> ForgeWorkspaceS
445452
})
446453
.collect();
447454

448-
Some(files)
455+
Ok(files)
449456
}
450457

451458
/// Only includes files with allowed extensions.
@@ -463,19 +470,8 @@ impl<F: 'static + ProviderRepository + WorkspaceIndexRepository> ForgeWorkspaceS
463470

464471
async_stream::stream! {
465472
info!("Discovering files for sync via git ls-files");
466-
467-
let walked_files: Vec<WalkedFile> = match service.git_ls_files(&dir_path).await {
468-
Some(files) => {
469-
info!(file_count = files.len(), "Discovered files via git ls-files");
470-
files
471-
}
472-
None => {
473-
yield Err(anyhow::anyhow!(
474-
"Failed to list files: 'git ls-files' failed or directory is not a git repository"
475-
));
476-
return;
477-
}
478-
};
473+
let walked_files: Vec<WalkedFile> = service.git_ls_files(&dir_path).await?;
474+
info!(file_count = walked_files.len(), "Discovered files via git ls-files");
479475

480476
// Filter files by allowed extension (pure function, no I/O)
481477
let filtered_files: Vec<_> = walked_files

0 commit comments

Comments
 (0)