Skip to content

Commit efbade0

Browse files
pks-tgitster
authored andcommitted
fetch: backfill tags before setting upstream
The fetch code flow is a bit hard to understand right now: 1. We optionally prune all references which have vanished on the remote side. 2. We fetch and update all other references locally. 3. We update the upstream branch in the gitconfig. 4. We backfill tags pointing into the history we have just fetched. It is quite confusing that we fetch objects and update references in both (2) and (4), which is further stressed by the point that we use a `skip` goto label to jump from (3) to (4) in case we fail to update the gitconfig as expected. Reorder the code to first update all local references, and only after we have done so update the upstream branch information. This improves the code flow and furthermore makes it easier to refactor the way we update references together. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2a0cafd commit efbade0

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

builtin/fetch.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map,
15361536
static int do_fetch(struct transport *transport,
15371537
struct refspec *rs)
15381538
{
1539-
struct ref *ref_map;
1539+
struct ref *ref_map = NULL;
15401540
int autotags = (transport->remote->fetch_tags == 1);
15411541
int retcode = 0;
15421542
const struct ref *remote_refs;
@@ -1620,11 +1620,24 @@ static int do_fetch(struct transport *transport,
16201620
retcode = 1;
16211621
}
16221622
if (fetch_and_consume_refs(transport, ref_map, worktrees)) {
1623-
free_refs(ref_map);
16241623
retcode = 1;
16251624
goto cleanup;
16261625
}
16271626

1627+
/*
1628+
* If neither --no-tags nor --tags was specified, do automated tag
1629+
* following.
1630+
*/
1631+
if (tags == TAGS_DEFAULT && autotags) {
1632+
struct ref *tags_ref_map = NULL, **tail = &tags_ref_map;
1633+
1634+
find_non_local_tags(remote_refs, &tags_ref_map, &tail);
1635+
if (tags_ref_map)
1636+
backfill_tags(transport, tags_ref_map, worktrees);
1637+
1638+
free_refs(tags_ref_map);
1639+
}
1640+
16281641
if (set_upstream) {
16291642
struct branch *branch = branch_get("HEAD");
16301643
struct ref *rm;
@@ -1644,7 +1657,7 @@ static int do_fetch(struct transport *transport,
16441657
if (!rm->peer_ref) {
16451658
if (source_ref) {
16461659
warning(_("multiple branches detected, incompatible with --set-upstream"));
1647-
goto skip;
1660+
goto cleanup;
16481661
} else {
16491662
source_ref = rm;
16501663
}
@@ -1658,7 +1671,7 @@ static int do_fetch(struct transport *transport,
16581671
warning(_("could not set upstream of HEAD to '%s' from '%s' when "
16591672
"it does not point to any branch."),
16601673
shortname, transport->remote->name);
1661-
goto skip;
1674+
goto cleanup;
16621675
}
16631676

16641677
if (!strcmp(source_ref->name, "HEAD") ||
@@ -1678,21 +1691,9 @@ static int do_fetch(struct transport *transport,
16781691
"you need to specify exactly one branch with the --set-upstream option"));
16791692
}
16801693
}
1681-
skip:
1682-
free_refs(ref_map);
1683-
1684-
/* if neither --no-tags nor --tags was specified, do automated tag
1685-
* following ... */
1686-
if (tags == TAGS_DEFAULT && autotags) {
1687-
struct ref **tail = &ref_map;
1688-
ref_map = NULL;
1689-
find_non_local_tags(remote_refs, &ref_map, &tail);
1690-
if (ref_map)
1691-
backfill_tags(transport, ref_map, worktrees);
1692-
free_refs(ref_map);
1693-
}
16941694

16951695
cleanup:
1696+
free_refs(ref_map);
16961697
free_worktrees(worktrees);
16971698
return retcode;
16981699
}

0 commit comments

Comments
 (0)