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 <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Feb 27, 2014 at 10:00 UTC e6f637122ecd60c59df27b8188b6ddac39ac0063
2 files changed +45 -7
remote.c
+44 -6
@@ -821,6 +821,32 @@ static int match_name_with_pattern(const char *key, const char *name,
821 return ret;
822 }
823
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 +
850 static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
851 {
852 int i;
@@ -1954,25 +1980,37 @@ static int get_stale_heads_cb(const char *refname,
1980 const unsigned char *sha1, int flags, void *cb_data)
1981 {
1982 struct stale_heads_info *info = cb_data;
1983 + struct string_list matches = STRING_LIST_INIT_DUP;
1984 struct refspec query;
1985 + int i, stale = 1;
1986 memset(&query, 0, sizeof(struct refspec));
1987 query.dst = (char *)refname;
1988
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 */
1992
1993 /*
1994 * If we did find a suitable refspec and it's not a symref and
1995 * 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.
1999 */
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) {
2008 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
2009 hashcpy(ref->new_sha1, sha1);
2010 }
2011
1975 - free(query.src);
2012 +clean_exit:
2013 + string_list_clear(&matches, 0);
2014 return 0;
2015 }
2016
t/t5510-fetch.sh
+1 -1
@@ -113,7 +113,7 @@ test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
113 git rev-parse origin/master
114 '
115
116 -test_expect_failure 'fetch --prune handles overlapping refspecs' '
116 +test_expect_success 'fetch --prune handles overlapping refspecs' '
117 cd "$D" &&
118 git update-ref refs/pull/42/head master &&
119 git clone . prune-overlapping &&