{"thread":{"id":"17871","subject":"[PATCH v4] git-archive: Add new option \"--output\" to write archive to a file instead of stdout.","startedAt":"2009-02-18T09:05:33Z","lastAt":"2009-02-21T20:38:04Z","messageCount":2,"participants":["carlos.duclos@nokia.com","René Scharfe"],"isPatch":true,"patchVersion":4,"patchTotal":null},"messages":[{"id":"105281","messageId":"599636D7828020419E3AB2DE5CCC8130036BF8B1DC@NOK-EUMSG-02.mgdnok.nokia.com","threadId":"17871","inReplyTo":null,"subject":"[PATCH v4] git-archive: Add new option \"--output\" to write archive to a file instead of stdout.","fromName":"","fromEmail":"carlos.duclos@nokia.com","sentAt":"2009-02-18T09:05:33Z","receivedAt":"2009-02-18T09:05:33Z","isPatch":true,"sender":{"key":"carlos.duclos@nokia.com","avatar":"https://gravatar.com/avatar/be05d03546ed19ca9104e62d4a330bc40b4115ebcf39d1a4c43884b663667951?d=mp&s=160"},"body":"Hi,\n\nPatch attached as MIME.\n\nPatch highlights:\n1. Remove duplicated tests\n1.1 Tests are present on t0024.\n1.2 Introduced some comments in t5000 to make it easy to read.\n2. Removed close(2) and comments from archive.c\n3. Rewrote commit message.\n4. Signed off.\n\nCheers!\n\n\nFrom 3a143244b84d80adba91f37307e30ec8fb3a6701 Mon Sep 17 00:00:00 2001\nFrom: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>\nDate: Mon, 16 Feb 2009 18:20:25 +0100\nSubject: [PATCH] git-archive: add --output=<file> to send output to a file instead of stdout.\n When archiving a repository there is no way to specify a file as output.\n This patch adds a new option \"--output\" that redirects the output to a file\n instead of stdout.\n\nSigned-off-by: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>\n---\n Documentation/git-archive.txt |    3 +++\n archive.c                     |   16 ++++++++++++++++\n t/t0024-crlf-archive.sh       |   19 +++++++++++++++++++\n t/t5000-tar-tree.sh           |    8 ++++++++\n 4 files changed, 46 insertions(+), 0 deletions(-)\n\ndiff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt\nindex 41cbf9c..5bde197 100644\n--- a/Documentation/git-archive.txt\n+++ b/Documentation/git-archive.txt\n@@ -47,6 +47,9 @@ OPTIONS\n --prefix=<prefix>/::\n \tPrepend <prefix>/ to each filename in the archive.\n \n+--output=<file>::\n+\tWrite the archive to <file> instead of stdout.\n+\n <extra>::\n \tThis can be any options that the archiver backend understand.\n \tSee next section.\ndiff --git a/archive.c b/archive.c\nindex e6de039..3a646e5 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -239,6 +239,16 @@ static void parse_treeish_arg(const char **argv,\n \tar_args->time = archive_time;\n }\n \n+static void create_output_file(const char *output_file)\n+{\n+\tint output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);\n+\tif (output_fd < 0)\n+\t\tdie(\"could not create archive file: %s \", output_file);\n+\tif (output_fd != 1)\n+\t\tif (dup2(output_fd, 1) < 0)\n+\t\t\tdie(\"could not redirect output\");\n+}\n+\n #define OPT__COMPR(s, v, h, p) \\\n \t{ OPTION_SET_INT, (s), NULL, (v), NULL, (h), \\\n \t  PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }\n@@ -253,6 +263,7 @@ static int parse_archive_args(int argc, const char **argv,\n \tconst char *base = NULL;\n \tconst char *remote = NULL;\n \tconst char *exec = NULL;\n+\tconst char *output = NULL;\n \tint compression_level = -1;\n \tint verbose = 0;\n \tint i;\n@@ -262,6 +273,8 @@ static int parse_archive_args(int argc, const char **argv,\n \t\tOPT_STRING(0, \"format\", &format, \"fmt\", \"archive format\"),\n \t\tOPT_STRING(0, \"prefix\", &base, \"prefix\",\n \t\t\t\"prepend prefix to each pathname in the archive\"),\n+\t\tOPT_STRING(0, \"output\", &output, \"file\", \n+\t\t\t\"write the archive to this file\"),\n \t\tOPT__VERBOSE(&verbose),\n \t\tOPT__COMPR('0', &compression_level, \"store only\", 0),\n \t\tOPT__COMPR('1', &compression_level, \"compress faster\", 1),\n@@ -294,6 +307,9 @@ static int parse_archive_args(int argc, const char **argv,\n \tif (!base)\n \t\tbase = \"\";\n \n+\tif (output)\n+\t\tcreate_output_file(output);\n+\n \tif (list) {\n \t\tfor (i = 0; i < ARRAY_SIZE(archivers); i++)\n \t\t\tprintf(\"%s\\n\", archivers[i].name);\ndiff --git a/t/t0024-crlf-archive.sh b/t/t0024-crlf-archive.sh\nindex e533039..237a8f6 100755\n--- a/t/t0024-crlf-archive.sh\n+++ b/t/t0024-crlf-archive.sh\n@@ -26,6 +26,15 @@ test_expect_success 'tar archive' '\n \n '\n \n+test_expect_success 'tar archive output redirected' '\n+\n+\tgit archive --format=tar --output=test.tar HEAD &&\n+\t( mkdir untarred2 && cd untarred2 && \"$TAR\" -xf ../test.tar )\n+\n+\ttest_cmp sample untarred2/sample\n+\n+'\n+\n \"$UNZIP\" -v >/dev/null 2>&1\n if [ $? -eq 127 ]; then\n \techo \"Skipping ZIP test, because unzip was not found\"\n@@ -43,4 +52,14 @@ test_expect_success 'zip archive' '\n \n '\n \n+test_expect_success 'zip archive output redirected' '\n+\n+\tgit archive --format=zip --output=test.zip HEAD &&\n+\n+\t( mkdir unzipped2 && cd unzipped2 && unzip ../test.zip ) &&\n+\n+\ttest_cmp sample unzipped2/sample\n+\n+'\n+\n test_done\ndiff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh\nindex c942c8b..a74dd4a 100755\n--- a/t/t5000-tar-tree.sh\n+++ b/t/t5000-tar-tree.sh\n@@ -66,6 +66,11 @@ test_expect_success \\\n     'remove ignored file' \\\n     'rm a/ignored'\n \n+\n+#\n+# Tar tests\n+#\n+\n test_expect_success \\\n     'git archive' \\\n     'git archive HEAD >b.tar'\n@@ -159,6 +164,9 @@ test_expect_success \\\n       diff g/prefix/a/substfile1.expected g/prefix/a/substfile1 &&\n       diff a/substfile2 g/prefix/a/substfile2\n '\n+#\n+# Zip tests\n+#\n \n test_expect_success \\\n     'git archive --format=zip' \\\n-- \n1.6.2.rc0.63.g7cbd0.dirty\n\n"},{"id":"105743","messageId":"49A0662C.4010606@lsrfire.ath.cx","threadId":"17871","inReplyTo":"599636D7828020419E3AB2DE5CCC8130036BF8B1DC@NOK-EUMSG-02.mgdnok.nokia.com","subject":"Re: [PATCH v4] git-archive: Add new option \"--output\" to write archive to a file instead of stdout.","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-02-21T20:38:04Z","receivedAt":"2009-02-21T20:38:04Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"carlos.duclos@nokia.com schrieb:\n> Hi,\n> \n> Patch attached as MIME.\n> \n> Patch highlights:\n> 1. Remove duplicated tests\n> 1.1 Tests are present on t0024.\n> 1.2 Introduced some comments in t5000 to make it easy to read.\n> 2. Removed close(2) and comments from archive.c\n> 3. Rewrote commit message.\n> 4. Signed off.\n> \n> Cheers!\n> \n> From 3a143244b84d80adba91f37307e30ec8fb3a6701 Mon Sep 17 00:00:00 2001\n> From: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>\n> Date: Mon, 16 Feb 2009 18:20:25 +0100\n> Subject: [PATCH] git-archive: add --output=<file> to send output to a file instead of stdout.\n>  When archiving a repository there is no way to specify a file as output.\n>  This patch adds a new option \"--output\" that redirects the output to a file\n>  instead of stdout.\n> \n> Signed-off-by: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>\n> ---\n>  Documentation/git-archive.txt |    3 +++\n>  archive.c                     |   16 ++++++++++++++++\n>  t/t0024-crlf-archive.sh       |   19 +++++++++++++++++++\n>  t/t5000-tar-tree.sh           |    8 ++++++++\n>  4 files changed, 46 insertions(+), 0 deletions(-)\n> \n> diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt\n> index 41cbf9c..5bde197 100644\n> --- a/Documentation/git-archive.txt\n> +++ b/Documentation/git-archive.txt\n> @@ -47,6 +47,9 @@ OPTIONS\n>  --prefix=<prefix>/::\n>  \tPrepend <prefix>/ to each filename in the archive.\n>  \n> +--output=<file>::\n> +\tWrite the archive to <file> instead of stdout.\n> +\n>  <extra>::\n>  \tThis can be any options that the archiver backend understand.\n>  \tSee next section.\n> diff --git a/archive.c b/archive.c\n> index e6de039..3a646e5 100644\n> --- a/archive.c\n> +++ b/archive.c\n> @@ -239,6 +239,16 @@ static void parse_treeish_arg(const char **argv,\n>  \tar_args->time = archive_time;\n>  }\n>  \n> +static void create_output_file(const char *output_file)\n> +{\n> +\tint output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);\n> +\tif (output_fd < 0)\n> +\t\tdie(\"could not create archive file: %s \", output_file);\n> +\tif (output_fd != 1)\n> +\t\tif (dup2(output_fd, 1) < 0)\n> +\t\t\tdie(\"could not redirect output\");\n> +}\n> +\n\noutput_fd is leaked.  Sure, it points to the same file as stdout, but I\ndon't see why we would want to keep two handles open.  (Leaking when we\ndie() is OK, though.)\n\n> diff --git a/t/t0024-crlf-archive.sh b/t/t0024-crlf-archive.sh\n> index e533039..237a8f6 100755\n> --- a/t/t0024-crlf-archive.sh\n> +++ b/t/t0024-crlf-archive.sh\n> @@ -26,6 +26,15 @@ test_expect_success 'tar archive' '\n>  \n>  '\n>  \n> +test_expect_success 'tar archive output redirected' '\n> +\n> +\tgit archive --format=tar --output=test.tar HEAD &&\n> +\t( mkdir untarred2 && cd untarred2 && \"$TAR\" -xf ../test.tar )\n> +\n> +\ttest_cmp sample untarred2/sample\n> +\n> +'\n> +\n>  \"$UNZIP\" -v >/dev/null 2>&1\n>  if [ $? -eq 127 ]; then\n>  \techo \"Skipping ZIP test, because unzip was not found\"\n> @@ -43,4 +52,14 @@ test_expect_success 'zip archive' '\n>  \n>  '\n>  \n> +test_expect_success 'zip archive output redirected' '\n> +\n> +\tgit archive --format=zip --output=test.zip HEAD &&\n> +\n> +\t( mkdir unzipped2 && cd unzipped2 && unzip ../test.zip ) &&\n> +\n> +\ttest_cmp sample unzipped2/sample\n\nI had something even more simple in mind.  In t5000 there are these tests:\n\n\ttest_expect_success \\\n\t    'git archive' \\\n\t    'git archive HEAD >b.tar'\n\n\ttest_expect_success \\\n\t    'git tar-tree' \\\n\t    'git tar-tree HEAD >b2.tar'\n\n\ttest_expect_success \\\n\t    'git archive vs. git tar-tree' \\\n\t    'diff b.tar b2.tar'\n\nCould you add another one for archive with --output?  (The \"vs.\" test\ncould be switched to test_cmp and merged with the second one, but this\nshould be done in a separate patch.)  The same can be done for zip.  No\nuntarring or unzipping needed.\n\nt5000 is the right file for these tests, by the way, as they are not\nabout CR/LF issues, but basic archive functionality.\n\n> +\n> +'\n> +\n>  test_done\n> diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh\n> index c942c8b..a74dd4a 100755\n> --- a/t/t5000-tar-tree.sh\n> +++ b/t/t5000-tar-tree.sh\n> @@ -66,6 +66,11 @@ test_expect_success \\\n>      'remove ignored file' \\\n>      'rm a/ignored'\n>  \n> +\n> +#\n> +# Tar tests\n> +#\n> +\n>  test_expect_success \\\n>      'git archive' \\\n>      'git archive HEAD >b.tar'\n> @@ -159,6 +164,9 @@ test_expect_success \\\n>        diff g/prefix/a/substfile1.expected g/prefix/a/substfile1 &&\n>        diff a/substfile2 g/prefix/a/substfile2\n>  '\n> +#\n> +# Zip tests\n> +#\n>  \n>  test_expect_success \\\n>      'git archive --format=zip' \\\n> -- \n> 1.6.2.rc0.63.g7cbd0.dirty\n> \n\nThese comments are nice additions, but they don't belong in this patch.\nEven better would be to move the zip tests into their own file (also in\na separate patch).\n\nWe're getting there. :)\n\nThanks,\nRené\n"}]}