fetch: centralize printing of reference updates

In order to print updated references during a fetch, the two different call sites that do this will first call `format_display()` followed by a call to `fputs()`. This is needlessly roundabout now that we have the `display_state` structure that encapsulates all of the printing logic for references. Move displaying the reference updates into `format_display()` and rename it to `display_ref_update()` to better match its new purpose, which finalizes the conversion to make both the formatting and printing logic of reference updates self-contained. This will make it easier to add new output formats and printing to a different file descriptor than stderr. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 20, 2023 at 13:35 UTC d6606e02aaff963f51fe9cbda048613ff0cd5870
1 file changed +55 -53
builtin/fetch.c
+55 -53
@@ -48,6 +48,8 @@ enum {
48 };
49
50 struct display_state {
51 + struct strbuf buf;
52 +
53 int refcol_width;
54 int compact_format;
55
@@ -787,6 +789,8 @@ static void display_state_init(struct display_state *display_state, struct ref *
789
790 memset(display_state, 0, sizeof(*display_state));
791
792 + strbuf_init(&display_state->buf, 0);
793 +
794 if (raw_url)
795 display_state->url = transport_anonymize_url(raw_url);
796 else
@@ -834,14 +838,15 @@ static void display_state_init(struct display_state *display_state, struct ref *
838
839 static void display_state_release(struct display_state *display_state)
840 {
841 + strbuf_release(&display_state->buf);
842 free(display_state->url);
843 }
844
845 static void print_remote_to_local(struct display_state *display_state,
841 - struct strbuf *display_buffer,
846 const char *remote, const char *local)
847 {
844 - strbuf_addf(display_buffer, "%-*s -> %s", display_state->refcol_width, remote, local);
848 + strbuf_addf(&display_state->buf, "%-*s -> %s",
849 + display_state->refcol_width, remote, local);
850 }
851
852 static int find_and_replace(struct strbuf *haystack,
@@ -871,14 +876,14 @@ static int find_and_replace(struct strbuf *haystack,
876 return 1;
877 }
878
874 -static void print_compact(struct display_state *display_state, struct strbuf *display_buffer,
879 +static void print_compact(struct display_state *display_state,
880 const char *remote, const char *local)
881 {
882 struct strbuf r = STRBUF_INIT;
883 struct strbuf l = STRBUF_INIT;
884
885 if (!strcmp(remote, local)) {
881 - strbuf_addf(display_buffer, "%-*s -> *", display_state->refcol_width, remote);
886 + strbuf_addf(&display_state->buf, "%-*s -> *", display_state->refcol_width, remote);
887 return;
888 }
889
@@ -887,46 +892,49 @@ static void print_compact(struct display_state *display_state, struct strbuf *di
892
893 if (!find_and_replace(&r, local, "*"))
894 find_and_replace(&l, remote, "*");
890 - print_remote_to_local(display_state, display_buffer, r.buf, l.buf);
895 + print_remote_to_local(display_state, r.buf, l.buf);
896
897 strbuf_release(&r);
898 strbuf_release(&l);
899 }
900
896 -static void format_display(struct display_state *display_state,
897 - struct strbuf *display_buffer, char code,
898 - const char *summary, const char *error,
899 - const char *remote, const char *local,
900 - int summary_width)
901 +static void display_ref_update(struct display_state *display_state, char code,
902 + const char *summary, const char *error,
903 + const char *remote, const char *local,
904 + int summary_width)
905 {
906 int width;
907
908 if (verbosity < 0)
909 return;
910
911 + strbuf_reset(&display_state->buf);
912 +
913 if (!display_state->shown_url) {
908 - strbuf_addf(display_buffer, _("From %.*s\n"),
914 + strbuf_addf(&display_state->buf, _("From %.*s\n"),
915 display_state->url_len, display_state->url);
916 display_state->shown_url = 1;
917 }
918
919 width = (summary_width + strlen(summary) - gettext_width(summary));
920
915 - strbuf_addf(display_buffer, " %c %-*s ", code, width, summary);
921 + strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
922 if (!display_state->compact_format)
917 - print_remote_to_local(display_state, display_buffer, remote, prettify_refname(local));
923 + print_remote_to_local(display_state, remote, prettify_refname(local));
924 else
919 - print_compact(display_state, display_buffer, remote, prettify_refname(local));
925 + print_compact(display_state, remote, prettify_refname(local));
926 if (error)
921 - strbuf_addf(display_buffer, " (%s)", error);
922 - strbuf_addch(display_buffer, '\n');
927 + strbuf_addf(&display_state->buf, " (%s)", error);
928 + strbuf_addch(&display_state->buf, '\n');
929 +
930 + fputs(display_state->buf.buf, stderr);
931 }
932
933 static int update_local_ref(struct ref *ref,
934 struct ref_transaction *transaction,
935 struct display_state *display_state,
936 const char *remote, const struct ref *remote_ref,
929 - struct strbuf *display, int summary_width)
937 + int summary_width)
938 {
939 struct commit *current = NULL, *updated;
940 int fast_forward = 0;
@@ -936,8 +944,8 @@ static int update_local_ref(struct ref *ref,
944
945 if (oideq(&ref->old_oid, &ref->new_oid)) {
946 if (verbosity > 0)
939 - format_display(display_state, display, '=', _("[up to date]"), NULL,
940 - remote, ref->name, summary_width);
947 + display_ref_update(display_state, '=', _("[up to date]"), NULL,
948 + remote, ref->name, summary_width);
949 return 0;
950 }
951
@@ -948,9 +956,9 @@ static int update_local_ref(struct ref *ref,
956 * If this is the head, and it's not okay to update
957 * the head, and the old value of the head isn't empty...
958 */
951 - format_display(display_state, display, '!', _("[rejected]"),
952 - _("can't fetch into checked-out branch"),
953 - remote, ref->name, summary_width);
959 + display_ref_update(display_state, '!', _("[rejected]"),
960 + _("can't fetch into checked-out branch"),
961 + remote, ref->name, summary_width);
962 return 1;
963 }
964
@@ -959,14 +967,14 @@ static int update_local_ref(struct ref *ref,
967 if (force || ref->force) {
968 int r;
969 r = s_update_ref("updating tag", ref, transaction, 0);
962 - format_display(display_state, display, r ? '!' : 't', _("[tag update]"),
963 - r ? _("unable to update local ref") : NULL,
964 - remote, ref->name, summary_width);
970 + display_ref_update(display_state, r ? '!' : 't', _("[tag update]"),
971 + r ? _("unable to update local ref") : NULL,
972 + remote, ref->name, summary_width);
973 return r;
974 } else {
967 - format_display(display_state, display, '!', _("[rejected]"),
968 - _("would clobber existing tag"),
969 - remote, ref->name, summary_width);
975 + display_ref_update(display_state, '!', _("[rejected]"),
976 + _("would clobber existing tag"),
977 + remote, ref->name, summary_width);
978 return 1;
979 }
980 }
@@ -997,9 +1005,9 @@ static int update_local_ref(struct ref *ref,
1005 }
1006
1007 r = s_update_ref(msg, ref, transaction, 0);
1000 - format_display(display_state, display, r ? '!' : '*', what,
1001 - r ? _("unable to update local ref") : NULL,
1002 - remote, ref->name, summary_width);
1008 + display_ref_update(display_state, r ? '!' : '*', what,
1009 + r ? _("unable to update local ref") : NULL,
1010 + remote, ref->name, summary_width);
1011 return r;
1012 }
1013
@@ -1019,9 +1027,9 @@ static int update_local_ref(struct ref *ref,
1027 strbuf_addstr(&quickref, "..");
1028 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
1029 r = s_update_ref("fast-forward", ref, transaction, 1);
1022 - format_display(display_state, display, r ? '!' : ' ', quickref.buf,
1023 - r ? _("unable to update local ref") : NULL,
1024 - remote, ref->name, summary_width);
1030 + display_ref_update(display_state, r ? '!' : ' ', quickref.buf,
1031 + r ? _("unable to update local ref") : NULL,
1032 + remote, ref->name, summary_width);
1033 strbuf_release(&quickref);
1034 return r;
1035 } else if (force || ref->force) {
@@ -1031,14 +1039,14 @@ static int update_local_ref(struct ref *ref,
1039 strbuf_addstr(&quickref, "...");
1040 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
1041 r = s_update_ref("forced-update", ref, transaction, 1);
1034 - format_display(display_state, display, r ? '!' : '+', quickref.buf,
1035 - r ? _("unable to update local ref") : _("forced update"),
1036 - remote, ref->name, summary_width);
1042 + display_ref_update(display_state, r ? '!' : '+', quickref.buf,
1043 + r ? _("unable to update local ref") : _("forced update"),
1044 + remote, ref->name, summary_width);
1045 strbuf_release(&quickref);
1046 return r;
1047 } else {
1040 - format_display(display_state, display, '!', _("[rejected]"), _("non-fast-forward"),
1041 - remote, ref->name, summary_width);
1048 + display_ref_update(display_state, '!', _("[rejected]"), _("non-fast-forward"),
1049 + remote, ref->name, summary_width);
1050 return 1;
1051 }
1052 }
@@ -1266,10 +1274,9 @@ static int store_updated_refs(struct display_state *display_state,
1274 note.buf, display_state->url,
1275 display_state->url_len);
1276
1269 - strbuf_reset(&note);
1277 if (ref) {
1278 rc |= update_local_ref(ref, transaction, display_state, what,
1272 - rm, &note, summary_width);
1279 + rm, summary_width);
1280 free(ref);
1281 } else if (write_fetch_head || dry_run) {
1282 /*
@@ -1277,13 +1284,11 @@ static int store_updated_refs(struct display_state *display_state,
1284 * would be written to FETCH_HEAD, if --dry-run
1285 * is set).
1286 */
1280 - format_display(display_state, &note, '*',
1281 - *kind ? kind : "branch", NULL,
1282 - *what ? what : "HEAD",
1283 - "FETCH_HEAD", summary_width);
1287 + display_ref_update(display_state, '*',
1288 + *kind ? kind : "branch", NULL,
1289 + *what ? what : "HEAD",
1290 + "FETCH_HEAD", summary_width);
1291 }
1285 - if (note.len)
1286 - fputs(note.buf, stderr);
1292 }
1293 }
1294
@@ -1419,12 +1424,9 @@ static int prune_refs(struct display_state *display_state,
1424 int summary_width = transport_summary_width(stale_refs);
1425
1426 for (ref = stale_refs; ref; ref = ref->next) {
1422 - struct strbuf sb = STRBUF_INIT;
1423 - format_display(display_state, &sb, '-', _("[deleted]"), NULL,
1424 - _("(none)"), ref->name,
1425 - summary_width);
1426 - fputs(sb.buf, stderr);
1427 - strbuf_release(&sb);
1427 + display_ref_update(display_state, '-', _("[deleted]"), NULL,
1428 + _("(none)"), ref->name,
1429 + summary_width);
1430 warn_dangling_symref(stderr, dangling_msg, ref->name);
1431 }
1432 }