From 6e94e6835f397cd2602ca1eb12002e51aa6b0500 Mon Sep 17 00:00:00 2001 From: Rene Scharfe Date: Fri, 25 Jul 2008 12:41:21 +0200 Subject: archive: add write_archive() Both archive and upload-archive have to parse command line arguments and then call the archiver specific write function. Move the duplicate code to a new function, write_archive(). Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index df97724696..502b339e6b 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -232,9 +232,6 @@ static const char *extract_remote_arg(int *ac, const char **av) int cmd_archive(int argc, const char **argv, const char *prefix) { - const struct archiver *ar = NULL; - struct archiver_args args; - int tree_idx; const char *remote = NULL; remote = extract_remote_arg(&argc, argv); @@ -243,13 +240,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix) setvbuf(stderr, NULL, _IOLBF, BUFSIZ); - tree_idx = parse_archive_args(argc, argv, &ar, &args); - if (prefix == NULL) - prefix = setup_git_directory(); - - argv += tree_idx; - parse_treeish_arg(argv, &args, prefix); - parse_pathspec_arg(argv + 1, &args); - - return ar->write_archive(&args); + return write_archive(argc, argv, prefix, 1); } -- cgit v1.2.1 From c0885435537e4b93709d2bf39ce36454186057a1 Mon Sep 17 00:00:00 2001 From: Rene Scharfe Date: Fri, 25 Jul 2008 12:41:22 +0200 Subject: archive: move parameter parsing code to archive.c write_archive() in archive.c is the only callsite for the command line parsing functions located in builtin-archive.c. Move them to the place where they are used, un-export them and make them static, as hinted at by Stephan. Cc: Stephan Beyer Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 137 ------------------------------------------------------ 1 file changed, 137 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 502b339e6b..4dd2716c0f 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -5,21 +5,9 @@ #include "cache.h" #include "builtin.h" #include "archive.h" -#include "commit.h" -#include "tree-walk.h" #include "pkt-line.h" #include "sideband.h" -static const char archive_usage[] = \ -"git archive --format= [--prefix=/] [--verbose] [] [path...]"; - -#define USES_ZLIB_COMPRESSION 1 - -const struct archiver archivers[] = { - { "tar", write_tar_archive }, - { "zip", write_zip_archive, USES_ZLIB_COMPRESSION }, -}; - static int run_remote_archiver(const char *remote, int argc, const char **argv) { @@ -74,131 +62,6 @@ static int run_remote_archiver(const char *remote, int argc, return !!rv; } -static const struct archiver *lookup_archiver(const char *name) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(archivers); i++) { - if (!strcmp(name, archivers[i].name)) - return &archivers[i]; - } - return NULL; -} - -void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args) -{ - ar_args->pathspec = get_pathspec(ar_args->base, pathspec); -} - -void parse_treeish_arg(const char **argv, struct archiver_args *ar_args, - const char *prefix) -{ - const char *name = argv[0]; - const unsigned char *commit_sha1; - time_t archive_time; - struct tree *tree; - const struct commit *commit; - unsigned char sha1[20]; - - if (get_sha1(name, sha1)) - die("Not a valid object name"); - - commit = lookup_commit_reference_gently(sha1, 1); - if (commit) { - commit_sha1 = commit->object.sha1; - archive_time = commit->date; - } else { - commit_sha1 = NULL; - archive_time = time(NULL); - } - - tree = parse_tree_indirect(sha1); - if (tree == NULL) - die("not a tree object"); - - if (prefix) { - unsigned char tree_sha1[20]; - unsigned int mode; - int err; - - err = get_tree_entry(tree->object.sha1, prefix, - tree_sha1, &mode); - if (err || !S_ISDIR(mode)) - die("current working directory is untracked"); - - tree = parse_tree_indirect(tree_sha1); - } - ar_args->tree = tree; - ar_args->commit_sha1 = commit_sha1; - ar_args->commit = commit; - ar_args->time = archive_time; -} - -int parse_archive_args(int argc, const char **argv, const struct archiver **ar, - struct archiver_args *args) -{ - const char *format = "tar"; - const char *base = ""; - int compression_level = -1; - int verbose = 0; - int i; - - for (i = 1; i < argc; i++) { - const char *arg = argv[i]; - - if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) { - for (i = 0; i < ARRAY_SIZE(archivers); i++) - printf("%s\n", archivers[i].name); - exit(0); - } - if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) { - verbose = 1; - continue; - } - if (!prefixcmp(arg, "--format=")) { - format = arg + 9; - continue; - } - if (!prefixcmp(arg, "--prefix=")) { - base = arg + 9; - continue; - } - if (!strcmp(arg, "--")) { - i++; - break; - } - if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') { - compression_level = arg[1] - '0'; - continue; - } - if (arg[0] == '-') - die("Unknown argument: %s", arg); - break; - } - - /* We need at least one parameter -- tree-ish */ - if (argc - 1 < i) - usage(archive_usage); - *ar = lookup_archiver(format); - if (!*ar) - die("Unknown archive format '%s'", format); - - args->compression_level = Z_DEFAULT_COMPRESSION; - if (compression_level != -1) { - if ((*ar)->flags & USES_ZLIB_COMPRESSION) - args->compression_level = compression_level; - else { - die("Argument not supported for format '%s': -%d", - format, compression_level); - } - } - args->verbose = verbose; - args->base = base; - args->baselen = strlen(base); - - return i; -} - static const char *extract_remote_arg(int *ac, const char **av) { int ix, iy, cnt = *ac; -- cgit v1.2.1 From 819b2b58246a7927376930e266b4ef8b43096115 Mon Sep 17 00:00:00 2001 From: Rene Scharfe Date: Fri, 25 Jul 2008 12:41:25 +0200 Subject: archive: allow --exec and --remote without equal sign Allow "--remote repo" and "--exec cmd" in addition to "--remote=repo" and "--exec=cmd" to make their usage consistent with parameters handled by parse_options(). Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 4dd2716c0f..22445acbfc 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -15,7 +15,7 @@ static int run_remote_archiver(const char *remote, int argc, int fd[2], i, len, rv; struct child_process *conn; const char *exec = "git-upload-archive"; - int exec_at = 0; + int exec_at = 0, exec_value_at = 0; for (i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -24,7 +24,14 @@ static int run_remote_archiver(const char *remote, int argc, die("multiple --exec specified"); exec = arg + 7; exec_at = i; - break; + } else if (!strcmp(arg, "--exec")) { + if (exec_at) + die("multiple --exec specified"); + if (i + 1 >= argc) + die("option --exec requires a value"); + exec = argv[i + 1]; + exec_at = i; + exec_value_at = ++i; } } @@ -32,7 +39,7 @@ static int run_remote_archiver(const char *remote, int argc, conn = git_connect(fd, url, exec, 0); for (i = 1; i < argc; i++) { - if (i == exec_at) + if (i == exec_at || i == exec_value_at) continue; packet_write(fd[1], "argument %s\n", argv[i]); } @@ -78,6 +85,13 @@ static const char *extract_remote_arg(int *ac, const char **av) die("Multiple --remote specified"); remote = arg + 9; continue; + } else if (!strcmp(arg, "--remote")) { + if (remote) + die("Multiple --remote specified"); + if (++ix >= cnt) + die("option --remote requires a value"); + remote = av[ix]; + continue; } if (arg[0] != '-') no_more_options = 1; -- cgit v1.2.1 From 34baebcee1d66be4cc096a69ba448cd24dcedf21 Mon Sep 17 00:00:00 2001 From: Heikki Orsila Date: Sat, 30 Aug 2008 14:12:53 +0300 Subject: Start conforming code to "git subcmd" style User notifications are presented as 'git cmd', and code comments are presented as '"cmd"' or 'git's cmd', rather than 'git-cmd'. Signed-off-by: Heikki Orsila Signed-off-by: Junio C Hamano --- builtin-archive.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 22445acbfc..5ceec433fd 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -47,18 +47,18 @@ static int run_remote_archiver(const char *remote, int argc, len = packet_read_line(fd[0], buf, sizeof(buf)); if (!len) - die("git-archive: expected ACK/NAK, got EOF"); + die("git archive: expected ACK/NAK, got EOF"); if (buf[len-1] == '\n') buf[--len] = 0; if (strcmp(buf, "ACK")) { if (len > 5 && !prefixcmp(buf, "NACK ")) - die("git-archive: NACK %s", buf + 5); - die("git-archive: protocol error"); + die("git archive: NACK %s", buf + 5); + die("git archive: protocol error"); } len = packet_read_line(fd[0], buf, sizeof(buf)); if (len) - die("git-archive: expected a flush"); + die("git archive: expected a flush"); /* Now, start reading from fd[0] and spit it out to stdout */ rv = recv_sideband("archive", fd[0], 1, 2); -- cgit v1.2.1 From b99b5b40cffb5269e4aa38b6b60391b55039e27d Mon Sep 17 00:00:00 2001 From: Charles Bailey Date: Thu, 18 Sep 2008 21:01:20 +0100 Subject: Make git archive respect core.autocrlf when creating zip format archives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is currently no call to git_config at the start of cmd_archive. When creating tar archives the core config is read as a side-effect of reading the tar specific config, but this doesn't happen for zip archives. The consequence is that in a configuration with core.autocrlf set, although files in a tar archive are created with crlf line endings, files in a zip archive retain unix line endings. Signed-off-by: Charles Bailey Acked-by: René Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 5ceec433fd..432ce2acc6 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -111,6 +111,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix) { const char *remote = NULL; + git_config(git_default_config, NULL); + remote = extract_remote_arg(&argc, argv); if (remote) return run_remote_archiver(remote, argc, argv); -- cgit v1.2.1 From ddff8563510a2c5c675d488a02e2642306430fc1 Mon Sep 17 00:00:00 2001 From: Charles Bailey Date: Sat, 25 Oct 2008 11:38:14 -0400 Subject: git-archive: work in bare repos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This moves the call to git_config to a place where it doesn't break the logic for using git archive in a bare repository but retains the fix to make git archive respect core.autocrlf. Tests are by René Scharfe. Signed-off-by: Charles Bailey Tested-by: Deskin Miller Signed-off-by: Junio C Hamano --- builtin-archive.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 432ce2acc6..5ceec433fd 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -111,8 +111,6 @@ int cmd_archive(int argc, const char **argv, const char *prefix) { const char *remote = NULL; - git_config(git_default_config, NULL); - remote = extract_remote_arg(&argc, argv); if (remote) return run_remote_archiver(remote, argc, argv); -- cgit v1.2.1 From 52e7787609d18af76a8c1befb0a06123fb7ce89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sun, 8 Mar 2009 19:21:53 +0100 Subject: archive: use parseopt for local-only options Replace the hand-rolled parsers that find and remove --remote and --exec by a parseopt parser that also handles --output. All three options only have a meaning if no remote server is used or on the local side. They must be rejected by upload-archive and should not be sent to the server by archive. We can't use a single parser for both remote and local side because the remote end possibly understands a different set of options than the local side. A local parser would then wrongly accuse options valid on the other side as being incorrect. This patch implements a very forgiving parser that understands only the three options mentioned above. All others are passed to the normal, complete parser in archive.c (running either locally in archive, or remotely in upload-archive). This normal parser definition contains dummy entries for the three options, in order for them to appear in the help screen. The parseopt parser allows multiple occurrences of --remote and --exec unlike the previous one; the one specified last wins. This looseness is acceptable, I think. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 103 ++++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 65 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 5ceec433fd..60adef9363 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -5,44 +5,35 @@ #include "cache.h" #include "builtin.h" #include "archive.h" +#include "parse-options.h" #include "pkt-line.h" #include "sideband.h" -static int run_remote_archiver(const char *remote, int argc, - const char **argv) +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); + } +} + +static int run_remote_archiver(int argc, const char **argv, + const char *remote, const char *exec) { char *url, buf[LARGE_PACKET_MAX]; int fd[2], i, len, rv; struct child_process *conn; - const char *exec = "git-upload-archive"; - int exec_at = 0, exec_value_at = 0; - - for (i = 1; i < argc; i++) { - const char *arg = argv[i]; - if (!prefixcmp(arg, "--exec=")) { - if (exec_at) - die("multiple --exec specified"); - exec = arg + 7; - exec_at = i; - } else if (!strcmp(arg, "--exec")) { - if (exec_at) - die("multiple --exec specified"); - if (i + 1 >= argc) - die("option --exec requires a value"); - exec = argv[i + 1]; - exec_at = i; - exec_value_at = ++i; - } - } url = xstrdup(remote); conn = git_connect(fd, url, exec, 0); - for (i = 1; i < argc; i++) { - if (i == exec_at || i == exec_value_at) - continue; + for (i = 1; i < argc; i++) packet_write(fd[1], "argument %s\n", argv[i]); - } packet_flush(fd[1]); len = packet_read_line(fd[0], buf, sizeof(buf)); @@ -69,51 +60,33 @@ static int run_remote_archiver(const char *remote, int argc, return !!rv; } -static const char *extract_remote_arg(int *ac, const char **av) -{ - int ix, iy, cnt = *ac; - int no_more_options = 0; - const char *remote = NULL; - - for (ix = iy = 1; ix < cnt; ix++) { - const char *arg = av[ix]; - if (!strcmp(arg, "--")) - no_more_options = 1; - if (!no_more_options) { - if (!prefixcmp(arg, "--remote=")) { - if (remote) - die("Multiple --remote specified"); - remote = arg + 9; - continue; - } else if (!strcmp(arg, "--remote")) { - if (remote) - die("Multiple --remote specified"); - if (++ix >= cnt) - die("option --remote requires a value"); - remote = av[ix]; - continue; - } - if (arg[0] != '-') - no_more_options = 1; - } - if (ix != iy) - av[iy] = arg; - iy++; - } - if (remote) { - av[--cnt] = NULL; - *ac = cnt; - } - return remote; -} +#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \ + PARSE_OPT_KEEP_ARGV0 | \ + PARSE_OPT_KEEP_UNKNOWN | \ + PARSE_OPT_NO_INTERNAL_HELP ) 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; + struct option local_opts[] = { + OPT_STRING(0, "output", &output, "file", + "write the archive to this file"), + OPT_STRING(0, "remote", &remote, "repo", + "retrieve the archive from remote repository "), + OPT_STRING(0, "exec", &exec, "cmd", + "path to the remote git-upload-archive command"), + OPT_END() + }; + + argc = parse_options(argc, argv, local_opts, NULL, PARSE_OPT_KEEP_ALL); + + if (output) + create_output_file(output); - remote = extract_remote_arg(&argc, argv); if (remote) - return run_remote_archiver(remote, argc, argv); + return run_remote_archiver(argc, argv, remote, exec); setvbuf(stderr, NULL, _IOLBF, BUFSIZ); -- cgit v1.2.1 From 34df8abaf358c83cc1447d0a81bda7848685a1c9 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 10 Mar 2009 22:54:17 +0100 Subject: recv_sideband: Bands #2 and #3 always go to stderr This removes the last parameter of recv_sideband, by which the callers told which channel bands #2 and #3 should be written to. Sayeth Shawn Pearce: The definition of the streams in the current sideband protocol are rather well defined for the one protocol that uses it, fetch-pack/receive-pack: stream #1: pack data stream #2: stderr messages, progress, meant for tty stream #3: abort message, remote is dead, goodbye! Since both callers of the function passed 2 for the parameter, we hereby remove it and send bands #2 and #3 to stderr explicitly using fprintf. This has the nice side-effect that these two streams pass through our ANSI emulation layer on Windows. Signed-off-by: Johannes Sixt Acked-by: Nicolas Pitre Signed-off-by: Junio C Hamano --- builtin-archive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 60adef9363..ab50cebba0 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -52,7 +52,7 @@ static int run_remote_archiver(int argc, const char **argv, die("git archive: expected a flush"); /* Now, start reading from fd[0] and spit it out to stdout */ - rv = recv_sideband("archive", fd[0], 1, 2); + rv = recv_sideband("archive", fd[0], 1); close(fd[0]); close(fd[1]); rv |= finish_connect(conn); -- cgit v1.2.1 From 377829201783b8a648e07af6ce7d747e0f45dc19 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Sat, 23 May 2009 11:53:12 -0700 Subject: parse-opts: prepare for OPT_FILENAME To give OPT_FILENAME the prefix, we pass the prefix to parse_options() which passes the prefix to parse_options_start() which sets the prefix member of parse_opts_ctx accordingly. If there isn't a prefix in the calling context, passing NULL will suffice. Signed-off-by: Stephen Boyd Signed-off-by: Junio C Hamano --- builtin-archive.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index ab50cebba0..3c5a5a7822 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -80,7 +80,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix) OPT_END() }; - argc = parse_options(argc, argv, local_opts, NULL, PARSE_OPT_KEEP_ALL); + argc = parse_options(argc, argv, prefix, local_opts, NULL, + PARSE_OPT_KEEP_ALL); if (output) create_output_file(output); -- cgit v1.2.1 From 0721c314a5c8fddc877140ab5a333c42c62f780d Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:47 +0200 Subject: Use die_errno() instead of die() when checking syscalls Lots of die() calls did not actually report the kind of error, which can leave the user confused as to the real problem. Use die_errno() where we check a system/library call that sets errno on failure, or one of the following that wrap such calls: Function Passes on error from -------- -------------------- odb_pack_keep open read_ancestry fopen read_in_full xread strbuf_read xread strbuf_read_file open or strbuf_read_file strbuf_readlink readlink write_in_full xwrite Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin-archive.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 3c5a5a7822..f9a4bea41e 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -13,10 +13,10 @@ 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); + die_errno("could not create archive file '%s'", output_file); if (output_fd != 1) { if (dup2(output_fd, 1) < 0) - die("could not redirect output"); + die_errno("could not redirect output"); else close(output_fd); } -- cgit v1.2.1 From 05d3951ec9c531d348fe0dbb9ae058d38728a550 Mon Sep 17 00:00:00 2001 From: Dmitry Potapov Date: Sun, 13 Sep 2009 16:05:52 +0400 Subject: git-archive: add '-o' as a alias for '--output' The '-o' option is commonly used in many tools to specify the output file. Typing '--output' every time is a bit too long to be a practical alternative to redirecting output. But specifying the output name has the advantage of making possible to guess the desired output format by filename extension. Signed-off-by: Dmitry Potapov Signed-off-by: Junio C Hamano --- builtin-archive.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index f9a4bea41e..565314b04c 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -71,7 +71,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix) const char *output = NULL; const char *remote = NULL; struct option local_opts[] = { - OPT_STRING(0, "output", &output, "file", + OPT_STRING('o', "output", &output, "file", "write the archive to this file"), OPT_STRING(0, "remote", &remote, "repo", "retrieve the archive from remote repository "), -- cgit v1.2.1 From 0f4b377c20fb7d93f8bfeec39efb2b9392d6aebc Mon Sep 17 00:00:00 2001 From: Dmitry Potapov Date: Mon, 14 Sep 2009 00:17:01 +0400 Subject: git-archive: infer output format from filename when unspecified A command line $ git archive -o my-v2.0.zip v2.0 almost certainly wants the output in zip format, even though it does not specify any --format option. When --format is not given, but output filename is, try to infer what format is requested from the filename extension. Currently this code only knows about '.zip'. When the format is unspecified and the filename does not tell us, the output will be in 'tar' format as before. Of course, an explicit --format will not trigger this guesswork. Signed-off-by: Dmitry Potapov Signed-off-by: Junio C Hamano --- builtin-archive.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 565314b04c..12351e9dd5 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,31 @@ int cmd_archive(int argc, const char **argv, const char *prefix) "retrieve the archive from remote repository "), 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); + /* + * This is safe because either --format and/or --output must + * have been given on the original command line if we get to + * this point, and parse_options() must have eaten at least + * one argument, i.e. we have enough room to append to argv[]. + */ + argv[argc++] = fmt_opt; + argv[argc] = NULL; + } if (remote) return run_remote_archiver(argc, argv, remote, exec); -- cgit v1.2.1 From 782a0005fcb26bb7ef27f720fd139ae40a6f434b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 10 Dec 2009 15:27:51 -0800 Subject: Fix archive format with -- on the command line Giving --format from the command line, or using output file extention to DWIM the output format, with a pathspec that is disambiguated with an explicit double-dash on the command line, e.g. git archive -o file --format=zip HEAD -- path git archive -o file.zip HEAD -- path didn't work correctly. This was because the code reordered (when one was given) or added (when the format was inferred) a --format argument at the end, effectively making it to "archive HEAD -- path --format=zip", i.e. an extra pathspec that is unlikely to match anything. The command line argument list should always be "options, revs and then paths", and we should set a good example by inserting the --format at the beginning instead. Reported-by: Ilari Liusvaara Signed-off-by: Junio C Hamano --- builtin-archive.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 12351e9dd5..446d6bff30 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -106,13 +106,17 @@ int cmd_archive(int argc, const char **argv, const char *prefix) if (format) { sprintf(fmt_opt, "--format=%s", format); /* - * This is safe because either --format and/or --output must - * have been given on the original command line if we get to - * this point, and parse_options() must have eaten at least - * one argument, i.e. we have enough room to append to argv[]. + * We have enough room in argv[] to muck it in place, + * because either --format and/or --output must have + * been given on the original command line if we get + * to this point, and parse_options() must have eaten + * it, i.e. we can add back one element to the array. + * But argv[] may contain "--"; we should make it the + * first option. */ - argv[argc++] = fmt_opt; - argv[argc] = NULL; + memmove(argv + 2, argv + 1, sizeof(*argv) * argc); + argv[1] = fmt_opt; + argv[++argc] = NULL; } if (remote) -- cgit v1.2.1 From b236752a8722c77b5a9b4ed488a992ee05252843 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Wed, 9 Dec 2009 17:26:33 +0200 Subject: Support remote archive from all smart transports Previously, remote archive required internal (non remote-helper) smart transport. Extend the remote archive to also support smart transports implemented by remote helpers. Signed-off-by: Ilari Liusvaara Signed-off-by: Junio C Hamano --- builtin-archive.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 12351e9dd5..d34b3fd028 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -5,6 +5,7 @@ #include "cache.h" #include "builtin.h" #include "archive.h" +#include "transport.h" #include "parse-options.h" #include "pkt-line.h" #include "sideband.h" @@ -25,12 +26,16 @@ static void create_output_file(const char *output_file) static int run_remote_archiver(int argc, const char **argv, const char *remote, const char *exec) { - char *url, buf[LARGE_PACKET_MAX]; + char buf[LARGE_PACKET_MAX]; int fd[2], i, len, rv; - struct child_process *conn; + struct transport *transport; + struct remote *_remote; - url = xstrdup(remote); - conn = git_connect(fd, url, exec, 0); + _remote = remote_get(remote); + if (!_remote->url[0]) + die("git archive: Remote with no URL"); + transport = transport_get(_remote, _remote->url[0]); + transport_connect(transport, "git-upload-archive", exec, fd); for (i = 1; i < argc; i++) packet_write(fd[1], "argument %s\n", argv[i]); @@ -53,9 +58,7 @@ static int run_remote_archiver(int argc, const char **argv, /* Now, start reading from fd[0] and spit it out to stdout */ rv = recv_sideband("archive", fd[0], 1); - close(fd[0]); - close(fd[1]); - rv |= finish_connect(conn); + rv |= transport_disconnect(transport); return !!rv; } -- cgit v1.2.1 From fe12d8e84f745303d64757307e9a6a81a6608018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Mon, 8 Feb 2010 00:30:20 +0100 Subject: archive: simplify archive format guessing The code to guess an output archive's format consumed any --format options and built a new one. Jonathan noticed that it does so in an unsafe way, risking to overflow the static buffer fmt_opt. Change the code to keep the existing --format options intact and to only add a new one if a format could be guessed based on the output file name. The new option is added as the first one, allowing the existing ones to overrule it, i.e. explicit --format options given on the command line win over format guesses, as before. To simplify the code further, format_from_name() is changed to return the full --format option, thus no potentially dangerous sprintf() calls are needed any more. Reported-by: Jonathan Nieder Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-archive.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c index 446d6bff30..faf4554d5e 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -67,7 +67,7 @@ static const char *format_from_name(const char *filename) return NULL; ext++; if (!strcasecmp(ext, "zip")) - return "zip"; + return "--format=zip"; return NULL; } @@ -81,7 +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; + const char *format_option = NULL; struct option local_opts[] = { OPT_STRING('o', "output", &output, "file", "write the archive to this file"), @@ -89,33 +89,31 @@ int cmd_archive(int argc, const char **argv, const char *prefix) "retrieve the archive from remote repository "), 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) { create_output_file(output); - if (!format) - format = format_from_name(output); + format_option = format_from_name(output); } - if (format) { - sprintf(fmt_opt, "--format=%s", format); - /* - * We have enough room in argv[] to muck it in place, - * because either --format and/or --output must have - * been given on the original command line if we get - * to this point, and parse_options() must have eaten - * it, i.e. we can add back one element to the array. - * But argv[] may contain "--"; we should make it the - * first option. - */ + /* + * We have enough room in argv[] to muck it in place, because + * --output must have been given on the original command line + * if we get to this point, and parse_options() must have eaten + * it, i.e. we can add back one element to the array. + * + * We add a fake --format option at the beginning, with the + * format inferred from our output filename. This way explicit + * --format options can override it, and the fake option is + * inserted before any "--" that might have been given. + */ + if (format_option) { memmove(argv + 2, argv + 1, sizeof(*argv) * argc); - argv[1] = fmt_opt; + argv[1] = format_option; argv[++argc] = NULL; } -- cgit v1.2.1 From 81b50f3ce40bfdd66e5d967bf82be001039a9a98 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 22 Feb 2010 08:42:18 -0800 Subject: Move 'builtin-*' into a 'builtin/' subdirectory This shrinks the top-level directory a bit, and makes it much more pleasant to use auto-completion on the thing. Instead of [torvalds@nehalem git]$ em buil Display all 180 possibilities? (y or n) [torvalds@nehalem git]$ em builtin-sh builtin-shortlog.c builtin-show-branch.c builtin-show-ref.c builtin-shortlog.o builtin-show-branch.o builtin-show-ref.o [torvalds@nehalem git]$ em builtin-shor builtin-shortlog.c builtin-shortlog.o [torvalds@nehalem git]$ em builtin-shortlog.c you get [torvalds@nehalem git]$ em buil [type] builtin/ builtin.h [torvalds@nehalem git]$ em builtin [auto-completes to] [torvalds@nehalem git]$ em builtin/sh [type] shortlog.c shortlog.o show-branch.c show-branch.o show-ref.c show-ref.o [torvalds@nehalem git]$ em builtin/sho [auto-completes to] [torvalds@nehalem git]$ em builtin/shor [type] shortlog.c shortlog.o [torvalds@nehalem git]$ em builtin/shortlog.c which doesn't seem all that different, but not having that annoying break in "Display all 180 possibilities?" is quite a relief. NOTE! If you do this in a clean tree (no object files etc), or using an editor that has auto-completion rules that ignores '*.o' files, you won't see that annoying 'Display all 180 possibilities?' message - it will just show the choices instead. I think bash has some cut-off around 100 choices or something. So the reason I see this is that I'm using an odd editory, and thus don't have the rules to cut down on auto-completion. But you can simulate that by using 'ls' instead, or something similar. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin-archive.c | 129 ------------------------------------------------------ 1 file changed, 129 deletions(-) delete mode 100644 builtin-archive.c (limited to 'builtin-archive.c') diff --git a/builtin-archive.c b/builtin-archive.c deleted file mode 100644 index 6a887f5a9d..0000000000 --- a/builtin-archive.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2006 Franck Bui-Huu - * Copyright (c) 2006 Rene Scharfe - */ -#include "cache.h" -#include "builtin.h" -#include "archive.h" -#include "transport.h" -#include "parse-options.h" -#include "pkt-line.h" -#include "sideband.h" - -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_errno("could not create archive file '%s'", output_file); - if (output_fd != 1) { - if (dup2(output_fd, 1) < 0) - die_errno("could not redirect output"); - else - close(output_fd); - } -} - -static int run_remote_archiver(int argc, const char **argv, - const char *remote, const char *exec) -{ - char buf[LARGE_PACKET_MAX]; - int fd[2], i, len, rv; - struct transport *transport; - struct remote *_remote; - - _remote = remote_get(remote); - if (!_remote->url[0]) - die("git archive: Remote with no URL"); - transport = transport_get(_remote, _remote->url[0]); - transport_connect(transport, "git-upload-archive", exec, fd); - - for (i = 1; i < argc; i++) - packet_write(fd[1], "argument %s\n", argv[i]); - packet_flush(fd[1]); - - len = packet_read_line(fd[0], buf, sizeof(buf)); - if (!len) - die("git archive: expected ACK/NAK, got EOF"); - if (buf[len-1] == '\n') - buf[--len] = 0; - if (strcmp(buf, "ACK")) { - if (len > 5 && !prefixcmp(buf, "NACK ")) - die("git archive: NACK %s", buf + 5); - die("git archive: protocol error"); - } - - len = packet_read_line(fd[0], buf, sizeof(buf)); - if (len) - die("git archive: expected a flush"); - - /* Now, start reading from fd[0] and spit it out to stdout */ - rv = recv_sideband("archive", fd[0], 1); - rv |= transport_disconnect(transport); - - 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 "--format=zip"; - return NULL; -} - -#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \ - PARSE_OPT_KEEP_ARGV0 | \ - PARSE_OPT_KEEP_UNKNOWN | \ - PARSE_OPT_NO_INTERNAL_HELP ) - -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_option = NULL; - struct option local_opts[] = { - OPT_STRING('o', "output", &output, "file", - "write the archive to this file"), - OPT_STRING(0, "remote", &remote, "repo", - "retrieve the archive from remote repository "), - OPT_STRING(0, "exec", &exec, "cmd", - "path to the remote git-upload-archive command"), - OPT_END() - }; - - argc = parse_options(argc, argv, prefix, local_opts, NULL, - PARSE_OPT_KEEP_ALL); - - if (output) { - create_output_file(output); - format_option = format_from_name(output); - } - - /* - * We have enough room in argv[] to muck it in place, because - * --output must have been given on the original command line - * if we get to this point, and parse_options() must have eaten - * it, i.e. we can add back one element to the array. - * - * We add a fake --format option at the beginning, with the - * format inferred from our output filename. This way explicit - * --format options can override it, and the fake option is - * inserted before any "--" that might have been given. - */ - if (format_option) { - memmove(argv + 2, argv + 1, sizeof(*argv) * argc); - argv[1] = format_option; - argv[++argc] = NULL; - } - - if (remote) - return run_remote_archiver(argc, argv, remote, exec); - - setvbuf(stderr, NULL, _IOLBF, BUFSIZ); - - return write_archive(argc, argv, prefix, 1); -} -- cgit v1.2.1