Skip to content

Commit c0192df

Browse files
jacob-kellergitster
authored andcommitted
refspec: add support for negative refspecs
Both fetch and push support pattern refspecs which allow fetching or pushing references that match a specific pattern. Because these patterns are globs, they have somewhat limited ability to express more complex situations. For example, suppose you wish to fetch all branches from a remote except for a specific one. To allow this, you must setup a set of refspecs which match only the branches you want. Because refspecs are either explicit name matches, or simple globs, many patterns cannot be expressed. Add support for a new type of refspec, referred to as "negative" refspecs. These are prefixed with a '^' and mean "exclude any ref matching this refspec". They can only have one "side" which always refers to the source. During a fetch, this refers to the name of the ref on the remote. During a push, this refers to the name of the ref on the local side. With negative refspecs, users can express more complex patterns. For example: git fetch origin refs/heads/*:refs/remotes/origin/* ^refs/heads/dontwant will fetch all branches on origin into remotes/origin, but will exclude fetching the branch named dontwant. Refspecs today are commutative, meaning that order doesn't expressly matter. Rather than forcing an implied order, negative refspecs will always be applied last. That is, in order to match, a ref must match at least one positive refspec, and match none of the negative refspecs. This is similar to how negative pathspecs work. Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95e7c38 commit c0192df

File tree

7 files changed

+367
-13
lines changed

7 files changed

+367
-13
lines changed

Documentation/pull-fetch-param.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ The colon can be omitted when <dst> is empty. <src> is
3030
typically a ref, but it can also be a fully spelled hex object
3131
name.
3232
+
33+
A <refspec> may contain a `*` in its <src> to indicate a simple pattern
34+
match. Such a refspec functions like a glob that matches any ref with the
35+
same prefix. A pattern <refspec> must have a `*` in both the <src> and
36+
<dst>. It will map refs to the destination by replacing the `*` with the
37+
contents matched from the source.
38+
+
39+
If a refspec is prefixed by `^`, it will be interpreted as a negative
40+
refspec. Rather than specifying which refs to fetch or which local refs to
41+
update, such a refspec will instead specify refs to exclude. A ref will be
42+
considered to match if it matches at least one positive refspec, and does
43+
not match any negative refspec. Negative refspecs can be useful to restrict
44+
the scope of a pattern refspec so that it will not include specific refs.
45+
Negative refspecs can themselves be pattern refspecs. However, they may only
46+
contain a <src> and do not specify a <dst>. Fully spelled out hex object
47+
names are also not supported.
48+
+
3349
`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;
3450
it requests fetching everything up to the given tag.
3551
+

builtin/fetch.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,16 @@ static struct ref *get_ref_map(struct remote *remote,
530530
tail = &rm->next;
531531
}
532532

533+
/*
534+
* apply negative refspecs first, before we remove duplicates. This is
535+
* necessary as negative refspecs might remove an otherwise conflicting
536+
* duplicate.
537+
*/
538+
if (rs->nr)
539+
ref_map = apply_negative_refspecs(ref_map, rs);
540+
else
541+
ref_map = apply_negative_refspecs(ref_map, &remote->fetch);
542+
533543
ref_map = ref_remove_duplicates(ref_map);
534544

535545
refname_hash_init(&existing_refs);

refspec.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static struct refspec_item s_tag_refspec = {
88
1,
99
0,
1010
0,
11+
0,
1112
"refs/tags/*",
1213
"refs/tags/*"
1314
};
@@ -32,10 +33,17 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
3233
if (*lhs == '+') {
3334
item->force = 1;
3435
lhs++;
36+
} else if (*lhs == '^') {
37+
item->negative = 1;
38+
lhs++;
3539
}
3640

3741
rhs = strrchr(lhs, ':');
3842

