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

Re: some git confusion (where git's advice didn't help)

From
Duy Nguyen <pclouds@gmail.com>
Date
May 14, 2019, 11:29 UTC
Message-ID
<20190514112941.GA23603@ash>
In-Reply-To
<20190514112115.GA26957@sigill.intra.peff.net>
On Tue, May 14, 2019 at 07:21:15AM -0400, Jeff King wrote:
> I also think the "warning: refname ... is ambiguous" message would
> probably be a bit more helpful if it showed _which_ candidates it found
> (and which one it chose!).

Alternatively, just refuse to resolve ambiguous refs. It's not always printed in a short output that stands out to you. Something like this perhaps.

It could probably use some improvements, suggesting the ambiguous candidates too. It's just what I've been using for years.

-- 8< --
Subject: [PATCH] sha1_name.c: add an option to abort on ambiguous refs

There are cases when a warning on ambiguous ref may go unnoticed (e.g. git-log filling up the whole screen). There are also cases when people want to catch ambiguation early (e.g. it happens deep in some script). In either case, aborting the program would accomplish it.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config/core.txt |  3 ++-
 config.c                      |  5 ++++-
 sha1-name.c                   | 10 ++++++++--
 3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/Documentation/config/core.txt b/Documentation/config/core.txt
index 7e9b6c8f4c..f81634b642 100644
--- a/Documentation/config/core.txt
+++ b/Documentation/config/core.txt
@@ -340,7 +340,8 @@ core.sharedRepository::
 
 core.warnAmbiguousRefs::
 	If true, Git will warn you if the ref name you passed it is ambiguous
-	and might match multiple refs in the repository. True by default.
+	and might match multiple refs in the repository. If set to "fatal",
+	the program will abort on ambiguous refs. True by default.
 
 core.compression::
 	An integer -1..9, indicating a default compression level.
diff --git a/config.c b/config.c
index 0f0cdd8c0f..f314caeb87 100644
--- a/config.c
+++ b/config.c
@@ -1158,7 +1158,10 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
 	}
 
 	if (!strcmp(var, "core.warnambiguousrefs")) {
-		warn_ambiguous_refs = git_config_bool(var, value);
+		if (!strcasecmp(value, "fatal"))
+			warn_ambiguous_refs = 2;
+		else
+			warn_ambiguous_refs = git_config_bool(var, value);
 		return 0;
 	}
 
diff --git a/sha1-name.c b/sha1-name.c
index 6dda2c16df..e613c955d7 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -760,6 +760,8 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
 				warning(warn_msg, len, str);
 				if (advice_object_name_warning)
 					fprintf(stderr, "%s\n", _(object_name_msg));
+				if (warn_ambiguous_refs > 1)
+					die(_("cannot continue with ambiguous refs"));
 			}
 			free(real_ref);
 		}
@@ -817,8 +819,12 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid,
 
 	if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
 	    (refs_found > 1 ||
-	     !get_short_oid(str, len, &tmp_oid, GET_OID_QUIETLY)))
-		warning(warn_msg, len, str);
+	     !get_short_oid(str, len, &tmp_oid, GET_OID_QUIETLY))) {
+		if (warn_ambiguous_refs > 1)
+			die(warn_msg, len, str);
+		else
+			warning(warn_msg, len, str);
+	}
 
 	if (reflog_len) {
 		int nth, i;
-- 
2.21.0.1141.gd54ac2cb17

-- 8< --
Previous: Jeff KingNext: Jeff King
Message 3 of 7 in “some git confusion (where git's advice didn't help)”
  1. Ulrich WindlMay 14, 2019
  2. Jeff KingMay 14, 2019
  3. Duy NguyenMay 14, 2019
  4. Jeff KingMay 14, 2019
  5. 0/2 some fixes for help_unknown_ref()Jeff King, May 14, 2019
  6. 1/2 help_unknown_ref(): duplicate collected refnamesJeff King, May 14, 2019
  7. 2/2 help_unknown_ref(): check for refname ambiguityJeff King, May 14, 2019

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.