@matheusbsbs / matheus-bagliano / commits / a4f66a7876
Fork 0

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>

Jonathan Tan committed Sep 1, 2020 at 15:28 UTC a4f66a7876f6979f0a3ac254173cbdaf0a2a55c3
4 files changed +34 -21
cache.h
+12 -8
@@ -1555,21 +1555,25 @@ int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
1555 *
1556 * If the input was ok but there are not N branch switches in the
1557 * 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.
1558 */
1559 #define INTERPRET_BRANCH_LOCAL (1<<0)
1560 #define INTERPRET_BRANCH_REMOTE (1<<1)
1561 #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 +};
1571 int repo_interpret_branch_name(struct repository *r,
1572 const char *str, int len,
1573 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)
1577
1578 int validate_headref(const char *ref);
1579
refs.c
+2 -1
@@ -601,7 +601,8 @@ static char *substitute_branch_name(struct repository *r,
601 const char **string, int *len)
602 {
603 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);
606
607 if (ret == *len) {
608 size_t size;
revision.c
+2 -1
@@ -315,13 +315,14 @@ static void add_pending_object_with_path(struct rev_info *revs,
315 const char *name, unsigned mode,
316 const char *path)
317 {
318 + struct interpret_branch_name_options options = { 0 };
319 if (!obj)
320 return;
321 if (revs->no_walk && (obj->flags & UNINTERESTING))
322 revs->no_walk = 0;
323 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
324 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);
326
327 if (0 < len && name[len] && buf.len)
328 strbuf_addstr(&buf, name + len);
sha1-name.c
+18 -11
@@ -1427,9 +1427,12 @@ static int reinterpret(struct repository *r,
1427 struct strbuf tmp = STRBUF_INIT;
1428 int used = buf->len;
1429 int ret;
1430 + struct interpret_branch_name_options options = {
1431 + .allowed = allowed
1432 + };
1433
1434 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);
1436 /* that data was not interpreted, remove our cruft */
1437 if (ret < 0) {
1438 strbuf_setlen(buf, used);
@@ -1471,7 +1474,7 @@ static int interpret_branch_mark(struct repository *r,
1474 int (*get_mark)(const char *, int),
1475 const char *(*get_data)(struct branch *,
1476 struct strbuf *),
1474 - unsigned allowed)
1477 + const struct interpret_branch_name_options *options)
1478 {
1479 int len;
1480 struct branch *branch;
@@ -1496,7 +1499,7 @@ static int interpret_branch_mark(struct repository *r,
1499 if (!value)
1500 die("%s", err.buf);
1501
1499 - if (!branch_interpret_allowed(value, allowed))
1502 + if (!branch_interpret_allowed(value, options->allowed))
1503 return -1;
1504
1505 set_shortened_ref(r, buf, value);
@@ -1506,7 +1509,7 @@ static int interpret_branch_mark(struct repository *r,
1509 int repo_interpret_branch_name(struct repository *r,
1510 const char *name, int namelen,
1511 struct strbuf *buf,
1509 - unsigned allowed)
1512 + const struct interpret_branch_name_options *options)
1513 {
1514 char *at;
1515 const char *start;
@@ -1515,7 +1518,7 @@ int repo_interpret_branch_name(struct repository *r,
1518 if (!namelen)
1519 namelen = strlen(name);
1520
1518 - if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
1521 + if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1522 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1523 if (!len) {
1524 return len; /* syntax Ok, not enough switches */
@@ -1523,7 +1526,8 @@ int repo_interpret_branch_name(struct repository *r,
1526 if (len == namelen)
1527 return len; /* consumed all */
1528 else
1526 - return reinterpret(r, name, namelen, len, buf, allowed);
1529 + return reinterpret(r, name, namelen, len, buf,
1530 + options->allowed);
1531 }
1532 }
1533
@@ -1531,22 +1535,22 @@ int repo_interpret_branch_name(struct repository *r,
1535 (at = memchr(start, '@', namelen - (start - name)));
1536 start = at + 1) {
1537
1534 - if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
1538 + if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1539 len = interpret_empty_at(name, namelen, at - name, buf);
1540 if (len > 0)
1541 return reinterpret(r, name, namelen, len, buf,
1538 - allowed);
1542 + options->allowed);
1543 }
1544
1545 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1546 upstream_mark, branch_get_upstream,
1543 - allowed);
1547 + options);
1548 if (len > 0)
1549 return len;
1550
1551 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1552 push_mark, branch_get_push,
1549 - allowed);
1553 + options);
1554 if (len > 0)
1555 return len;
1556 }
@@ -1557,7 +1561,10 @@ int repo_interpret_branch_name(struct repository *r,
1561 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1562 {
1563 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);
1568
1569 if (used < 0)
1570 used = 0;