Re: [PATCH v2 1/2] sha1-name: replace unsigned int with option struct
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 29, 2020, 18:44 UTC
- Message-ID
- <xmqqsgc5i96h.fsf@gitster.c.googlers.com>
- In-Reply-To
- <1ce44900a08857332ee70b916c3d9e7e76751221.1598662525.git.jonathantanmy@google.com>
Jonathan Tan <jonathantanmy@google.com> writes:
> 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.
Makes sense.
Show 21 quoted lines
> #define INTERPRET_BRANCH_LOCAL (1<<0)
> #define INTERPRET_BRANCH_REMOTE (1<<1)
> #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,
> const char *str, int len,
> struct strbuf *buf,
> - unsigned allowed);
> -#define interpret_branch_name(str, len, buf, allowed) \
> - repo_interpret_branch_name(the_repository, str, len, buf, allowed)
> + const struct interpret_branch_name_options *options);
> +#define interpret_branch_name(str, len, buf, options) \
> + repo_interpret_branch_name(the_repository, str, len, buf, options)I was debating myself if we want to have
#define IBN_OPTIONS_INIT { 0 }or something similar (perhaps "#define IOI(abit) { .allowed = (abit) }"), but it probably is not worth it given that we have only 3 local sites that define it, 1 always initializes the field to 0, and the other just relay the value passed by its caller.
Show 16 quoted lines
> ...
> diff --git a/sha1-name.c b/sha1-name.c
> index 0b8cb5247a..a7a9de66c4 100644
> --- a/sha1-name.c
> +++ b/sha1-name.c
> @@ -1427,9 +1427,12 @@ static int reinterpret(struct repository *r,
> struct strbuf tmp = STRBUF_INIT;
> int used = buf->len;
> int ret;
> + struct interpret_branch_name_options options = {
> + .allowed = allowed
> + };
>
> 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);Show 9 quoted lines
> @@ -1557,7 +1561,10 @@ int repo_interpret_branch_name(struct repository *r,
> void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
> {
> 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);These are quite straight-forward rewrites. Looking good.
Thanks.