{"thread":{"id":"54302","subject":"[PATCH v3 0/1] implement negative refspecs","startedAt":"2020-09-25T21:08:42Z","lastAt":"2020-09-30T21:06:33Z","messageCount":7,"participants":["Jacob Keller","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":3,"patchTotal":1},"messages":[{"id":"406398","messageId":"20200925210740.1939450-1-jacob.e.keller@intel.com","threadId":"54302","inReplyTo":null,"subject":"[PATCH v3 0/1] implement negative refspecs","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2020-09-25T21:07:39Z","receivedAt":"2020-09-25T21:08:42Z","isPatch":true,"sender":{"key":"jacob.e.keller@intel.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"From: Jacob Keller <jacob.keller@gmail.com>\n\nThis series introduces support for a new refspec concept, the negative\nrefspec. It is a respin of a series posted at [1].\n\nThe primary motivation for negative refspecs is to allow restricting the\nspace that a pattern patch refspec will match over. Both fetch and push\nsupport pattern refspecs which allow matching refs using a simple glob.\nSince these only use a single '*' which greedily matches any string, many\ntypes of matches cannot be expressed.\n\nNegative refspecs exist to allow more control over what refs get fetched or\npushed when using pattern refspecs. They function in a similar manner as\nnegative pathspecs.\n\nSuppose you want to fetch all remote branches from a repository, except for\none specific branch. For example, a repository who recently renamed its\nprimary branch to \"main\", but has left the older \"master\" name in order to\navoid breaking existing workflows. You wish to only fetch \"main\". You do not\nneed the \"master\" branch name anymore because you've updated your work flow.\nHowever, fetching both main and master can lead to some annoyance with tab\ncompletion.\n\nYou could modify your remote config to explicitly fetch the set of branches\nyou care about. However, this means any new branch added to the remote will\nnot be fetched until you update your config, which requires manual\nintervention.\n\nWith a negative refspec, you can simply fetch all branches *except* for\nrefs/heads/master, i.e.\n\n  git fetch refs/heads/*:refs/remotes/origin/* ^refs/heads/master\n\nNegative refspecs restrict the set of refs that are matched for a given\nfetch or push. For a fetch, this refers to the name of the ref on the\nremote. For a push this refers to the name of the local ref being pushed\nout.\n\nA negative refspec is indicated by prefixing the refspec with a ^. Only\nregular or pattern refspecs are supported. Support for sha1 identification\nof objects is not implemented in this patch. Additionally, negative refspecs\ndo not expand using normal rules and thus should be the fully spelled out\nrefspec name.\n\n[1]: https://lore.kernel.org/git/20200821215247.758978-1-jacob.e.keller@intel.com/\n\nRange Diff since v2\n\n1:  7650f8bdf7fd ! 1:  1ff179f8af9e refspec: add support for negative refspecs\n    @@ Commit message\n         This is similar to how negative pathspecs work.\n     \n         Signed-off-by: Jacob Keller <jacob.keller@gmail.com>\n    -    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n    +\n    + ## Documentation/pull-fetch-param.txt ##\n    +@@ Documentation/pull-fetch-param.txt: The colon can be omitted when <dst> is empty.  <src> is\n    + typically a ref, but it can also be a fully spelled hex object\n    + name.\n    + +\n    ++A <refspec> may contain a `*` in its <src> to indicate a simple pattern\n    ++match. Such a refspec functions like a glob that matches any ref with the\n    ++same prefix. A pattern <refspec> must have a `*` in both the <src> and\n    ++<dst>. It will map refs to the destination by replacing the `*` with the\n    ++contents matched from the source.\n    +++\n    ++If a refspec is prefixed by `^`, it will be interpreted as a negative\n    ++refspec. Rather than specifying which refs to fetch or which local refs to\n    ++update, such a refspec will instead specify refs to exclude. A ref will be\n    ++considered to match if it matches at least one positive refspec, and does\n    ++not match any negative refspec. Negative refspecs can be useful to restrict\n    ++the scope of a pattern refspec so that it will not include specific refs.\n    ++Negative refspecs can themselves be pattern refspecs. However, they may only\n    ++contain a <src> and do not specify a <dst>. Fully spelled out hex object\n    ++names are also not supported.\n    +++\n    + `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;\n    + it requests fetching everything up to the given tag.\n    + +\n     \n      ## builtin/fetch.c ##\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n    @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     +\n      \tref_map = ref_remove_duplicates(ref_map);\n      \n    - \trefname_hash_init(&existing_refs);\n    + \tfor (rm = ref_map; rm; rm = rm->next) {\n     \n      ## refspec.c ##\n     @@ refspec.c: static struct refspec_item s_tag_refspec = {\n    @@ refspec.c: static int parse_refspec(struct refspec_item *item, const char *refsp\n     +\t\t/*\n     +\t\t * Negative refspecs only have a LHS, which indicates a ref\n     +\t\t * (or pattern of refs) to exclude from other matches. This\n    -+\t\t * can either be a simple ref, a glob pattern, or even an\n    -+\t\t * exact sha1 match.\n    ++\t\t * can either be a simple ref, or a glob pattern. Exact sha1\n    ++\t\t * match is not currently supported.\n     +\t\t */\n     +\t\tif (!*item->src)\n     +\t\t\treturn 0; /* negative refspecs must not be empty */\n     +\t\telse if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))\n    -+\t\t\titem->exact_sha1 = 1; /* ok */\n    ++\t\t\treturn 0; /* negative refpsecs cannot be exact sha1 */\n     +\t\telse if (!check_refname_format(item->src, flags))\n     +\t\t\t; /* valid looking ref is ok */\n     +\t\telse\n     +\t\t\treturn 0;\n     +\n    -+\t\t/* other rules for negative refspecs don't apply */\n    ++\t\t/* the other rules below do not apply to negative refspecs */\n     +\t\treturn 1;\n     +\t}\n     +\n    @@ remote.c: static int match_name_with_pattern(const char *key, const char *name,\n     +\treturn ref_map;\n     +}\n     +\n    - static void query_refspecs_multiple(struct refspec *rs,\n    - \t\t\t\t    struct refspec_item *query,\n    - \t\t\t\t    struct string_list *results)\n    - {\n    --\tint i;\n    ++static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)\n    ++{\n     +\tint i, matched_negative = 0;\n    - \tint find_src = !query->src;\n    ++\tint find_src = !query->src;\n     +\tstruct string_list reversed = STRING_LIST_INIT_NODUP;\n     +\tconst char *needle = find_src ? query->dst : query->src;\n    -+\tchar **result = find_src ? &query->src : &query->dst;\n    - \n    - \tif (find_src && !query->dst)\n    - \t\tBUG(\"query_refspecs_multiple: need either src or dst\");\n    - \n    ++\n     +\t/*\n    -+\t * If a ref matches any of the negative refspecs, then we should treat\n    -+\t * it as not matching this query. Note that negative refspecs apply to\n    -+\t * the source but we're checking only the destination. Reverse and\n    -+\t * capture any pattern refspecs in order to see if the source would\n    -+\t * have matched a negative refspec.\n    ++\t * Check whether the queried ref matches any negative refpsec. If so,\n    ++\t * then we should ultimately treat this as not matching the query at\n    ++\t * all.\n    ++\t *\n    ++\t * Note that negative refspecs always match the source, but the query\n    ++\t * item uses the destination. To handle this, we apply pattern\n    ++\t * refspecs in reverse to figure out if the query source matches any\n    ++\t * of the negative refspecs.\n     +\t */\n     +\tfor (i = 0; i < rs->nr; i++) {\n     +\t\tstruct refspec_item *refspec = &rs->items[i];\n    @@ remote.c: static int match_name_with_pattern(const char *key, const char *name,\n     +\n     +\tstring_list_clear(&reversed, 0);\n     +\n    -+\tif (matched_negative)\n    ++\treturn matched_negative;\n    ++}\n    ++\n    + static void query_refspecs_multiple(struct refspec *rs,\n    + \t\t\t\t    struct refspec_item *query,\n    + \t\t\t\t    struct string_list *results)\n    +@@ remote.c: static void query_refspecs_multiple(struct refspec *rs,\n    + \tif (find_src && !query->dst)\n    + \t\tBUG(\"query_refspecs_multiple: need either src or dst\");\n    + \n    ++\tif (query_matches_negative_refspec(rs, query))\n     +\t\treturn;\n     +\n      \tfor (i = 0; i < rs->nr; i++) {\n      \t\tstruct refspec_item *refspec = &rs->items[i];\n      \t\tconst char *key = find_src ? refspec->dst : refspec->src;\n    - \t\tconst char *value = find_src ? refspec->src : refspec->dst;\n    --\t\tconst char *needle = find_src ? query->dst : query->src;\n    --\t\tchar **result = find_src ? &query->src : &query->dst;\n    +@@ remote.c: static void query_refspecs_multiple(struct refspec *rs,\n    + \t\tconst char *needle = find_src ? query->dst : query->src;\n    + \t\tchar **result = find_src ? &query->src : &query->dst;\n      \n     -\t\tif (!refspec->dst)\n     +\t\tif (!refspec->dst || refspec->negative)\n      \t\t\tcontinue;\n      \t\tif (refspec->pattern) {\n      \t\t\tif (match_name_with_pattern(key, needle, value, result))\n    -@@ remote.c: static void query_refspecs_multiple(struct refspec *rs,\n    - \n    - int query_refspecs(struct refspec *rs, struct refspec_item *query)\n    - {\n    --\tint i;\n    -+\tint i, matched_negative = 0;\n    - \tint find_src = !query->src;\n    -+\tstruct string_list reversed = STRING_LIST_INIT_NODUP;\n    - \tconst char *needle = find_src ? query->dst : query->src;\n    - \tchar **result = find_src ? &query->src : &query->dst;\n    - \n    +@@ remote.c: int query_refspecs(struct refspec *rs, struct refspec_item *query)\n      \tif (find_src && !query->dst)\n      \t\tBUG(\"query_refspecs: need either src or dst\");\n      \n    -+\t/*\n    -+\t * If a ref matches any of the negative refspecs, then we should treat\n    -+\t * it as not matching this query. Note that negative refspecs apply to\n    -+\t * the source but we're checking only the destination. Reverse and\n    -+\t * capture any pattern refspecs in order to see if the source would\n    -+\t * have matched a negative refspec.\n    -+\t */\n    -+\tfor (i = 0; i < rs->nr; i++) {\n    -+\t\tstruct refspec_item *refspec = &rs->items[i];\n    -+\t\tchar *expn_name;\n    -+\n    -+\t\tif (refspec->negative)\n    -+\t\t\tcontinue;\n    -+\n    -+\t\t/* Note the reversal of src and dst */\n    -+\t\tif (refspec->pattern) {\n    -+\t\t\tconst char *key = refspec->dst ?: refspec->src;\n    -+\t\t\tconst char *value = refspec->src;\n    -+\n    -+\t\t\tif (match_name_with_pattern(key, needle, value, &expn_name))\n    -+\t\t\t\tstring_list_append_nodup(&reversed, expn_name);\n    -+\t\t} else {\n    -+\t\t\tif (!strcmp(needle, refspec->src))\n    -+\t\t\t\tstring_list_append(&reversed, refspec->src);\n    -+\t\t}\n    -+\t}\n    -+\n    -+\tfor (i = 0; !matched_negative && i < reversed.nr; i++) {\n    -+\t\tif (omit_name_by_refspec(reversed.items[i].string, rs))\n    -+\t\t\tmatched_negative = 1;\n    -+\t}\n    -+\n    -+\tstring_list_clear(&reversed, 0);\n    -+\n    -+\tif (matched_negative)\n    ++\tif (query_matches_negative_refspec(rs, query))\n     +\t\treturn -1;\n     +\n      \tfor (i = 0; i < rs->nr; i++) {\n    @@ t/t5582-fetch-negative-refspec.sh (new)\n     +\t)\n     +'\n     +\n    ++test_expect_success \"fetch with negative sha1 refspec fails\" '\n    ++\techo >file updated by origin yet again &&\n    ++\tgit commit -a -m \"updated by origin yet again\" &&\n    ++\t(\n    ++\t\tcd three &&\n    ++\t\tmaster_in_one=$(cd ../one && git rev-parse refs/heads/master) &&\n    ++\t\ttest_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$master_in_one\n    ++\t)\n    ++'\n    ++\n    ++test_expect_success \"fetch with negative pattern refspec\" '\n    ++\techo >file updated by origin once more &&\n    ++\tgit commit -a -m \"updated by origin once more\" &&\n    ++\t(\n    ++\t\tcd three &&\n    ++\t\talternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&\n    ++\t\techo $alternate_in_one >expect &&\n    ++\t\tgit fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&\n    ++\t\tcut -f -1 .git/FETCH_HEAD >actual &&\n    ++\t\ttest_cmp expect actual\n    ++\t)\n    ++'\n    ++\n    ++test_expect_success \"fetch with negative pattern refspec does not expand prefix\" '\n    ++\techo >file updated by origin another time &&\n    ++\tgit commit -a -m \"updated by origin another time\" &&\n    ++\t(\n    ++\t\tcd three &&\n    ++\t\talternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&\n    ++\t\tmaster_in_one=$(cd ../one && git rev-parse refs/heads/master) &&\n    ++\t\techo $alternate_in_one >expect &&\n    ++\t\techo $master_in_one >>expect &&\n    ++\t\tgit fetch ../one/.git refs/heads/*:refs/remotes/one/* ^master &&\n    ++\t\tcut -f -1 .git/FETCH_HEAD >actual &&\n    ++\t\ttest_cmp expect actual\n    ++\t)\n    ++'\n    ++\n     +test_expect_success \"fetch with negative refspec avoids duplicate conflict\" '\n     +\tcd \"$D\" &&\n     +\t(\n2:  ef767ce8f8ed < -:  ------------ SQUASH???\n\nJacob Keller (1):\n  refspec: add support for negative refspecs\n\n Documentation/pull-fetch-param.txt |  16 +++\n builtin/fetch.c                    |  10 ++\n refspec.c                          |  34 +++++-\n refspec.h                          |  14 ++-\n remote.c                           | 108 ++++++++++++++++-\n remote.h                           |   9 +-\n t/t5582-fetch-negative-refspec.sh  | 189 +++++++++++++++++++++++++++++\n 7 files changed, 367 insertions(+), 13 deletions(-)\n create mode 100755 t/t5582-fetch-negative-refspec.sh\n\n\nbase-commit: e1cfff676549cdcd702cbac105468723ef2722f4\n-- \n2.28.0.497.g54e85e7af1ac\n\n"},{"id":"406399","messageId":"20200925210740.1939450-2-jacob.e.keller@intel.com","threadId":"54302","inReplyTo":"20200925210740.1939450-1-jacob.e.keller@intel.com","subject":"[PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2020-09-25T21:07:40Z","receivedAt":"2020-09-25T21:08:44Z","isPatch":true,"sender":{"key":"jacob.e.keller@intel.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"From: Jacob Keller <jacob.keller@gmail.com>\n\nBoth fetch and push support pattern refspecs which allow fetching or\npushing references that match a specific pattern. Because these patterns\nare globs, they have somewhat limited ability to express more complex\nsituations.\n\nFor example, suppose you wish to fetch all branches from a remote except\nfor a specific one. To allow this, you must setup a set of refspecs\nwhich match only the branches you want. Because refspecs are either\nexplicit name matches, or simple globs, many patterns cannot be\nexpressed.\n\nAdd support for a new type of refspec, referred to as \"negative\"\nrefspecs. These are prefixed with a '^' and mean \"exclude any ref\nmatching this refspec\". They can only have one \"side\" which always\nrefers to the source. During a fetch, this refers to the name of the ref\non the remote. During a push, this refers to the name of the ref on the\nlocal side.\n\nWith negative refspecs, users can express more complex patterns. For\nexample:\n\n git fetch origin refs/heads/*:refs/remotes/origin/* ^refs/heads/dontwant\n\nwill fetch all branches on origin into remotes/origin, but will exclude\nfetching the branch named dontwant.\n\nRefspecs today are commutative, meaning that order doesn't expressly\nmatter. Rather than forcing an implied order, negative refspecs will\nalways be applied last. That is, in order to match, a ref must match at\nleast one positive refspec, and match none of the negative refspecs.\nThis is similar to how negative pathspecs work.\n\nSigned-off-by: Jacob Keller <jacob.keller@gmail.com>\n---\n Documentation/pull-fetch-param.txt |  16 +++\n builtin/fetch.c                    |  10 ++\n refspec.c                          |  34 +++++-\n refspec.h                          |  14 ++-\n remote.c                           | 108 ++++++++++++++++-\n remote.h                           |   9 +-\n t/t5582-fetch-negative-refspec.sh  | 189 +++++++++++++++++++++++++++++\n 7 files changed, 367 insertions(+), 13 deletions(-)\n create mode 100755 t/t5582-fetch-negative-refspec.sh\n\ndiff --git a/Documentation/pull-fetch-param.txt b/Documentation/pull-fetch-param.txt\nindex 95ea84990298..95a7390b2c78 100644\n--- a/Documentation/pull-fetch-param.txt\n+++ b/Documentation/pull-fetch-param.txt\n@@ -30,6 +30,22 @@ The colon can be omitted when <dst> is empty.  <src> is\n typically a ref, but it can also be a fully spelled hex object\n name.\n +\n+A <refspec> may contain a `*` in its <src> to indicate a simple pattern\n+match. Such a refspec functions like a glob that matches any ref with the\n+same prefix. A pattern <refspec> must have a `*` in both the <src> and\n+<dst>. It will map refs to the destination by replacing the `*` with the\n+contents matched from the source.\n++\n+If a refspec is prefixed by `^`, it will be interpreted as a negative\n+refspec. Rather than specifying which refs to fetch or which local refs to\n+update, such a refspec will instead specify refs to exclude. A ref will be\n+considered to match if it matches at least one positive refspec, and does\n+not match any negative refspec. Negative refspecs can be useful to restrict\n+the scope of a pattern refspec so that it will not include specific refs.\n+Negative refspecs can themselves be pattern refspecs. However, they may only\n+contain a <src> and do not specify a <dst>. Fully spelled out hex object\n+names are also not supported.\n++\n `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;\n it requests fetching everything up to the given tag.\n +\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 1b00c2bee90b..e95bf8877ddd 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -537,6 +537,16 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\ttail = &rm->next;\n \t}\n \n+\t/*\n+\t * apply negative refspecs first, before we remove duplicates. This is\n+\t * necessary as negative refspecs might remove an otherwise conflicting\n+\t * duplicate.\n+\t */\n+\tif (rs->nr)\n+\t\tref_map = apply_negative_refspecs(ref_map, rs);\n+\telse\n+\t\tref_map = apply_negative_refspecs(ref_map, &remote->fetch);\n+\n \tref_map = ref_remove_duplicates(ref_map);\n \n \tfor (rm = ref_map; rm; rm = rm->next) {\ndiff --git a/refspec.c b/refspec.c\nindex 8d0affc34a6b..8af357a0a35d 100644\n--- a/refspec.c\n+++ b/refspec.c\n@@ -8,6 +8,7 @@ static struct refspec_item s_tag_refspec = {\n \t1,\n \t0,\n \t0,\n+\t0,\n \t\"refs/tags/*\",\n \t\"refs/tags/*\"\n };\n@@ -32,10 +33,17 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet\n \tif (*lhs == '+') {\n \t\titem->force = 1;\n \t\tlhs++;\n+\t} else if (*lhs == '^') {\n+\t\titem->negative = 1;\n+\t\tlhs++;\n \t}\n \n \trhs = strrchr(lhs, ':');\n \n+\t/* negative refspecs only have one side */\n+\tif (item->negative && rhs)\n+\t\treturn 0;\n+\n \t/*\n \t * Before going on, special case \":\" (or \"+:\") as a refspec\n \t * for pushing matching refs.\n@@ -55,7 +63,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet\n \n \tllen = (rhs ? (rhs - lhs - 1) : strlen(lhs));\n \tif (1 <= llen && memchr(lhs, '*', llen)) {\n-\t\tif ((rhs && !is_glob) || (!rhs && fetch))\n+\t\tif ((rhs && !is_glob) || (!rhs && !item->negative && fetch))\n \t\t\treturn 0;\n \t\tis_glob = 1;\n \t} else if (rhs && is_glob) {\n@@ -66,6 +74,28 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet\n \titem->src = xstrndup(lhs, llen);\n \tflags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);\n \n+\tif (item->negative) {\n+\t\tstruct object_id unused;\n+\n+\t\t/*\n+\t\t * Negative refspecs only have a LHS, which indicates a ref\n+\t\t * (or pattern of refs) to exclude from other matches. This\n+\t\t * can either be a simple ref, or a glob pattern. Exact sha1\n+\t\t * match is not currently supported.\n+\t\t */\n+\t\tif (!*item->src)\n+\t\t\treturn 0; /* negative refspecs must not be empty */\n+\t\telse if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))\n+\t\t\treturn 0; /* negative refpsecs cannot be exact sha1 */\n+\t\telse if (!check_refname_format(item->src, flags))\n+\t\t\t; /* valid looking ref is ok */\n+\t\telse\n+\t\t\treturn 0;\n+\n+\t\t/* the other rules below do not apply to negative refspecs */\n+\t\treturn 1;\n+\t}\n+\n \tif (fetch) {\n \t\tstruct object_id unused;\n \n@@ -223,7 +253,7 @@ void refspec_ref_prefixes(const struct refspec *rs,\n \t\tconst struct refspec_item *item = &rs->items[i];\n \t\tconst char *prefix = NULL;\n \n-\t\tif (item->exact_sha1)\n+\t\tif (item->exact_sha1 || item->negative)\n \t\t\tcontinue;\n \t\tif (rs->fetch == REFSPEC_FETCH)\n \t\t\tprefix = item->src;\ndiff --git a/refspec.h b/refspec.h\nindex 7569248d11b1..955183239923 100644\n--- a/refspec.h\n+++ b/refspec.h\n@@ -5,12 +5,13 @@\n extern const struct refspec_item *tag_refspec;\n \n /**\n- * A struct refspec_item holds the parsed interpretation of a refspec.  If it will\n- * force updates (starts with a '+'), force is true.  If it is a pattern\n- * (sides end with '*') pattern is true.  src and dest are the two sides\n- * (including '*' characters if present); if there is only one side, it is src,\n- * and dst is NULL; if sides exist but are empty (i.e., the refspec either\n- * starts or ends with ':'), the corresponding side is \"\".\n+ * A struct refspec_item holds the parsed interpretation of a refspec.  If it\n+ * will force updates (starts with a '+'), force is true.  If it is a pattern\n+ * (sides end with '*') pattern is true.  If it is a negative refspec, (starts\n+ * with '^'), negative is true.  src and dest are the two sides (including '*'\n+ * characters if present); if there is only one side, it is src, and dst is\n+ * NULL; if sides exist but are empty (i.e., the refspec either starts or ends\n+ * with ':'), the corresponding side is \"\".\n  *\n  * remote_find_tracking(), given a remote and a struct refspec_item with either src\n  * or dst filled out, will fill out the other such that the result is in the\n@@ -22,6 +23,7 @@ struct refspec_item {\n \tunsigned pattern : 1;\n \tunsigned matching : 1;\n \tunsigned exact_sha1 : 1;\n+\tunsigned negative : 1;\n \n \tchar *src;\n \tchar *dst;\ndiff --git a/remote.c b/remote.c\nindex eafc14cbe759..26a127142344 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -682,6 +682,91 @@ static int match_name_with_pattern(const char *key, const char *name,\n \treturn ret;\n }\n \n+static int refspec_match(const struct refspec_item *refspec,\n+\t\t\t const char *name)\n+{\n+\tif (refspec->pattern)\n+\t\treturn match_name_with_pattern(refspec->src, name, NULL, NULL);\n+\n+\treturn !strcmp(refspec->src, name);\n+}\n+\n+static int omit_name_by_refspec(const char *name, struct refspec *rs)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < rs->nr; i++) {\n+\t\tif (rs->items[i].negative && refspec_match(&rs->items[i], name))\n+\t\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)\n+{\n+\tstruct ref **tail;\n+\n+\tfor (tail = &ref_map; *tail; ) {\n+\t\tstruct ref *ref = *tail;\n+\n+\t\tif (omit_name_by_refspec(ref->name, rs)) {\n+\t\t\t*tail = ref->next;\n+\t\t\tfree(ref->peer_ref);\n+\t\t\tfree(ref);\n+\t\t} else\n+\t\t\ttail = &ref->next;\n+\t}\n+\n+\treturn ref_map;\n+}\n+\n+static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)\n+{\n+\tint i, matched_negative = 0;\n+\tint find_src = !query->src;\n+\tstruct string_list reversed = STRING_LIST_INIT_NODUP;\n+\tconst char *needle = find_src ? query->dst : query->src;\n+\n+\t/*\n+\t * Check whether the queried ref matches any negative refpsec. If so,\n+\t * then we should ultimately treat this as not matching the query at\n+\t * all.\n+\t *\n+\t * Note that negative refspecs always match the source, but the query\n+\t * item uses the destination. To handle this, we apply pattern\n+\t * refspecs in reverse to figure out if the query source matches any\n+\t * of the negative refspecs.\n+\t */\n+\tfor (i = 0; i < rs->nr; i++) {\n+\t\tstruct refspec_item *refspec = &rs->items[i];\n+\t\tchar *expn_name;\n+\n+\t\tif (refspec->negative)\n+\t\t\tcontinue;\n+\n+\t\t/* Note the reversal of src and dst */\n+\t\tif (refspec->pattern) {\n+\t\t\tconst char *key = refspec->dst ?: refspec->src;\n+\t\t\tconst char *value = refspec->src;\n+\n+\t\t\tif (match_name_with_pattern(key, needle, value, &expn_name))\n+\t\t\t\tstring_list_append_nodup(&reversed, expn_name);\n+\t\t} else {\n+\t\t\tif (!strcmp(needle, refspec->src))\n+\t\t\t\tstring_list_append(&reversed, refspec->src);\n+\t\t}\n+\t}\n+\n+\tfor (i = 0; !matched_negative && i < reversed.nr; i++) {\n+\t\tif (omit_name_by_refspec(reversed.items[i].string, rs))\n+\t\t\tmatched_negative = 1;\n+\t}\n+\n+\tstring_list_clear(&reversed, 0);\n+\n+\treturn matched_negative;\n+}\n+\n static void query_refspecs_multiple(struct refspec *rs,\n \t\t\t\t    struct refspec_item *query,\n \t\t\t\t    struct string_list *results)\n@@ -692,6 +777,9 @@ static void query_refspecs_multiple(struct refspec *rs,\n \tif (find_src && !query->dst)\n \t\tBUG(\"query_refspecs_multiple: need either src or dst\");\n \n+\tif (query_matches_negative_refspec(rs, query))\n+\t\treturn;\n+\n \tfor (i = 0; i < rs->nr; i++) {\n \t\tstruct refspec_item *refspec = &rs->items[i];\n \t\tconst char *key = find_src ? refspec->dst : refspec->src;\n@@ -699,7 +787,7 @@ static void query_refspecs_multiple(struct refspec *rs,\n \t\tconst char *needle = find_src ? query->dst : query->src;\n \t\tchar **result = find_src ? &query->src : &query->dst;\n \n-\t\tif (!refspec->dst)\n+\t\tif (!refspec->dst || refspec->negative)\n \t\t\tcontinue;\n \t\tif (refspec->pattern) {\n \t\t\tif (match_name_with_pattern(key, needle, value, result))\n@@ -720,12 +808,15 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query)\n \tif (find_src && !query->dst)\n \t\tBUG(\"query_refspecs: need either src or dst\");\n \n+\tif (query_matches_negative_refspec(rs, query))\n+\t\treturn -1;\n+\n \tfor (i = 0; i < rs->nr; i++) {\n \t\tstruct refspec_item *refspec = &rs->items[i];\n \t\tconst char *key = find_src ? refspec->dst : refspec->src;\n \t\tconst char *value = find_src ? refspec->src : refspec->dst;\n \n-\t\tif (!refspec->dst)\n+\t\tif (!refspec->dst || refspec->negative)\n \t\t\tcontinue;\n \t\tif (refspec->pattern) {\n \t\t\tif (match_name_with_pattern(key, needle, value, result)) {\n@@ -1054,7 +1145,7 @@ static int match_explicit(struct ref *src, struct ref *dst,\n \tconst char *dst_value = rs->dst;\n \tchar *dst_guess;\n \n-\tif (rs->pattern || rs->matching)\n+\tif (rs->pattern || rs->matching || rs->negative)\n \t\treturn 0;\n \n \tmatched_src = matched_dst = NULL;\n@@ -1130,6 +1221,10 @@ static char *get_ref_match(const struct refspec *rs, const struct ref *ref,\n \tint matching_refs = -1;\n \tfor (i = 0; i < rs->nr; i++) {\n \t\tconst struct refspec_item *item = &rs->items[i];\n+\n+\t\tif (item->negative)\n+\t\t\tcontinue;\n+\n \t\tif (item->matching &&\n \t\t    (matching_refs == -1 || item->force)) {\n \t\t\tmatching_refs = i;\n@@ -1335,7 +1430,7 @@ int check_push_refs(struct ref *src, struct refspec *rs)\n \tfor (i = 0; i < rs->nr; i++) {\n \t\tstruct refspec_item *item = &rs->items[i];\n \n-\t\tif (item->pattern || item->matching)\n+\t\tif (item->pattern || item->matching || item->negative)\n \t\t\tcontinue;\n \n \t\tret |= match_explicit_lhs(src, item, NULL, NULL);\n@@ -1437,6 +1532,8 @@ int match_push_refs(struct ref *src, struct ref **dst,\n \t\tstring_list_clear(&src_ref_index, 0);\n \t}\n \n+\t*dst = apply_negative_refspecs(*dst, rs);\n+\n \tif (errs)\n \t\treturn -1;\n \treturn 0;\n@@ -1806,6 +1903,9 @@ int get_fetch_map(const struct ref *remote_refs,\n {\n \tstruct ref *ref_map, **rmp;\n \n+\tif (refspec->negative)\n+\t\treturn 0;\n+\n \tif (refspec->pattern) {\n \t\tref_map = get_expanded_map(remote_refs, refspec);\n \t} else {\ndiff --git a/remote.h b/remote.h\nindex 5e3ea5a26deb..104e75e0f74d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -193,6 +193,12 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);\n  */\n struct ref *ref_remove_duplicates(struct ref *ref_map);\n \n+/*\n+ * Remove all entries in the input list which match any negative refspec in\n+ * the refspec list.\n+ */\n+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);\n+\n int query_refspecs(struct refspec *rs, struct refspec_item *query);\n char *apply_refspecs(struct refspec *rs, const char *name);\n \n@@ -205,7 +211,8 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,\n /*\n  * Given a list of the remote refs and the specification of things to\n  * fetch, makes a (separate) list of the refs to fetch and the local\n- * refs to store into.\n+ * refs to store into. Note that negative refspecs are ignored here, and\n+ * should be handled separately.\n  *\n  * *tail is the pointer to the tail pointer of the list of results\n  * beforehand, and will be set to the tail pointer of the list of\ndiff --git a/t/t5582-fetch-negative-refspec.sh b/t/t5582-fetch-negative-refspec.sh\nnew file mode 100755\nindex 000000000000..8c61e28fec85\n--- /dev/null\n+++ b/t/t5582-fetch-negative-refspec.sh\n@@ -0,0 +1,189 @@\n+#!/bin/sh\n+# Copyright (c) 2020, Jacob Keller.\n+\n+test_description='\"git fetch\" with negative refspecs.\n+\n+'\n+\n+. ./test-lib.sh\n+\n+test_expect_success setup '\n+\techo >file original &&\n+\tgit add file &&\n+\tgit commit -a -m original\n+'\n+\n+test_expect_success \"clone and setup child repos\" '\n+\tgit clone . one &&\n+\t(\n+\t\tcd one &&\n+\t\techo >file updated by one &&\n+\t\tgit commit -a -m \"updated by one\" &&\n+\t\tgit switch -c alternate &&\n+\t\techo >file updated again by one &&\n+\t\tgit commit -a -m \"updated by one again\" &&\n+\t\tgit switch master\n+\t) &&\n+\tgit clone . two &&\n+\t(\n+\t\tcd two &&\n+\t\tgit config branch.master.remote one &&\n+\t\tgit config remote.one.url ../one/.git/ &&\n+\t\tgit config remote.one.fetch +refs/heads/*:refs/remotes/one/* &&\n+\t\tgit config --add remote.one.fetch ^refs/heads/alternate\n+\t) &&\n+\tgit clone . three\n+'\n+\n+test_expect_success \"fetch one\" '\n+\techo >file updated by origin &&\n+\tgit commit -a -m \"updated by origin\" &&\n+\t(\n+\t\tcd two &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/one/alternate &&\n+\t\tgit fetch one &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/one/alternate &&\n+\t\tgit rev-parse --verify refs/remotes/one/master &&\n+\t\tmine=$(git rev-parse refs/remotes/one/master) &&\n+\t\this=$(cd ../one && git rev-parse refs/heads/master) &&\n+\t\ttest \"z$mine\" = \"z$his\"\n+\t)\n+'\n+\n+test_expect_success \"fetch with negative refspec on commandline\" '\n+\techo >file updated by origin again &&\n+\tgit commit -a -m \"updated by origin again\" &&\n+\t(\n+\t\tcd three &&\n+\t\talternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&\n+\t\techo $alternate_in_one >expect &&\n+\t\tgit fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/master &&\n+\t\tcut -f -1 .git/FETCH_HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success \"fetch with negative sha1 refspec fails\" '\n+\techo >file updated by origin yet again &&\n+\tgit commit -a -m \"updated by origin yet again\" &&\n+\t(\n+\t\tcd three &&\n+\t\tmaster_in_one=$(cd ../one && git rev-parse refs/heads/master) &&\n+\t\ttest_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$master_in_one\n+\t)\n+'\n+\n+test_expect_success \"fetch with negative pattern refspec\" '\n+\techo >file updated by origin once more &&\n+\tgit commit -a -m \"updated by origin once more\" &&\n+\t(\n+\t\tcd three &&\n+\t\talternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&\n+\t\techo $alternate_in_one >expect &&\n+\t\tgit fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&\n+\t\tcut -f -1 .git/FETCH_HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success \"fetch with negative pattern refspec does not expand prefix\" '\n+\techo >file updated by origin another time &&\n+\tgit commit -a -m \"updated by origin another time\" &&\n+\t(\n+\t\tcd three &&\n+\t\talternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&\n+\t\tmaster_in_one=$(cd ../one && git rev-parse refs/heads/master) &&\n+\t\techo $alternate_in_one >expect &&\n+\t\techo $master_in_one >>expect &&\n+\t\tgit fetch ../one/.git refs/heads/*:refs/remotes/one/* ^master &&\n+\t\tcut -f -1 .git/FETCH_HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success \"fetch with negative refspec avoids duplicate conflict\" '\n+\tcd \"$D\" &&\n+\t(\n+\t\tcd one &&\n+\t\tgit branch dups/a &&\n+\t\tgit branch dups/b &&\n+\t\tgit branch dups/c &&\n+\t\tgit branch other/a &&\n+\t\tgit rev-parse --verify refs/heads/other/a >../expect &&\n+\t\tgit rev-parse --verify refs/heads/dups/b >>../expect &&\n+\t\tgit rev-parse --verify refs/heads/dups/c >>../expect\n+\t) &&\n+\t(\n+\t\tcd three &&\n+\t\tgit fetch ../one/.git ^refs/heads/dups/a refs/heads/dups/*:refs/dups/* refs/heads/other/a:refs/dups/a &&\n+\t\tgit rev-parse --verify refs/dups/a >../actual &&\n+\t\tgit rev-parse --verify refs/dups/b >>../actual &&\n+\t\tgit rev-parse --verify refs/dups/c >>../actual\n+\t) &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success \"push --prune with negative refspec\" '\n+\t(\n+\t\tcd two &&\n+\t\tgit branch prune/a &&\n+\t\tgit branch prune/b &&\n+\t\tgit branch prune/c &&\n+\t\tgit push ../three refs/heads/prune/* &&\n+\t\tgit branch -d prune/a &&\n+\t\tgit branch -d prune/b &&\n+\t\tgit push --prune ../three refs/heads/prune/* ^refs/heads/prune/b\n+\t) &&\n+\t(\n+\t\tcd three &&\n+\t\ttest_write_lines b c >expect &&\n+\t\tgit for-each-ref --format=\"%(refname:lstrip=3)\" refs/heads/prune/ >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success \"push --prune with negative refspec apply to the destination\" '\n+\t(\n+\t\tcd two &&\n+\t\tgit branch ours/a &&\n+\t\tgit branch ours/b &&\n+\t\tgit branch ours/c &&\n+\t\tgit push ../three refs/heads/ours/*:refs/heads/theirs/* &&\n+\t\tgit branch -d ours/a &&\n+\t\tgit branch -d ours/b &&\n+\t\tgit push --prune ../three refs/heads/ours/*:refs/heads/theirs/* ^refs/heads/theirs/b\n+\t) &&\n+\t(\n+\t\tcd three &&\n+\t\ttest_write_lines b c >expect &&\n+\t\tgit for-each-ref --format=\"%(refname:lstrip=3)\" refs/heads/theirs/ >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success \"fetch --prune with negative refspec\" '\n+\t(\n+\t\tcd two &&\n+\t\tgit branch fetch/a &&\n+\t\tgit branch fetch/b &&\n+\t\tgit branch fetch/c\n+\t) &&\n+\t(\n+\t\tcd three &&\n+\t\tgit fetch ../two/.git refs/heads/fetch/*:refs/heads/copied/*\n+\t) &&\n+\t(\n+\t\tcd two &&\n+\t\tgit branch -d fetch/a &&\n+\t\tgit branch -d fetch/b\n+\t) &&\n+\t(\n+\t\tcd three &&\n+\t\ttest_write_lines b c >expect &&\n+\t\tgit fetch -v ../two/.git --prune refs/heads/fetch/*:refs/heads/copied/* ^refs/heads/fetch/b &&\n+\t\tgit for-each-ref --format=\"%(refname:lstrip=3)\" refs/heads/copied/ >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_done\n-- \n2.28.0.497.g54e85e7af1ac\n\n"},{"id":"406644","messageId":"xmqq1riki8ji.fsf@gitster.c.googlers.com","threadId":"54302","inReplyTo":"20200925210740.1939450-2-jacob.e.keller@intel.com","subject":"Re: [PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-09-29T21:21:05Z","receivedAt":"2020-09-29T21:21:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jacob Keller <jacob.e.keller@intel.com> writes:\n\n> Refspecs today are commutative, meaning that order doesn't expressly\n> matter. Rather than forcing an implied order, negative refspecs will\n> always be applied last. That is, in order to match, a ref must match at\n> least one positive refspec, and match none of the negative refspecs.\n> This is similar to how negative pathspecs work.\n\nLooked sensible.  Let's move this forward.\n\n"},{"id":"406677","messageId":"nycvar.QRO.7.76.6.2009301428580.50@tvgsbejvaqbjf.bet","threadId":"54302","inReplyTo":"20200925210740.1939450-2-jacob.e.keller@intel.com","subject":"Re: [PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2020-09-30T12:36:00Z","receivedAt":"2020-09-30T12:36:13Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Jake,\n\nOn Fri, 25 Sep 2020, Jacob Keller wrote:\n\n> diff --git a/remote.c b/remote.c\n> index eafc14cbe759..26a127142344 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -682,6 +682,91 @@ static int match_name_with_pattern(const char *key, const char *name,\n>  \treturn ret;\n>  }\n>\n> +static int refspec_match(const struct refspec_item *refspec,\n> +\t\t\t const char *name)\n> +{\n> +\tif (refspec->pattern)\n> +\t\treturn match_name_with_pattern(refspec->src, name, NULL, NULL);\n> +\n> +\treturn !strcmp(refspec->src, name);\n> +}\n> +\n> +static int omit_name_by_refspec(const char *name, struct refspec *rs)\n> +{\n> +\tint i;\n> +\n> +\tfor (i = 0; i < rs->nr; i++) {\n> +\t\tif (rs->items[i].negative && refspec_match(&rs->items[i], name))\n> +\t\t\treturn 1;\n> +\t}\n> +\treturn 0;\n> +}\n> +\n> +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)\n> +{\n> +\tstruct ref **tail;\n> +\n> +\tfor (tail = &ref_map; *tail; ) {\n> +\t\tstruct ref *ref = *tail;\n> +\n> +\t\tif (omit_name_by_refspec(ref->name, rs)) {\n> +\t\t\t*tail = ref->next;\n> +\t\t\tfree(ref->peer_ref);\n> +\t\t\tfree(ref);\n> +\t\t} else\n> +\t\t\ttail = &ref->next;\n> +\t}\n> +\n> +\treturn ref_map;\n> +}\n> +\n> +static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)\n> +{\n> +\tint i, matched_negative = 0;\n> +\tint find_src = !query->src;\n> +\tstruct string_list reversed = STRING_LIST_INIT_NODUP;\n> +\tconst char *needle = find_src ? query->dst : query->src;\n> +\n> +\t/*\n> +\t * Check whether the queried ref matches any negative refpsec. If so,\n> +\t * then we should ultimately treat this as not matching the query at\n> +\t * all.\n> +\t *\n> +\t * Note that negative refspecs always match the source, but the query\n> +\t * item uses the destination. To handle this, we apply pattern\n> +\t * refspecs in reverse to figure out if the query source matches any\n> +\t * of the negative refspecs.\n> +\t */\n> +\tfor (i = 0; i < rs->nr; i++) {\n> +\t\tstruct refspec_item *refspec = &rs->items[i];\n> +\t\tchar *expn_name;\n> +\n> +\t\tif (refspec->negative)\n> +\t\t\tcontinue;\n> +\n> +\t\t/* Note the reversal of src and dst */\n> +\t\tif (refspec->pattern) {\n> +\t\t\tconst char *key = refspec->dst ?: refspec->src;\n\nWould you mind fixing this? This keeps getting reverted...\n\n-- snipsnap --\nFrom 4aea2a58a9f6bb1cbbc7a03db63a1465f9a801da Mon Sep 17 00:00:00 2001\nFrom: Johannes Schindelin <johannes.schindelin@gmx.de>\nDate: Sun, 23 Aug 2020 22:27:17 +0200\nSubject: [PATCH] fixup??? refspec: add support for negative refspecs\n\nThe `?:` operator is not supported e.g. by Visual C. Let's not use it.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n remote.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/remote.c b/remote.c\nindex f7d20c059b3e..1659535c1a8d 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -724,7 +724,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite\n\n \t\t/* Note the reversal of src and dst */\n \t\tif (refspec->pattern) {\n-\t\t\tconst char *key = refspec->dst ?: refspec->src;\n+\t\t\tconst char *key = refspec->dst ? refspec->dst : refspec->src;\n \t\t\tconst char *value = refspec->src;\n\n \t\t\tif (match_name_with_pattern(key, needle, value, &expn_name))\n--\n2.28.0.windows.1.52.gbcabfe850c5e\n\n"},{"id":"406708","messageId":"CA+P7+xpDoqspwEG_PABvP33FL-+mo6zXvWxf5FKOiSrydXjBKQ@mail.gmail.com","threadId":"54302","inReplyTo":"nycvar.QRO.7.76.6.2009301428580.50@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2020-09-30T20:49:53Z","receivedAt":"2020-09-30T20:50:06Z","isPatch":true,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"On Wed, Sep 30, 2020 at 5:36 AM Johannes Schindelin\n<Johannes.Schindelin@gmx.de> wrote:\n>\n> Hi Jake,\n>\n> On Fri, 25 Sep 2020, Jacob Keller wrote:\n>\n> > diff --git a/remote.c b/remote.c\n> > index eafc14cbe759..26a127142344 100644\n> > --- a/remote.c\n> > +++ b/remote.c\n> > @@ -682,6 +682,91 @@ static int match_name_with_pattern(const char *key, const char *name,\n> >       return ret;\n> >  }\n> >\n> > +static int refspec_match(const struct refspec_item *refspec,\n> > +                      const char *name)\n> > +{\n> > +     if (refspec->pattern)\n> > +             return match_name_with_pattern(refspec->src, name, NULL, NULL);\n> > +\n> > +     return !strcmp(refspec->src, name);\n> > +}\n> > +\n> > +static int omit_name_by_refspec(const char *name, struct refspec *rs)\n> > +{\n> > +     int i;\n> > +\n> > +     for (i = 0; i < rs->nr; i++) {\n> > +             if (rs->items[i].negative && refspec_match(&rs->items[i], name))\n> > +                     return 1;\n> > +     }\n> > +     return 0;\n> > +}\n> > +\n> > +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)\n> > +{\n> > +     struct ref **tail;\n> > +\n> > +     for (tail = &ref_map; *tail; ) {\n> > +             struct ref *ref = *tail;\n> > +\n> > +             if (omit_name_by_refspec(ref->name, rs)) {\n> > +                     *tail = ref->next;\n> > +                     free(ref->peer_ref);\n> > +                     free(ref);\n> > +             } else\n> > +                     tail = &ref->next;\n> > +     }\n> > +\n> > +     return ref_map;\n> > +}\n> > +\n> > +static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)\n> > +{\n> > +     int i, matched_negative = 0;\n> > +     int find_src = !query->src;\n> > +     struct string_list reversed = STRING_LIST_INIT_NODUP;\n> > +     const char *needle = find_src ? query->dst : query->src;\n> > +\n> > +     /*\n> > +      * Check whether the queried ref matches any negative refpsec. If so,\n> > +      * then we should ultimately treat this as not matching the query at\n> > +      * all.\n> > +      *\n> > +      * Note that negative refspecs always match the source, but the query\n> > +      * item uses the destination. To handle this, we apply pattern\n> > +      * refspecs in reverse to figure out if the query source matches any\n> > +      * of the negative refspecs.\n> > +      */\n> > +     for (i = 0; i < rs->nr; i++) {\n> > +             struct refspec_item *refspec = &rs->items[i];\n> > +             char *expn_name;\n> > +\n> > +             if (refspec->negative)\n> > +                     continue;\n> > +\n> > +             /* Note the reversal of src and dst */\n> > +             if (refspec->pattern) {\n> > +                     const char *key = refspec->dst ?: refspec->src;\n>\n> Would you mind fixing this? This keeps getting reverted...\n>\n\nAck.... I swore I fixed this... I'm not sure what happened on my end here.\n"},{"id":"406711","messageId":"xmqqv9fvf018.fsf@gitster.c.googlers.com","threadId":"54302","inReplyTo":"CA+P7+xpDoqspwEG_PABvP33FL-+mo6zXvWxf5FKOiSrydXjBKQ@mail.gmail.com","subject":"Re: [PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-09-30T21:05:23Z","receivedAt":"2020-09-30T21:05:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jacob Keller <jacob.keller@gmail.com> writes:\n\n> Ack.... I swore I fixed this... I'm not sure what happened on my end here.\n\nI do recall adding a SQUASH??? commit on top when I queued the last\nround, but I didn't do so for this round (and the patches changed\nsufficiently enough that the old squash commit no longer applies).\n\n"},{"id":"406712","messageId":"CA+P7+xpGnw6Ht3yVM51HzMFg_xQp5QfJWB6hoOyAD3h=UUiWvg@mail.gmail.com","threadId":"54302","inReplyTo":"xmqqv9fvf018.fsf@gitster.c.googlers.com","subject":"Re: [PATCH v3 1/1] refspec: add support for negative refspecs","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2020-09-30T21:06:18Z","receivedAt":"2020-09-30T21:06:33Z","isPatch":true,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"On Wed, Sep 30, 2020 at 2:05 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Jacob Keller <jacob.keller@gmail.com> writes:\n>\n> > Ack.... I swore I fixed this... I'm not sure what happened on my end here.\n>\n> I do recall adding a SQUASH??? commit on top when I queued the last\n> round, but I didn't do so for this round (and the patches changed\n> sufficiently enough that the old squash commit no longer applies).\n>\n\nLet me send a v4 with this change built in. I remember making it, but\nI suspect in the other refactors I accidentally resurrected the\nversion without it :(\n"}]}