git-archive: add --output=<file> to send output to a file

When archiving a repository there is no way to specify a file as output.
This patch adds a new option "--output" that redirects the output to a
file instead of stdout.

Signed-off-by: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Carlos Manuel Duclos Vergara 2009-02-16 18:20:25 +01:00 committed by Junio C Hamano
parent 734cd5726c
commit aec0c1bbfb
3 changed files with 31 additions and 0 deletions

View File

@ -10,6 +10,7 @@ SYNOPSIS
-------- --------
[verse] [verse]
'git archive' --format=<fmt> [--list] [--prefix=<prefix>/] [<extra>] 'git archive' --format=<fmt> [--list] [--prefix=<prefix>/] [<extra>]
[--output=<file>]
[--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish> [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
[path...] [path...]


@ -47,6 +48,9 @@ OPTIONS
--prefix=<prefix>/:: --prefix=<prefix>/::
Prepend <prefix>/ to each filename in the archive. Prepend <prefix>/ to each filename in the archive.


--output=<file>::
Write the archive to <file> instead of stdout.

<extra>:: <extra>::
This can be any options that the archiver backend understand. This can be any options that the archiver backend understand.
See next section. See next section.

View File

@ -239,6 +239,19 @@ static void parse_treeish_arg(const char **argv,
ar_args->time = archive_time; ar_args->time = archive_time;
} }


static void create_output_file(const char *output_file)
{
int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (output_fd < 0)
die("could not create archive file: %s ", output_file);
if (output_fd != 1) {
if (dup2(output_fd, 1) < 0)
die("could not redirect output");
else
close(output_fd);
}
}

#define OPT__COMPR(s, v, h, p) \ #define OPT__COMPR(s, v, h, p) \
{ OPTION_SET_INT, (s), NULL, (v), NULL, (h), \ { OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) } PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }
@ -253,6 +266,7 @@ static int parse_archive_args(int argc, const char **argv,
const char *base = NULL; const char *base = NULL;
const char *remote = NULL; const char *remote = NULL;
const char *exec = NULL; const char *exec = NULL;
const char *output = NULL;
int compression_level = -1; int compression_level = -1;
int verbose = 0; int verbose = 0;
int i; int i;
@ -262,6 +276,8 @@ static int parse_archive_args(int argc, const char **argv,
OPT_STRING(0, "format", &format, "fmt", "archive format"), OPT_STRING(0, "format", &format, "fmt", "archive format"),
OPT_STRING(0, "prefix", &base, "prefix", OPT_STRING(0, "prefix", &base, "prefix",
"prepend prefix to each pathname in the archive"), "prepend prefix to each pathname in the archive"),
OPT_STRING(0, "output", &output, "file",
"write the archive to this file"),
OPT__VERBOSE(&verbose), OPT__VERBOSE(&verbose),
OPT__COMPR('0', &compression_level, "store only", 0), OPT__COMPR('0', &compression_level, "store only", 0),
OPT__COMPR('1', &compression_level, "compress faster", 1), OPT__COMPR('1', &compression_level, "compress faster", 1),
@ -294,6 +310,9 @@ static int parse_archive_args(int argc, const char **argv,
if (!base) if (!base)
base = ""; base = "";


if (output)
create_output_file(output);

if (list) { if (list) {
for (i = 0; i < ARRAY_SIZE(archivers); i++) for (i = 0; i < ARRAY_SIZE(archivers); i++)
printf("%s\n", archivers[i].name); printf("%s\n", archivers[i].name);

View File

@ -86,6 +86,10 @@ test_expect_success \
'git archive vs. the same in a bare repo' \ 'git archive vs. the same in a bare repo' \
'test_cmp b.tar b3.tar' 'test_cmp b.tar b3.tar'


test_expect_success 'git archive with --output' \
'git archive --output=b4.tar HEAD &&
test_cmp b.tar b4.tar'

test_expect_success \ test_expect_success \
'validate file modification time' \ 'validate file modification time' \
'mkdir extract && 'mkdir extract &&
@ -172,6 +176,10 @@ test_expect_success \
'git archive --format=zip vs. the same in a bare repo' \ 'git archive --format=zip vs. the same in a bare repo' \
'test_cmp d.zip d1.zip' 'test_cmp d.zip d1.zip'


test_expect_success 'git archive --format=zip with --output' \
'git archive --format=zip --output=d2.zip HEAD &&
test_cmp d.zip d2.zip'

$UNZIP -v >/dev/null 2>&1 $UNZIP -v >/dev/null 2>&1
if [ $? -eq 127 ]; then if [ $? -eq 127 ]; then
echo "Skipping ZIP tests, because unzip was not found" echo "Skipping ZIP tests, because unzip was not found"