git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] Update :/abc ambiguity check

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 21, 2013, 13:00 UTC
Message-ID
<1358773249-24384-1-git-send-email-pclouds@gmail.com>
:/abc may mean two things:
- as a revision, it means the revision that has "abc" in commit
  message.
- as a pathpec, it means "abc" from root.

Currently we see ":/abc" as a rev (most of the time), but never see it as a pathspec even if "abc" exists and "git log :/abc" will gladly take ":/abc" as rev even it's ambiguous. This patch makes it:

- ambiguous when "abc" exists on worktree
- a rev if abc does not exist on worktree
- a path if abc is not found in any commits (although better use
  "--" to avoid ambiguation because searching through commit DAG is
  expensive)

A plus from this patch is, because ":/" never matches anything as a rev, it is never considered a valid rev and because root directory always exists, ":/" is always unambiguously seen as a pathspec.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 This makes "git grep foo :/" work. For such an often used command,
 the less I type the better.
 The ".. is expensive" thing is not cool. But it hopefully does not
 happen often. I expect people rely on shell's tab completion more
 than :/typing-path-to-somewhere-manually. Copy/paste happens more
 often. I usually type ":" then copy a path from diff --git line.
 We should probably kill ":/abc as a rev" and replace it with @{/abc}
 or something less ambiguous.
 setup.c                       |  9 ++++++++-
 t/t4208-log-magic-pathspec.sh | 17 +++++++++++++++--
 2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/setup.c b/setup.c
index f108c4b..47acc11 100644
--- a/setup.c
+++ b/setup.c
@@ -66,7 +66,14 @@ int check_filename(const char *prefix, const char *arg)
 	const char *name;
 	struct stat st;
 
-	name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
+	if (!prefixcmp(arg, ":/")) {
+		if (arg[2] == '\0') /* ":/" is root dir, always exists */
+			return 1;
+		name = arg + 2;
+	} else if (prefix)
+		name = prefix_filename(prefix, strlen(prefix), arg);
+	else
+		name = arg;
 	if (!lstat(name, &st))
 		return 1; /* file exists */
 	if (errno == ENOENT || errno == ENOTDIR)
diff --git a/t/t4208-log-magic-pathspec.sh b/t/t4208-log-magic-pathspec.sh
index 2c482b6..72300b5 100755
--- a/t/t4208-log-magic-pathspec.sh
+++ b/t/t4208-log-magic-pathspec.sh
@@ -11,11 +11,24 @@ test_expect_success 'setup' '
 	mkdir sub
 '
 
-test_expect_success '"git log :/" should be ambiguous' '
-	test_must_fail git log :/ 2>error &&
+test_expect_success '"git log :/" should not be ambiguous' '
+	git log :/
+'
+
+test_expect_success '"git log :/a" should be ambiguous (applied both rev and worktree)' '
+	: >a &&
+	test_must_fail git log :/a 2>error &&
 	grep ambiguous error
 '
 
+test_expect_success '"git log :/a -- " should not be ambiguous' '
+	git log :/a --
+'
+
+test_expect_success '"git log -- :/a" should not be ambiguous' '
+	git log -- :/a
+'
+
 test_expect_success '"git log :" should be ambiguous' '
 	test_must_fail git log : 2>error &&
 	grep ambiguous error
-- 
1.8.0.rc2.23.g1fb49df
Next: Nguyễn Thái Ngọc Duy
Message 1 of 5 in “Update :/abc ambiguity check”
  1. 1/2 Update :/abc ambiguity checkNguyễn Thái Ngọc Duy, Jan 21, 2013
  2. 2/2 grep: avoid accepting ambiguous revisionNguyễn Thái Ngọc Duy, Jan 21, 2013
  3. Junio C HamanoJan 21, 2013
  4. Duy NguyenJan 22, 2013
  5. Junio C HamanoJan 22, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.