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