Skip to content

Commit c4ef5ed

Browse files
pks-tgitster
authored andcommitted
fetch: centralize logic to print remote URL
When fetching from a remote, we not only print the actual references that have changed, but will also print the URL from which we have fetched them to standard output. The logic to handle this is duplicated across two different callsites with some non-trivial logic to compute the anonymized URL. Furthermore, we're using global state to track whether we have already shown the URL to the user or not. Refactor the code by moving it into `format_display()`. Like this, we can convert the global variable into a member of `display_state`. And second, we can deduplicate the logic to compute the anonymized URL. This also works as expected when fetching from multiple remotes, for example via a group of remotes, as we do this by forking a standalone git-fetch(1) process per remote that is to be fetched. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 331b7d2 commit c4ef5ed

File tree

1 file changed

+44
-55
lines changed

1 file changed

+44
-55
lines changed

builtin/fetch.c

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ enum {
5050
struct display_state {
5151
int refcol_width;
5252
int compact_format;
53+
54+
char *url;
55+
int url_len, shown_url;
5356
};
5457

5558
static int fetch_prune_config = -1; /* unspecified */
@@ -84,7 +87,6 @@ static const char *submodule_prefix = "";
8487
static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
8588
static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
8689
static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
87-
static int shown_url = 0;
8890
static struct refspec refmap = REFSPEC_INIT_FETCH;
8991
static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
9092
static struct string_list server_options = STRING_LIST_INIT_DUP;
@@ -776,13 +778,27 @@ static int refcol_width(const struct ref *ref, int compact_format)
776778
return rlen;
777779
}
778780

779-
static void display_state_init(struct display_state *display_state, struct ref *ref_map)
781+
static void display_state_init(struct display_state *display_state, struct ref *ref_map,
782+
const char *raw_url)
780783
{
781784
struct ref *rm;
782785
const char *format = "full";
786+
int i;
783787

784788
memset(display_state, 0, sizeof(*display_state));
785789

790+
if (raw_url)
791+
display_state->url = transport_anonymize_url(raw_url);
792+
else
793+
display_state->url = xstrdup("foreign");
794+
795+
display_state->url_len = strlen(display_state->url);
796+
for (i = display_state->url_len - 1; display_state->url[i] == '/' && 0 <= i; i--)
797+
;
798+
display_state->url_len = i + 1;
799+
if (4 < i && !strncmp(".git", display_state->url + i - 3, 4))
800+
display_state->url_len = i - 3;
801+
786802
if (verbosity < 0)
787803
return;
788804

@@ -816,6 +832,11 @@ static void display_state_init(struct display_state *display_state, struct ref *
816832
}
817833
}
818834

835+
static void display_state_release(struct display_state *display_state)
836+
{
837+
free(display_state->url);
838+
}
839+
819840
static void print_remote_to_local(struct display_state *display_state,
820841
struct strbuf *display_buffer,
821842
const char *remote, const char *local)
@@ -883,6 +904,12 @@ static void format_display(struct display_state *display_state,
883904
if (verbosity < 0)
884905
return;
885906

907+
if (!display_state->shown_url) {
908+
strbuf_addf(display_buffer, _("From %.*s\n"),
909+
display_state->url_len, display_state->url);
910+
display_state->shown_url = 1;
911+
}
912+
886913
width = (summary_width + strlen(summary) - gettext_width(summary));
887914

888915
strbuf_addf(display_buffer, " %c %-*s ", code, width, summary);
@@ -1122,33 +1149,28 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
11221149
"to avoid this check\n");
11231150

11241151
static int store_updated_refs(struct display_state *display_state,
1125-
const char *raw_url, const char *remote_name,
1152+
const char *remote_name,
11261153
int connectivity_checked,
11271154
struct ref_transaction *transaction, struct ref *ref_map,
11281155
struct fetch_head *fetch_head)
11291156
{
1130-
int url_len, i, rc = 0;
1157+
int rc = 0;
11311158
struct strbuf note = STRBUF_INIT;
11321159
const char *what, *kind;
11331160
struct ref *rm;
1134-
char *url;
11351161
int want_status;
11361162
int summary_width = 0;
11371163

11381164
if (verbosity >= 0)
11391165
summary_width = transport_summary_width(ref_map);
11401166

1141-
if (raw_url)
1142-
url = transport_anonymize_url(raw_url);
1143-
else
1144-
url = xstrdup("foreign");
1145-
11461167
if (!connectivity_checked) {
11471168
struct check_connected_options opt = CHECK_CONNECTED_INIT;
11481169

11491170
rm = ref_map;
11501171
if (check_connected(iterate_ref_map, &rm, &opt)) {
1151-
rc = error(_("%s did not send all necessary objects\n"), url);
1172+
rc = error(_("%s did not send all necessary objects\n"),
1173+
display_state->url);
11521174
goto abort;
11531175
}
11541176
}
@@ -1232,13 +1254,6 @@ static int store_updated_refs(struct display_state *display_state,
12321254
what = rm->name;
12331255
}
12341256

1235-
url_len = strlen(url);
1236-
for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1237-
;
1238-
url_len = i + 1;
1239-
if (4 < i && !strncmp(".git", url + i - 3, 4))
1240-
url_len = i - 3;
1241-
12421257
strbuf_reset(&note);
12431258
if (*what) {
12441259
if (*kind)
@@ -1248,7 +1263,8 @@ static int store_updated_refs(struct display_state *display_state,
12481263

12491264
append_fetch_head(fetch_head, &rm->old_oid,
12501265
rm->fetch_head_status,
1251-
note.buf, url, url_len);
1266+
note.buf, display_state->url,
1267+
display_state->url_len);
12521268

12531269
strbuf_reset(&note);
12541270
if (ref) {
@@ -1266,14 +1282,8 @@ static int store_updated_refs(struct display_state *display_state,
12661282
*what ? what : "HEAD",
12671283
"FETCH_HEAD", summary_width);
12681284
}
1269-
if (note.len) {
1270-
if (!shown_url) {
1271-
fprintf(stderr, _("From %.*s\n"),
1272-
url_len, url);
1273-
shown_url = 1;
1274-
}
1285+
if (note.len)
12751286
fputs(note.buf, stderr);
1276-
}
12771287
}
12781288
}
12791289

