Skip to content

Commit ce9636d

Browse files
pks-tgitster
authored andcommitted
fetch: move reference width calculation into display_state
In order to print references in proper columns we need to calculate the width of the reference column before starting to print the references. This is done with the help of a global variable `refcol_width`. Refactor the code to instead use a new structure `display_state` that contains the computed width and plumb it through the stack as required. This is only the first step towards de-globalizing the state required to print references. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73876f4 commit ce9636d

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

builtin/fetch.c

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ enum {
4747
TAGS_SET = 2
4848
};
4949

50+
struct display_state {
51+
int refcol_width;
52+
};
53+
5054
static int fetch_prune_config = -1; /* unspecified */
5155
static int fetch_show_forced_updates = 1;
5256
static uint64_t forced_updates_ms = 0;
@@ -741,16 +745,15 @@ static int s_update_ref(const char *action,
741745
return ret;
742746
}
743747

744-
static int refcol_width = 10;
745748
static int compact_format;
746749

747-
static void adjust_refcol_width(const struct ref *ref)
750+
static int refcol_width(const struct ref *ref)
748751
{
749752
int max, rlen, llen, len;
750753

751754
/* uptodate lines are only shown on high verbosity level */
752755
if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
753-
return;
756+
return 0;
754757

755758
max = term_columns();
756759
rlen = utf8_strwidth(prettify_refname(ref->name));
@@ -769,22 +772,18 @@ static void adjust_refcol_width(const struct ref *ref)
769772
}
770773
len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
771774
if (len >= max)
772-
return;
775+
return 0;
773776

774-
/*
775-
* Not precise calculation for compact mode because '*' can
776-
* appear on the left hand side of '->' and shrink the column
777-
* back.
778-
*/
779-
if (refcol_width < rlen)
780-
refcol_width = rlen;
777+
return rlen;
781778
}
782779

783-
static void prepare_format_display(struct ref *ref_map)
780+
static void display_state_init(struct display_state *display_state, struct ref *ref_map)
784781
{
785782
struct ref *rm;
786783
const char *format = "full";
787784

785+
memset(display_state, 0, sizeof(*display_state));
786+
788787
if (verbosity < 0)
789788
return;
790789

@@ -797,20 +796,32 @@ static void prepare_format_display(struct ref *ref_map)
797796
die(_("invalid value for '%s': '%s'"),
798797
"fetch.output", format);
799798

799+
display_state->refcol_width = 10;
800800
for (rm = ref_map; rm; rm = rm->next) {
801+
int width;
802+
801803
if (rm->status == REF_STATUS_REJECT_SHALLOW ||
802804
!rm->peer_ref ||
803805
!strcmp(rm->name, "HEAD"))
804806
continue;
805807

806-
adjust_refcol_width(rm);
808+
width = refcol_width(rm);
809+
810+
/*
811+
* Not precise calculation for compact mode because '*' can
812+
* appear on the left hand side of '->' and shrink the column
813+
* back.
814+
*/
815+
if (display_state->refcol_width < width)
816+
display_state->refcol_width = width;
807817
}
808818
}
809819

