Skip to content

Commit 6317972

Browse files
avargitster
authored andcommitted
fetch: make the --prune-tags work with <url>
Make the new --prune-tags option work properly when git-fetch is invoked with a <url> parameter instead of a <remote name> parameter. This change is split off from the introduction of --prune-tags due to the relative complexity of munging the incoming argv, which is easier to review as a separate change. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 97716d2 commit 6317972

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

Documentation/git-fetch.txt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,15 @@ It's reasonable to e.g. configure `fetch.pruneTags=true` in
175175
run, without making every invocation of `git fetch` without `--prune`
176176
an error.
177177

178-
Another special case of `--prune-tags` is that
179-
`refs/tags/*:refs/tags/*` will not be implicitly provided if an URL is
180-
being fetched. I.e.:
181-
182-
------------------------------------------------
183-
$ git fetch <url> --prune --prune-tags
184-
------------------------------------------------
185-
186-
Will prune no tags, as opposed to:
178+
Pruning tags with `--prune-tags` also works when fetching a URL
179+
instead of a named remote. These will all prune tags not found on
180+
origin:
187181

188182
------------------------------------------------
189183
$ git fetch origin --prune --prune-tags
190-
------------------------------------------------
191-
192-
To prune tags given a URL supply the refspec explicitly:
193-
194-
------------------------------------------------
195-
$ git fetch <url> --prune 'refs/tags/*:refs/tags/*'
184+
$ git fetch origin --prune 'refs/tags/*:refs/tags/*'
185+
$ git fetch <url of origin> --prune --prune-tags
186+
$ git fetch <url of origin> --prune 'refs/tags/*:refs/tags/*'
196187
------------------------------------------------
197188

198189
OUTPUT

builtin/fetch.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,10 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, int pru
12831283
static const char **refs = NULL;
12841284
struct refspec *refspec;
12851285
int ref_nr = 0;
1286+
int j = 0;
12861287
int exit_code;
1288+
int maybe_prune_tags;
1289+
int remote_via_config = remote_is_configured(remote, 0);
12871290

12881291
if (!remote)
12891292
die(_("No remote repository specified. Please, specify either a URL or a\n"
@@ -1311,13 +1314,21 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, int pru
13111314
prune_tags = PRUNE_TAGS_BY_DEFAULT;
13121315
}
13131316

1314-
if (prune_tags_ok && prune_tags && remote_is_configured(remote, 0))
1317+
maybe_prune_tags = prune_tags_ok && prune_tags;
1318+
if (maybe_prune_tags && remote_via_config)
13151319
add_prune_tags_to_fetch_refspec(remote);
13161320

1321+
if (argc > 0 || (maybe_prune_tags && !remote_via_config)) {
1322+
size_t nr_alloc = st_add3(argc, maybe_prune_tags, 1);
1323+
refs = xcalloc(nr_alloc, sizeof(const char *));
1324+
if (maybe_prune_tags) {
1325+
refs[j++] = xstrdup("refs/tags/*:refs/tags/*");
1326+
ref_nr++;
1327+
}
1328+
}
1329+
13171330
if (argc > 0) {
1318-
int j = 0;
13191331
int i;
1320-
refs = xcalloc(st_add(argc, 1), sizeof(const char *));
13211332
for (i = 0; i < argc; i++) {
13221333
if (!strcmp(argv[i], "tag")) {
13231334
i++;

t/t5510-fetch.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -738,18 +738,15 @@ test_configured_prune unset unset unset true pruned kept \
738738
"--prune origin +refs/heads/*:refs/remotes/origin/*"
739739

740740
# Pruning that also takes place if a file:// url replaces a named
741-
# remote, with the exception of --prune-tags on the command-line
742-
# (arbitrary limitation).
743-
#
744-
# However, because there's no implicit
741+
# remote. However, because there's no implicit
745742
# +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
746743
# command-line negates --prune-tags, the branches will not be pruned.
747744
test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
748745
test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
749746
test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
750-
test_configured_prune_type unset unset unset unset kept kept "origin --prune --prune-tags" "link"
747+
test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
751748
test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
752-
test_configured_prune_type unset unset unset unset kept kept "--prune --prune-tags origin" "link"
749+
test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
753750
test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
754751
test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
755752
test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
@@ -761,16 +758,17 @@ test_configured_prune_type unset true true unset kept pruned "origin" "link"
761758
test_configured_prune_type unset true unset true pruned pruned "origin" "name"
762759
test_configured_prune_type unset true unset true kept pruned "origin" "link"
763760

764-
# Interaction between --prune-tags and no "fetch" config in the remote
765-
# at all.
761+
# When all remote.origin.fetch settings are deleted a --prune
762+
# --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
763+
# tags, but not tracking branches, will be deleted.
766764
test_expect_success 'remove remote.origin.fetch "one"' '
767765
(
768766
cd one &&
769767
git config --unset-all remote.origin.fetch
770768
)
771769
'
772770
test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
773-
test_configured_prune_type unset unset unset unset kept kept "origin --prune --prune-tags" "link"
771+
test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
774772

775773
test_expect_success 'all boundary commits are excluded' '
776774
test_commit base &&

0 commit comments

Comments
 (0)