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: Separate revisions from paths in git commands (#63666)

itsfuad801c087af22d
diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs
index 72ecb59f90c..7962bd6cc9a 100644
--- a/crates/git/src/repository.rs
+++ b/crates/git/src/repository.rs
@@ -738,7 +738,7 @@ pub enum LogSource {
impl LogSource {
fn get_args(&self) -> Vec<Cow<'_, str>> {
- match self {
+ let mut args = match self {
LogSource::All => vec![
Cow::Borrowed("--ignore-missing"), // needed in case of unborn HEAD
Cow::Borrowed("--branches"),
@@ -753,7 +753,14 @@ impl LogSource {
Cow::Borrowed("--"),
Cow::Borrowed(path.as_unix_str()),
],
+ };
+ // Without a terminator git cannot tell a branch named `docs/rewrite` from a
+ // `docs/rewrite` directory in the working tree, and refuses the argument as
+ // ambiguous. `Path` states its own separator before the path it passes.
+ if !matches!(self, LogSource::Path(_)) {
+ args.push(Cow::Borrowed("--"));
}
+ args
}
}
@@ -1411,6 +1418,9 @@ impl GitRepository for RealGitRepository {
"--no-patch",
"--format=%H%x00%B%x00%at%x00%ae%x00%an%x00",
&commit,
+ // `commit` reaches here as whatever the user typed, so it can name
+ // a branch that also names a path in the working tree.
+ "--",
])
.output()
.await?;
@@ -1464,6 +1474,7 @@ impl GitRepository for RealGitRepository {
"--first-parent",
])
.arg(&commit)
+ .arg("--")
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
@@ -1925,6 +1936,7 @@ impl GitRepository for RealGitRepository {
"--merge-base",
base.as_str(),
head.as_str(),
+ "--",
]
.map(OsString::from)
.to_vec(),
@@ -1936,6 +1948,7 @@ impl GitRepository for RealGitRepository {
"--no-renames",
"--merge-base",
base.as_str(),
+ "--",
]
.map(OsString::from)
.to_vec(),
@@ -1947,6 +1960,7 @@ impl GitRepository for RealGitRepository {
"--no-renames",
base.as_str(),
head.as_str(),
+ "--",
]
.map(OsString::from)
.to_vec(),
@@ -2494,7 +2508,7 @@ impl GitRepository for RealGitRepository {
}
DiffType::HeadToWorktree => git.build_command(&["diff"]).output().await?,
DiffType::MergeBase { base_ref } => {
- git.build_command(&["diff", "--merge-base", base_ref.as_ref()])
+ git.build_command(&["diff", "--merge-base", base_ref.as_ref(), "--"])
.output()
.await?
}
@@ -3984,13 +3998,20 @@ impl GitBinary {
command.args(["-c", "protocol.ext.allow=never"]);
command.args(["-c", "diff.external="]);
}
- command.args(args);
-
// If the `diff` command is being used, we'll want to add the
// `--no-ext-diff` flag when working on an untrusted repository,
- // preventing any external diff programs from being invoked.
- if !self.is_trusted && args.iter().any(|arg| arg.as_ref() == "diff") {
- command.arg("--no-ext-diff");
+ // preventing any external diff programs from being invoked. It goes
+ // directly after the subcommand: callers end their revisions with `--`,
+ // and anything after that separator is read as a pathspec rather than as
+ // an option.
+ let mut args = args.iter();
+ if let Some(subcommand) = args.next() {
+ let is_diff = subcommand.as_ref() == "diff";
+ command.arg(subcommand);
+ if !self.is_trusted && is_diff {
+ command.arg("--no-ext-diff");
+ }
+ command.args(args);
}
if let Some(index_file_path) = self.index_file_path.as_ref() {
@@ -5292,6 +5313,164 @@ mod tests {
assert_eq!(graph_data[0].sha, commit_sha);
}
+ /// A branch whose name also names a path in the working tree - `docs/rewrite` in a
+ /// repository that also has a `docs/rewrite` directory - made git reject the revision
+ /// as ambiguous, so the git panel's History tab reported "Failed to load commit
+ /// history" and commit search silently returned nothing.
+ #[gpui::test]
+ async fn test_initial_graph_data_with_branch_named_after_a_path(cx: &mut TestAppContext) {
+ disable_git_global_config();
+
+ cx.executor().allow_parking();
+
+ let repo_dir = tempfile::tempdir().unwrap();
+
+ git_init_repo(repo_dir.path());
+ fs::create_dir_all(repo_dir.path().join("docs/rewrite")).unwrap();
+ fs::write(repo_dir.path().join("docs/rewrite/notes.md"), "notes").unwrap();
+ git_command(repo_dir.path(), ["add", "."]);
+ git_command(repo_dir.path(), ["commit", "-m", "Add rewrite notes"]);
+ git_command(repo_dir.path(), ["checkout", "-b", "docs/rewrite"]);
+
+ let commit_sha: Oid = git_command_output(repo_dir.path(), ["rev-parse", "HEAD"])
+ .parse()
+ .unwrap();
+
+ let repo = RealGitRepository::new(
+ &repo_dir.path().join(".git"),
+ None,
+ Some("git".into()),
+ cx.executor(),
+ )
+ .unwrap();
+
+ let (request_tx, request_rx) = async_channel::unbounded();
+
+ repo.initial_graph_data(
+ LogSource::Branch("docs/rewrite".into()),
+ LogOrder::DateOrder,
+ request_tx,
+ )
+ .await
+ .unwrap();
+
+ let graph_data = request_rx.recv().await.unwrap();
+ assert_eq!(graph_data.len(), 1);
+ assert_eq!(graph_data[0].sha, commit_sha);
+ }
+
+ /// The branch diff passes the base ref straight to `git diff`, so a branch named after
+ /// a path in the working tree broke it the same way. `git diff` takes the same `--`
+ /// terminator, so the base ref is terminated like the log sources are.
+ #[gpui::test]
+ async fn test_merge_base_worktree_diff_with_branch_named_after_a_path(cx: &mut TestAppContext) {
+ disable_git_global_config();
+
+ cx.executor().allow_parking();
+
+ let repo_dir = tempfile::tempdir().unwrap();
+
+ git_init_repo(repo_dir.path());
+ fs::create_dir_all(repo_dir.path().join("docs/rewrite")).unwrap();
+ let notes_path = repo_dir.path().join("docs/rewrite/notes.md");
+ fs::write(&notes_path, "notes\n").unwrap();
+ git_command(repo_dir.path(), ["add", "."]);
+ git_command(repo_dir.path(), ["commit", "-m", "Add rewrite notes"]);
+ git_command(repo_dir.path(), ["checkout", "-b", "docs/rewrite"]);
+
+ let base_oid: Oid =
+ git_command_output(repo_dir.path(), ["rev-parse", "HEAD:docs/rewrite/notes.md"])
+ .parse()
+ .unwrap();
+
+ fs::write(&notes_path, "edited\n").unwrap();
+
+ let repo = RealGitRepository::new(
+ &repo_dir.path().join(".git"),
+ None,
+ Some("git".into()),
+ cx.executor(),
+ )
+ .unwrap();
+
+ let diff = repo
+ .diff_tree(DiffTreeType::MergeBaseWithWorktree {
+ base: "docs/rewrite".into(),
+ })
+ .await
+ .unwrap();
+
+ assert_eq!(
+ diff,
+ TreeDiff {
+ entries: HashMap::from_iter([(
+ RepoPath::new("docs/rewrite/notes.md").unwrap(),
+ TreeDiffStatus::Modified { old: base_oid },
+ )]),
+ }
+ );
+ }
+
+ /// `show` takes the revision the user typed in the "open commit by ref" input, so a
+ /// branch named after a path in the working tree reaches git unqualified there too.
+ #[gpui::test]
+ async fn test_show_with_branch_named_after_a_path(cx: &mut TestAppContext) {
+ disable_git_global_config();
+
+ cx.executor().allow_parking();
+
+ let repo_dir = tempfile::tempdir().unwrap();
+
+ git_init_repo(repo_dir.path());
+ fs::create_dir_all(repo_dir.path().join("docs/rewrite")).unwrap();
+ fs::write(repo_dir.path().join("docs/rewrite/notes.md"), "notes").unwrap();
+ git_command(repo_dir.path(), ["add", "."]);
+ git_command(repo_dir.path(), ["commit", "-m", "Add rewrite notes"]);
+ git_command(repo_dir.path(), ["checkout", "-b", "docs/rewrite"]);
+
+ let commit_sha: Oid = git_command_output(repo_dir.path(), ["rev-parse", "HEAD"])
+ .parse()
+ .unwrap();
+
+ let repo = RealGitRepository::new(
+ &repo_dir.path().join(".git"),
+ None,
+ Some("git".into()),
+ cx.executor(),
+ )
+ .unwrap();
+
+ let details = repo.show("docs/rewrite".to_string()).await.unwrap();
+ assert_eq!(details.sha.as_ref(), commit_sha.to_string());
+ }
+
+ #[test]
+ fn test_log_source_terminates_revisions() {
+ // Every revision-taking source has to end the revision list, or git cannot tell a
+ // branch name from an identically named path. `Path` is the exception: it states
+ // the separator itself, before the path it is passing.
+ for source in [
+ LogSource::All,
+ LogSource::Branch("docs/rewrite".into()),
+ LogSource::Sha(Oid::from_str("0000000000000000000000000000000000000000").unwrap()),
+ ] {
+ let args = source.get_args();
+ assert_eq!(
+ args.last().map(|arg| arg.as_ref()),
+ Some("--"),
+ "{source:?} must terminate its revisions"
+ );
+ }
+
+ let path_source = LogSource::Path(RepoPath::new("docs/rewrite").unwrap());
+ let path_args = path_source.get_args();
+ assert_eq!(
+ path_args.iter().filter(|arg| arg.as_ref() == "--").count(),
+ 1,
+ "Path states the separator itself and must not gain a second one"
+ );
+ }
+
#[gpui::test]
async fn test_build_command_untrusted_includes_both_safety_args(cx: &mut TestAppContext) {
cx.executor().allow_parking();