Skip to content

Commit c5a84e9

Browse files
mhaggergitster
authored andcommitted
fetch --tags: fetch tags *in addition to* other stuff
Previously, fetch's "--tags" option was considered equivalent to specifying the refspec "refs/tags/*:refs/tags/*" on the command line; in particular, it caused the remote.<name>.refspec configuration to be ignored. But it is not very useful to fetch tags without also fetching other references, whereas it *is* quite useful to be able to fetch tags *in addition to* other references. So change the semantics of this option to do the latter. If a user wants to fetch *only* tags, then it is still possible to specifying an explicit refspec: git fetch <remote> 'refs/tags/*:refs/tags/*' Please note that the documentation prior to 1.8.0.3 was ambiguous about this aspect of "fetch --tags" behavior. Commit f0cb2f1 2012-12-14 fetch --tags: clarify documentation made the documentation match the old behavior. This commit changes the documentation to match the new behavior. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0281c93 commit c5a84e9

File tree

7 files changed

+78
-38
lines changed

7 files changed

+78
-38
lines changed

Documentation/fetch-options.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ endif::git-pull[]
6161
ifndef::git-pull[]
6262
-t::
6363
--tags::
64-
This is a short-hand for giving `refs/tags/*:refs/tags/*`
65-
refspec from the command line, to ask all tags to be fetched
66-
and stored locally. Because this acts as an explicit
67-
refspec, the default refspecs (configured with the
68-
remote.$name.fetch variable) are overridden and not used.
64+
Request that all tags be fetched from the remote in addition
65+
to whatever else is being fetched. Its effect is similar to
66+
that of the refspec `refs/tags/*:refs/tags/*`.
6967

7068
--recurse-submodules[=yes|on-demand|no]::
7169
This option controls if and under what conditions new commits of

builtin/fetch.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ static struct ref *get_ref_map(struct transport *transport,
269269
struct ref *ref_map = NULL;
270270
struct ref **tail = &ref_map;
271271

272-
const struct ref *remote_refs = transport_get_remote_refs(transport);
272+
/* opportunistically-updated references: */
273+
struct ref *orefs = NULL, **oref_tail = &orefs;
273274

