Skip to content

Commit e8c1e6c

Browse files
carlosmngitster
authored andcommitted
fetch: treat --tags like refs/tags/*:refs/tags/* when pruning
If --tags is specified, add that refspec to the list given to prune_refs so it knows to treat it as a filter on what refs to should consider for prunning. This way git fetch --prune --tags origin only prunes tags and doesn't delete the branch refs. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed43de6 commit e8c1e6c

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

builtin/fetch.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,29 @@ static int do_fetch(struct transport *transport,
735735
return 1;
736736
}
737737
if (prune) {
738-
if (ref_count)
738+
/* If --tags was specified, pretend the user gave us the canonical tags refspec */
739+
if (tags == TAGS_SET) {
740+
const char *tags_str = "refs/tags/*:refs/tags/*";
741+
struct refspec *tags_refspec, *refspec;
742+
743+
/* Copy the refspec and add the tags to it */
744+
refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
745+
tags_refspec = parse_fetch_refspec(1, &tags_str);
746+
memcpy(refspec, refs, ref_count * sizeof(struct refspec));
747+
memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
748+
ref_count++;
749+
750+
prune_refs(refspec, ref_count, ref_map);
751+
752+
ref_count--;
753+
/* The rest of the strings belong to fetch_one */
754+
free_refspec(1, tags_refspec);
755+
free(refspec);
756+
} else if (ref_count) {
739757
prune_refs(refs, ref_count, ref_map);
740-
else
758+
} else {
741759
prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
760+
}
742761
}
743762
free_refs(ref_map);
744763

t/t5510-fetch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
105105
git rev-parse origin/master
106106
'
107107

108-
test_expect_failure 'fetch --prune --tags does not delete the remote-tracking branches' '
108+
test_expect_success 'fetch --prune --tags does not delete the remote-tracking branches' '
109109
cd "$D" &&
110110
git clone . prune-tags &&
111111
cd prune-tags &&
@@ -116,7 +116,7 @@ test_expect_failure 'fetch --prune --tags does not delete the remote-tracking br
116116
test_must_fail git rev-parse somebranch
117117
'
118118

119-
test_expect_failure 'fetch --prune --tags with branch does not delete other remote-tracking branches' '
119+
test_expect_success 'fetch --prune --tags with branch does not delete other remote-tracking branches' '
120120
cd "$D" &&
121121
git clone . prune-tags-branch &&
122122
cd prune-tags-branch &&

0 commit comments

Comments
 (0)