global: introduce `USE_THE_REPOSITORY_VARIABLE` macro
Use of the `the_repository` variable is deprecated nowadays, and we
slowly but steadily convert the codebase to not use it anymore. Instead,
callers should be passing down the repository to work on via parameters.
It is hard though to prove that a given code unit does not use this
variable anymore. The most trivial case, merely demonstrating that there
is no direct use of `the_repository`, is already a bit of a pain during
code reviews as the reviewer needs to manually verify claims made by the
patch author. The bigger problem though is that we have many interfaces
that implicitly rely on `the_repository`.
Introduce a new `USE_THE_REPOSITORY_VARIABLE` macro that allows code
units to opt into usage of `the_repository`. The intent of this macro is
to demonstrate that a certain code unit does not use this variable
anymore, and to keep it from new dependencies on it in future changes,
be it explicit or implicit
For now, the macro only guards `the_repository` itself as well as
`the_hash_algo`. There are many more known interfaces where we have an
implicit dependency on `the_repository`, but those are not guarded at
the current point in time. Over time though, we should start to add
guards as required (or even better, just remove them).
Define the macro as required in our code units. As expected, most of our
code still relies on the global variable. Nearly all of our builtins
rely on the variable as there is no way yet to pass `the_repository` to
their entry point. For now, declare the macro in "biultin.h" to keep the
required changes at least a little bit more contained.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-06-14 14:50:23 +08:00
|
|
|
#define USE_THE_REPOSITORY_VARIABLE
|
|
|
|
|
2023-02-24 08:09:24 +08:00
|
|
|
#include "git-compat-util.h"
|
2023-03-21 14:25:53 +08:00
|
|
|
#include "gettext.h"
|
2023-04-23 04:17:20 +08:00
|
|
|
#include "hash.h"
|
2023-02-24 08:09:27 +08:00
|
|
|
#include "hex.h"
|
2020-07-29 04:23:39 +08:00
|
|
|
#include "strvec.h"
|
2018-05-17 06:57:48 +08:00
|
|
|
#include "refs.h"
|
|
|
|
#include "refspec.h"
|
2023-04-23 04:17:26 +08:00
|
|
|
#include "strbuf.h"
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
/*
|
|
|
|
* Parses the provided refspec 'refspec' and populates the refspec_item 'item'.
|
|
|
|
* Returns 1 if successful and 0 if the refspec is invalid.
|
|
|
|
*/
|
|
|
|
static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch)
|
2018-05-17 06:57:48 +08:00
|
|
|
{
|
2018-05-17 06:57:50 +08:00
|
|
|
size_t llen;
|
|
|
|
int is_glob;
|
|
|
|
const char *lhs, *rhs;
|
|
|
|
int flags;
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
is_glob = 0;
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
lhs = refspec;
|
|
|
|
if (*lhs == '+') {
|
|
|
|
item->force = 1;
|
|
|
|
lhs++;
|
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>
2020-10-01 05:25:29 +08:00
|
|
|
} else if (*lhs == '^') {
|
|
|
|
item->negative = 1;
|
|
|
|
lhs++;
|
2018-05-17 06:57:50 +08:00
|
|
|
}
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
rhs = strrchr(lhs, ':');
|
2018-05-17 06:57:48 +08:00
|
|
|
|
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>
2020-10-01 05:25:29 +08:00
|
|
|
/* negative refspecs only have one side */
|
|
|
|
if (item->negative && rhs)
|
|
|
|
return 0;
|
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
/*
|
|
|
|
* Before going on, special case ":" (or "+:") as a refspec
|
|
|
|
* for pushing matching refs.
|
|
|
|
*/
|
|
|
|
if (!fetch && rhs == lhs && rhs[1] == '\0') {
|
|
|
|
item->matching = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
if (rhs) {
|
|
|
|
size_t rlen = strlen(++rhs);
|
|
|
|
is_glob = (1 <= rlen && strchr(rhs, '*'));
|
|
|
|
item->dst = xstrndup(rhs, rlen);
|
2018-06-01 10:33:19 +08:00
|
|
|
} else {
|
|
|
|
item->dst = NULL;
|
2018-05-17 06:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
|
|
|
|
if (1 <= llen && memchr(lhs, '*', llen)) {
|
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>
2020-10-01 05:25:29 +08:00
|
|
|
if ((rhs && !is_glob) || (!rhs && !item->negative && fetch))
|
2018-05-17 06:57:50 +08:00
|
|
|
return 0;
|
|
|
|
is_glob = 1;
|
|
|
|
} else if (rhs && is_glob) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->pattern = is_glob;
|
2020-11-26 08:16:16 +08:00
|
|
|
if (llen == 1 && *lhs == '@')
|
|
|
|
item->src = xstrdup("HEAD");
|
|
|
|
else
|
|
|
|
item->src = xstrndup(lhs, llen);
|
2018-05-17 06:57:50 +08:00
|
|
|
flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 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>
2020-10-01 05:25:29 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
if (fetch) {
|
|
|
|
struct object_id unused;
|
|
|
|
|
|
|
|
/* LHS */
|
|
|
|
if (!*item->src)
|
|
|
|
; /* empty is ok; it means "HEAD" */
|
2019-02-19 08:05:21 +08:00
|
|
|
else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
|
2018-05-17 06:57:50 +08:00
|
|
|
item->exact_sha1 = 1; /* ok */
|
|
|
|
else if (!check_refname_format(item->src, flags))
|
|
|
|
; /* valid looking ref is ok */
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
/* RHS */
|
|
|
|
if (!item->dst)
|
|
|
|
; /* missing is ok; it is the same as empty */
|
|
|
|
else if (!*item->dst)
|
|
|
|
; /* empty is ok; it means "do not store" */
|
|
|
|
else if (!check_refname_format(item->dst, flags))
|
|
|
|
; /* valid looking ref is ok */
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else {
|
2018-05-17 06:57:48 +08:00
|
|
|
/*
|
2018-05-17 06:57:50 +08:00
|
|
|
* LHS
|
|
|
|
* - empty is allowed; it means delete.
|
|
|
|
* - when wildcarded, it must be a valid looking ref.
|
|
|
|
* - otherwise, it must be an extended SHA-1, but
|
|
|
|
* there is no existing way to validate this.
|
2018-05-17 06:57:48 +08:00
|
|
|
*/
|
2018-05-17 06:57:50 +08:00
|
|
|
if (!*item->src)
|
|
|
|
; /* empty is ok */
|
|
|
|
else if (is_glob) {
|
|
|
|
if (check_refname_format(item->src, flags))
|
|
|
|
return 0;
|
2018-05-17 06:57:48 +08:00
|
|
|
}
|
2018-05-17 06:57:50 +08:00
|
|
|
else
|
|
|
|
; /* anything goes, for now */
|
|
|
|
/*
|
|
|
|
* RHS
|
|
|
|
* - missing is allowed, but LHS then must be a
|
|
|
|
* valid looking ref.
|
|
|
|
* - empty is not allowed.
|
|
|
|
* - otherwise it must be a valid looking ref.
|
|
|
|
*/
|
|
|
|
if (!item->dst) {
|
|
|
|
if (check_refname_format(item->src, flags))
|
|
|
|
return 0;
|
|
|
|
} else if (!*item->dst) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (check_refname_format(item->dst, flags))
|
|
|
|
return 0;
|
2018-05-17 06:57:48 +08:00
|
|
|
}
|
2018-05-17 06:57:50 +08:00
|
|
|
}
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-05-17 06:57:50 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2018-05-17 06:57:48 +08:00
|
|
|
|
2018-06-06 03:54:39 +08:00
|
|
|
int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
|
2018-05-17 06:57:51 +08:00
|
|
|
{
|
|
|
|
memset(item, 0, sizeof(*item));
|
2018-06-06 03:54:39 +08:00
|
|
|
return parse_refspec(item, refspec, fetch);
|
|
|
|
}
|
2018-05-17 06:57:51 +08:00
|
|
|
|
2018-06-06 03:54:39 +08:00
|
|
|
void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
|
|
|
|
int fetch)
|
|
|
|
{
|
|
|
|
if (!refspec_item_init(item, refspec, fetch))
|
2018-07-21 15:49:36 +08:00
|
|
|
die(_("invalid refspec '%s'"), refspec);
|
2018-05-17 06:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_item_clear(struct refspec_item *item)
|
|
|
|
{
|
|
|
|
FREE_AND_NULL(item->src);
|
|
|
|
FREE_AND_NULL(item->dst);
|
|
|
|
item->force = 0;
|
|
|
|
item->pattern = 0;
|
|
|
|
item->matching = 0;
|
|
|
|
item->exact_sha1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_init(struct refspec *rs, int fetch)
|
|
|
|
{
|
|
|
|
memset(rs, 0, sizeof(*rs));
|
|
|
|
rs->fetch = fetch;
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:49:30 +08:00
|
|
|
static void refspec_append_nodup(struct refspec *rs, char *refspec)
|
2018-05-17 06:57:51 +08:00
|
|
|
{
|
|
|
|
struct refspec_item item;
|
|
|
|
|
2018-06-06 03:54:38 +08:00
|
|
|
refspec_item_init_or_die(&item, refspec, rs->fetch);
|
2018-05-17 06:57:51 +08:00
|
|
|
|
|
|
|
ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
|
|
|
|
rs->items[rs->nr++] = item;
|
|
|
|
|
|
|
|
ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
|
2020-09-05 22:49:30 +08:00
|
|
|
rs->raw[rs->raw_nr++] = refspec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_append(struct refspec *rs, const char *refspec)
|
|
|
|
{
|
|
|
|
refspec_append_nodup(rs, xstrdup(refspec));
|
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_appendf(struct refspec *rs, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
refspec_append_nodup(rs, xstrvfmt(fmt, ap));
|
|
|
|
va_end(ap);
|
2018-05-17 06:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nr; i++)
|
|
|
|
refspec_append(rs, refspecs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_clear(struct refspec *rs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < rs->nr; i++)
|
|
|
|
refspec_item_clear(&rs->items[i]);
|
|
|
|
|
|
|
|
FREE_AND_NULL(rs->items);
|
|
|
|
rs->alloc = 0;
|
|
|
|
rs->nr = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < rs->raw_nr; i++)
|
|
|
|
free((char *)rs->raw[i]);
|
|
|
|
FREE_AND_NULL(rs->raw);
|
|
|
|
rs->raw_alloc = 0;
|
|
|
|
rs->raw_nr = 0;
|
|
|
|
|
|
|
|
rs->fetch = 0;
|
|
|
|
}
|
2018-05-17 06:57:52 +08:00
|
|
|
|
|
|
|
int valid_fetch_refspec(const char *fetch_refspec_str)
|
|
|
|
{
|
|
|
|
struct refspec_item refspec;
|
2018-06-06 03:54:40 +08:00
|
|
|
int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
|
2018-05-17 06:57:52 +08:00
|
|
|
refspec_item_clear(&refspec);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-17 07:48:21 +08:00
|
|
|
|
2020-10-01 11:46:13 +08:00
|
|
|
int valid_remote_name(const char *name)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct strbuf refspec = STRBUF_INIT;
|
|
|
|
strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
|
|
|
|
result = valid_fetch_refspec(refspec.buf);
|
|
|
|
strbuf_release(&refspec);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:48:21 +08:00
|
|
|
void refspec_ref_prefixes(const struct refspec *rs,
|
2020-07-29 04:25:12 +08:00
|
|
|
struct strvec *ref_prefixes)
|
2018-05-17 07:48:21 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < rs->nr; i++) {
|
|
|
|
const struct refspec_item *item = &rs->items[i];
|
|
|
|
const char *prefix = NULL;
|
|
|
|
|
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>
2020-10-01 05:25:29 +08:00
|
|
|
if (item->exact_sha1 || item->negative)
|
2018-05-31 15:23:39 +08:00
|
|
|
continue;
|
2018-05-17 07:48:21 +08:00
|
|
|
if (rs->fetch == REFSPEC_FETCH)
|
|
|
|
prefix = item->src;
|
|
|
|
else if (item->dst)
|
|
|
|
prefix = item->dst;
|
|
|
|
else if (item->src && !item->exact_sha1)
|
|
|
|
prefix = item->src;
|
|
|
|
|
2020-12-01 08:46:46 +08:00
|
|
|
if (!prefix)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (item->pattern) {
|
|
|
|
const char *glob = strchr(prefix, '*');
|
|
|
|
strvec_pushf(ref_prefixes, "%.*s",
|
|
|
|
(int)(glob - prefix),
|
|
|
|
prefix);
|
|
|
|
} else {
|
|
|
|
expand_ref_prefix(ref_prefixes, prefix);
|
2018-05-17 07:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|