Skip to content

Commit d6606e0

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c4ef5ed commit d6606e0

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

builtin/fetch.c

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ enum {
4848
};
4949

5050
struct display_state {
51+
struct strbuf buf;
52+
5153
int refcol_width;
5254
int compact_format;
5355

@@ -787,6 +789,8 @@ static void display_state_init(struct display_state *display_state, struct ref *
787789

788790
memset(display_state, 0, sizeof(*display_state));
789791

792+
strbuf_init(&display_state->buf, 0);
793+
790794
if (raw_url)
791795
display_state->url = transport_anonymize_url(raw_url);
792796
else
@@ -834,14 +838,15 @@ static void display_state_init(struct display_state *display_state, struct ref *
834838

835839
static void display_state_release(struct display_state *display_state)
836840
{
841+
strbuf_release(&display_state->buf);
837842
free(display_state->url);
838843
}
839844

840845
static void print_remote_to_local(struct display_state *display_state,
841-
struct strbuf *display_buffer,
842846
const char *remote, const char *local)
843847
{
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);
845850
}
846851

847852
static int find_and_replace(struct strbuf *haystack,
@@ -871,14 +876,14 @@ static int find_and_replace(struct strbuf *haystack,
871876
return 1;
872877
}
873878

874-
static void print_compact(struct display_state *display_state, struct strbuf *display_buffer,
879+
static void print_compact(struct display_state *display_state,
875880
const char *remote, const char *local)
876881
{
877882
struct strbuf r = STRBUF_INIT;
878883
struct strbuf l = STRBUF_INIT;
879884

880885
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);
882887
return;
883888
}
884889

@@ -887,46 +892,49 @@ static void print_compact(struct display_state *display_state, struct strbuf *di
887892

888893
if (!find_and_replace(&r, local, "*"))
889894
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);
891896

892897
strbuf_release(&r);
893898
strbuf_release(&l);
894899
}
895900

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)
901905
{
902906
int width;
903907

904908
if (verbosity < 0)
905909
return;
906910

911+
strbuf_reset(&display_state->buf);
912+
907913
if (!display_state->shown_url) {
908-
strbuf_addf(display_buffer, _("From %.*s\n"),
914+
strbuf_addf(&display_state->buf, _("From %.*s\n"),
909915
display_state->url_len, display_state->url);
910916
display_state->shown_url = 1;
911917
}
912918

913919
width = (summary_width + strlen(summary) - gettext_width(summary));
914920

915-
strbuf_addf(display_buffer, " %c %-*s ", code, width, summary);
921+
strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
916922
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));
918924
else
919-
print_compact(display_state, display_buffer, remote, prettify_refname(local));
925+
print_compact(display_state, remote, prettify_refname(local));
920926
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);
923931
}
924932

925933
static int update_local_ref(struct ref *ref,
926934
struct ref_transaction *transaction,
927935
struct display_state *display_state,
928936
const char *remote, const struct ref *remote_ref,
929-
struct strbuf *display, int summary_width)
937+
int summary_width)
930938
{
931939
struct commit *current = NULL, *updated;
932940
int fast_forward = 0;
@@ -936,8 +944,8 @@ static int update_local_ref(struct ref *ref,
936944

937945
if (oideq(&ref->old_oid, &ref->new_oid)) {
938946
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);
941949
return 0;
942950
}
943951

@@ -948,9 +956,9 @@ static int update_local_ref(struct ref *ref,
948956
* If this is the head, and it's not okay to update
949957
* the head, and the old value of the head isn't empty...
950958
*/
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);
954962
return 1;
955963
}
956964

@@ -959,14 +967,14 @@ static int update_local_ref(struct ref *ref,
959967
if (force || ref->force) {
960968
int r;
961969
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);
965973
return r;
966974
} 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);
970978
return 1;
971979
}
972980
}
@@ -997,9 +1005,9 @@ static int update_local_ref(struct ref *ref,
9971005
}
9981006

9991007
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);
10031011
return r;
10041012
}
10051013

@@ -1019,9 +1027,9 @@ static int update_local_ref(struct ref *ref,
10191027
strbuf_addstr(&quickref, "..");
10201028
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
10211029
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);
10251033
strbuf_release(&quickref);
10261034
return r;
10271035
} else if (force || ref->force) {
@@ -1031,14 +1039,14 @@ static int update_local_ref(struct ref *ref,
10311039
strbuf_addstr(&quickref, "...");
10321040
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
10331041
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);
10371045
strbuf_release(&quickref);
10381046
return r;
10391047
} 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);
10421050
return 1;
10431051
}
10441052
}
@@ -1266,24 +1274,21 @@ static int store_updated_refs(struct display_state *display_state,
12661274
note.buf, display_state->url,
12671275
display_state->url_len);
12681276

1269-
strbuf_reset(&note);
12701277
if (ref) {
12711278
rc |= update_local_ref(ref, transaction, display_state, what,
1272-
rm, &note, summary_width);
1279+
rm, summary_width);
12731280
free(ref);
12741281
} else if (write_fetch_head || dry_run) {
12751282
/*
12761283
* Display fetches written to FETCH_HEAD (or
12771284
* would be written to FETCH_HEAD, if --dry-run
12781285
* is set).
12791286
*/
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);
12841291
}
1285-
if (note.len)
1286-
fputs(note.buf, stderr);
12871292
}
12881293
}
12891294

@@ -1419,12 +1424,9 @@ static int prune_refs(struct display_state *display_state,
14191424
int summary_width = transport_summary_width(stale_refs);
14201425

14211426
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);
14281430
warn_dangling_symref(stderr, dangling_msg, ref->name);
14291431
}
14301432
}

0 commit comments

Comments
 (0)