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

[PATCH 3/3] object-name: advice to avoid refs that resemble hashes

From
Rubén Justo <rjusto@gmail.com>
Date
Dec 8, 2024, 08:12 UTC
Message-ID
<43a66f17-c910-498a-8faa-f801194e6c8e@gmail.com>
In-Reply-To
<0e139151-7162-42b3-afae-248c28bf4c4b@gmail.com>

If we detect a reference resembling a hash, we advice the user to avoid using it and delete it.

Let's use the `advise_if_enabled()` API to display the advice with the aim of achieving simplicity and consistency in how the advice is presented.

While we're here, let's add some tests for this advice to gain visibility if we unintentionally make changes about it.

Finally, the change from `const char*` to `const char[]` is to avoid problems with "-Werror=format-security".

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 object-name.c                       |  9 ++++-----
 t/t1512-rev-parse-disambiguation.sh | 15 ++++++++++++++-
 2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/object-name.c b/object-name.c
index c892fbe80a..baf5422013 100644
--- a/object-name.c
+++ b/object-name.c
@@ -943,7 +943,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 			 struct object_id *oid, unsigned int flags)
 {
 	static const char *warn_msg = "refname '%.*s' is ambiguous.";
-	static const char *object_name_msg = N_(
+	static const char object_name_msg[] = N_(
 	"Git normally never creates a ref that ends with 40 hex characters\n"
 	"because it will be ignored when you just specify 40-hex. These refs\n"
 	"may be created by mistake. For example,\n"
@@ -951,8 +951,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 	"  git switch -c $br $(git rev-parse ...)\n"
 	"\n"
 	"where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
-	"examine these refs and maybe delete them. Turn this message off by\n"
-	"running \"git config advice.objectNameWarning false\"");
+	"examine these refs and maybe delete them.");
 	struct object_id tmp_oid;
 	char *real_ref = NULL;
 	int refs_found = 0;
@@ -964,8 +963,8 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 			refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
 			if (refs_found > 0) {
 				warning(warn_msg, len, str);
-				if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
-					fprintf(stderr, "%s\n", _(object_name_msg));
+				advise_if_enabled(ADVICE_OBJECT_NAME_WARNING,
+						  object_name_msg);
 			}
 			free(real_ref);
 		}
diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh
index 70f1e0a998..18bf4f0046 100755
--- a/t/t1512-rev-parse-disambiguation.sh
+++ b/t/t1512-rev-parse-disambiguation.sh
@@ -371,13 +371,26 @@ test_expect_success 'rev-parse --disambiguate drops duplicates' '
 	test_cmp expect actual
 '
 
+test_expect_success 'ambiguous 40-hex ref (with advice declined)' '
+	git config set advice.objectNameWarning false &&
+	TREE=$(git mktree </dev/null) &&
+	REF=$(git rev-parse HEAD) &&
+	VAL=$(git commit-tree $TREE </dev/null) &&
+	git update-ref refs/heads/$REF $VAL &&
+	test $(git rev-parse $REF 2>err) = $REF &&
+	grep "refname.*${REF}.*ambiguous" err &&
+	test_grep ! hint: err
+'
+
 test_expect_success 'ambiguous 40-hex ref' '
+	git config unset advice.objectNameWarning &&
 	TREE=$(git mktree </dev/null) &&
 	REF=$(git rev-parse HEAD) &&
 	VAL=$(git commit-tree $TREE </dev/null) &&
 	git update-ref refs/heads/$REF $VAL &&
 	test $(git rev-parse $REF 2>err) = $REF &&
-	grep "refname.*${REF}.*ambiguous" err
+	grep "refname.*${REF}.*ambiguous" err &&
+	test_grep hint: err
 '
 
 test_expect_success 'ambiguous short sha1 ref' '
-- 
2.47.1.407.gf6b6eee3e5
Previous: Rubén JustoNext: Bence Ferdinandy
Message 10 of 16 in “advice: suggest using subcommand "git config set"”
  1. advice: suggest using subcommand "git config set"Bence Ferdinandy, Dec 4, 2024
  2. Justin ToblerDec 4, 2024
  3. Bence FerdinandyDec 5, 2024
  4. Patrick SteinhardtDec 5, 2024
  5. advice: suggest using subcommand "git config set"Bence Ferdinandy, Dec 5, 2024
  6. Patrick SteinhardtDec 6, 2024
  7. Rubén JustoDec 8, 2024
  8. 1/3 advice: enhance `detach_advice()` to `detach_advice_if_enabled()`Rubén Justo, Dec 8, 2024
  9. 2/3 commit: use `advise_if_enabled()` in `read_graft_file()`Rubén Justo, Dec 8, 2024
  10. 3/3 object-name: advice to avoid refs that resemble hashesRubén Justo, Dec 8, 2024
  11. Bence FerdinandyDec 9, 2024
  12. Bence FerdinandyDec 9, 2024
  13. Rubén JustoDec 9, 2024
  14. Bence FerdinandyDec 11, 2024
  15. Rubén JustoDec 11, 2024
  16. Junio C HamanoDec 6, 2024

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.