@@ -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);
}
@@ -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' '
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(-)