summaryrefslogtreecommitdiff
path: root/refspec.c
diff options
context:
space:
mode:
authorJacob Keller <jacob.keller@gmail.com>2020-09-30 21:25:29 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-09-30 21:52:00 (GMT)
commitc0192df6306d4d9ad77f6015a053925b13155834 (patch)
treebf6240c3b988240c871ee132b0cc9ccdb883ac99 /refspec.c
parent95e7c385393488cb20c29697d8655f94ce83c413 (diff)
downloadgit-c0192df6306d4d9ad77f6015a053925b13155834.zip
git-c0192df6306d4d9ad77f6015a053925b13155834.tar.gz
git-c0192df6306d4d9ad77f6015a053925b13155834.tar.bz2
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>
Diffstat (limited to 'refspec.c')
-rw-r--r--refspec.c34
1 files changed, 32 insertions, 2 deletions
diff --git a/refspec.c b/refspec.c
index f10ef28..6350100 100644
--- a/refspec.c
+++ b/refspec.c
@@ -8,6 +8,7 @@ static struct refspec_item s_tag_refspec = {
1,
0,
0,
+ 0,
"refs/tags/*",
"refs/tags/*"
};
@@ -32,10 +33,17 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
if (*lhs == '+') {
item->force = 1;
lhs++;
+ } else if (*lhs == '^') {
+ item->negative = 1;
+ lhs++;
}
rhs = strrchr(lhs, ':');
+ /* negative refspecs only have one side */
+ if (item->negative && rhs)
+ return 0;
+
/*
* Before going on, special case ":" (or "+:") as a refspec
* for pushing matching refs.
@@ -55,7 +63,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
if (1 <= llen && memchr(lhs, '*', llen)) {
- if ((rhs && !is_glob) || (!rhs && fetch))
+ if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
return 0;
is_glob = 1;
} else if (rhs && is_glob) {
@@ -66,6 +74,28 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
item->src = xstrndup(lhs, llen);
flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
+ if (item->negative) {
+ struct object_id unused;
+
+ /*
+ * Negative refspecs only have a LHS, which indicates a ref
+ * (or pattern of refs) to exclude from other matches. This
+ * can either be a simple ref, or a glob pattern. Exact sha1
+ * match is not currently supported.
+ */
+ if (!*item->src)
+ return 0; /* negative refspecs must not be empty */
+ else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
+ return 0; /* negative refpsecs cannot be exact sha1 */
+ else if (!check_refname_format(item->src, flags))
+ ; /* valid looking ref is ok */
+ else
+ return 0;
+
+ /* the other rules below do not apply to negative refspecs */
+ return 1;
+ }
+
if (fetch) {
struct object_id unused;
@@ -209,7 +239,7 @@ void refspec_ref_prefixes(const struct refspec *rs,
const struct refspec_item *item = &rs->items[i];
const char *prefix = NULL;
- if (item->exact_sha1)
+ if (item->exact_sha1 || item->negative)
continue;
if (rs->fetch == REFSPEC_FETCH)
prefix = item->src;