git blame: ignore uncommitted files or repos without commits (#10685)

This fixes useless error messages popping up in case a file hasn't been
committed yet or the repo doesn't have commits yet.

Release Notes:

- Fixed git blame functionality not handling errors correctly when there
are no commits yet or when file isn't committed yet.
This commit is contained in:
Thorsten Ball 2024-04-17 17:51:26 +02:00 committed by GitHub
parent d7becce9aa
commit 4f1861edb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -63,6 +63,9 @@ impl Blame {
}
}
const GIT_BLAME_NO_COMMIT_ERROR: &'static str = "fatal: no such ref: HEAD";
const GIT_BLAME_NO_PATH: &'static str = "fatal: no such path";
fn run_git_blame(
git_binary: &Path,
working_directory: &Path,
@ -98,6 +101,10 @@ fn run_git_blame(
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let trimmed = stderr.trim();
if trimmed == GIT_BLAME_NO_COMMIT_ERROR || trimmed.contains(GIT_BLAME_NO_PATH) {
return Ok(String::new());
}
return Err(anyhow!("git blame process failed: {}", stderr));
}