Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit e6f6371

Browse files
carlosmngitster
authored andcommitted
fetch: handle overlaping refspecs on --prune
We need to consider that a remote-tracking branch may match more than one rhs of a fetch refspec. In such a case, it is not enough to stop at the first match but look at all of the matches in order to determine whether a head is stale. To this goal, introduce a variant of query_refspecs which returns all of the matching refspecs and loop over those answers to check for staleness. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f377e7a commit e6f6371

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

remote.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,32 @@ static int match_name_with_pattern(const char *key, const char *name,
821821
return ret;
822822
}
823823

824+
static void query_refspecs_multiple(struct refspec *refs, int ref_count, struct refspec *query, struct string_list *results)
825+
{
826+
int i;
827+
int find_src = !query->src;
828+
829+
if (find_src && !query->dst)
830+
error("query_refspecs_multiple: need either src or dst");
831+
832+
for (i = 0; i < ref_count; i++) {
833+
struct refspec *refspec = &refs[i];
834+
const char *key = find_src ? refspec->dst : refspec->src;
835+
const char *value = find_src ? refspec->src : refspec->dst;
836+
const char *needle = find_src ? query->dst : query->src;
837+
char **result = find_src ? &query->src : &query->dst;
838+
839+
if (!refspec->dst)
840+
continue;
841+
if (refspec->pattern) {
842+
if (match_name_with_pattern(key, needle, value, result))
843+
string_list_append_nodup(results, *result);
844+
} else if (!strcmp(needle, key)) {
845+
string_list_append(results, value);
846+
}
847+
}
848+
}
849+
824850
static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
825851
{
826852
int i;
@@ -1954,25 +1980,37 @@ static int get_stale_heads_cb(const char *refname,
19541980
const unsigned char *sha1, int flags, void *cb_data)
19551981
{
19561982
struct stale_heads_info *info = cb_data;
1983+
struct string_list matches = STRING_LIST_INIT_DUP;
19571984
struct refspec query;
1985+
int i, stale = 1;
19581986
memset(&query, 0, sizeof(struct refspec));
19591987
query.dst = (char *)refname;
19601988

1961-
if (query_refspecs(info->refs, info->ref_count, &query))
1962-
return 0; /* No matches */
1989+
query_refspecs_multiple(info->refs, info->ref_count, &query, &matches);
1990+
if (matches.nr == 0)
1991+
goto clean_exit; /* No matches */
19631992

19641993
/*
19651994
* If we did find a suitable refspec and it's not a symref and
19661995
* it's not in the list of refs that currently exist in that
1967-
* remote we consider it to be stale.
1996+
* remote, we consider it to be stale. In order to deal with
1997+
* overlapping refspecs, we need to go over all of the
1998+
* matching refs.
19681999
*/
1969-
if (!((flags & REF_ISSYMREF) ||
1970-
string_list_has_string(info->ref_names, query.src))) {
2000+
if (flags & REF_ISSYMREF)
2001+
goto clean_exit;
2002+
2003+
for (i = 0; stale && i < matches.nr; i++)
2004+
if (string_list_has_string(info->ref_names, matches.items[i].string))
2005+
stale = 0;
2006+
2007+
if (stale) {
19712008
struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
19722009
hashcpy(ref->new_sha1, sha1);
19732010
}
19742011

1975-
free(query.src);
2012+
clean_exit:
2013+
string_list_clear(&matches, 0);
19762014
return 0;
19772015
}
19782016

t/t5510-fetch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
113113
git rev-parse origin/master
114114
'
115115

116-
test_expect_failure 'fetch --prune handles overlapping refspecs' '
116+
test_expect_success 'fetch --prune handles overlapping refspecs' '
117117
cd "$D" &&
118118
git update-ref refs/pull/42/head master &&
119119
git clone . prune-overlapping &&

0 commit comments

Comments
 (0)