Skip to content

Commit a4f66a7

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit a4f66a7

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

cache.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,21 +1555,25 @@ int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
15551555
*
15561556
* If the input was ok but there are not N branch switches in the
15571557
* reflog, it returns 0.
1558-
*
1559-
* If "allowed" is non-zero, it is a treated as a bitfield of allowable
1560-
* expansions: local branches ("refs/heads/"), remote branches
1561-
* ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
1562-
* allowed, even ones to refs outside of those namespaces.
15631558
*/
15641559
#define INTERPRET_BRANCH_LOCAL (1<<0)
15651560
#define INTERPRET_BRANCH_REMOTE (1<<1)
15661561
#define INTERPRET_BRANCH_HEAD (1<<2)
1562+
struct interpret_branch_name_options {
1563+
/*
1564+
* If "allowed" is non-zero, it is a treated as a bitfield of allowable
1565+
* expansions: local branches ("refs/heads/"), remote branches
1566+
* ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
1567+
* allowed, even ones to refs outside of those namespaces.
1568+
*/
1569+
unsigned allowed;
1570+
};
15671571
int repo_interpret_branch_name(struct repository *r,
15681572
const char *str, int len,
15691573
struct strbuf *buf,
1570-
unsigned allowed);
1571-
#define interpret_branch_name(str, len, buf, allowed) \
1572-
repo_interpret_branch_name(the_repository, str, len, buf, allowed)
1574+
const struct interpret_branch_name_options *options);
1575+
#define interpret_branch_name(str, len, buf, options) \
1576+
repo_interpret_branch_name(the_repository, str, len, buf, options)
15731577

15741578
int validate_headref(const char *ref);
15751579

refs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ static char *substitute_branch_name(struct repository *r,
601601
const char **string, int *len)
602602
{
603603
struct strbuf buf = STRBUF_INIT;
604-
int ret = repo_interpret_branch_name(r, *string, *len, &buf, 0);
604+
struct interpret_branch_name_options options = { 0 } ;
605+
int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);
605606

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

revision.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,14 @@ static void add_pending_object_with_path(struct rev_info *revs,
315315
const char *name, unsigned mode,
316316
const char *path)
317317
{
318+
struct interpret_branch_name_options options = { 0 };
318319
if (!obj)
319320
return;
320321
if (revs->no_walk && (obj->flags & UNINTERESTING))
321322
revs->no_walk = 0;
322323
if (revs->reflog_info && obj->type == OBJ_COMMIT) {
323324
struct strbuf buf = STRBUF_INIT;
324-
int len = interpret_branch_name(name, 0, &buf, 0);
325+
int len = interpret_branch_name(name, 0, &buf, &options);
325326

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

sha1-name.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,9 +1427,12 @@ static int reinterpret(struct repository *r,
14271427
struct strbuf tmp = STRBUF_INIT;
14281428
int used = buf->len;
14291429
int ret;
1430+
struct interpret_branch_name_options options = {
1431+
.allowed = allowed
1432+
};
14301433

14311434
strbuf_add(buf, name + len, namelen - len);
1432-
ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, allowed);
1435+
ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
14331436
/* that data was not interpreted, remove our cruft */
14341437
if (ret < 0) {
14351438
strbuf_setlen(buf, used);
@@ -1471,7 +1474,7 @@ static int interpret_branch_mark(struct repository *r,
14711474
int (*get_mark)(const char *, int),
14721475
const char *(*get_data)(struct branch *,
14731476
struct strbuf *),
1474-
unsigned allowed)
1477+
const struct interpret_branch_name_options *options)
14751478
{
14761479
int len;
14771480
struct branch *branch;
@@ -1496,7 +1499,7 @@ static int interpret_branch_mark(struct repository *r,
14961499
if (!value)
14971500
die("%s", err.buf);
14981501

1499-
if (!branch_interpret_allowed(value, allowed))
1502+
if (!branch_interpret_allowed(value, options->allowed))
15001503
return -1;
15011504

15021505
set_shortened_ref(r, buf, value);
@@ -1506,7 +1509,7 @@ static int interpret_branch_mark(struct repository *r,
15061509
int repo_interpret_branch_name(struct repository *r,
15071510
const char *name, int namelen,
15081511
struct strbuf *buf,
1509-
unsigned allowed)
1512+
const struct interpret_branch_name_options *options)
15101513
{
15111514
char *at;
15121515
const char *start;
@@ -1515,38 +1518,39 @@ int repo_interpret_branch_name(struct repository *r,
15151518
if (!namelen)
15161519
namelen = strlen(name);
15171520

1518-
if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
1521+
if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
15191522
len = interpret_nth_prior_checkout(r, name, namelen, buf);
15201523
if (!len) {
15211524
return len; /* syntax Ok, not enough switches */
15221525
} else if (len > 0) {
15231526
if (len == namelen)
15241527
return len; /* consumed all */
15251528
else
1526-
return reinterpret(r, name, namelen, len, buf, allowed);
1529+
return reinterpret(r, name, namelen, len, buf,
1530+
options->allowed);
15271531
}
15281532
}
15291533

15301534
for (start = name;
15311535
(at = memchr(start, '@', namelen - (start - name)));
15321536
start = at + 1) {
15331537

1534-
if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
1538+
if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
15351539
len = interpret_empty_at(name, namelen, at - name, buf);
15361540
if (len > 0)
15371541
return reinterpret(r, name, namelen, len, buf,
1538-
allowed);
1542+
options->allowed);
15391543
}
15401544

15411545
len = interpret_branch_mark(r, name, namelen, at - name, buf,
15421546
upstream_mark, branch_get_upstream,
1543-
allowed);
1547+
options);
15441548
if (len > 0)
15451549
return len;
15461550

15471551
len = interpret_branch_mark(r, name, namelen, at - name, buf,
15481552
push_mark, branch_get_push,
1549-
allowed);
1553+
options);
15501554
if (len > 0)
15511555
return len;
15521556
}
@@ -1557,7 +1561,10 @@ int repo_interpret_branch_name(struct repository *r,
15571561
void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
15581562
{
15591563
int len = strlen(name);
1560-
int used = interpret_branch_name(name, len, sb, allowed);
1564+
struct interpret_branch_name_options options = {
1565+
.allowed = allowed
1566+
};
1567+
int used = interpret_branch_name(name, len, sb, &options);
15611568

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

0 commit comments

Comments
 (0)