Skip to content

Commit f568b12

Browse files
steadmongitster
authored andcommitted
Fix git fetch --tags in repo with no configured remote
In 3f763dd (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching. However, this broke the logic for the `--tags` flag. Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags. Fix this by adding HEAD to the ref_prefixes list prior to handling the `--tags` flag, and removing the now obsolete check whether ref_prefixes is empty or not. Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e02082c commit f568b12

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

builtin/fetch.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,15 +1705,14 @@ static int do_fetch(struct transport *transport,
17051705
}
17061706
}
17071707

1708+
strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1709+
17081710
if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
17091711
must_list_refs = 1;
1710-
if (transport_ls_refs_options.ref_prefixes.nr)
1711-
strvec_push(&transport_ls_refs_options.ref_prefixes,
1712-
"refs/tags/");
1712+
strvec_push(&transport_ls_refs_options.ref_prefixes,
1713+
"refs/tags/");
17131714
}
17141715

1715-
strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1716-
17171716
if (must_list_refs) {
17181717
trace2_region_enter("fetch", "remote_refs", the_repository);
17191718
remote_refs = transport_get_remote_refs(transport,

t/t5510-fetch.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
189189
git rev-parse sometag
190190
'
191191

192+
test_expect_success 'fetch --tags gets tags even without a configured remote' '
193+
REMOTE="$(pwd)/test_tag_1" &&
194+
git init test_tag_1 &&
195+
(
196+
cd test_tag_1 &&
197+
test_commit foo
198+
) &&
199+
git init test_tag_2 &&
200+
(
201+
cd test_tag_2 &&
202+
git fetch --tags "file://$REMOTE" &&
203+
echo "foo" >expect &&
204+
git tag >actual &&
205+
test_cmp expect actual
206+
)
207+
'
208+
192209
test_expect_success REFFILES 'fetch --prune fails to delete branches' '
193210
cd "$D" &&
194211
git clone . prune-fail &&

0 commit comments

Comments
 (0)