X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=archive.c;h=e2280df56723809c5af1d566f0721e2639fff10c;hb=e502f9e7e6ceb8dfbdb94e2675355847740fc28f;hp=b8b45bad77230b0258622e993dbbac3a20e3716c;hpb=1f8dc671557a7330eaee9c0296bfad669d26a1b8;p=git.git diff --git a/archive.c b/archive.c index b8b45bad7..e2280df56 100644 --- a/archive.c +++ b/archive.c @@ -1,7 +1,28 @@ #include "cache.h" #include "commit.h" +#include "tree-walk.h" #include "attr.h" #include "archive.h" +#include "parse-options.h" + +static char const * const archive_usage[] = { + "git archive [options] [path...]", + "git archive --list", + "git archive --remote [--exec ] [options] [path...]", + "git archive --remote [--exec ] --list", + NULL +}; + +#define USES_ZLIB_COMPRESSION 1 + +const struct archiver { + const char *name; + write_archive_fn_t write_archive; + unsigned int flags; +} archivers[] = { + { "tar", write_tar_archive }, + { "zip", write_zip_archive, USES_ZLIB_COMPRESSION }, +}; static void format_subst(const struct commit *commit, const char *src, size_t len, @@ -27,7 +48,7 @@ static void format_subst(const struct commit *commit, strbuf_add(&fmt, b + 8, c - b - 8); strbuf_add(buf, src, b - src); - format_commit_message(commit, fmt.buf, buf); + format_commit_message(commit, fmt.buf, buf, DATE_NORMAL); len -= c + 1 - src; src = c + 1; } @@ -155,3 +176,167 @@ int write_archive_entries(struct archiver_args *args, err = 0; return err; } + +static const struct archiver *lookup_archiver(const char *name) +{ + int i; + + if (!name) + return NULL; + + for (i = 0; i < ARRAY_SIZE(archivers); i++) { + if (!strcmp(name, archivers[i].name)) + return &archivers[i]; + } + return NULL; +} + +static void parse_pathspec_arg(const char **pathspec, + struct archiver_args *ar_args) +{ + ar_args->pathspec = get_pathspec(ar_args->base, pathspec); +} + +static 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; +} + +#define OPT__COMPR(s, v, h, p) \ + { OPTION_SET_INT, (s), NULL, (v), NULL, (h), \ + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) } +#define OPT__COMPR_HIDDEN(s, v, p) \ + { OPTION_SET_INT, (s), NULL, (v), NULL, "", \ + PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_HIDDEN, NULL, (p) } + +static int parse_archive_args(int argc, const char **argv, + const struct archiver **ar, struct archiver_args *args) +{ + const char *format = "tar"; + const char *base = NULL; + const char *remote = NULL; + const char *exec = NULL; + int compression_level = -1; + int verbose = 0; + int i; + int list = 0; + struct option opts[] = { + OPT_GROUP(""), + OPT_STRING(0, "format", &format, "fmt", "archive format"), + OPT_STRING(0, "prefix", &base, "prefix", + "prepend prefix to each pathname in the archive"), + OPT__VERBOSE(&verbose), + OPT__COMPR('0', &compression_level, "store only", 0), + OPT__COMPR('1', &compression_level, "compress faster", 1), + OPT__COMPR_HIDDEN('2', &compression_level, 2), + OPT__COMPR_HIDDEN('3', &compression_level, 3), + OPT__COMPR_HIDDEN('4', &compression_level, 4), + OPT__COMPR_HIDDEN('5', &compression_level, 5), + OPT__COMPR_HIDDEN('6', &compression_level, 6), + OPT__COMPR_HIDDEN('7', &compression_level, 7), + OPT__COMPR_HIDDEN('8', &compression_level, 8), + OPT__COMPR('9', &compression_level, "compress better", 9), + OPT_GROUP(""), + OPT_BOOLEAN('l', "list", &list, + "list supported archive formats"), + OPT_GROUP(""), + 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, opts, archive_usage, 0); + + if (remote) + die("Unexpected option --remote"); + if (exec) + die("Option --exec can only be used together with --remote"); + + if (!base) + base = ""; + + if (list) { + for (i = 0; i < ARRAY_SIZE(archivers); i++) + printf("%s\n", archivers[i].name); + exit(0); + } + + /* We need at least one parameter -- tree-ish */ + if (argc < 1) + usage_with_options(archive_usage, opts); + *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 argc; +} + +int write_archive(int argc, const char **argv, const char *prefix, + int setup_prefix) +{ + const struct archiver *ar = NULL; + struct archiver_args args; + + argc = parse_archive_args(argc, argv, &ar, &args); + if (setup_prefix && prefix == NULL) + prefix = setup_git_directory(); + + parse_treeish_arg(argv, &args, prefix); + parse_pathspec_arg(argv + 1, &args); + + return ar->write_archive(&args); +}