Skip to content

Commit 7b97730

Browse files
peffgitster
authored andcommitted
upload-archive: allow user to turn off filters
Some tar filters may be very expensive to run, so sites do not want to expose them via upload-archive. This patch lets users configure tar.<filter>.remote to turn them off. By default, gzip filters are left on, as they are about as expensive as creating zip archives. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e804e0 commit 7b97730

File tree

8 files changed

+49
-13
lines changed

8 files changed

+49
-13
lines changed

Documentation/git-archive.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ tar.<format>.command::
114114
The "tar.gz" and "tgz" formats are defined automatically and default to
115115
`gzip -cn`. You may override them with custom commands.
116116

117+
tar.<format>.remote::
118+
If true, enable `<format>` for use by remote clients via
119+
linkgit:git-upload-archive[1]. Defaults to false for
120+
user-defined formats, but true for the "tar.gz" and "tgz"
121+
formats.
122+
117123
ATTRIBUTES
118124
----------
119125

archive-tar.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ static int tar_filter_config(const char *var, const char *value, void *data)
274274
ar->data = xstrdup(value);
275275
return 0;
276276
}
277+
if (!strcmp(type, "remote")) {
278+
if (git_config_bool(var, value))
279+
ar->flags |= ARCHIVER_REMOTE;
280+
else
281+
ar->flags &= ~ARCHIVER_REMOTE;
282+
return 0;
283+
}
277284

278285
return 0;
279286
}
@@ -349,7 +356,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
349356
static struct archiver tar_archiver = {
350357
"tar",
351358
write_tar_archive,
352-
0
359+
ARCHIVER_REMOTE
353360
};
354361

355362
void init_tar_archiver(void)
@@ -358,7 +365,9 @@ void init_tar_archiver(void)
358365
register_archiver(&tar_archiver);
359366

