From 4f1861edb604d8de957c07e26fada0a6c4a41371 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 17 Apr 2024 17:51:26 +0200 Subject: [PATCH] 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. --- crates/git/src/blame.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/git/src/blame.rs b/crates/git/src/blame.rs index 54ff7f664acc1ca49ac65a763a321732c6fef12f..8eedefc6fe0842e13a8aad9b27539fc157c22381 100644 --- a/crates/git/src/blame.rs +++ b/crates/git/src/blame.rs @@ -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)); }