@@ -1293,7 +1303,6 @@ static int store_updated_refs(struct display_state *display_state,
12931303

12941304
abort:
12951305
strbuf_release(&note);
1296-
free(url);
12971306
return rc;
12981307
}
12991308

@@ -1365,7 +1374,7 @@ static int fetch_and_consume_refs(struct display_state *display_state,
13651374
}
13661375

13671376
trace2_region_enter("fetch", "consume_refs", the_repository);
1368-
ret = store_updated_refs(display_state, transport->url, transport->remote->name,
1377+
ret = store_updated_refs(display_state, transport->remote->name,
13691378
connectivity_checked, transaction, ref_map,
13701379
fetch_head);
13711380
trace2_region_leave("fetch", "consume_refs", the_repository);
@@ -1378,30 +1387,15 @@ static int fetch_and_consume_refs(struct display_state *display_state,
13781387
static int prune_refs(struct display_state *display_state,
13791388
struct refspec *rs,
13801389
struct ref_transaction *transaction,
1381-
struct ref *ref_map,
1382-
const char *raw_url)
1390+
struct ref *ref_map)
13831391
{
1384-
int url_len, i, result = 0;
1392+
int result = 0;
13851393
struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
13861394
struct strbuf err = STRBUF_INIT;
1387-
char *url;
13881395
const char *dangling_msg = dry_run
13891396
? _(" (%s will become dangling)")
13901397
: _(" (%s has become dangling)");
13911398

1392-
if (raw_url)
1393-
url = transport_anonymize_url(raw_url);
1394-
else
1395-
url = xstrdup("foreign");
1396-
1397-
url_len = strlen(url);
1398-
for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1399-
;
1400-
1401-
url_len = i + 1;
1402-
if (4 < i && !strncmp(".git", url + i - 3, 4))
1403-
url_len = i - 3;
1404-
14051399
if (!dry_run) {
14061400
if (transaction) {
14071401
for (ref = stale_refs; ref; ref = ref->next) {
@@ -1426,10 +1420,6 @@ static int prune_refs(struct display_state *display_state,
14261420

14271421
for (ref = stale_refs; ref; ref = ref->next) {
14281422
struct strbuf sb = STRBUF_INIT;
1429-
if (!shown_url) {
1430-
fprintf(stderr, _("From %.*s\n"), url_len, url);
1431-
shown_url = 1;
1432-
}
14331423
format_display(display_state, &sb, '-', _("[deleted]"), NULL,
14341424
_("(none)"), ref->name,
14351425
summary_width);
@@ -1441,7 +1431,6 @@ static int prune_refs(struct display_state *display_state,
14411431

14421432
cleanup:
14431433
strbuf_release(&err);
1444-
free(url);
14451434
free_refs(stale_refs);
14461435
return result;
14471436
}
@@ -1596,7 +1585,7 @@ static int do_fetch(struct transport *transport,
15961585
{
15971586
struct ref_transaction *transaction = NULL;
15981587
struct ref *ref_map = NULL;
1599-
struct display_state display_state;
1588+
struct display_state display_state = { 0 };
16001589
int autotags = (transport->remote->fetch_tags == 1);
16011590
int retcode = 0;
16021591
const struct ref *remote_refs;
@@ -1678,7 +1667,7 @@ static int do_fetch(struct transport *transport,
16781667
if (retcode)
16791668
goto cleanup;
16801669

1681-
display_state_init(&display_state, ref_map);
1670+
display_state_init(&display_state, ref_map, transport->url);
16821671

16831672
if (atomic_fetch) {
16841673
transaction = ref_transaction_begin(&err);
@@ -1697,11 +1686,10 @@ static int do_fetch(struct transport *transport,
16971686
* don't care whether --tags was specified.
16981687
*/
16991688
if (rs->nr) {
1700-
retcode = prune_refs(&display_state, rs, transaction, ref_map, transport->url);
1689+
retcode = prune_refs(&display_state, rs, transaction, ref_map);
17011690
} else {
17021691
retcode = prune_refs(&display_state, &transport->remote->fetch,
1703-
transaction, ref_map,
1704-
transport->url);
1692+
transaction, ref_map);
17051693
}
17061694
if (retcode != 0)
17071695
retcode = 1;
@@ -1812,6 +1800,7 @@ static int do_fetch(struct transport *transport,
18121800
error("%s", err.buf);
18131801
}
18141802

1803+
display_state_release(&display_state);
18151804
close_fetch_head(&fetch_head);
18161805
strbuf_release(&err);
18171806
free_refs(ref_map);

0 commit comments

Comments
 (0)