]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-name: iterate ambiguous objects before showing header
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 27 Jan 2022 05:26:48 +0000 (06:26 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 27 Jan 2022 20:09:34 +0000 (12:09 -0800)
Change the "The candidates are" header that's shown for ambiguous
objects to be shown after we've iterated over all of the objects.

If we get any errors while doing so we don't want to split up the the
header and the list as a result. The two will now be printed together,
as shown in the updated testcase.

As we're accumulating the lines into as "struct strbuf" before
emitting them we need to add a trailing newline to the call in
show_ambiguous_object(). This and the change from "The candidates
are:" to "The candidates are:\n%s" helps to give translators more
context.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-name.c
t/t1512-rev-parse-disambiguation.sh

index cbf459f5664c93b2b2cc41aae93d35b5d184a265..6154e1ec6f858f28a37f3600d49b0c4b97f7276e 100644 (file)
@@ -351,9 +351,16 @@ static int init_object_disambiguation(struct repository *r,
        return 0;
 }
 
+struct ambiguous_output {
+       const struct disambiguate_state *ds;
+       struct strbuf advice;
+};
+
 static int show_ambiguous_object(const struct object_id *oid, void *data)
 {
-       const struct disambiguate_state *ds = data;
+       struct ambiguous_output *state = data;
+       const struct disambiguate_state *ds = state->ds;
+       struct strbuf *advice = &state->advice;
        struct strbuf desc = STRBUF_INIT;
        int type;
        const char *hash;
@@ -452,7 +459,7 @@ out:
         * you'll probably want to swap the "%s" and leading " " space
         * around.
         */
-       advise(_("  %s"), desc.buf);
+       strbuf_addf(advice, _("  %s\n"), desc.buf);
 
        strbuf_release(&desc);
        return 0;
@@ -551,6 +558,10 @@ static enum get_oid_result get_short_oid(struct repository *r,
 
        if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
                struct oid_array collect = OID_ARRAY_INIT;
+               struct ambiguous_output out = {
+                       .ds = &ds,
+                       .advice = STRBUF_INIT,
+               };
 
                error(_("short object ID %s is ambiguous"), ds.hex_pfx);
 
@@ -563,13 +574,21 @@ static enum get_oid_result get_short_oid(struct repository *r,
                if (!ds.ambiguous)
                        ds.fn = NULL;
 
-               advise(_("The candidates are:"));
                repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect);
                sort_ambiguous_oid_array(r, &collect);
 
-               if (oid_array_for_each(&collect, show_ambiguous_object, &ds))
+               if (oid_array_for_each(&collect, show_ambiguous_object, &out))
                        BUG("show_ambiguous_object shouldn't return non-zero");
+
+               /*
+                * TRANSLATORS: The argument is the list of ambiguous
+                * objects composed in show_ambiguous_object(). See
+                * its "TRANSLATORS" comments for details.
+                */
+               advise(_("The candidates are:\n%s"), out.advice.buf);
+
                oid_array_clear(&collect);
+               strbuf_release(&out.advice);
        }
 
        return status;
index 0282ada1338b3f34acce8b7884d993fa6fe710f3..be687aebb260ae6f1d565cba0eed0e0436f6542b 100755 (executable)
@@ -73,7 +73,6 @@ test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' '
 
        test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF
        error: short object ID bad0... is ambiguous
-       hint: The candidates are:
        fatal: invalid object type
        EOF
 '
@@ -94,11 +93,11 @@ test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' '
 
        test_cmp_failed_rev_parse blob.corrupt cafe <<-\EOF
        error: short object ID cafe... is ambiguous
-       hint: The candidates are:
        error: inflate: data stream error (incorrect header check)
        error: unable to unpack cafe... header
        error: inflate: data stream error (incorrect header check)
        error: unable to unpack cafe... header
+       hint: The candidates are:
        hint:   cafe... [bad object]
        hint:   cafe... blob
        fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree.