Skip to content

Commit 2983cec

Browse files
pks-tgitster
authored andcommitted
fetch: control lifecycle of FETCH_HEAD in a single place
There are two different locations where we're appending to FETCH_HEAD: first when storing updated references, and second when backfilling tags. Both times we open the file, append to it and then commit it into place, which is essentially duplicate work. Improve the lifecycle of updating FETCH_HEAD by opening and committing it once in `do_fetch()`, where we pass the structure down to the code which wants to append to it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent efbade0 commit 2983cec

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

builtin/fetch.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,8 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
10841084

10851085
static int store_updated_refs(const char *raw_url, const char *remote_name,
10861086
int connectivity_checked, struct ref *ref_map,
1087-
struct worktree **worktrees)
1087+
struct fetch_head *fetch_head, struct worktree **worktrees)
10881088
{
1089-
struct fetch_head fetch_head;
10901089
int url_len, i, rc = 0;
10911090
struct strbuf note = STRBUF_INIT, err = STRBUF_INIT;
10921091
struct ref_transaction *transaction = NULL;
@@ -1096,10 +1095,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
10961095
int want_status;
10971096
int summary_width = transport_summary_width(ref_map);
10981097

1099-
rc = open_fetch_head(&fetch_head);
1100-
if (rc)
1101-
return -1;
1102-
11031098
if (raw_url)
11041099
url = transport_anonymize_url(raw_url);
11051100
else
@@ -1206,7 +1201,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
12061201
strbuf_addf(&note, "'%s' of ", what);
12071202
}
12081203

1209-
append_fetch_head(&fetch_head, &rm->old_oid,
1204+
append_fetch_head(fetch_head, &rm->old_oid,
12101205
rm->fetch_head_status,
12111206
note.buf, url, url_len);
12121207

@@ -1246,9 +1241,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
12461241
}
12471242
}
12481243

1249-
if (!rc)
1250-
commit_fetch_head(&fetch_head);
1251-
12521244
if (rc & STORE_REF_ERROR_DF_CONFLICT)
12531245
error(_("some local refs could not be updated; try running\n"
12541246
" 'git remote prune %s' to remove any old, conflicting "
@@ -1268,7 +1260,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
12681260
strbuf_release(&err);
12691261
ref_transaction_free(transaction);
12701262
free(url);
1271-
close_fetch_head(&fetch_head);
12721263
return rc;
12731264
}
12741265

@@ -1309,6 +1300,7 @@ static int check_exist_and_connected(struct ref *ref_map)
13091300

13101301
static int fetch_and_consume_refs(struct transport *transport,
13111302
struct ref *ref_map,
1303+
struct fetch_head *fetch_head,
13121304
struct worktree **worktrees)
13131305
{
13141306
int connectivity_checked = 1;
@@ -1331,7 +1323,7 @@ static int fetch_and_consume_refs(struct transport *transport,
13311323

13321324
trace2_region_enter("fetch", "consume_refs", the_repository);
13331325
ret = store_updated_refs(transport->url, transport->remote->name,
1334-
connectivity_checked, ref_map, worktrees);
1326+
connectivity_checked, ref_map, fetch_head, worktrees);
13351327
trace2_region_leave("fetch", "consume_refs", the_repository);
13361328

13371329
out:
@@ -1503,7 +1495,9 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
15031495
return transport;
15041496
}
15051497

1506-
static void backfill_tags(struct transport *transport, struct ref *ref_map,
1498+
static void backfill_tags(struct transport *transport,
1499+
struct ref *ref_map,
1500+
struct fetch_head *fetch_head,
15071501
struct worktree **worktrees)
15081502
{
15091503
int cannot_reuse;
@@ -1525,7 +1519,7 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map,
15251519
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
15261520
transport_set_option(transport, TRANS_OPT_DEPTH, "0");
15271521
transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1528-
fetch_and_consume_refs(transport, ref_map, worktrees);
1522+
fetch_and_consume_refs(transport, ref_map, fetch_head, worktrees);
15291523

15301524
if (gsecondary) {
15311525
transport_disconnect(gsecondary);
@@ -1544,6 +1538,7 @@ static int do_fetch(struct transport *transport,
15441538
TRANSPORT_LS_REFS_OPTIONS_INIT;
15451539
int must_list_refs = 1;
15461540
struct worktree **worktrees = get_worktrees();
1541+
struct fetch_head fetch_head = { 0 };
15471542

15481543
if (tags == TAGS_DEFAULT) {
15491544
if (transport->remote->fetch_tags == 2)
@@ -1601,6 +1596,10 @@ static int do_fetch(struct transport *transport,
16011596
if (!update_head_ok)
16021597
check_not_current_branch(ref_map, worktrees);
16031598

1599+
retcode = open_fetch_head(&fetch_head);
1600+
if (retcode)
1601+
goto cleanup;
1602+
16041603
if (tags == TAGS_DEFAULT && autotags)
16051604
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
16061605
if (prune) {
@@ -1619,7 +1618,8 @@ static int do_fetch(struct transport *transport,
16191618
if (retcode != 0)
16201619
retcode = 1;
16211620
}
1622-
if (fetch_and_consume_refs(transport, ref_map, worktrees)) {
1621+
1622+
if (fetch_and_consume_refs(transport, ref_map, &fetch_head, worktrees)) {
16231623
retcode = 1;
16241624
goto cleanup;
16251625
}
@@ -1633,11 +1633,13 @@ static int do_fetch(struct transport *transport,
16331633

16341634
find_non_local_tags(remote_refs, &tags_ref_map, &tail);
16351635
if (tags_ref_map)
1636-
backfill_tags(transport, tags_ref_map, worktrees);
1636+
backfill_tags(transport, tags_ref_map, &fetch_head, worktrees);
16371637

16381638
free_refs(tags_ref_map);
16391639
}
16401640

1641+
commit_fetch_head(&fetch_head);
1642+
16411643
if (set_upstream) {
16421644
struct branch *branch = branch_get("HEAD");
16431645
struct ref *rm;
@@ -1693,6 +1695,7 @@ static int do_fetch(struct transport *transport,
16931695
}
16941696

16951697
cleanup:
1698+
close_fetch_head(&fetch_head);
16961699
free_refs(ref_map);
16971700
free_worktrees(worktrees);
16981701
return retcode;

0 commit comments

Comments
 (0)