2018-05-17 01:57:48 +03:00
|
|
|
#include "cache.h"
|
2020-07-28 23:23:39 +03:00
|
|
|
#include "strvec.h"
|
2018-05-17 01:57:48 +03:00
|
|
|
#include "refs.h"
|
|
|
|
#include "refspec.h"
|
|
|
|
|
2018-05-17 01:57:49 +03:00
|
|
|
static struct refspec_item s_tag_refspec = {
|
2018-05-17 01:57:48 +03:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"refs/tags/*",
|
|
|
|
"refs/tags/*"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* See TAG_REFSPEC for the string version */
|
2018-05-17 01:57:49 +03:00
|
|
|
const struct refspec_item *tag_refspec = &s_tag_refspec;
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03: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 01:57:48 +03:00
|
|
|
{
|
2018-05-17 01:57:50 +03:00
|
|
|
size_t llen;
|
|
|
|
int is_glob;
|
|
|
|
const char *lhs, *rhs;
|
|
|
|
int flags;
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03:00
|
|
|
is_glob = 0;
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03:00
|
|
|
lhs = refspec;
|
|
|
|
if (*lhs == '+') {
|
|
|
|
item->force = 1;
|
|
|
|
lhs++;
|
|
|
|
}
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03:00
|
|
|
rhs = strrchr(lhs, ':');
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03: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 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03:00
|
|
|
if (rhs) {
|
|
|
|
size_t rlen = strlen(++rhs);
|
|
|
|
is_glob = (1 <= rlen && strchr(rhs, '*'));
|
|
|
|
item->dst = xstrndup(rhs, rlen);
|
2018-06-01 05:33:19 +03:00
|
|
|
} else {
|
|
|
|
item->dst = NULL;
|
2018-05-17 01:57:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
|
|
|
|
if (1 <= llen && memchr(lhs, '*', llen)) {
|
|
|
|
if ((rhs && !is_glob) || (!rhs && fetch))
|
|
|
|
return 0;
|
|
|
|
is_glob = 1;
|
|
|
|
} else if (rhs && is_glob) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->pattern = is_glob;
|
|
|
|
item->src = xstrndup(lhs, llen);
|
|
|
|
flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
struct object_id unused;
|
|
|
|
|
|
|
|
/* LHS */
|
|
|
|
if (!*item->src)
|
|
|
|
; /* empty is ok; it means "HEAD" */
|
2019-02-19 03:05:21 +03:00
|
|
|
else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
|
2018-05-17 01:57:50 +03: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 01:57:48 +03:00
|
|
|
/*
|
2018-05-17 01:57:50 +03: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 01:57:48 +03:00
|
|
|
*/
|
2018-05-17 01:57:50 +03:00
|
|
|
if (!*item->src)
|
|
|
|
; /* empty is ok */
|
|
|
|
else if (is_glob) {
|
|
|
|
if (check_refname_format(item->src, flags))
|
|
|
|
return 0;
|
2018-05-17 01:57:48 +03:00
|
|
|
}
|
2018-05-17 01:57:50 +03: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 01:57:48 +03:00
|
|
|
}
|
2018-05-17 01:57:50 +03:00
|
|
|
}
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-05-17 01:57:50 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2018-05-17 01:57:48 +03:00
|
|
|
|
2018-06-05 22:54:39 +03:00
|
|
|
int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
|
2018-05-17 01:57:51 +03:00
|
|
|
{
|
|
|
|
memset(item, 0, sizeof(*item));
|
2018-06-05 22:54:39 +03:00
|
|
|
return parse_refspec(item, refspec, fetch);
|
|
|
|
}
|
2018-05-17 01:57:51 +03:00
|
|
|
|
2018-06-05 22:54:39 +03: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 10:49:36 +03:00
|
|
|
die(_("invalid refspec '%s'"), refspec);
|
2018-05-17 01:57:51 +03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refspec_append(struct refspec *rs, const char *refspec)
|
|
|
|
{
|
|
|
|
struct refspec_item item;
|
|
|
|
|
2018-06-05 22:54:38 +03:00
|
|
|
refspec_item_init_or_die(&item, refspec, rs->fetch);
|
2018-05-17 01:57:51 +03: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);
|
|
|
|
rs->raw[rs->raw_nr++] = xstrdup(refspec);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 01:57:52 +03:00
|
|
|
|
|
|
|
int valid_fetch_refspec(const char *fetch_refspec_str)
|
|
|
|
{
|
|
|
|
struct refspec_item refspec;
|
2018-06-05 22:54:40 +03:00
|
|
|
int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
|
2018-05-17 01:57:52 +03:00
|
|
|
refspec_item_clear(&refspec);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-17 02:48:21 +03:00
|
|
|
|
|
|
|
void refspec_ref_prefixes(const struct refspec *rs,
|
2020-07-28 23:25:12 +03:00
|
|
|
struct strvec *ref_prefixes)
|
2018-05-17 02:48:21 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < rs->nr; i++) {
|
|
|
|
const struct refspec_item *item = &rs->items[i];
|
|
|
|
const char *prefix = NULL;
|
|
|
|
|
2018-05-31 10:23:39 +03:00
|
|
|
if (item->exact_sha1)
|
|
|
|
continue;
|
2018-05-17 02:48:21 +03: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;
|
|
|
|
|
|
|
|
if (prefix) {
|
|
|
|
if (item->pattern) {
|
|
|
|
const char *glob = strchr(prefix, '*');
|
2020-07-28 23:25:12 +03:00
|
|
|
strvec_pushf(ref_prefixes, "%.*s",
|
strvec: fix indentation in renamed calls
Code which split an argv_array call across multiple lines, like:
argv_array_pushl(&args, "one argument",
"another argument", "and more",
NULL);
was recently mechanically renamed to use strvec, which results in
mis-matched indentation like:
strvec_pushl(&args, "one argument",
"another argument", "and more",
NULL);
Let's fix these up to align the arguments with the opening paren. I did
this manually by sifting through the results of:
git jump grep 'strvec_.*,$'
and liberally applying my editor's auto-format. Most of the changes are
of the form shown above, though I also normalized a few that had
originally used a single-tab indentation (rather than our usual style of
aligning with the open paren). I also rewrapped a couple of obvious
cases (e.g., where previously too-long lines became short enough to fit
on one), but I wasn't aggressive about it. In cases broken to three or
more lines, the grouping of arguments is sometimes meaningful, and it
wasn't worth my time or reviewer time to ponder each case individually.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-07-28 23:26:31 +03:00
|
|
|
(int)(glob - prefix),
|
|
|
|
prefix);
|
2018-05-17 02:48:21 +03:00
|
|
|
} else {
|
|
|
|
expand_ref_prefix(ref_prefixes, prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|