builtin-archive.con commit archive: remove args member from struct archiver (3453300)
   1/*
   2 * Copyright (c) 2006 Franck Bui-Huu
   3 * Copyright (c) 2006 Rene Scharfe
   4 */
   5#include "cache.h"
   6#include "builtin.h"
   7#include "archive.h"
   8#include "commit.h"
   9#include "tree-walk.h"
  10#include "exec_cmd.h"
  11#include "pkt-line.h"
  12#include "sideband.h"
  13#include "attr.h"
  14
  15static const char archive_usage[] = \
  16"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
  17
  18const struct archiver archivers[] = {
  19        { "tar", write_tar_archive, NULL },
  20        { "zip", write_zip_archive, parse_extra_zip_args },
  21};
  22
  23static int run_remote_archiver(const char *remote, int argc,
  24                               const char **argv)
  25{
  26        char *url, buf[LARGE_PACKET_MAX];
  27        int fd[2], i, len, rv;
  28        struct child_process *conn;
  29        const char *exec = "git-upload-archive";
  30        int exec_at = 0;
  31
  32        for (i = 1; i < argc; i++) {
  33                const char *arg = argv[i];
  34                if (!prefixcmp(arg, "--exec=")) {
  35                        if (exec_at)
  36                                die("multiple --exec specified");
  37                        exec = arg + 7;
  38                        exec_at = i;
  39                        break;
  40                }
  41        }
  42
  43        url = xstrdup(remote);
  44        conn = git_connect(fd, url, exec, 0);
  45
  46        for (i = 1; i < argc; i++) {
  47                if (i == exec_at)
  48                        continue;
  49                packet_write(fd[1], "argument %s\n", argv[i]);
  50        }
  51        packet_flush(fd[1]);
  52
  53        len = packet_read_line(fd[0], buf, sizeof(buf));
  54        if (!len)
  55                die("git-archive: expected ACK/NAK, got EOF");
  56        if (buf[len-1] == '\n')
  57                buf[--len] = 0;
  58        if (strcmp(buf, "ACK")) {
  59                if (len > 5 && !prefixcmp(buf, "NACK "))
  60                        die("git-archive: NACK %s", buf + 5);
  61                die("git-archive: protocol error");
  62        }
  63
  64        len = packet_read_line(fd[0], buf, sizeof(buf));
  65        if (len)
  66                die("git-archive: expected a flush");
  67
  68        /* Now, start reading from fd[0] and spit it out to stdout */
  69        rv = recv_sideband("archive", fd[0], 1, 2);
  70        close(fd[0]);
  71        close(fd[1]);
  72        rv |= finish_connect(conn);
  73
  74        return !!rv;
  75}
  76
  77static const struct archiver *lookup_archiver(const char *name)
  78{
  79        int i;
  80
  81        for (i = 0; i < ARRAY_SIZE(archivers); i++) {
  82                if (!strcmp(name, archivers[i].name))
  83                        return &archivers[i];
  84        }
  85        return NULL;
  86}
  87
  88void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
  89{
  90        ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
  91}
  92
  93void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
  94                       const char *prefix)
  95{
  96        const char *name = argv[0];
  97        const unsigned char *commit_sha1;
  98        time_t archive_time;
  99        struct tree *tree;
 100        const struct commit *commit;
 101        unsigned char sha1[20];
 102
 103        if (get_sha1(name, sha1))
 104                die("Not a valid object name");
 105
 106        commit = lookup_commit_reference_gently(sha1, 1);
 107        if (commit) {
 108                commit_sha1 = commit->object.sha1;
 109                archive_time = commit->date;
 110        } else {
 111                commit_sha1 = NULL;
 112                archive_time = time(NULL);
 113        }
 114
 115        tree = parse_tree_indirect(sha1);
 116        if (tree == NULL)
 117                die("not a tree object");
 118
 119        if (prefix) {
 120                unsigned char tree_sha1[20];
 121                unsigned int mode;
 122                int err;
 123
 124                err = get_tree_entry(tree->object.sha1, prefix,
 125                                     tree_sha1, &mode);
 126                if (err || !S_ISDIR(mode))
 127                        die("current working directory is untracked");
 128
 129                tree = parse_tree_indirect(tree_sha1);
 130        }
 131        ar_args->tree = tree;
 132        ar_args->commit_sha1 = commit_sha1;
 133        ar_args->commit = commit;
 134        ar_args->time = archive_time;
 135}
 136
 137int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
 138                struct archiver_args *args)
 139{
 140        const char *extra_argv[MAX_EXTRA_ARGS];
 141        int extra_argc = 0;
 142        const char *format = "tar";
 143        const char *base = "";
 144        int verbose = 0;
 145        int i;
 146
 147        for (i = 1; i < argc; i++) {
 148                const char *arg = argv[i];
 149
 150                if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
 151                        for (i = 0; i < ARRAY_SIZE(archivers); i++)
 152                                printf("%s\n", archivers[i].name);
 153                        exit(0);
 154                }
 155                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 156                        verbose = 1;
 157                        continue;
 158                }
 159                if (!prefixcmp(arg, "--format=")) {
 160                        format = arg + 9;
 161                        continue;
 162                }
 163                if (!prefixcmp(arg, "--prefix=")) {
 164                        base = arg + 9;
 165                        continue;
 166                }
 167                if (!strcmp(arg, "--")) {
 168                        i++;
 169                        break;
 170                }
 171                if (arg[0] == '-') {
 172                        if (extra_argc > MAX_EXTRA_ARGS - 1)
 173                                die("Too many extra options");
 174                        extra_argv[extra_argc++] = arg;
 175                        continue;
 176                }
 177                break;
 178        }
 179
 180        /* We need at least one parameter -- tree-ish */
 181        if (argc - 1 < i)
 182                usage(archive_usage);
 183        *ar = lookup_archiver(format);
 184        if (!*ar)
 185                die("Unknown archive format '%s'", format);
 186
 187        if (extra_argc) {
 188                if (!(*ar)->parse_extra)
 189                        die("'%s' format does not handle %s",
 190                            (*ar)->name, extra_argv[0]);
 191                args->extra = (*ar)->parse_extra(extra_argc, extra_argv);
 192        }
 193        args->verbose = verbose;
 194        args->base = base;
 195
 196        return i;
 197}
 198
 199static const char *extract_remote_arg(int *ac, const char **av)
 200{
 201        int ix, iy, cnt = *ac;
 202        int no_more_options = 0;
 203        const char *remote = NULL;
 204
 205        for (ix = iy = 1; ix < cnt; ix++) {
 206                const char *arg = av[ix];
 207                if (!strcmp(arg, "--"))
 208                        no_more_options = 1;
 209                if (!no_more_options) {
 210                        if (!prefixcmp(arg, "--remote=")) {
 211                                if (remote)
 212                                        die("Multiple --remote specified");
 213                                remote = arg + 9;
 214                                continue;
 215                        }
 216                        if (arg[0] != '-')
 217                                no_more_options = 1;
 218                }
 219                if (ix != iy)
 220                        av[iy] = arg;
 221                iy++;
 222        }
 223        if (remote) {
 224                av[--cnt] = NULL;
 225                *ac = cnt;
 226        }
 227        return remote;
 228}
 229
 230int cmd_archive(int argc, const char **argv, const char *prefix)
 231{
 232        const struct archiver *ar = NULL;
 233        struct archiver_args args;
 234        int tree_idx;
 235        const char *remote = NULL;
 236
 237        remote = extract_remote_arg(&argc, argv);
 238        if (remote)
 239                return run_remote_archiver(remote, argc, argv);
 240
 241        setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
 242
 243        tree_idx = parse_archive_args(argc, argv, &ar, &args);
 244        if (prefix == NULL)
 245                prefix = setup_git_directory();
 246
 247        argv += tree_idx;
 248        parse_treeish_arg(argv, &args, prefix);
 249        parse_pathspec_arg(argv + 1, &args);
 250
 251        return ar->write_archive(&args);
 252}