[PATCH 1/1] snapshot: strip bit from struct cgit_snapshot_format
John Keeping
john at keeping.me.uk
Sat Jun 9 13:16:48 CEST 2018
On Sat, Jun 09, 2018 at 12:11:11AM +0200, Christian Hesse wrote:
> From: Christian Hesse <mail at eworm.de>
>
> We had a static bit value in struct cgit_snapshot_format. We do not rely
> on it and things can be calculated on the fly. So strip it.
>
> Signed-off-by: Christian Hesse <mail at eworm.de>
> ---
> cgit.c | 3 ++-
> cgit.h | 1 -
> shared.c | 5 ++++-
> ui-shared.c | 3 ++-
> ui-snapshot.c | 28 ++++++++++------------------
> 5 files changed, 18 insertions(+), 22 deletions(-)
>
> diff --git a/cgit.c b/cgit.c
> index d2f7b9c..5f634fc 100644
> --- a/cgit.c
> +++ b/cgit.c
> @@ -763,9 +763,10 @@ static char *build_snapshot_setting(int bitmap)
> {
> const struct cgit_snapshot_format *f;
> struct strbuf result = STRBUF_INIT;
> + int i = 0;
>
> for (f = cgit_snapshot_formats; f->suffix; f++) {
> - if (f->bit & bitmap) {
> + if (1 << i++ & bitmap) {
I think this would be nicer if we introduce a BIT macro like Linux has,
something like:
#define BIT(x) (1U << (x))
but even better, can't we have something like:
static inline unsigned cgit_snapshot_bit(
const struct cgit_snapshot_format *f)
{
return BIT(f - &cgit_snapshot_formats[0]);
}
and avoid needing the loop index completely?
> if (result.len)
> strbuf_addch(&result, ' ');
> strbuf_addstr(&result, f->suffix);
> diff --git a/cgit.h b/cgit.h
> index a686390..707a3f5 100644
> --- a/cgit.h
> +++ b/cgit.h
> @@ -314,7 +314,6 @@ struct cgit_snapshot_format {
> const char *suffix;
> const char *mimetype;
> write_archive_fn_t write_func;
> - int bit;
> };
>
> extern const char *cgit_version;
> diff --git a/shared.c b/shared.c
> index 0a11e68..c61a0a1 100644
> --- a/shared.c
> +++ b/shared.c
> @@ -397,12 +397,15 @@ int cgit_parse_snapshots_mask(const char *str)
> string_list_remove_empty_items(&tokens, 0);
>
> for_each_string_list_item(item, &tokens) {
> + int i = 0;
> +
> for (f = cgit_snapshot_formats; f->suffix; f++) {
> if (!strcmp(item->string, f->suffix) ||
> !strcmp(item->string, f->suffix + 1)) {
> - rv |= f->bit;
> + rv |= 1 << i;
> break;
> }
> + i++;
> }
> }
>
> diff --git a/ui-shared.c b/ui-shared.c
> index 51a25a0..6092004 100644
> --- a/ui-shared.c
> +++ b/ui-shared.c
> @@ -1118,6 +1118,7 @@ void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
> struct strbuf filename = STRBUF_INIT;
> const char *basename;
> size_t prefixlen;
> + int i = 0;
>
> basename = cgit_snapshot_prefix(repo);
> if (starts_with(ref, basename))
> @@ -1130,7 +1131,7 @@ void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
>
> prefixlen = filename.len;
> for (f = cgit_snapshot_formats; f->suffix; f++) {
> - if (!(repo->snapshots & f->bit))
> + if (!(repo->snapshots & 1 << i++))
> continue;
> strbuf_setlen(&filename, prefixlen);
> strbuf_addstr(&filename, f->suffix);
> diff --git a/ui-snapshot.c b/ui-snapshot.c
> index 76d0573..04f41cc 100644
> --- a/ui-snapshot.c
> +++ b/ui-snapshot.c
> @@ -86,11 +86,11 @@ static int write_tar_xz_archive(const char *hex, const char *prefix)
> }
>
> const struct cgit_snapshot_format cgit_snapshot_formats[] = {
> - { ".zip", "application/x-zip", write_zip_archive, 0x01 },
> - { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
> - { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
> - { ".tar", "application/x-tar", write_tar_archive, 0x08 },
> - { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
> + { ".tar", "application/x-tar", write_tar_archive },
> + { ".tar.gz", "application/x-gzip", write_tar_gzip_archive },
> + { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive },
> + { ".tar.xz", "application/x-xz", write_tar_xz_archive },
> + { ".zip", "application/x-zip", write_zip_archive },
> { NULL }
> };
>
> @@ -119,17 +119,6 @@ const struct object_id *cgit_snapshot_get_sig(const char *ref,
> return get_note(tree, &oid);
> }
>
> -static const struct cgit_snapshot_format *get_format(const char *filename)
> -{
> - const struct cgit_snapshot_format *fmt;
> -
> - for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
> - if (ends_with(filename, fmt->suffix))
> - return fmt;
> - }
> - return NULL;
> -}
> -
> static int make_snapshot(const struct cgit_snapshot_format *format,
> const char *hex, const char *prefix,
> const char *filename)
> @@ -246,6 +235,7 @@ void cgit_print_snapshot(const char *head, const char *hex,
> const char *sig_filename = NULL;
> char *adj_filename = NULL;
> char *prefix = NULL;
> + int i = 0;
>
> if (!filename) {
> cgit_print_error_page(400, "Bad request",
> @@ -262,8 +252,10 @@ void cgit_print_snapshot(const char *head, const char *hex,
> filename = adj_filename;
> }
>
> - f = get_format(filename);
> - if (!f || (!sig_filename && !(ctx.repo->snapshots & f->bit))) {
> + for (f = cgit_snapshot_formats; f->suffix && !ends_with(filename, f->suffix); f++)
> + i++;
> +
> + if (!f->suffix || (!sig_filename && !(ctx.repo->snapshots & 1 << i))) {
> cgit_print_error_page(400, "Bad request",
> "Unsupported snapshot format: %s", filename);
> return;
> _______________________________________________
> CGit mailing list
> CGit at lists.zx2c4.com
> https://lists.zx2c4.com/mailman/listinfo/cgit
More information about the CGit
mailing list