Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_graph: Exclude non-standard refs (#54291)

Tim Vermeulen5f038c2a3a29
diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs
index 445178e8a6f..6cfa48282ba 100644
--- a/crates/git/src/repository.rs
+++ b/crates/git/src/repository.rs
@@ -736,14 +736,20 @@ pub enum LogSource {
}
impl LogSource {
- fn get_arg(&self) -> Result<&str> {
+ fn get_args(&self) -> Result<Vec<&str>> {
match self {
- LogSource::All => Ok("--all"),
- LogSource::Branch(branch) => Ok(branch.as_str()),
- LogSource::Sha(oid) => {
- str::from_utf8(oid.as_bytes()).context("Failed to build str from sha")
- }
- LogSource::Path(_) => Ok("--follow"),
+ LogSource::All => Ok(vec![
+ "--ignore-missing", // needed in case of unborn HEAD
+ "--branches",
+ "--remotes",
+ "--tags",
+ "HEAD",
+ ]),
+ LogSource::Branch(branch) => Ok(vec![branch.as_str()]),
+ LogSource::Sha(oid) => Ok(vec![
+ str::from_utf8(oid.as_bytes()).context("Failed to build str from sha")?,
+ ]),
+ LogSource::Path(path) => Ok(vec!["--follow", "--", path.as_unix_str()]),
}
}
}
@@ -2978,17 +2984,8 @@ impl GitRepository for RealGitRepository {
let git = self.git_binary();
async move {
- let mut git_log_command = vec![
- "log",
- GRAPH_COMMIT_FORMAT,
- log_order.as_arg(),
- log_source.get_arg()?,
- ];
-
- if let LogSource::Path(path) = &log_source {
- git_log_command.extend(["--", path.as_unix_str()]);
- }
-
+ let mut git_log_command = vec!["log", GRAPH_COMMIT_FORMAT, log_order.as_arg()];
+ git_log_command.extend(log_source.get_args()?);
let mut command = git.build_command(&git_log_command);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
@@ -3058,7 +3055,7 @@ impl GitRepository for RealGitRepository {
let git = self.git_binary();
async move {
- let mut args = vec!["log", SEARCH_COMMIT_FORMAT, log_source.get_arg()?];
+ let mut args = vec!["log", SEARCH_COMMIT_FORMAT];
args.push("--fixed-strings");
@@ -3069,10 +3066,7 @@ impl GitRepository for RealGitRepository {
args.push("--grep");
args.push(search_args.query.as_str());
- if let LogSource::Path(path) = &log_source {
- args.extend(["--", path.as_unix_str()]);
- }
-
+ args.extend(log_source.get_args()?);
let mut command = git.build_command(&args);
command.stdout(Stdio::piped());
command.stderr(Stdio::null());
@@ -4661,6 +4655,67 @@ mod tests {
);
}
+ #[gpui::test]
+ async fn test_initial_graph_data_ref_set(cx: &mut TestAppContext) {
+ disable_git_global_config();
+ cx.executor().allow_parking();
+
+ let repo_dir = tempfile::tempdir().unwrap();
+ git2::Repository::init(repo_dir.path()).unwrap();
+
+ let repo = RealGitRepository::new(
+ &repo_dir.path().join(".git"),
+ None,
+ Some("git".into()),
+ cx.executor(),
+ )
+ .unwrap();
+ let git = repo.git_binary();
+
+ let graph_commits = async || {
+ let (tx, rx) = smol::channel::unbounded();
+ repo.initial_graph_data(LogSource::All, LogOrder::DateOrder, tx)
+ .await
+ .unwrap();
+ let mut commits = std::collections::HashSet::new();
+ while let Ok(chunk) = rx.try_recv() {
+ for commit in chunk {
+ commits.insert(commit.sha);
+ }
+ }
+ commits
+ };
+
+ smol::fs::write(repo_dir.path().join("file1"), "1")
+ .await
+ .unwrap();
+ let branch_sha = repo.checkpoint().await.unwrap().commit_sha;
+ repo.update_ref("refs/heads/main".into(), branch_sha.to_string())
+ .await
+ .unwrap();
+
+ smol::fs::write(repo_dir.path().join("file2"), "2")
+ .await
+ .unwrap();
+ let hidden_sha = repo.checkpoint().await.unwrap().commit_sha;
+ repo.update_ref("refs/custom/hidden".into(), hidden_sha.to_string())
+ .await
+ .unwrap();
+
+ let graph = graph_commits().await;
+ assert!(graph.contains(&branch_sha));
+ assert!(!graph.contains(&hidden_sha));
+
+ git.build_command(&["update-ref", "--no-deref", "HEAD", &hidden_sha.to_string()])
+ .output()
+ .await
+ .unwrap();
+
+ let graph = graph_commits().await;
+ assert!(graph.contains(&branch_sha));
+ assert!(graph.contains(&hidden_sha));
+ }
+
#[test]
fn test_original_repo_path_from_common_dir() {
// Normal repo: common_dir is <work_dir>/.git