@matheusbsbs / matheus-bagliano / commits / c0192df630
Fork 0

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 <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Sep 30, 2020 at 14:25 UTC c0192df6306d4d9ad77f6015a053925b13155834
7 files changed +367 -13
Documentation/pull-fetch-param.txt
+16
@@ -30,6 +30,22 @@ The colon can be omitted when <dst> is empty. <src> is
30 typically a ref, but it can also be a fully spelled hex object
31 name.
32 +
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 ++
49 `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;
50 it requests fetching everything up to the given tag.
51 +
builtin/fetch.c
+10
@@ -530,6 +530,16 @@ static struct ref *get_ref_map(struct remote *remote,
530 tail = &rm->next;
531 }
532
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 +
543 ref_map = ref_remove_duplicates(ref_map);
544
545 refname_hash_init(&existing_refs);
refspec.c
+32 -2
@@ -8,6 +8,7 @@ static struct refspec_item s_tag_refspec = {
8 1,
9 0,
10 0,
11 + 0,
12 "refs/tags/*",
13 "refs/tags/*"
14 };
@@ -32,10 +33,17 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
33 if (*lhs == '+') {
34 item->force = 1;
35 lhs++;
36 + } else if (*lhs == '^') {
37 + item->negative = 1;
38 + lhs++;
39 }
40
41 rhs = strrchr(lhs, ':');
42
43 + /* negative refspecs only have one side */
44 + if (item->negative && rhs)
45 + return 0;
46 +
47 /*
48 * Before going on, special case ":" (or "+:") as a refspec
49 * for pushing matching refs.
@@ -55,7 +63,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
63
64 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
65 if (1 <= llen && memchr(lhs, '*', llen)) {
58 - if ((rhs && !is_glob) || (!rhs && fetch))
66 + if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
67 return 0;
68 is_glob = 1;
69 } else if (rhs && is_glob) {
@@ -66,6 +74,28 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
74 item->src = xstrndup(lhs, llen);
75 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
76
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 +
99 if (fetch) {
100 struct object_id unused;
101
@@ -209,7 +239,7 @@ void refspec_ref_prefixes(const struct refspec *rs,
239 const struct refspec_item *item = &rs->items[i];
240 const char *prefix = NULL;
241
212 - if (item->exact_sha1)
242 + if (item->exact_sha1 || item->negative)
243 continue;
244 if (rs->fetch == REFSPEC_FETCH)
245 prefix = item->src;
refspec.h
+8 -6
@@ -5,12 +5,13 @@
5 extern const struct refspec_item *tag_refspec;
6
7 /**
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 "".
15 *
16 * remote_find_tracking(), given a remote and a struct refspec_item with either src
17 * or dst filled out, will fill out the other such that the result is in the
@@ -22,6 +23,7 @@ struct refspec_item {
23 unsigned pattern : 1;
24 unsigned matching : 1;
25 unsigned exact_sha1 : 1;
26 + unsigned negative : 1;
27
28 char *src;
29 char *dst;
remote.c
+104 -4
@@ -686,6 +686,91 @@ static int match_name_with_pattern(const char *key, const char *name,
686 return ret;
687 }
688
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 +
774 static void query_refspecs_multiple(struct refspec *rs,
775 struct refspec_item *query,
776 struct string_list *results)
@@ -696,6 +781,9 @@ static void query_refspecs_multiple(struct refspec *rs,
781 if (find_src && !query->dst)
782 BUG("query_refspecs_multiple: need either src or dst");
783
784 + if (query_matches_negative_refspec(rs, query))
785 + return;
786 +
787 for (i = 0; i < rs->nr; i++) {
788 struct refspec_item *refspec = &rs->items[i];
789 const char *key = find_src ? refspec->dst : refspec->src;
@@ -703,7 +791,7 @@ static void query_refspecs_multiple(struct refspec *rs,
791 const char *needle = find_src ? query->dst : query->src;
792 char **result = find_src ? &query->src : &query->dst;
793
706 - if (!refspec->dst)
794 + if (!refspec->dst || refspec->negative)
795 continue;
796 if (refspec->pattern) {
797 if (match_name_with_pattern(key, needle, value, result))
@@ -724,12 +812,15 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query)
812 if (find_src && !query->dst)
813 BUG("query_refspecs: need either src or dst");
814
815 + if (query_matches_negative_refspec(rs, query))
816 + return -1;
817 +
818 for (i = 0; i < rs->nr; i++) {
819 struct refspec_item *refspec = &rs->items[i];
820 const char *key = find_src ? refspec->dst : refspec->src;
821 const char *value = find_src ? refspec->src : refspec->dst;
822
732 - if (!refspec->dst)
823 + if (!refspec->dst || refspec->negative)
824 continue;
825 if (refspec->pattern) {
826 if (match_name_with_pattern(key, needle, value, result)) {
@@ -1058,7 +1149,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
1149 const char *dst_value = rs->dst;
1150 char *dst_guess;
1151
1061 - if (rs->pattern || rs->matching)
1152 + if (rs->pattern || rs->matching || rs->negative)
1153 return 0;
1154
1155 matched_src = matched_dst = NULL;
@@ -1134,6 +1225,10 @@ static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
1225 int matching_refs = -1;
1226 for (i = 0; i < rs->nr; i++) {
1227 const struct refspec_item *item = &rs->items[i];
1228 +
1229 + if (item->negative)
1230 + continue;
1231 +
1232 if (item->matching &&
1233 (matching_refs == -1 || item->force)) {
1234 matching_refs = i;
@@ -1339,7 +1434,7 @@ int check_push_refs(struct ref *src, struct refspec *rs)
1434 for (i = 0; i < rs->nr; i++) {
1435 struct refspec_item *item = &rs->items[i];
1436
1342 - if (item->pattern || item->matching)
1437 + if (item->pattern || item->matching || item->negative)
1438 continue;
1439
1440 ret |= match_explicit_lhs(src, item, NULL, NULL);
@@ -1441,6 +1536,8 @@ int match_push_refs(struct ref *src, struct ref **dst,
1536 string_list_clear(&src_ref_index, 0);
1537 }
1538
1539 + *dst = apply_negative_refspecs(*dst, rs);
1540 +
1541 if (errs)
1542 return -1;
1543 return 0;
@@ -1810,6 +1907,9 @@ int get_fetch_map(const struct ref *remote_refs,
1907 {
1908 struct ref *ref_map, **rmp;
1909
1910 + if (refspec->negative)
1911 + return 0;
1912 +
1913 if (refspec->pattern) {
1914 ref_map = get_expanded_map(remote_refs, refspec);
1915 } else {
remote.h
+8 -1
@@ -193,6 +193,12 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
193 */
194 struct ref *ref_remove_duplicates(struct ref *ref_map);
195
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 +
202 int query_refspecs(struct refspec *rs, struct refspec_item *query);
203 char *apply_refspecs(struct refspec *rs, const char *name);
204
@@ -205,7 +211,8 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
211 /*
212 * Given a list of the remote refs and the specification of things to
213 * 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.
216 *
217 * *tail is the pointer to the tail pointer of the list of results
218 * beforehand, and will be set to the tail pointer of the list of
t/t5582-fetch-negative-refspec.sh new
+189
@@ -0,0 +1,189 @@
1 +#!/bin/sh
2 +# Copyright (c) 2020, Jacob Keller.
3 +
4 +test_description='"git fetch" with negative refspecs.
5 +
6 +'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success setup '
11 + echo >file original &&
12 + git add file &&
13 + git commit -a -m original
14 +'
15 +
16 +test_expect_success "clone and setup child repos" '
17 + git clone . one &&
18 + (
19 + cd one &&
20 + echo >file updated by one &&
21 + git commit -a -m "updated by one" &&
22 + git switch -c alternate &&
23 + echo >file updated again by one &&
24 + git commit -a -m "updated by one again" &&
25 + git switch master
26 + ) &&
27 + git clone . two &&
28 + (
29 + cd two &&
30 + git config branch.master.remote one &&
31 + git config remote.one.url ../one/.git/ &&
32 + git config remote.one.fetch +refs/heads/*:refs/remotes/one/* &&
33 + git config --add remote.one.fetch ^refs/heads/alternate
34 + ) &&
35 + git clone . three
36 +'
37 +
38 +test_expect_success "fetch one" '
39 + echo >file updated by origin &&
40 + git commit -a -m "updated by origin" &&
41 + (
42 + cd two &&
43 + test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
44 + git fetch one &&
45 + test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
46 + git rev-parse --verify refs/remotes/one/master &&
47 + mine=$(git rev-parse refs/remotes/one/master) &&
48 + his=$(cd ../one && git rev-parse refs/heads/master) &&
49 + test "z$mine" = "z$his"
50 + )
51 +'
52 +
53 +test_expect_success "fetch with negative refspec on commandline" '
54 + echo >file updated by origin again &&
55 + git commit -a -m "updated by origin again" &&
56 + (
57 + cd three &&
58 + alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
59 + echo $alternate_in_one >expect &&
60 + git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/master &&
61 + cut -f -1 .git/FETCH_HEAD >actual &&
62 + test_cmp expect actual
63 + )
64 +'
65 +
66 +test_expect_success "fetch with negative sha1 refspec fails" '
67 + echo >file updated by origin yet again &&
68 + git commit -a -m "updated by origin yet again" &&
69 + (
70 + cd three &&
71 + master_in_one=$(cd ../one && git rev-parse refs/heads/master) &&
72 + test_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$master_in_one
73 + )
74 +'
75 +
76 +test_expect_success "fetch with negative pattern refspec" '
77 + echo >file updated by origin once more &&
78 + git commit -a -m "updated by origin once more" &&
79 + (
80 + cd three &&
81 + alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
82 + echo $alternate_in_one >expect &&
83 + git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&
84 + cut -f -1 .git/FETCH_HEAD >actual &&
85 + test_cmp expect actual
86 + )
87 +'
88 +
89 +test_expect_success "fetch with negative pattern refspec does not expand prefix" '
90 + echo >file updated by origin another time &&
91 + git commit -a -m "updated by origin another time" &&
92 + (
93 + cd three &&
94 + alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
95 + master_in_one=$(cd ../one && git rev-parse refs/heads/master) &&
96 + echo $alternate_in_one >expect &&
97 + echo $master_in_one >>expect &&
98 + git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^master &&
99 + cut -f -1 .git/FETCH_HEAD >actual &&
100 + test_cmp expect actual
101 + )
102 +'
103 +
104 +test_expect_success "fetch with negative refspec avoids duplicate conflict" '
105 + cd "$D" &&
106 + (
107 + cd one &&
108 + git branch dups/a &&
109 + git branch dups/b &&
110 + git branch dups/c &&
111 + git branch other/a &&
112 + git rev-parse --verify refs/heads/other/a >../expect &&
113 + git rev-parse --verify refs/heads/dups/b >>../expect &&
114 + git rev-parse --verify refs/heads/dups/c >>../expect
115 + ) &&
116 + (
117 + cd three &&
118 + git fetch ../one/.git ^refs/heads/dups/a refs/heads/dups/*:refs/dups/* refs/heads/other/a:refs/dups/a &&
119 + git rev-parse --verify refs/dups/a >../actual &&
120 + git rev-parse --verify refs/dups/b >>../actual &&
121 + git rev-parse --verify refs/dups/c >>../actual
122 + ) &&
123 + test_cmp expect actual
124 +'
125 +
126 +test_expect_success "push --prune with negative refspec" '
127 + (
128 + cd two &&
129 + git branch prune/a &&
130 + git branch prune/b &&
131 + git branch prune/c &&
132 + git push ../three refs/heads/prune/* &&
133 + git branch -d prune/a &&
134 + git branch -d prune/b &&
135 + git push --prune ../three refs/heads/prune/* ^refs/heads/prune/b
136 + ) &&
137 + (
138 + cd three &&
139 + test_write_lines b c >expect &&
140 + git for-each-ref --format="%(refname:lstrip=3)" refs/heads/prune/ >actual &&
141 + test_cmp expect actual
142 + )
143 +'
144 +
145 +test_expect_success "push --prune with negative refspec apply to the destination" '
146 + (
147 + cd two &&
148 + git branch ours/a &&
149 + git branch ours/b &&
150 + git branch ours/c &&
151 + git push ../three refs/heads/ours/*:refs/heads/theirs/* &&
152 + git branch -d ours/a &&
153 + git branch -d ours/b &&
154 + git push --prune ../three refs/heads/ours/*:refs/heads/theirs/* ^refs/heads/theirs/b
155 + ) &&
156 + (
157 + cd three &&
158 + test_write_lines b c >expect &&
159 + git for-each-ref --format="%(refname:lstrip=3)" refs/heads/theirs/ >actual &&
160 + test_cmp expect actual
161 + )
162 +'
163 +
164 +test_expect_success "fetch --prune with negative refspec" '
165 + (
166 + cd two &&
167 + git branch fetch/a &&
168 + git branch fetch/b &&
169 + git branch fetch/c
170 + ) &&
171 + (
172 + cd three &&
173 + git fetch ../two/.git refs/heads/fetch/*:refs/heads/copied/*
174 + ) &&
175 + (
176 + cd two &&
177 + git branch -d fetch/a &&
178 + git branch -d fetch/b
179 + ) &&
180 + (
181 + cd three &&
182 + test_write_lines b c >expect &&
183 + git fetch -v ../two/.git --prune refs/heads/fetch/*:refs/heads/copied/* ^refs/heads/fetch/b &&
184 + git for-each-ref --format="%(refname:lstrip=3)" refs/heads/copied/ >actual &&
185 + test_cmp expect actual
186 + )
187 +'
188 +
189 +test_done