sha1-name: replace unsigned int with option struct

In preparation for a future patch adding a boolean parameter to
repo_interpret_branch_name(), which might be easily confused with an
existing unsigned int parameter, refactor repo_interpret_branch_name()
to take an option struct instead of the unsigned int parameter.

The static function interpret_branch_mark() is also updated to take the
option struct in preparation for that future patch, since it will also
make use of the to-be-introduced boolean parameter.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Jonathan Tan 2020-09-01 15:28:07 -07:00 committed by Junio C Hamano
parent 47ae905ffb
commit a4f66a7876
4 changed files with 34 additions and 21 deletions

20
cache.h
View File

@ -1555,21 +1555,25 @@ int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
* *
* If the input was ok but there are not N branch switches in the * If the input was ok but there are not N branch switches in the
* reflog, it returns 0. * reflog, it returns 0.
*
* If "allowed" is non-zero, it is a treated as a bitfield of allowable
* expansions: local branches ("refs/heads/"), remote branches
* ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
* allowed, even ones to refs outside of those namespaces.
*/ */
#define INTERPRET_BRANCH_LOCAL (1<<0) #define INTERPRET_BRANCH_LOCAL (1<<0)
#define INTERPRET_BRANCH_REMOTE (1<<1) #define INTERPRET_BRANCH_REMOTE (1<<1)
#define INTERPRET_BRANCH_HEAD (1<<2) #define INTERPRET_BRANCH_HEAD (1<<2)
struct interpret_branch_name_options {
/*
* If "allowed" is non-zero, it is a treated as a bitfield of allowable
* expansions: local branches ("refs/heads/"), remote branches
* ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
* allowed, even ones to refs outside of those namespaces.
*/
unsigned allowed;
};
int repo_interpret_branch_name(struct repository *r, int repo_interpret_branch_name(struct repository *r,
const char *str, int len, const char *str, int len,
struct strbuf *buf, struct strbuf *buf,
unsigned allowed); const struct interpret_branch_name_options *options);
#define interpret_branch_name(str, len, buf, allowed) \ #define interpret_branch_name(str, len, buf, options) \
repo_interpret_branch_name(the_repository, str, len, buf, allowed) repo_interpret_branch_name(the_repository, str, len, buf, options)


int validate_headref(const char *ref); int validate_headref(const char *ref);



3
refs.c
View File

@ -601,7 +601,8 @@ static char *substitute_branch_name(struct repository *r,
const char **string, int *len) const char **string, int *len)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int ret = repo_interpret_branch_name(r, *string, *len, &buf, 0); struct interpret_branch_name_options options = { 0 } ;
int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);


