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