43+
/* negative refspecs only have one side */
44+
if (item->negative && rhs)
45+
return 0;
46+
3947
/*
4048
* Before going on, special case ":" (or "+:") as a refspec
4149
* for pushing matching refs.
@@ -55,7 +63,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
5563

5664
llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
5765
if (1 <= llen && memchr(lhs, '*', llen)) {
58-
if ((rhs && !is_glob) || (!rhs && fetch))
66+
if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
5967
return 0;
6068
is_glob = 1;
6169
} else if (rhs && is_glob) {
@@ -66,6 +74,28 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
6674
item->src = xstrndup(lhs, llen);
6775
flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
6876

77+
if (item->negative) {
78+
struct object_id unused;
79+
80+
/*
81+
* Negative refspecs only have a LHS, which indicates a ref
82+
* (or pattern of refs) to exclude from other matches. This
83+
* can either be a simple ref, or a glob pattern. Exact sha1
84+
* match is not currently supported.
85+
*/
86+
if (!*item->src)
87+
return 0; /* negative refspecs must not be empty */
88+
else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
89+
return 0; /* negative refpsecs cannot be exact sha1 */
90+
else if (!check_refname_format(item->src, flags))
91+
; /* valid looking ref is ok */
92+
else
93+
return 0;
94+
95+
/* the other rules below do not apply to negative refspecs */
96+
return 1;
97+
}
98+
6999
if (fetch) {
70100
struct object_id unused;
71101

@@ -209,7 +239,7 @@ void refspec_ref_prefixes(const struct refspec *rs,
209239
const struct refspec_item *item = &rs->items[i];
210240
const char *prefix = NULL;
211241

212-
if (item->exact_sha1)
242+
if (item->exact_sha1 || item->negative)
213243
continue;
214244
if (rs->fetch == REFSPEC_FETCH)
215245
prefix = item->src;

refspec.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
extern const struct refspec_item *tag_refspec;
66
77
/**
8-
* A struct refspec_item holds the parsed interpretation of a refspec. If it will
9-
* force updates (starts with a '+'), force is true. If it is a pattern
10-
* (sides end with '*') pattern is true. src and dest are the two sides
11-
* (including '*' characters if present); if there is only one side, it is src,
12-
* and dst is NULL; if sides exist but are empty (i.e., the refspec either
13-
* starts or ends with ':'), the corresponding side is "".
8+
* A struct refspec_item holds the parsed interpretation of a refspec. If it
9+
* will force updates (starts with a '+'), force is true. If it is a pattern
10+
* (sides end with '*') pattern is true. If it is a negative refspec, (starts
11+
* with '^'), negative is true. src and dest are the two sides (including '*'
12+
* characters if present); if there is only one side, it is src, and dst is
13+
* NULL; if sides exist but are empty (i.e., the refspec either starts or ends
14+
* with ':'), the corresponding side is "".
1415
*
1516
* remote_find_tracking(), given a remote and a struct refspec_item with either src
1617
* or dst filled out, will fill out the other such that the result is in the
@@ -22,6 +23,7 @@ struct refspec_item {
2223
unsigned pattern : 1;
2324
unsigned matching : 1;
2425
unsigned exact_sha1 : 1;
26+
unsigned negative : 1;
2527

2628
char *src;
2729
char *dst;

remote.c

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,91 @@ static int match_name_with_pattern(const char *key, const char *name,
686686
return ret;
687687
}
688688

689+
static int refspec_match(const struct refspec_item *refspec,
690+
const char *name)
691+
{
692+
if (refspec->pattern)
693+
return match_name_with_pattern(refspec->src, name, NULL, NULL);
694+
695+
return !strcmp(refspec->src, name);
696+
}
697+
698+
static int omit_name_by_refspec(const char *name, struct refspec *rs)
699+
{
700+
int i;
701+
702+
for (i = 0; i < rs->nr; i++) {
703+
if (rs->items[i].negative && refspec_match(&rs->items[i], name))
704+
return 1;
705+
}
706+
return 0;
707+
}
708+
709+
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
710+
{
711+
struct ref **tail;
712+
713+
for (tail = &ref_map; *tail; ) {
714+
struct ref *ref = *tail;
715+
716+
if (omit_name_by_refspec(ref->name, rs)) {
717+
*tail = ref->next;
718+
free(ref->peer_ref);
719+
free(ref);
720+
} else
721+
tail = &ref->next;
722+
}
723+
724+
return ref_map;
725+
}
726+
727+
static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
728+
{
729+
int i, matched_negative = 0;
730+
int find_src = !query->src;
731+
struct string_list reversed = STRING_LIST_INIT_NODUP;
732+
const char *needle = find_src ? query->dst : query->src;
733+
734+
/*
735+
* Check whether the queried ref matches any negative refpsec. If so,
736+
* then we should ultimately treat this as not matching the query at
737+
* all.
738+
*
739+
* Note that negative refspecs always match the source, but the query
740+
* item uses the destination. To handle this, we apply pattern
741+
* refspecs in reverse to figure out if the query source matches any
742+
* of the negative refspecs.
743+
*/
744+
for (i = 0; i < rs->nr; i++) {
745+
struct refspec_item *refspec = &rs->items[i];
746+
char *expn_name;
747+
748+
if (refspec->negative)
749+
continue;
750+
751+
/* Note the reversal of src and dst */
752+
if (refspec->pattern) {
753+
const char *key = refspec->dst ? refspec->dst : refspec->src;
754+
const char *value = refspec->src;
755+
756+
if (match_name_with_pattern(key, needle, value, &expn_name))
757+
string_list_append_nodup(&reversed, expn_name);
758+
} else {
759+
if (!strcmp(needle, refspec->src))
760+
string_list_append(&reversed, refspec->src);
761+
}
762+
}
763+
764+
for (i = 0; !matched_negative && i < reversed.nr; i++) {
765+
if (omit_name_by_refspec(reversed.items[i].string, rs))
766+
matched_negative = 1;
767+
}
768+
769+
string_list_clear(&reversed, 0);
770+
771+
return matched_negative;
772+
}
773+
689774
static void query_refspecs_multiple(struct refspec *rs,
690775
struct refspec_item *query,
691776
struct string_list *results)
@@ -696,14 +781,17 @@ static void query_refspecs_multiple(struct refspec *rs,
696781
if (find_src && !query->dst)
697782
BUG("query_refspecs_multiple: need either src or dst");
698783

784+
if (query_matches_negative_refspec(rs, query))
785+
return;
786+
699787
for (i = 0; i < rs->nr; i++) {
700788
struct refspec_item *refspec = &rs->items[i];
701789
const char *key = find_src ? refspec->dst : refspec->src;
702790
const char *value = find_src ? refspec->src : refspec->dst;
703791
const char *needle = find_src ? query->dst : query->src;
704792
char **result = find_src ? &query->src : &query->dst;
705793

706-
if (!refspec->dst)
794+
if (!refspec->dst || refspec->negative)
707795
continue;
708796
if (refspec->pattern) {
709797
if (match_name_with_pattern(key, needle, value, result))
@@ -724,12 +812,15 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query)
724812
if (find_src && !query->dst)
725813
BUG("query_refspecs: need either src or dst");
726814

815+
if (query_matches_negative_refspec(rs, query))
816+
return -1;
817+
727818
for (i = 0; i < rs->nr; i++) {
728819
struct refspec_item *refspec = &rs->items[i];
729820
const char *key = find_src ? refspec->dst : refspec->src;
730821
const char *value = find_src ? refspec->src : refspec->dst;
731822

732-
if (!refspec->dst)
823+
if (!refspec->dst || refspec->negative)
733824
continue;
734825
if (refspec->pattern) {
735826
if (match_name_with_pattern(key, needle, value, result)) {
@@ -1058,7 +1149,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
10581149
const char *dst_value = rs->dst;
10591150
char *dst_guess;
10601151

1061-
if (rs->pattern || rs->matching)
1152+
if (rs->pattern || rs->matching || rs->negative)
10621153
return 0;
10631154

10641155
matched_src = matched_dst = NULL;
@@ -1134,6 +1225,10 @@ static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
11341225
int matching_refs = -1;
11351226
for (i = 0; i < rs->nr; i++) {
11361227
const struct refspec_item *item = &rs->items[i];
1228+
1229+
if (item->negative)
1230+
continue;
1231+
11371232
if (item->matching &&
11381233
(matching_refs == -1 || item->force)) {
11391234
matching_refs = i;
@@ -1339,7 +1434,7 @@ int check_push_refs(struct ref *src, struct refspec *rs)
13391434
for (i = 0; i < rs->nr; i++) {
13401435
struct refspec_item *item = &rs->items[i];
13411436

1342-
if (item->pattern || item->matching)
1437+
if (item->pattern || item->matching || item->negative)
13431438
continue;
13441439

13451440
ret |= match_explicit_lhs(src, item, NULL, NULL);
@@ -1441,6 +1536,8 @@ int match_push_refs(struct ref *src, struct ref **dst,
14411536
string_list_clear(&src_ref_index, 0);
14421537
}
14431538

1539+
*dst = apply_negative_refspecs(*dst, rs);
1540+
14441541
if (errs)
14451542
return -1;
14461543
return 0;
@@ -1810,6 +1907,9 @@ int get_fetch_map(const struct ref *remote_refs,
18101907
{
18111908
struct ref *ref_map, **rmp;
18121909

1910+
if (refspec->negative)
1911+
return 0;
1912+
18131913
if (refspec->pattern) {
18141914
ref_map = get_expanded_map(remote_refs, refspec);
18151915
} else {

remote.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
193193
*/
194194
struct ref *ref_remove_duplicates(struct ref *ref_map);
195195

196+
/*
197+
* Remove all entries in the input list which match any negative refspec in
198+
* the refspec list.
199+
*/
200+
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
201+
196202
int query_refspecs(struct refspec *rs, struct refspec_item *query);
197203
char *apply_refspecs(struct refspec *rs, const char *name);
198204

@@ -205,7 +211,8 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
205211
/*
206212
* Given a list of the remote refs and the specification of things to
207213
* fetch, makes a (separate) list of the refs to fetch and the local
208-
* refs to store into.
214+
* refs to store into. Note that negative refspecs are ignored here, and
215+
* should be handled separately.
209216
*
210217
* *tail is the pointer to the tail pointer of the list of results
211218
* beforehand, and will be set to the tail pointer of the list of

0 commit comments

Comments
 (0)