360367
tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
368+
tar_filter_config("tar.tgz.remote", "true", NULL);
361369
tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
370+
tar_filter_config("tar.tar.gz.remote", "true", NULL);
362371
git_config(git_tar_config, NULL);
363372
for (i = 0; i < nr_tar_filters; i++) {
364373
/* omit any filters that never had a command configured */

archive-zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static int write_zip_archive(const struct archiver *ar,
283283
static struct archiver zip_archiver = {
284284
"zip",
285285
write_zip_archive,
286-
ARCHIVER_WANT_COMPRESSION_LEVELS
286+
ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
287287
};
288288

289289
void init_zip_archiver(void)

archive.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ static void parse_treeish_arg(const char **argv,
299299

300300
static int parse_archive_args(int argc, const char **argv,
301301
const struct archiver **ar, struct archiver_args *args,
302-
const char *name_hint)
302+
const char *name_hint, int is_remote)
303303
{
304304
const char *format = NULL;
305305
const char *base = NULL;
@@ -356,7 +356,8 @@ static int parse_archive_args(int argc, const char **argv,
356356

357357
if (list) {
358358
for (i = 0; i < nr_archivers; i++)
359-
printf("%s\n", archivers[i]->name);
359+
if (!is_remote || archivers[i]->flags & ARCHIVER_REMOTE)
360+
printf("%s\n", archivers[i]->name);
360361
exit(0);
361362
}
362363

@@ -369,7 +370,7 @@ static int parse_archive_args(int argc, const char **argv,
369370
if (argc < 1)
370371
usage_with_options(archive_usage, opts);
371372
*ar = lookup_archiver(format);
372-
if (!*ar)
373+
if (!*ar || (is_remote && !((*ar)->flags & ARCHIVER_REMOTE)))
373374
die("Unknown archive format '%s'", format);
374375

375376
args->compression_level = Z_DEFAULT_COMPRESSION;
@@ -390,7 +391,7 @@ static int parse_archive_args(int argc, const char **argv,
390391
}
391392

392393
int write_archive(int argc, const char **argv, const char *prefix,
393-
int setup_prefix, const char *name_hint)
394+
int setup_prefix, const char *name_hint, int remote)
394395
{
395396
int nongit = 0;
396397
const struct archiver *ar = NULL;
@@ -403,7 +404,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
403404
init_tar_archiver();
404405
init_zip_archiver();
405406

406-
argc = parse_archive_args(argc, argv, &ar, &args, name_hint);
407+
argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
407408
if (nongit) {
408409
/*
409410
* We know this will die() with an error, so we could just

archive.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct archiver_args {
1515
};
1616

1717
#define ARCHIVER_WANT_COMPRESSION_LEVELS 1
18+
#define ARCHIVER_REMOTE 2
1819
struct archiver {
1920
const char *name;
2021
int (*write_archive)(const struct archiver *, struct archiver_args *);
@@ -29,7 +30,7 @@ extern void init_zip_archiver(void);
2930
typedef int (*write_archive_entry_fn_t)(struct archiver_args *args, const unsigned char *sha1, const char *path, size_t pathlen, unsigned int mode, void *buffer, unsigned long size);
3031

3132
extern int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry);
32-
extern int write_archive(int argc, const char **argv, const char *prefix, int setup_prefix, const char *name_hint);
33+
extern int write_archive(int argc, const char **argv, const char *prefix, int setup_prefix, const char *name_hint, int remote);
3334

3435
const char *archive_format_from_filename(const char *filename);
3536

builtin/archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
106106

107107
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
108108

109-
return write_archive(argc, argv, prefix, 1, output);
109+
return write_archive(argc, argv, prefix, 1, output, 0);
110110
}

builtin/upload-archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
6464
sent_argv[sent_argc] = NULL;
6565

6666
/* parse all options sent by the client */
67-
return write_archive(sent_argc, sent_argv, prefix, 0, NULL);
67+
return write_archive(sent_argc, sent_argv, prefix, 0, NULL, 1);
6868
}
6969

7070
__attribute__((format (printf, 1, 2)))

t/t5000-tar-tree.sh

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ test_expect_success 'git-archive --prefix=olde-' '
256256

257257
test_expect_success 'setup tar filters' '
258258
git config tar.tar.foo.command "tr ab ba" &&
259-
git config tar.bar.command "tr ab ba"
259+
git config tar.bar.command "tr ab ba" &&
260+
git config tar.bar.remote true
260261
'
261262

262263
test_expect_success 'archive --list mentions user filter' '
@@ -265,9 +266,9 @@ test_expect_success 'archive --list mentions user filter' '
265266
grep "^bar\$" output
266267
'
267268

268-
test_expect_success 'archive --list shows remote user filters' '
269+
test_expect_success 'archive --list shows only enabled remote filters' '
269270
git archive --list --remote=. >output &&
270-
grep "^tar\.foo\$" output &&
271+
! grep "^tar\.foo\$" output &&
271272
grep "^bar\$" output
272273
'
273274

@@ -297,6 +298,13 @@ test_expect_success 'extension matching requires dot' '
297298
test_cmp b.tar config-implicittar.foo
298299
'
299300

301+
test_expect_success 'only enabled filters are available remotely' '
302+
test_must_fail git archive --remote=. --format=tar.foo HEAD \
303+
>remote.tar.foo &&
304+
git archive --remote=. --format=bar >remote.bar HEAD &&
305+
test_cmp remote.bar config.bar
306+
'
307+
300308
if $GZIP --version >/dev/null 2>&1; then
301309
test_set_prereq GZIP
302310
else
@@ -333,4 +341,15 @@ test_expect_success GZIP,GUNZIP 'extract tgz file' '
333341
test_cmp b.tar j.tar
334342
'
335343

344+
test_expect_success GZIP 'remote tar.gz is allowed by default' '
345+
git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
346+
test_cmp j.tgz remote.tar.gz
347+
'
348+
349+
test_expect_success GZIP 'remote tar.gz can be disabled' '
350+
git config tar.tar.gz.remote false &&
351+
test_must_fail git archive --remote=. --format=tar.gz HEAD \
352+
>remote.tar.gz
353+
'
354+
336355
test_done

0 commit comments

Comments
 (0)