object-name: make ambiguous object output translatable

Change the output of show_ambiguous_object() added in [1] and last tweaked in [2] and the preceding commit to be more friendly to translators. By being able to customize the "<SP><SP>%s\n" format we're even ready for RTL languages, who'd presumably like to change that to "%s<SP><SP>\n". In the case of the existing "tag [tag could not be parsed]" output we'll now instead emit "[bad tag, could not parse it]". This is consistent with the "[bad object]" output. Rephrasing the message like this is possible because we're not unconditionally adding the type_name() at the beginning. 1. 1ffa26c461 (get_short_sha1: list ambiguous objects on error, 2016-09-26) 2. 5cc044e0257 (get_short_oid: sort ambiguous objects by type, then SHA-1, 2018-05-10) Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 27, 2022 at 06:26 UTC ba5e8a0eb8011f7ffb246804e91c9ba0db3befe4
1 file changed +68 -10
object-name.c
+68 -10
@@ -356,40 +356,98 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
356 const struct disambiguate_state *ds = data;
357 struct strbuf desc = STRBUF_INIT;
358 int type;
359 + const char *hash;
360
361 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
362 return 0;
363
364 + hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
365 type = oid_object_info(ds->repo, oid, NULL);
366
367 if (type < 0) {
366 - strbuf_addstr(&desc, "[bad object]");
368 + /*
369 + * TRANSLATORS: This is a line of ambiguous object
370 + * output shown when we cannot look up or parse the
371 + * object in question. E.g. "deadbeef [bad object]".
372 + */
373 + strbuf_addf(&desc, _("%s [bad object]"), hash);
374 goto out;
375 }
376
377 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
378 type == OBJ_BLOB || type == OBJ_TAG);
372 - strbuf_addstr(&desc, type_name(type));
379
380 if (type == OBJ_COMMIT) {
381 + struct strbuf date = STRBUF_INIT;
382 + struct strbuf msg = STRBUF_INIT;
383 struct commit *commit = lookup_commit(ds->repo, oid);
384 +
385 if (commit) {
386 struct pretty_print_context pp = {0};
387 pp.date_mode.type = DATE_SHORT;
379 - format_commit_message(commit, " %ad - %s", &desc, &pp);
388 + format_commit_message(commit, "%ad", &date, &pp);
389 + format_commit_message(commit, "%s", &msg, &pp);
390 }
391 +
392 + /*
393 + * TRANSLATORS: This is a line of ambiguous commit
394 + * object output. E.g.:
395 + *
396 + * "deadbeef commit 2021-01-01 - Some Commit Message"
397 + */
398 + strbuf_addf(&desc, _("%s commit %s - %s"),
399 + hash, date.buf, msg.buf);
400 +
401 + strbuf_release(&date);
402 + strbuf_release(&msg);
403 } else if (type == OBJ_TAG) {
404 struct tag *tag = lookup_tag(ds->repo, oid);
383 - if (!parse_tag(tag) && tag->tag)
384 - strbuf_addf(&desc, " %s", tag->tag);
385 - else
386 - strbuf_addstr(&desc, " [tag could not be parsed]");
405 +
406 + if (!parse_tag(tag) && tag->tag) {
407 + /*
408 + * TRANSLATORS: This is a line of ambiguous
409 + * tag object output. E.g.:
410 + *
411 + * "deadbeef tag Some Tag Message"
412 + *
413 + * The second argument is the "tag" string
414 + * from object.c.
415 + */
416 + strbuf_addf(&desc, _("%s tag %s"), hash, tag->tag);
417 + } else {
418 + /*
419 + * TRANSLATORS: This is a line of ambiguous
420 + * tag object output where we couldn't parse
421 + * the tag itself. E.g.:
422 + *
423 + * "deadbeef tag [bad tag, could not parse it]"
424 + */
425 + strbuf_addf(&desc, _("%s [bad tag, could not parse it]"),
426 + hash);
427 + }
428 + } else if (type == OBJ_TREE) {
429 + /*
430 + * TRANSLATORS: This is a line of ambiguous <type>
431 + * object output. E.g. "deadbeef tree".
432 + */
433 + strbuf_addf(&desc, _("%s tree"), hash);
434 + } else if (type == OBJ_BLOB) {
435 + /*
436 + * TRANSLATORS: This is a line of ambiguous <type>
437 + * object output. E.g. "deadbeef blob".
438 + */
439 + strbuf_addf(&desc, _("%s blob"), hash);
440 }
441
442 +
443 out:
390 - advise(" %s %s",
391 - repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
392 - desc.buf);
444 + /*
445 + * TRANSLATORS: This is line item of ambiguous object output
446 + * from describe_ambiguous_object() above. For RTL languages
447 + * you'll probably want to swap the "%s" and leading " " space
448 + * around.
449 + */
450 + advise(_(" %s"), desc.buf);
451
452 strbuf_release(&desc);
453 return 0;