get_sha1: avoid repeating ourselves via ONLY_TO_DIE
authorJeff King <peff@peff.net>
Mon, 26 Sep 2016 11:59:15 +0000 (07:59 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Sep 2016 18:46:30 +0000 (11:46 -0700)
When the revision code cannot parse an argument like
"HEAD:foo", it will call maybe_die_on_misspelt_object_name(),
which re-runs get_sha1() with an extra ONLY_TO_DIE flag. We
then spend more effort to generate a better error message.

Unfortunately, a side effect is that our second call may
repeat the same error messages from the original get_sha1()
call. You can see this with:

$ git show 0017
error: short SHA1 0017 is ambiguous.
error: short SHA1 0017 is ambiguous.
fatal: ambiguous argument '0017': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

where the second "error:" line comes from the ONLY_TO_DIE
call.

To fix this, we can make ONLY_TO_DIE imply QUIETLY. This is
a little odd, because the whole point of ONLY_TO_DIE is to
output error messages. But what we want to do is tell the
rest of the get_sha1() code (particularly get_sha1_1()) that
the _regular_ messages should be quiet, but the only-to-die
ones should not.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sha1_name.c
t/t1512-rev-parse-disambiguation.sh
index 0ff83a99851ba091023cc972741aca94bd0368fd..e7e3a387288cc4d60da0ed51f5650c5fb148ba89 100644 (file)
@@ -1386,6 +1386,9 @@ static int get_sha1_with_context_1(const char *name,
        const char *cp;
        int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
 
+       if (only_to_die)
+               flags |= GET_SHA1_QUIETLY;
+
        memset(oc, 0, sizeof(*oc));
        oc->mode = S_IFINVALID;
        ret = get_sha1_1(name, namelen, sha1, flags);
index e221167cfbe3fb34cf5aba88cd04f6a1dc9eede5..16f970982bd45d82e21c4314d823acbbd3f4f495 100755 (executable)
@@ -291,4 +291,10 @@ test_expect_success 'ambiguous short sha1 ref' '
        grep "refname.*${REF}.*ambiguous" err
 '
 
+test_expect_success C_LOCALE_OUTPUT 'ambiguity errors are not repeated' '
+       test_must_fail git rev-parse 00000 2>stderr &&
+       grep "is ambiguous" stderr >errors &&
+       test_line_count = 1 errors
+'
+
 test_done