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

[PATCH] checkout: swap the order of ambiguity check for :/ syntax

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 22, 2016, 12:35 UTC
Message-ID
<20160822123502.3521-1-pclouds@gmail.com>

This should speed up "git checkout :/long/path/taken/from/diffstat" because we don't have to walk through the commit graph to see if "long/path/taken/from/diffstat" exists in any commit message.

This is in the the same spirit as 4db86e8 (Update :/abc ambiguity check
- 2013-01-21), but instead of considering this case ("abc" in ":/abc"
exists on worktree) ambiguous and shouting, we just assume the user
means "pathspec" not "ref".

It's not wonderful, but it's in line with how git-checkout stops caring about ambiguity after the first argument can be resolved as a ref (there's even a test for it, t2010.6). In other words, we assume the user means "ref" not "pathspec" when that happens. Here we simply swap the order of checking specifically for :/ on practical ground.

Last note, to be pedantic, we should check if "abc" from ":/abc" exists as an _index_ entry, not on disk. But chances are they exist in both places anyway...

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/checkout.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 8672d07..6f016db 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -973,6 +973,22 @@ static int parse_branchname_arg(int argc, const char **argv,
 	if (!strcmp(arg, "-"))
 		arg = "@{-1}";
 
+	if (dash_dash_pos < 0 && starts_with(arg, ":/") &&
+	    check_filename(opts->prefix, arg)) {
+		/*
+		 * Normally if the first argument is ambiguous, we
+		 * choose to believe the user specifies an extended
+		 * SHA-1 syntax unless it turns out not true, then we
+		 * see if it's a pathspec.
+		 *
+		 * :/ here is an exception because resolving :/abc may
+		 * involve walking through the entire commit
+		 * graph. Expensive and slow. If :/abc points to an
+		 * existing file, ignore ambiguity and go with
+		 * pathspec (i.e. skip get_sha1_mb()).
+		 */
+		return 0;	/* case (2) */
+	}
 	if (get_sha1_mb(arg, rev)) {
 		/*
 		 * Either case (3) or (4), with <something> not being
-- 
2.8.2.524.g6ff3d78
Next: Junio C Hamano
Message 1 of 12 in “checkout: swap the order of ambiguity check for :/ syntax”
  1. checkout: swap the order of ambiguity check for :/ syntaxNguyễn Thái Ngọc Duy, Aug 22, 2016
  2. Junio C HamanoAug 24, 2016
  3. Duy NguyenAug 25, 2016
  4. Junio C HamanoAug 25, 2016
  5. Duy NguyenAug 26, 2016
  6. 0/3 fix checkout ambiguation in subdirNguyễn Thái Ngọc Duy, Sep 7, 2016
  7. 1/3 checkout: add some spaces between code and commentNguyễn Thái Ngọc Duy, Sep 7, 2016
  8. 2/3 checkout.txt: document a common case that ignores ambiguation rulesNguyễn Thái Ngọc Duy, Sep 7, 2016
  9. Junio C HamanoSep 8, 2016
  10. Junio C HamanoSep 9, 2016
  11. 3/3 checkout: fix ambiguity check in subdirNguyễn Thái Ngọc Duy, Sep 7, 2016
  12. Junio C HamanoSep 8, 2016

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.