fetch: honor the user-provided refspecs when pruning refs

If the user gave us refspecs on the command line, we should use those when deciding whether to prune a ref instead of relying on the refspecs in the config. Previously, running git fetch --prune origin refs/heads/master:refs/remotes/origin/master would delete every other ref under the origin namespace because we were using the refspec to filter the available refs but using the configured refspec to figure out if a ref had been deleted on the remote. This is clearly the wrong thing to do. Change prune_refs and get_stale_heads to simply accept a list of references and a list of refspecs. The caller of either function needs to decide what refspecs should be used to decide whether a ref is stale. Signed-off-by: Carlos Martín Nieto <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Oct 15, 2011 at 07:04 UTC ed43de6ec35dfd4c4bd33ae9b5f2ebe38282209f
5 files changed +36 -20
builtin/fetch.c
+8 -4
@@ -540,10 +540,10 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
540 return ret;
541 }
542
543 -static int prune_refs(struct transport *transport, struct ref *ref_map)
543 +static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
544 {
545 int result = 0;
546 - struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
546 + struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
547 const char *dangling_msg = dry_run
548 ? _(" (%s will become dangling)\n")
549 : _(" (%s has become dangling)\n");
@@ -734,8 +734,12 @@ static int do_fetch(struct transport *transport,
734 free_refs(ref_map);
735 return 1;
736 }
737 - if (prune)
738 - prune_refs(transport, ref_map);
737 + if (prune) {
738 + if (ref_count)
739 + prune_refs(refs, ref_count, ref_map);
740 + else
741 + prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
742 + }
743 free_refs(ref_map);
744
745 /* if neither --no-tags nor --tags was specified, do automated tag
builtin/remote.c
+2 -1
@@ -349,7 +349,8 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
349 else
350 string_list_append(&states->tracked, abbrev_branch(ref->name));
351 }
352 - stale_refs = get_stale_heads(states->remote, fetch_map);
352 + stale_refs = get_stale_heads(states->remote->fetch,
353 + states->remote->fetch_refspec_nr, fetch_map);
354 for (ref = stale_refs; ref; ref = ref->next) {
355 struct string_list_item *item =
356 string_list_append(&states->stale, abbrev_branch(ref->name));
remote.c
+23 -12
@@ -1678,36 +1678,47 @@ struct ref *guess_remote_head(const struct ref *head,
1678 }
1679
1680 struct stale_heads_info {
1681 - struct remote *remote;
1681 struct string_list *ref_names;
1682 struct ref **stale_refs_tail;
1683 + struct refspec *refs;
1684 + int ref_count;
1685 };
1686
1687 static int get_stale_heads_cb(const char *refname,
1688 const unsigned char *sha1, int flags, void *cb_data)
1689 {
1690 struct stale_heads_info *info = cb_data;
1690 - struct refspec refspec;
1691 - memset(&refspec, 0, sizeof(refspec));
1692 - refspec.dst = (char *)refname;
1693 - if (!remote_find_tracking(info->remote, &refspec)) {
1694 - if (!((flags & REF_ISSYMREF) ||
1695 - string_list_has_string(info->ref_names, refspec.src))) {
1696 - struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1697 - hashcpy(ref->new_sha1, sha1);
1698 - }
1691 + struct refspec query;
1692 + memset(&query, 0, sizeof(struct refspec));
1693 + query.dst = (char *)refname;
1694 +
1695 + if (query_refspecs(info->refs, info->ref_count, &query))
1696 + return 0; /* No matches */
1697 +
1698 + /*
1699 + * If we did find a suitable refspec and it's not a symref and
1700 + * it's not in the list of refs that currently exist in that
1701 + * remote we consider it to be stale.
1702 + */
1703 + if (!((flags & REF_ISSYMREF) ||
1704 + string_list_has_string(info->ref_names, query.src))) {
1705 + struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1706 + hashcpy(ref->new_sha1, sha1);
1707 }
1708 +
1709 + free(query.src);
1710 return 0;
1711 }
1712
1703 -struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map)
1713 +struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map)
1714 {
1715 struct ref *ref, *stale_refs = NULL;
1716 struct string_list ref_names = STRING_LIST_INIT_NODUP;
1717 struct stale_heads_info info;
1708 - info.remote = remote;
1718 info.ref_names = &ref_names;
1719 info.stale_refs_tail = &stale_refs;
1720 + info.refs = refs;
1721 + info.ref_count = ref_count;
1722 for (ref = fetch_map; ref; ref = ref->next)
1723 string_list_append(&ref_names, ref->name);
1724 sort_string_list(&ref_names);
remote.h
+1 -1
@@ -164,6 +164,6 @@ struct ref *guess_remote_head(const struct ref *head,
164 int all);
165
166 /* Return refs which no longer exist on remote */
167 -struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map);
167 +struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map);
168
169 #endif
t/t5510-fetch.sh
+2 -2
@@ -86,7 +86,7 @@ test_expect_success 'fetch --prune on its own works as expected' '
86 test_must_fail git rev-parse origin/extrabranch
87 '
88
89 -test_expect_failure 'fetch --prune with a branch name keeps branches' '
89 +test_expect_success 'fetch --prune with a branch name keeps branches' '
90 cd "$D" &&
91 git clone . prune-branch &&
92 cd prune-branch &&
@@ -96,7 +96,7 @@ test_expect_failure 'fetch --prune with a branch name keeps branches' '
96 git rev-parse origin/extrabranch
97 '
98
99 -test_expect_failure 'fetch --prune with a namespace keeps other namespaces' '
99 +test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
100 cd "$D" &&
101 git clone . prune-namespace &&
102 cd prune-namespace &&