archive: support compression levels beyond 9

Compression programs like zip, gzip, bzip2 and xz allow to adjust the trade-off between CPU cost and size gain with numerical options from -1 for fast compression and -9 for high compression ratio. zip also accepts -0 for storing files verbatim. git archive directly support these single-digit compression levels for ZIP output and passes them to filters like gzip. Zstandard additionally supports compression level options -10 to -19, or up to -22 with --ultra. This *seems* to work with git archive in most cases, e.g. it will produce an archive with -19 without complaining, but since it only supports single-digit compression level options this is the same as -1 -9 and thus -9. Allow git archive to accept multi-digit compression levels to support the full range supported by zstd. Explicitly reject them for the ZIP format, as otherwise deflateInit2() would just fail with a somewhat cryptic "stream consistency error". Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 9, 2020 at 17:05 UTC cde8ea9c66e46ed6427499e0bc049d1d5c55379a
3 files changed +14 -16
archive-tar.c
+2 -1
@@ -374,7 +374,8 @@ static int tar_filter_config(const char *var, const char *value, void *data)
374 ar = xcalloc(1, sizeof(*ar));
375 ar->name = xmemdupz(name, namelen);
376 ar->write_archive = write_tar_filter_archive;
377 - ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
377 + ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
378 + ARCHIVER_HIGH_COMPRESSION_LEVELS;
379 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
380 tar_filters[nr_tar_filters++] = ar;
381 }
archive.c
+11 -15
@@ -529,10 +529,12 @@ static int add_file_cb(const struct option *opt, const char *arg, int unset)
529 return 0;
530 }
531
532 -#define OPT__COMPR(s, v, h, p) \
533 - OPT_SET_INT_F(s, NULL, v, h, p, PARSE_OPT_NONEG)
534 -#define OPT__COMPR_HIDDEN(s, v, p) \
535 - OPT_SET_INT_F(s, NULL, v, "", p, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN)
532 +static int number_callback(const struct option *opt, const char *arg, int unset)
533 +{
534 + BUG_ON_OPT_NEG(unset);
535 + *(int *)opt->value = strtol(arg, NULL, 10);
536 + return 0;
537 +}
538
539 static int parse_archive_args(int argc, const char **argv,
540 const struct archiver **ar, struct archiver_args *args,
@@ -561,16 +563,8 @@ static int parse_archive_args(int argc, const char **argv,
563 OPT_BOOL(0, "worktree-attributes", &worktree_attributes,
564 N_("read .gitattributes in working directory")),
565 OPT__VERBOSE(&verbose, N_("report archived files on stderr")),
564 - OPT__COMPR('0', &compression_level, N_("store only"), 0),
565 - OPT__COMPR('1', &compression_level, N_("compress faster"), 1),
566 - OPT__COMPR_HIDDEN('2', &compression_level, 2),
567 - OPT__COMPR_HIDDEN('3', &compression_level, 3),
568 - OPT__COMPR_HIDDEN('4', &compression_level, 4),
569 - OPT__COMPR_HIDDEN('5', &compression_level, 5),
570 - OPT__COMPR_HIDDEN('6', &compression_level, 6),
571 - OPT__COMPR_HIDDEN('7', &compression_level, 7),
572 - OPT__COMPR_HIDDEN('8', &compression_level, 8),
573 - OPT__COMPR('9', &compression_level, N_("compress better"), 9),
566 + OPT_NUMBER_CALLBACK(&compression_level,
567 + N_("set compression level"), number_callback),
568 OPT_GROUP(""),
569 OPT_BOOL('l', "list", &list,
570 N_("list supported archive formats")),
@@ -617,7 +611,9 @@ static int parse_archive_args(int argc, const char **argv,
611
612 args->compression_level = Z_DEFAULT_COMPRESSION;
613 if (compression_level != -1) {
620 - if ((*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS)
614 + int levels_ok = (*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS;
615 + int high_ok = (*ar)->flags & ARCHIVER_HIGH_COMPRESSION_LEVELS;
616 + if (levels_ok && (compression_level <= 9 || high_ok))
617 args->compression_level = compression_level;
618 else {
619 die(_("Argument not supported for format '%s': -%d"),
archive.h
+1
@@ -36,6 +36,7 @@ const char *archive_format_from_filename(const char *filename);
36
37 #define ARCHIVER_WANT_COMPRESSION_LEVELS 1
38 #define ARCHIVER_REMOTE 2
39 +#define ARCHIVER_HIGH_COMPRESSION_LEVELS 4
40 struct archiver {
41 const char *name;
42 int (*write_archive)(const struct archiver *, struct archiver_args *);