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 f0cb2f137c 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>

Michael Haggerty committed Oct 30, 2013 at 06:32 UTC c5a84e92a2fe9e8748e32341c344d7a6c0f52a50
7 files changed +78 -38
Documentation/fetch-options.txt
+3 -5
@@ -61,11 +61,9 @@ endif::git-pull[]
61 ifndef::git-pull[]
62 -t::
63 --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/*`.
67
68 --recurse-submodules[=yes|on-demand|no]::
69 This option controls if and under what conditions new commits of
builtin/fetch.c
+35 -24
@@ -269,12 +269,12 @@ static struct ref *get_ref_map(struct transport *transport,
269 struct ref *ref_map = NULL;
270 struct ref **tail = &ref_map;
271
272 - const struct ref *remote_refs = transport_get_remote_refs(transport);
272 + /* opportunistically-updated references: */
273 + struct ref *orefs = NULL, **oref_tail = &orefs;
274
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);
276
277 + if (refspec_count) {
278 for (i = 0; i < refspec_count; i++) {
279 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
280 if (refspecs[i].dst && refspecs[i].dst[0])
@@ -310,12 +310,6 @@ static struct ref *get_ref_map(struct transport *transport,
310
311 if (tags == TAGS_SET)
312 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 - }
313 } else {
314 /* Use the defaults */
315 struct remote *remote = transport->remote;
@@ -353,9 +347,19 @@ static struct ref *get_ref_map(struct transport *transport,
347 }
348 }
349
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)
354 find_non_local_tags(transport, &ref_map, &tail);
355
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 +
363 ref_remove_duplicates(ref_map);
364
365 return ref_map;
@@ -846,31 +850,38 @@ static int do_fetch(struct transport *transport,
850 goto cleanup;
851 }
852 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 +
864 if (tags == TAGS_SET) {
865 + /*
866 + * --tags was specified. Pretend that the user also
867 + * gave us the canonical tags refspec
868 + */
869 const char *tags_str = "refs/tags/*:refs/tags/*";
870 struct refspec *tags_refspec, *refspec;
871
872 /* 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));
874 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));
877
864 - prune_refs(refspec, ref_count, ref_map);
878 + prune_refs(refspec, prune_refspec_count + 1, ref_map);
879
866 - ref_count--;
880 /* The rest of the strings belong to fetch_one */
881 free_refspec(1, tags_refspec);
882 free(refspec);
870 - } else if (ref_count) {
871 - prune_refs(refs, ref_count, ref_map);
883 } else {
873 - prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
884 + prune_refs(prune_refspecs, prune_refspec_count, ref_map);
885 }
886 }
887 free_refs(ref_map);
git-pull.sh
+1 -1
@@ -172,7 +172,7 @@ error_on_no_merge_candidates () {
172 do
173 case "$opt" in
174 -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:"
176 echo " git fetch --tags"
177 exit 1
178 esac
t/t5510-fetch.sh
+19 -3
@@ -113,7 +113,7 @@ test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
113 git rev-parse origin/master
114 '
115
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' '
117 cd "$D" &&
118 git clone . prune-tags &&
119 cd prune-tags &&
@@ -124,7 +124,7 @@ test_expect_success 'fetch --prune --tags does not delete the remote-tracking br
124
125 git fetch --prune --tags origin &&
126 git rev-parse origin/master &&
127 - git rev-parse origin/fake-remote &&
127 + test_must_fail git rev-parse origin/fake-remote &&
128 test_must_fail git rev-parse sometag
129 '
130
@@ -132,10 +132,26 @@ test_expect_success 'fetch --prune --tags with branch does not delete other remo
132 cd "$D" &&
133 git clone . prune-tags-branch &&
134 cd prune-tags-branch &&
135 + git tag sometag master &&
136 git update-ref refs/remotes/origin/extrabranch master &&
137
138 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
155 '
156
157 test_expect_success 'fetch tags when there is no tags' '
t/t5515/fetch.br-unconfig_--tags_.._.git
+1
@@ -1,4 +1,5 @@
1 # br-unconfig --tags ../.git
2 +0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
3 6c9dec2b923228c9ff994c6cfe4ae16c12408dc5 not-for-merge tag 'tag-master' of ../
4 8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
5 22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
t/t5515/fetch.master_--tags_.._.git
+1
@@ -1,4 +1,5 @@
1 # master --tags ../.git
2 +0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
3 6c9dec2b923228c9ff994c6cfe4ae16c12408dc5 not-for-merge tag 'tag-master' of ../
4 8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
5 22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
t/t5525-fetch-tagopt.sh
+18 -5
@@ -8,7 +8,8 @@ setup_clone () {
8 git clone --mirror . $1 &&
9 git remote add remote_$1 $1 &&
10 (cd $1 &&
11 - git tag tag_$1)
11 + git tag tag_$1 &&
12 + git branch branch_$1)
13 }
14
15 test_expect_success setup '
@@ -21,21 +22,33 @@ test_expect_success setup '
22
23 test_expect_success "fetch with tagopt=--no-tags does not get tag" '
24 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
27 '
28
29 test_expect_success "fetch --tags with tagopt=--no-tags gets tag" '
30 + (
31 + cd one &&
32 + git branch second_branch_one
33 + ) &&
34 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
37 '
38
39 test_expect_success "fetch --no-tags with tagopt=--tags does not get tag" '
40 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
43 '
44
45 test_expect_success "fetch with tagopt=--tags gets tag" '
46 + (
47 + cd two &&
48 + git branch second_branch_two
49 + ) &&
50 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
53 '
54 test_done