git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5] git-archive: Add new option "--output" to write archive to a file instead of stdout.
@ 2009-02-23  8:25 carlos.duclos
  2009-02-25  6:13 ` [PATCH] git-archive: add --output=<file> to send output to a file Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: carlos.duclos @ 2009-02-23  8:25 UTC (permalink / raw)
  To: git

[-- Attachment #1: Type: text/plain, Size: 166 bytes --]

Patch attached as MIME.

Highlights:
1. Closing output_fd. Notice this is not needed since dup2 will close newfd.
2. Moved tests from t0024 to t5000.

Cheers!

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-git-archive-add-output-file-to-send-output-to-a.patch --]
[-- Type: text/x-diff; name="0001-git-archive-add-output-file-to-send-output-to-a.patch", Size: 3590 bytes --]

From 8bcb1f18b9bf94ad9b2ac5175095394e7db44a1a Mon Sep 17 00:00:00 2001
From: Carlos Manuel Duclos Vergara <carlos.duclos@nokia.com>
Date: Mon, 16 Feb 2009 18:20:25 +0100
Subject: [PATCH] git-archive: add --output=<file> to send output to a file instead of stdout.
 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>
---
 Documentation/git-archive.txt |    3 +++
 archive.c                     |   18 ++++++++++++++++++
 t/t5000-tar-tree.sh           |    8 ++++++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt
index 41cbf9c..5bde197 100644
--- a/Documentation/git-archive.txt
+++ b/Documentation/git-archive.txt
@@ -47,6 +47,9 @@ OPTIONS
 --prefix=<prefix>/::
 	Prepend <prefix>/ to each filename in the archive.
 
+--output=<file>::
+	Write the archive to <file> instead of stdout.
+
 <extra>::
 	This can be any options that the archiver backend understand.
 	See next section.
diff --git a/archive.c b/archive.c
index e6de039..3bf4a73 100644
--- a/archive.c
+++ b/archive.c
@@ -239,6 +239,18 @@ static void parse_treeish_arg(const char **argv,
 	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) \
 	{ OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
 	  PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }
@@ -253,6 +265,7 @@ static int parse_archive_args(int argc, const char **argv,
 	const char *base = NULL;
 	const char *remote = NULL;
 	const char *exec = NULL;
+	const char *output = NULL;
 	int compression_level = -1;
 	int verbose = 0;
 	int i;
@@ -262,6 +275,8 @@ static int parse_archive_args(int argc, const char **argv,
 		OPT_STRING(0, "format", &format, "fmt", "archive format"),
 		OPT_STRING(0, "prefix", &base, "prefix",
 			"prepend prefix to each pathname in the archive"),
+		OPT_STRING(0, "output", &output, "file",
+			"write the archive to this file"),
 		OPT__VERBOSE(&verbose),
 		OPT__COMPR('0', &compression_level, "store only", 0),
 		OPT__COMPR('1', &compression_level, "compress faster", 1),
@@ -294,6 +309,9 @@ static int parse_archive_args(int argc, const char **argv,
 	if (!base)
 		base = "";
 
+	if (output)
+		create_output_file(output);
+
 	if (list) {
 		for (i = 0; i < ARRAY_SIZE(archivers); i++)
 			printf("%s\n", archivers[i].name);
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index c942c8b..0d60e0e 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -79,6 +79,10 @@ test_expect_success \
     'diff b.tar b2.tar'
 
 test_expect_success \
+    'git archive' \
+    'git archive --output=z.tar HEAD'
+
+test_expect_success \
     'git archive in a bare repo' \
     '(cd bare.git && git archive HEAD) >b3.tar'
 
@@ -172,6 +176,10 @@ test_expect_success \
     'git archive --format=zip vs. the same in a bare repo' \
     'test_cmp d.zip d1.zip'
 
+test_expect_success \
+    'git archive --format=zip' \
+    'git archive --format=zip --output=z.zip HEAD'
+
 $UNZIP -v >/dev/null 2>&1
 if [ $? -eq 127 ]; then
 	echo "Skipping ZIP tests, because unzip was not found"
-- 
1.6.2.rc0.63.g7cbd0.dirty


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] git-archive: add --output=<file> to send output to a file
  2009-02-23  8:25 [PATCH v5] git-archive: Add new option "--output" to write archive to a file instead of stdout carlos.duclos
@ 2009-02-25  6:13 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2009-02-25  6:13 UTC (permalink / raw)
  To: Carlos Manuel Duclos Vergara; +Cc: git

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>
---
 * I've fixed it up slightly; especially the tests, which were not making
   sure the results match what are expected.

   Thanks.

 Documentation/git-archive.txt |    3 +++
 archive.c                     |   19 +++++++++++++++++++
 t/t5000-tar-tree.sh           |    8 ++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt
index 41cbf9c..5bde197 100644
--- a/Documentation/git-archive.txt
+++ b/Documentation/git-archive.txt
@@ -47,6 +47,9 @@ OPTIONS
 --prefix=<prefix>/::
 	Prepend <prefix>/ to each filename in the archive.
 
+--output=<file>::
+	Write the archive to <file> instead of stdout.
+
 <extra>::
 	This can be any options that the archiver backend understand.
 	See next section.
diff --git a/archive.c b/archive.c
index e6de039..c6aea83 100644
--- a/archive.c
+++ b/archive.c
@@ -239,6 +239,19 @@ static void parse_treeish_arg(const char **argv,
 	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) \
 	{ OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
 	  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 *remote = NULL;
 	const char *exec = NULL;
+	const char *output = NULL;
 	int compression_level = -1;
 	int verbose = 0;
 	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, "prefix", &base, "prefix",
 			"prepend prefix to each pathname in the archive"),
+		OPT_STRING(0, "output", &output, "file",
+			"write the archive to this file"),
 		OPT__VERBOSE(&verbose),
 		OPT__COMPR('0', &compression_level, "store only", 0),
 		OPT__COMPR('1', &compression_level, "compress faster", 1),
@@ -294,6 +310,9 @@ static int parse_archive_args(int argc, const char **argv,
 	if (!base)
 		base = "";
 
+	if (output)
+		create_output_file(output);
+
 	if (list) {
 		for (i = 0; i < ARRAY_SIZE(archivers); i++)
 			printf("%s\n", archivers[i].name);
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index c942c8b..b7e3628 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -86,6 +86,10 @@ test_expect_success \
     'git archive vs. the same in a bare repo' \
     '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 \
     'validate file modification time' \
     'mkdir extract &&
@@ -172,6 +176,10 @@ test_expect_success \
     'git archive --format=zip vs. the same in a bare repo' \
     '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
 if [ $? -eq 127 ]; then
 	echo "Skipping ZIP tests, because unzip was not found"
-- 
1.6.2.rc2

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-02-25  6:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-23  8:25 [PATCH v5] git-archive: Add new option "--output" to write archive to a file instead of stdout carlos.duclos
2009-02-25  6:13 ` [PATCH] git-archive: add --output=<file> to send output to a file Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).