diff mbox series

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

Message ID 43a66f17-c910-498a-8faa-f801194e6c8e@gmail.com (mailing list archive)
State New
Headers show
Series [1/3] advice: enhance `detach_advice()` to `detach_advice_if_enabled()` | expand

Commit Message

Rubén Justo Dec. 8, 2024, 8:12 a.m. UTC
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 mbox series

Patch

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' '