fetch: refactor ref update status formatting code
This makes it easier to change the formatting later. And it makes sure translators cannot mess up format specifiers and break Git. There are a couple call sites where the length of the second column is TRANSPORT_SUMMARY_WIDTH instead of calculated by TRANSPORT_SUMMARY(), which is enforced now. The result should be the same because these call sites do not contain characters outside ASCII range. The two strbuf_addf() calls instead of one is mostly to reduce diff-noise in a future patch where "ref -> ref" is reformatted differently. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jun 26, 2016 at 07:58 UTC
d0b39a03cd5d6d5f9e2eac4907d9f47ec3d821da
1 file changed
+38
-39
builtin/fetch.c
+38
-39
@@ -451,6 +451,16 @@ fail:
451
452
#define REFCOL_WIDTH 10
453
454
+static void format_display(struct strbuf *display, char code,
455
+ const char *summary, const char *error,
456
+ const char *remote, const char *local)
457
+{
458
+ strbuf_addf(display, "%c %-*s ", code, TRANSPORT_SUMMARY(summary));
459
+ strbuf_addf(display, "%-*s -> %s", REFCOL_WIDTH, remote, local);
460
+ if (error)
461
+ strbuf_addf(display, " (%s)", error);
462
+}
463
+
464
static int update_local_ref(struct ref *ref,
465
const char *remote,
466
const struct ref *remote_ref,
@@ -467,9 +477,8 @@ static int update_local_ref(struct ref *ref,
477
478
if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
479
if (verbosity > 0)
470
- strbuf_addf(display, "= %-*s %-*s -> %s",
471
- TRANSPORT_SUMMARY(_("[up to date]")),
472
- REFCOL_WIDTH, remote, pretty_ref);
480
+ format_display(display, '=', _("[up to date]"), NULL,
481
+ remote, pretty_ref);
482
return 0;
483
}
484
@@ -481,10 +490,9 @@ static int update_local_ref(struct ref *ref,
490
* If this is the head, and it's not okay to update
491
* the head, and the old value of the head isn't empty...
492
*/
484
- strbuf_addf(display,
485
- _("! %-*s %-*s -> %s (can't fetch in current branch)"),
486
- TRANSPORT_SUMMARY(_("[rejected]")),
487
- REFCOL_WIDTH, remote, pretty_ref);
493
+ format_display(display, '!', _("[rejected]"),
494
+ _("can't fetch in current branch"),
495
+ remote, pretty_ref);
496
return 1;
497
}
498
@@ -492,11 +500,9 @@ static int update_local_ref(struct ref *ref,
500
starts_with(ref->name, "refs/tags/")) {
501
int r;
502
r = s_update_ref("updating tag", ref, 0);
495
- strbuf_addf(display, "%c %-*s %-*s -> %s%s",
496
- r ? '!' : '-',
497
- TRANSPORT_SUMMARY(_("[tag update]")),
498
- REFCOL_WIDTH, remote, pretty_ref,
499
- r ? _(" (unable to update local ref)") : "");
503
+ format_display(display, r ? '!' : '-', _("[tag update]"),
504
+ r ? _("unable to update local ref") : NULL,
505
+ remote, pretty_ref);
506
return r;
507
}
508
@@ -527,11 +533,9 @@ static int update_local_ref(struct ref *ref,
533
(recurse_submodules != RECURSE_SUBMODULES_ON))
534
check_for_new_submodule_commits(ref->new_oid.hash);
535
r = s_update_ref(msg, ref, 0);
530
- strbuf_addf(display, "%c %-*s %-*s -> %s%s",
531
- r ? '!' : '*',
532
- TRANSPORT_SUMMARY(what),
533
- REFCOL_WIDTH, remote, pretty_ref,
534
- r ? _(" (unable to update local ref)") : "");
536
+ format_display(display, r ? '!' : '*', what,
537
+ r ? _("unable to update local ref") : NULL,
538
+ remote, pretty_ref);
539
return r;
540
}
541
@@ -545,11 +549,9 @@ static int update_local_ref(struct ref *ref,
549
(recurse_submodules != RECURSE_SUBMODULES_ON))
550
check_for_new_submodule_commits(ref->new_oid.hash);
551
r = s_update_ref("fast-forward", ref, 1);
548
- strbuf_addf(display, "%c %-*s %-*s -> %s%s",
549
- r ? '!' : ' ',
550
- TRANSPORT_SUMMARY_WIDTH, quickref.buf,
551
- REFCOL_WIDTH, remote, pretty_ref,
552
- r ? _(" (unable to update local ref)") : "");
552
+ format_display(display, r ? '!' : ' ', quickref.buf,
553
+ r ? _("unable to update local ref") : NULL,
554
+ remote, pretty_ref);
555
strbuf_release(&quickref);
556
return r;
557
} else if (force || ref->force) {
@@ -562,18 +564,14 @@ static int update_local_ref(struct ref *ref,
564
(recurse_submodules != RECURSE_SUBMODULES_ON))
565
check_for_new_submodule_commits(ref->new_oid.hash);
566
r = s_update_ref("forced-update", ref, 1);
565
- strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
566
- r ? '!' : '+',
567
- TRANSPORT_SUMMARY_WIDTH, quickref.buf,
568
- REFCOL_WIDTH, remote, pretty_ref,
569
- r ? _("unable to update local ref") : _("forced update"));
567
+ format_display(display, r ? '!' : '+', quickref.buf,
568
+ r ? _("unable to update local ref") : _("forced update"),
569
+ remote, pretty_ref);
570
strbuf_release(&quickref);
571
return r;
572
} else {
573
- strbuf_addf(display, "! %-*s %-*s -> %s %s",
574
- TRANSPORT_SUMMARY(_("[rejected]")),
575
- REFCOL_WIDTH, remote, pretty_ref,
576
- _("(non-fast-forward)"));
573
+ format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
574
+ remote, pretty_ref);
575
return 1;
576
}
577
}
@@ -714,11 +712,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
712
rc |= update_local_ref(ref, what, rm, ¬e);
713
free(ref);
714
} else
717
- strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
718
- TRANSPORT_SUMMARY_WIDTH,
719
- *kind ? kind : "branch",
720
- REFCOL_WIDTH,
721
- *what ? what : "HEAD");
715
+ format_display(¬e, '*',
716
+ *kind ? kind : "branch", NULL,
717
+ *what ? what : "HEAD",
718
+ "FETCH_HEAD");
719
if (note.len) {
720
if (verbosity >= 0 && !shown_url) {
721
fprintf(stderr, _("From %.*s\n"),
@@ -812,13 +809,15 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
809
810
if (verbosity >= 0) {
811
for (ref = stale_refs; ref; ref = ref->next) {
812
+ struct strbuf sb = STRBUF_INIT;
813
if (!shown_url) {
814
fprintf(stderr, _("From %.*s\n"), url_len, url);
815
shown_url = 1;
816
}
819
- fprintf(stderr, " x %-*s %-*s -> %s\n",
820
- TRANSPORT_SUMMARY(_("[deleted]")),
821
- REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
817
+ format_display(&sb, 'x', _("[deleted]"), NULL,
818
+ _("(none)"), prettify_refname(ref->name));
819
+ fprintf(stderr, " %s\n",sb.buf);
820
+ strbuf_release(&sb);
821
warn_dangling_symref(stderr, dangling_msg, ref->name);
822
}
823
}