274-
if (refspec_count || tags == TAGS_SET) {
275-
/* opportunistically-updated references: */
276-
struct ref *orefs = NULL, **oref_tail = &orefs;
275+
const struct ref *remote_refs = transport_get_remote_refs(transport);
277276

277+
if (refspec_count) {
278278
for (i = 0; i < refspec_count; i++) {
279279
get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
280280
if (refspecs[i].dst && refspecs[i].dst[0])
@@ -310,12 +310,6 @@ static struct ref *get_ref_map(struct transport *transport,
310310

311311
if (tags == TAGS_SET)
312312
get_fetch_map(remote_refs, tag_refspec, &tail, 0);
313-
314-
*tail = orefs;
315-
for (rm = orefs; rm; rm = rm->next) {
316-
rm->fetch_head_status = FETCH_HEAD_IGNORE;
317-
tail = &rm->next;
318-
}
319313
} else {
320314
/* Use the defaults */
321315
struct remote *remote = transport->remote;
@@ -353,9 +347,19 @@ static struct ref *get_ref_map(struct transport *transport,
353347
}
354348
}
355349

356-
if (tags == TAGS_DEFAULT && *autotags)
350+
if (tags == TAGS_SET)
351+
/* also fetch all tags */
352+
get_fetch_map(remote_refs, tag_refspec, &tail, 0);
353+
else if (tags == TAGS_DEFAULT && *autotags)
357354
find_non_local_tags(transport, &ref_map, &tail);
358355

356+
/* Now append any refs to be updated opportunistically: */
357+
*tail = orefs;
358+
for (rm = orefs; rm; rm = rm->next) {
359+
rm->fetch_head_status = FETCH_HEAD_IGNORE;
360+
tail = &rm->next;
361+
}
362+
359363
ref_remove_duplicates(ref_map);
360364

361365
return ref_map;
@@ -846,31 +850,38 @@ static int do_fetch(struct transport *transport,
846850
goto cleanup;
847851
}
848852
if (prune) {
849-
/*
850-
* If --tags was specified, pretend that the user gave us
851-
* the canonical tags refspec
852-
*/
853+
struct refspec *prune_refspecs;
854+
int prune_refspec_count;
855+
856+
if (ref_count) {
857+
prune_refspecs = refs;
858+
prune_refspec_count = ref_count;
859+
} else {
860+
prune_refspecs = transport->remote->fetch;
861+
prune_refspec_count = transport->remote->fetch_refspec_nr;
862+
}
863+
853864
if (tags == TAGS_SET) {
865+
/*
866+
* --tags was specified. Pretend that the user also
867+
* gave us the canonical tags refspec
868+
*/
854869
const char *tags_str = "refs/tags/*:refs/tags/*";
855870
struct refspec *tags_refspec, *refspec;
856871

857872
/* Copy the refspec and add the tags to it */
858-
refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
873+
refspec = xcalloc(prune_refspec_count + 1, sizeof(*refspec));
859874
tags_refspec = parse_fetch_refspec(1, &tags_str);
860-
memcpy(refspec, refs, ref_count * sizeof(struct refspec));
861-
memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
862-
ref_count++;
875+
memcpy(refspec, prune_refspecs, prune_refspec_count * sizeof(*refspec));
876+
memcpy(&refspec[prune_refspec_count], tags_refspec, sizeof(*refspec));
863877

864-
prune_refs(refspec, ref_count, ref_map);
878+
prune_refs(refspec, prune_refspec_count + 1, ref_map);
865879

866-
ref_count--;
867880
/* The rest of the strings belong to fetch_one */
868881
free_refspec(1, tags_refspec);
869882
free(refspec);
870-
} else if (ref_count) {
871-
prune_refs(refs, ref_count, ref_map);
872883
} else {
873-
prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
884+
prune_refs(prune_refspecs, prune_refspec_count, ref_map);
874885
}
875886
}
876887
free_refs(ref_map);

git-pull.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ error_on_no_merge_candidates () {
172172
do
173173
case "$opt" in
174174
-t|--t|--ta|--tag|--tags)
175-
echo "Fetching tags only, you probably meant:"
175+
echo "It doesn't make sense to pull all tags; you probably meant:"
176176
echo " git fetch --tags"
177177
exit 1
178178
esac

t/t5510-fetch.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
113113
git rev-parse origin/master
114114
'
115115

116-
test_expect_success 'fetch --prune --tags does not delete the remote-tracking branches' '
116+
test_expect_success 'fetch --prune --tags prunes tags and branches' '
117117
cd "$D" &&
118118
git clone . prune-tags &&
119119
cd prune-tags &&
@@ -124,18 +124,34 @@ test_expect_success 'fetch --prune --tags does not delete the remote-tracking br
124124
125125
git fetch --prune --tags origin &&
126126
git rev-parse origin/master &&
127-
git rev-parse origin/fake-remote &&
127+
test_must_fail git rev-parse origin/fake-remote &&
128128
test_must_fail git rev-parse sometag
129129
'
130130

131131
test_expect_success 'fetch --prune --tags with branch does not delete other remote-tracking branches' '
132132
cd "$D" &&
133133
git clone . prune-tags-branch &&
134134
cd prune-tags-branch &&
135+
git tag sometag master &&
135136
git update-ref refs/remotes/origin/extrabranch master &&
136137
137138
git fetch --prune --tags origin master &&
138-
git rev-parse origin/extrabranch
139+
git rev-parse origin/extrabranch &&
140+
test_must_fail git rev-parse sometag
141+
'
142+
143+
test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
144+
cd "$D" &&
145+
git clone . prune-tags-refspec &&
146+
cd prune-tags-refspec &&
147+
git tag sometag master &&
148+
git update-ref refs/remotes/origin/foo/otherbranch master &&
149+
git update-ref refs/remotes/origin/extrabranch master &&
150+
151+
git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
152+
test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
153+
git rev-parse origin/extrabranch &&
154+
test_must_fail git rev-parse sometag
139155
'
140156

141157
test_expect_success 'fetch tags when there is no tags' '

t/t5515/fetch.br-unconfig_--tags_.._.git

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# br-unconfig --tags ../.git
2+
0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
23
6c9dec2b923228c9ff994c6cfe4ae16c12408dc5 not-for-merge tag 'tag-master' of ../
34
8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
45
22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../

t/t5515/fetch.master_--tags_.._.git

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# master --tags ../.git
2+
0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
23
6c9dec2b923228c9ff994c6cfe4ae16c12408dc5 not-for-merge tag 'tag-master' of ../
34
8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
45
22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../

t/t5525-fetch-tagopt.sh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ setup_clone () {
88
git clone --mirror . $1 &&
99
git remote add remote_$1 $1 &&
1010
(cd $1 &&
11-
git tag tag_$1)
11+
git tag tag_$1 &&
12+
git branch branch_$1)
1213
}
1314

1415
test_expect_success setup '
@@ -21,21 +22,33 @@ test_expect_success setup '
2122

2223
test_expect_success "fetch with tagopt=--no-tags does not get tag" '
2324
git fetch remote_one &&
24-
test_must_fail git show-ref tag_one
25+
test_must_fail git show-ref tag_one &&
26+
git show-ref remote_one/branch_one
2527
'
2628

2729
test_expect_success "fetch --tags with tagopt=--no-tags gets tag" '
30+
(
31+
cd one &&
32+
git branch second_branch_one
33+
) &&
2834
git fetch --tags remote_one &&
29-
git show-ref tag_one
35+
git show-ref tag_one &&
36+
git show-ref remote_one/second_branch_one
3037
'
3138

3239
test_expect_success "fetch --no-tags with tagopt=--tags does not get tag" '
3340
git fetch --no-tags remote_two &&
34-
test_must_fail git show-ref tag_two
41+
test_must_fail git show-ref tag_two &&
42+
git show-ref remote_two/branch_two
3543
'
3644

3745
test_expect_success "fetch with tagopt=--tags gets tag" '
46+
(
47+
cd two &&
48+
git branch second_branch_two
49+
) &&
3850
git fetch remote_two &&
39-
git show-ref tag_two
51+
git show-ref tag_two &&
52+
git show-ref remote_two/second_branch_two
4053
'
4154
test_done

0 commit comments

Comments
 (0)