git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Dmitry Potapov <dpotapov@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, John Tapsell <johnflux@gmail.com>
Subject: [PATCH v2 2/2] teach git-archive to auto detect the output format
Date: Mon, 14 Sep 2009 00:17:01 +0400	[thread overview]
Message-ID: <20090913201701.GH30385@dpotapov.dyndns.org> (raw)
In-Reply-To: <7vzl8yr81j.fsf@alter.siamese.dyndns.org>

When I type something like this:
  git archive -o my-v2.0.zip v2.0
it is almost certainly that I want to create a zip archive, and not
a tar file.

This patch teaches git-archive to auto detect the output format from the
file name. Currently, only '.zip' is supported. If the auto detect failed,
the tar format is used as before. The auto detect is not used when the
output format is specified explicitly.

Signed-off-by: Dmitry Potapov <dpotapov@gmail.com>
---

I have corrected all remarks except this:

On Sun, Sep 13, 2009 at 11:52:56AM -0700, Junio C Hamano wrote:
> > +		sprintf(fmt_opt, "--format=%s", format);
> > +		argv[argc++] = fmt_opt;
> > +		argv[argc] = NULL;
> 
> Did you make sure you are allowed to write into argv[] and the array is
> large enough?  You probably need to make a copy of the array.

Either --output or --format option was used before, and this option is
extracted from argv[] by parse_options(). So it should be space for at
least one argument in argv.


 Documentation/git-archive.txt |   13 +++++++++++--
 builtin-archive.c             |   25 ++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt
index 1917f2e..3d1c1e7 100644
--- a/Documentation/git-archive.txt
+++ b/Documentation/git-archive.txt
@@ -34,8 +34,11 @@ OPTIONS
 -------
 
 --format=<fmt>::
-	Format of the resulting archive: 'tar' or 'zip'.  The default
-	is 'tar'.
+	Format of the resulting archive: 'tar' or 'zip'. If this option
+	is not given, and the output file is specified, the format is
+	inferred from the filename if possible (e.g. writing to "foo.zip"
+	makes the output to be in the zip format). Otherwise the output
+	format is `tar`.
 
 -l::
 --list::
@@ -130,6 +133,12 @@ git archive --format=zip --prefix=git-docs/ HEAD:Documentation/ > git-1.4.0-docs
 	Put everything in the current head's Documentation/ directory
 	into 'git-1.4.0-docs.zip', with the prefix 'git-docs/'.
 
+git archive -o latest.zip HEAD::
+
+	Create a Zip archive that contains the contents of the latest
+	commit on the current branch. Note that the output format is
+	inferred by the extension of the output file.
+
 
 SEE ALSO
 --------
diff --git a/builtin-archive.c b/builtin-archive.c
index 565314b..6efba6f 100644
--- a/builtin-archive.c
+++ b/builtin-archive.c
@@ -60,6 +60,17 @@ static int run_remote_archiver(int argc, const char **argv,
 	return !!rv;
 }
 
+static const char* format_from_name(const char *filename)
+{
+	const char *ext = strrchr(filename, '.');
+	if (!ext)
+		return NULL;
+	ext++;
+	if (!strcasecmp(ext, "zip"))
+		return "zip";
+	return NULL;
+}
+
 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | 	\
 			     PARSE_OPT_KEEP_ARGV0 | 	\
 			     PARSE_OPT_KEEP_UNKNOWN |	\
@@ -70,6 +81,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	const char *exec = "git-upload-archive";
 	const char *output = NULL;
 	const char *remote = NULL;
+	const char *format = NULL;
 	struct option local_opts[] = {
 		OPT_STRING('o', "output", &output, "file",
 			"write the archive to this file"),
@@ -77,14 +89,25 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 			"retrieve the archive from remote repository <repo>"),
 		OPT_STRING(0, "exec", &exec, "cmd",
 			"path to the remote git-upload-archive command"),
+		OPT_STRING(0, "format", &format, "fmt", "archive format"),
 		OPT_END()
 	};
+	char fmt_opt[32];
 
 	argc = parse_options(argc, argv, prefix, local_opts, NULL,
 			     PARSE_OPT_KEEP_ALL);
 
-	if (output)
+	if (output) {
 		create_output_file(output);
+		if (!format)
+			format = format_from_name(output);
+	}
+
+	if (format) {
+		sprintf(fmt_opt, "--format=%s", format);
+		argv[argc++] = fmt_opt;
+		argv[argc] = NULL;
+	}
 
 	if (remote)
 		return run_remote_archiver(argc, argv, remote, exec);
-- 
1.6.5.rc1.2.g6bb993

  reply	other threads:[~2009-09-13 20:22 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-09 21:27 obnoxious CLI complaints Brendan Miller
2009-09-09 21:54 ` Jakub Narebski
2009-09-09 22:06   ` Wincent Colaiuta
2009-09-10 16:50     ` Jakub Narebski
2009-09-10 18:53       ` Junio C Hamano
2009-09-10 22:19         ` René Scharfe
2009-09-11  3:15         ` Björn Steinbrink
2009-09-10 19:46       ` John Tapsell
2009-09-10 20:17         ` Sverre Rabbelier
2009-09-10 20:23         ` Jakub Narebski
2009-09-10 22:04           ` John Tapsell
2009-09-10 22:49             ` Junio C Hamano
2009-09-10 23:19               ` demerphq
2009-09-11  0:37                 ` Junio C Hamano
2009-09-11  0:18               ` John Tapsell
2009-09-11  0:25                 ` Junio C Hamano
2009-09-10  0:09   ` Brendan Miller
2009-09-10  1:25     ` Todd Zullinger
2009-09-10  9:16     ` Jakub Narebski
2009-09-10 18:18       ` Eric Schaefer
2009-09-10 18:52         ` Sverre Rabbelier
2009-09-10 22:19       ` René Scharfe
2009-09-11 14:47         ` Linus Torvalds
2009-09-11 22:01           ` René Scharfe
2009-09-11 22:16             ` Linus Torvalds
2009-09-12 10:31     ` Dmitry Potapov
2009-09-12 18:32       ` John Tapsell
2009-09-12 21:44         ` Dmitry Potapov
2009-09-12 22:21           ` John Tapsell
2009-09-12 22:35             ` A Large Angry SCM
2009-09-12 22:43             ` Dmitry Potapov
2009-09-12 23:08               ` John Tapsell
2009-09-13  2:47                 ` Junio C Hamano
2009-09-13 17:36                   ` [PATCH 1/2] git-archive: add '-o' as a alias for '--output' Dmitry Potapov
2009-09-13 17:36                     ` [PATCH 2/2] teach git-archive to auto detect the output format Dmitry Potapov
2009-09-13 18:52                       ` Junio C Hamano
2009-09-13 20:17                         ` Dmitry Potapov [this message]
2009-09-13 21:27                           ` [PATCH v2 " Junio C Hamano
2009-09-13 18:34                     ` [PATCH 1/2] git-archive: add '-o' as a alias for '--output' Junio C Hamano
2009-09-13 20:13                       ` [PATCH v2 " Dmitry Potapov
2009-09-17  0:48                   ` obnoxious CLI complaints Brendan Miller
2009-09-17  1:27                     ` Junio C Hamano
2009-09-09 21:58 ` Sverre Rabbelier
2009-09-09 22:58 ` Pierre Habouzit
2009-09-10  1:32 ` Björn Steinbrink
2009-09-10 18:54   ` Matthieu Moy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090913201701.GH30385@dpotapov.dyndns.org \
    --to=dpotapov@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johnflux@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).