810-
static void print_remote_to_local(struct strbuf *display,
820+
static void print_remote_to_local(struct display_state *display_state,
821+
struct strbuf *display_buffer,
811822
const char *remote, const char *local)
812823
{
813-
strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
824+
strbuf_addf(display_buffer, "%-*s -> %s", display_state->refcol_width, remote, local);
814825
}
815826

816827
static int find_and_replace(struct strbuf *haystack,
@@ -840,14 +851,14 @@ static int find_and_replace(struct strbuf *haystack,
840851
return 1;
841852
}
842853

843-
static void print_compact(struct strbuf *display,
854+
static void print_compact(struct display_state *display_state, struct strbuf *display_buffer,
844855
const char *remote, const char *local)
845856
{
846857
struct strbuf r = STRBUF_INIT;
847858
struct strbuf l = STRBUF_INIT;
848859

849860
if (!strcmp(remote, local)) {
850-
strbuf_addf(display, "%-*s -> *", refcol_width, remote);
861+
strbuf_addf(display_buffer, "%-*s -> *", display_state->refcol_width, remote);
851862
return;
852863
}
853864

@@ -856,13 +867,14 @@ static void print_compact(struct strbuf *display,
856867

857868
if (!find_and_replace(&r, local, "*"))
858869
find_and_replace(&l, remote, "*");
859-
print_remote_to_local(display, r.buf, l.buf);
870+
print_remote_to_local(display_state, display_buffer, r.buf, l.buf);
860871

861872
strbuf_release(&r);
862873
strbuf_release(&l);
863874
}
864875

865-
static void format_display(struct strbuf *display, char code,
876+
static void format_display(struct display_state *display_state,
877+
struct strbuf *display_buffer, char code,
866878
const char *summary, const char *error,
867879
const char *remote, const char *local,
868880
int summary_width)
@@ -874,17 +886,18 @@ static void format_display(struct strbuf *display, char code,
874886

875887
width = (summary_width + strlen(summary) - gettext_width(summary));
876888

877-
strbuf_addf(display, "%c %-*s ", code, width, summary);
889+
strbuf_addf(display_buffer, "%c %-*s ", code, width, summary);
878890
if (!compact_format)
879-
print_remote_to_local(display, remote, local);
891+
print_remote_to_local(display_state, display_buffer, remote, local);
880892
else
881-
print_compact(display, remote, local);
893+
print_compact(display_state, display_buffer, remote, local);
882894
if (error)
883-
strbuf_addf(display, " (%s)", error);
895+
strbuf_addf(display_buffer, " (%s)", error);
884896
}
885897

886898
static int update_local_ref(struct ref *ref,
887899
struct ref_transaction *transaction,
900+
struct display_state *display_state,
888901
const char *remote, const struct ref *remote_ref,
889902
struct strbuf *display, int summary_width)
890903
{
@@ -897,7 +910,7 @@ static int update_local_ref(struct ref *ref,
897910

898911
if (oideq(&ref->old_oid, &ref->new_oid)) {
899912
if (verbosity > 0)
900-
format_display(display, '=', _("[up to date]"), NULL,
913+
format_display(display_state, display, '=', _("[up to date]"), NULL,
901914
remote, pretty_ref, summary_width);
902915
return 0;
903916
}
@@ -909,7 +922,7 @@ static int update_local_ref(struct ref *ref,
909922
* If this is the head, and it's not okay to update
910923
* the head, and the old value of the head isn't empty...
911924
*/
912-
format_display(display, '!', _("[rejected]"),
925+
format_display(display_state, display, '!', _("[rejected]"),
913926
_("can't fetch into checked-out branch"),
914927
remote, pretty_ref, summary_width);
915928
return 1;
@@ -920,12 +933,13 @@ static int update_local_ref(struct ref *ref,
920933
if (force || ref->force) {
921934
int r;
922935
r = s_update_ref("updating tag", ref, transaction, 0);
923-
format_display(display, r ? '!' : 't', _("[tag update]"),
936+
format_display(display_state, display, r ? '!' : 't', _("[tag update]"),
924937
r ? _("unable to update local ref") : NULL,
925938
remote, pretty_ref, summary_width);
926939
return r;
927940
} else {
928-
format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
941+
format_display(display_state, display, '!', _("[rejected]"),
942+
_("would clobber existing tag"),
929943
remote, pretty_ref, summary_width);
930944
return 1;
931945
}
@@ -957,7 +971,7 @@ static int update_local_ref(struct ref *ref,
957971
}
958972

959973
r = s_update_ref(msg, ref, transaction, 0);
960-
format_display(display, r ? '!' : '*', what,
974+
format_display(display_state, display, r ? '!' : '*', what,
961975
r ? _("unable to update local ref") : NULL,
962976
remote, pretty_ref, summary_width);
963977
return r;
@@ -979,7 +993,7 @@ static int update_local_ref(struct ref *ref,
979993
strbuf_addstr(&quickref, "..");
980994
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
981995
r = s_update_ref("fast-forward", ref, transaction, 1);
982-
format_display(display, r ? '!' : ' ', quickref.buf,
996+
format_display(display_state, display, r ? '!' : ' ', quickref.buf,
983997
r ? _("unable to update local ref") : NULL,
984998
remote, pretty_ref, summary_width);
985999
strbuf_release(&quickref);
@@ -991,13 +1005,13 @@ static int update_local_ref(struct ref *ref,
9911005
strbuf_addstr(&quickref, "...");
9921006
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
9931007
r = s_update_ref("forced-update", ref, transaction, 1);
994-
format_display(display, r ? '!' : '+', quickref.buf,
1008+
format_display(display_state, display, r ? '!' : '+', quickref.buf,
9951009
r ? _("unable to update local ref") : _("forced update"),
9961010
remote, pretty_ref, summary_width);
9971011
strbuf_release(&quickref);
9981012
return r;
9991013
} else {
1000-
format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
1014+
format_display(display_state, display, '!', _("[rejected]"), _("non-fast-forward"),
10011015
remote, pretty_ref, summary_width);
10021016
return 1;
10031017
}
@@ -1108,7 +1122,8 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
11081122
"'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
11091123
"to avoid this check\n");
11101124

1111-
static int store_updated_refs(const char *raw_url, const char *remote_name,
1125+
static int store_updated_refs(struct display_state *display_state,
1126+
const char *raw_url, const char *remote_name,
11121127
int connectivity_checked,
11131128
struct ref_transaction *transaction, struct ref *ref_map,
11141129
struct fetch_head *fetch_head)
@@ -1139,8 +1154,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
11391154
}
11401155
}
11411156

1142-
prepare_format_display(ref_map);
1143-
11441157
/*
11451158
* We do a pass for each fetch_head_status type in their enum order, so
11461159
* merged entries are written before not-for-merge. That lets readers
@@ -1240,7 +1253,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
12401253

12411254
strbuf_reset(&note);
12421255
if (ref) {
1243-
rc |= update_local_ref(ref, transaction, what,
1256+
rc |= update_local_ref(ref, transaction, display_state, what,
12441257
rm, &note, summary_width);
12451258
free(ref);
12461259
} else if (write_fetch_head || dry_run) {
@@ -1249,7 +1262,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
12491262
* would be written to FETCH_HEAD, if --dry-run
12501263
* is set).
12511264
*/
1252-
format_display(&note, '*',
1265+
format_display(display_state, &note, '*',
12531266
*kind ? kind : "branch", NULL,
12541267
*what ? what : "HEAD",
12551268
"FETCH_HEAD", summary_width);
@@ -1328,7 +1341,8 @@ static int check_exist_and_connected(struct ref *ref_map)
13281341
return check_connected(iterate_ref_map, &rm, &opt);
13291342
}
13301343

1331-
static int fetch_and_consume_refs(struct transport *transport,
1344+
static int fetch_and_consume_refs(struct display_state *display_state,
1345+
struct transport *transport,
13321346
struct ref_transaction *transaction,
13331347
struct ref *ref_map,
13341348
struct fetch_head *fetch_head)
@@ -1352,7 +1366,7 @@ static int fetch_and_consume_refs(struct transport *transport,
13521366
}
13531367

13541368
trace2_region_enter("fetch", "consume_refs", the_repository);
1355-
ret = store_updated_refs(transport->url, transport->remote->name,
1369+
ret = store_updated_refs(display_state, transport->url, transport->remote->name,
13561370
connectivity_checked, transaction, ref_map,
13571371
fetch_head);
13581372
trace2_region_leave("fetch", "consume_refs", the_repository);
@@ -1362,7 +1376,8 @@ static int fetch_and_consume_refs(struct transport *transport,
13621376
return ret;
13631377
}
13641378

1365-
static int prune_refs(struct refspec *rs,
1379+
static int prune_refs(struct display_state *display_state,
1380+
struct refspec *rs,
13661381
struct ref_transaction *transaction,
13671382
struct ref *ref_map,
13681383
const char *raw_url)
@@ -1416,7 +1431,7 @@ static int prune_refs(struct refspec *rs,
14161431
fprintf(stderr, _("From %.*s\n"), url_len, url);
14171432
shown_url = 1;
14181433
}
1419-
format_display(&sb, '-', _("[deleted]"), NULL,
1434+
format_display(display_state, &sb, '-', _("[deleted]"), NULL,
14201435
_("(none)"), prettify_refname(ref->name),
14211436
summary_width);
14221437
fprintf(stderr, " %s\n",sb.buf);
@@ -1542,7 +1557,8 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
15421557
return transport;
15431558
}
15441559

1545-
static int backfill_tags(struct transport *transport,
1560+
static int backfill_tags(struct display_state *display_state,
1561+
struct transport *transport,
15461562
struct ref_transaction *transaction,
15471563
struct ref *ref_map,
15481564
struct fetch_head *fetch_head)
@@ -1566,7 +1582,7 @@ static int backfill_tags(struct transport *transport,
15661582
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
15671583
transport_set_option(transport, TRANS_OPT_DEPTH, "0");
15681584
transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1569-
retcode = fetch_and_consume_refs(transport, transaction, ref_map, fetch_head);
1585+
retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map, fetch_head);
15701586

15711587
if (gsecondary) {
15721588
transport_disconnect(gsecondary);
@@ -1581,6 +1597,7 @@ static int do_fetch(struct transport *transport,
15811597
{
15821598
struct ref_transaction *transaction = NULL;
15831599
struct ref *ref_map = NULL;
1600+
struct display_state display_state;
15841601
int autotags = (transport->remote->fetch_tags == 1);
15851602
int retcode = 0;
15861603
const struct ref *remote_refs;
@@ -1662,6 +1679,8 @@ static int do_fetch(struct transport *transport,
16621679
if (retcode)
16631680
goto cleanup;
16641681

1682+
display_state_init(&display_state, ref_map);
1683+
16651684
if (atomic_fetch) {
16661685
transaction = ref_transaction_begin(&err);
16671686
if (!transaction) {
@@ -1679,17 +1698,17 @@ static int do_fetch(struct transport *transport,
16791698
* don't care whether --tags was specified.
16801699
*/
16811700
if (rs->nr) {
1682-
retcode = prune_refs(rs, transaction, ref_map, transport->url);
1701+
retcode = prune_refs(&display_state, rs, transaction, ref_map, transport->url);
16831702
} else {
1684-
retcode = prune_refs(&transport->remote->fetch,
1703+
retcode = prune_refs(&display_state, &transport->remote->fetch,
16851704
transaction, ref_map,
16861705
transport->url);
16871706
}
16881707
if (retcode != 0)
16891708
retcode = 1;
16901709
}
16911710

1692-
if (fetch_and_consume_refs(transport, transaction, ref_map, &fetch_head)) {
1711+
if (fetch_and_consume_refs(&display_state, transport, transaction, ref_map, &fetch_head)) {
16931712
retcode = 1;
16941713
goto cleanup;
16951714
}
@@ -1711,7 +1730,7 @@ static int do_fetch(struct transport *transport,
17111730
* when `--atomic` is passed: in that case we'll abort
17121731
* the transaction and don't commit anything.
17131732
*/
1714-
if (backfill_tags(transport, transaction, tags_ref_map,
1733+
if (backfill_tags(&display_state, transport, transaction, tags_ref_map,
17151734
&fetch_head))
17161735
retcode = 1;
17171736
}

0 commit comments

Comments
 (0)