if (ret == *len) { if (ret == *len) {
size_t size; size_t size;

View File

@ -315,13 +315,14 @@ static void add_pending_object_with_path(struct rev_info *revs,
const char *name, unsigned mode, const char *name, unsigned mode,
const char *path) const char *path)
{ {
struct interpret_branch_name_options options = { 0 };
if (!obj) if (!obj)
return; return;
if (revs->no_walk && (obj->flags & UNINTERESTING)) if (revs->no_walk && (obj->flags & UNINTERESTING))
revs->no_walk = 0; revs->no_walk = 0;
if (revs->reflog_info && obj->type == OBJ_COMMIT) { if (revs->reflog_info && obj->type == OBJ_COMMIT) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int len = interpret_branch_name(name, 0, &buf, 0); int len = interpret_branch_name(name, 0, &buf, &options);


if (0 < len && name[len] && buf.len) if (0 < len && name[len] && buf.len)
strbuf_addstr(&buf, name + len); strbuf_addstr(&buf, name + len);

View File

@ -1427,9 +1427,12 @@ static int reinterpret(struct repository *r,
struct strbuf tmp = STRBUF_INIT; struct strbuf tmp = STRBUF_INIT;
int used = buf->len; int used = buf->len;
int ret; int ret;
struct interpret_branch_name_options options = {
.allowed = allowed
};


strbuf_add(buf, name + len, namelen - len); strbuf_add(buf, name + len, namelen - len);
ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, allowed); ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
/* that data was not interpreted, remove our cruft */ /* that data was not interpreted, remove our cruft */
if (ret < 0) { if (ret < 0) {
strbuf_setlen(buf, used); strbuf_setlen(buf, used);
@ -1471,7 +1474,7 @@ static int interpret_branch_mark(struct repository *r,
int (*get_mark)(const char *, int), int (*get_mark)(const char *, int),
const char *(*get_data)(struct branch *, const char *(*get_data)(struct branch *,
struct strbuf *), struct strbuf *),
unsigned allowed) const struct interpret_branch_name_options *options)
{ {
int len; int len;
struct branch *branch; struct branch *branch;
@ -1496,7 +1499,7 @@ static int interpret_branch_mark(struct repository *r,
if (!value) if (!value)
die("%s", err.buf); die("%s", err.buf);


if (!branch_interpret_allowed(value, allowed)) if (!branch_interpret_allowed(value, options->allowed))
return -1; return -1;


set_shortened_ref(r, buf, value); set_shortened_ref(r, buf, value);
@ -1506,7 +1509,7 @@ static int interpret_branch_mark(struct repository *r,
int repo_interpret_branch_name(struct repository *r, int repo_interpret_branch_name(struct repository *r,
const char *name, int namelen, const char *name, int namelen,
struct strbuf *buf, struct strbuf *buf,
unsigned allowed) const struct interpret_branch_name_options *options)
{ {
char *at; char *at;
const char *start; const char *start;
@ -1515,7 +1518,7 @@ int repo_interpret_branch_name(struct repository *r,
if (!namelen) if (!namelen)
namelen = strlen(name); namelen = strlen(name);


if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) { if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
len = interpret_nth_prior_checkout(r, name, namelen, buf); len = interpret_nth_prior_checkout(r, name, namelen, buf);
if (!len) { if (!len) {
return len; /* syntax Ok, not enough switches */ return len; /* syntax Ok, not enough switches */
@ -1523,7 +1526,8 @@ int repo_interpret_branch_name(struct repository *r,
if (len == namelen) if (len == namelen)
return len; /* consumed all */ return len; /* consumed all */
else else
return reinterpret(r, name, namelen, len, buf, allowed); return reinterpret(r, name, namelen, len, buf,
options->allowed);
} }
} }


@ -1531,22 +1535,22 @@ int repo_interpret_branch_name(struct repository *r,
(at = memchr(start, '@', namelen - (start - name))); (at = memchr(start, '@', namelen - (start - name)));
start = at + 1) { start = at + 1) {


if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) { if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
len = interpret_empty_at(name, namelen, at - name, buf); len = interpret_empty_at(name, namelen, at - name, buf);
if (len > 0) if (len > 0)
return reinterpret(r, name, namelen, len, buf, return reinterpret(r, name, namelen, len, buf,
allowed); options->allowed);
} }


len = interpret_branch_mark(r, name, namelen, at - name, buf, len = interpret_branch_mark(r, name, namelen, at - name, buf,
upstream_mark, branch_get_upstream, upstream_mark, branch_get_upstream,
allowed); options);
if (len > 0) if (len > 0)
return len; return len;


len = interpret_branch_mark(r, name, namelen, at - name, buf, len = interpret_branch_mark(r, name, namelen, at - name, buf,
push_mark, branch_get_push, push_mark, branch_get_push,
allowed); options);
if (len > 0) if (len > 0)
return len; return len;
} }
@ -1557,7 +1561,10 @@ int repo_interpret_branch_name(struct repository *r,
void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
{ {
int len = strlen(name); int len = strlen(name);
int used = interpret_branch_name(name, len, sb, allowed); struct interpret_branch_name_options options = {
.allowed = allowed
};
int used = interpret_branch_name(name, len, sb, &options);


if (used < 0) if (used < 0)
used = 0; used = 0;