Skip to content

Commit ed43de6

Browse files
carlosmngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c500352 commit ed43de6

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

builtin/fetch.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
540540
return ret;
541541
}
542542

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)
544544
{
545545
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);
547547
const char *dangling_msg = dry_run
548548
? _(" (%s will become dangling)\n")
549549
: _(" (%s has become dangling)\n");
@@ -734,8 +734,12 @@ static int do_fetch(struct transport *transport,
734734
free_refs(ref_map);
735735
return 1;
736736
}
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+
}
739743
free_refs(ref_map);
740744

741745
/* if neither --no-tags nor --tags was specified, do automated tag

builtin/remote.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
349349
else
350350
string_list_append(&states->tracked, abbrev_branch(ref->name));
351351
}
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);
353354
for (ref = stale_refs; ref; ref = ref->next) {
354355
struct string_list_item *item =
355356
string_list_append(&states->stale, abbrev_branch(ref->name));

remote.c

+23-12
Original file line numberDiff line numberDiff line change
@@ -1678,36 +1678,47 @@ struct ref *guess_remote_head(const struct ref *head,
16781678
}
16791679

16801680
struct stale_heads_info {
1681-
struct remote *remote;
16821681
struct string_list *ref_names;
16831682
struct ref **stale_refs_tail;
1683+
struct refspec *refs;
1684+
int ref_count;
16841685
};
16851686

16861687
static int get_stale_heads_cb(const char *refname,
16871688
const unsigned char *sha1, int flags, void *cb_data)
16881689
{
16891690
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);
16991707
}
1708+
1709+
free(query.src);
17001710
return 0;
17011711
}
17021712

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)
17041714
{
17051715
struct ref *ref, *stale_refs = NULL;
17061716
struct string_list ref_names = STRING_LIST_INIT_NODUP;
17071717
struct stale_heads_info info;
1708-
info.remote = remote;
17091718
info.ref_names = &ref_names;
17101719
info.stale_refs_tail = &stale_refs;
1720+
info.refs = refs;
1721+
info.ref_count = ref_count;
17111722
for (ref = fetch_map; ref; ref = ref->next)
17121723
string_list_append(&ref_names, ref->name);
17131724
sort_string_list(&ref_names);

remote.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,6 @@ struct ref *guess_remote_head(const struct ref *head,
164164
int all);
165165

166166
/* 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);
168168

169169
#endif

t/t5510-fetch.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test_expect_success 'fetch --prune on its own works as expected' '
8686
test_must_fail git rev-parse origin/extrabranch
8787
'
8888

89-
test_expect_failure 'fetch --prune with a branch name keeps branches' '
89+
test_expect_success 'fetch --prune with a branch name keeps branches' '
9090
cd "$D" &&
9191
git clone . prune-branch &&
9292
cd prune-branch &&
@@ -96,7 +96,7 @@ test_expect_failure 'fetch --prune with a branch name keeps branches' '
9696
git rev-parse origin/extrabranch
9797
'
9898

99-
test_expect_failure 'fetch --prune with a namespace keeps other namespaces' '
99+
test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
100100
cd "$D" &&
101101
git clone . prune-namespace &&
102102
cd prune-namespace &&

0 commit comments

Comments
 (0)