builtin-archive.con commit Add infrastructure to run a function asynchronously. (2d22c20)
   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
  18static struct archiver_desc
  19{
  20        const char *name;
  21        write_archive_fn_t write_archive;
  22        parse_extra_args_fn_t parse_extra;
  23} archivers[] = {
  24        { "tar", write_tar_archive, NULL },
  25        { "zip", write_zip_archive, parse_extra_zip_args },
  26};
  27
  28static int run_remote_archiver(const char *remote, int argc,
  29                               const char **argv)
  30{
  31        char *url, buf[LARGE_PACKET_MAX];
  32        int fd[2], i, len, rv;
  33        struct child_process *conn;
  34        const char *exec = "git-upload-archive";
  35        int exec_at = 0;
  36
  37        for (i = 1; i < argc; i++) {
  38                const char *arg = argv[i];
  39                if (!prefixcmp(arg, "--exec=")) {
  40                        if (exec_at)
  41                                die("multiple --exec specified");
  42                        exec = arg + 7;
  43                        exec_at = i;
  44                        break;
  45                }
  46        }
  47
  48        url = xstrdup(remote);
  49        conn = git_connect(fd, url, exec, 0);
  50
  51        for (i = 1; i < argc; i++) {
  52                if (i == exec_at)
  53                        continue;
  54                packet_write(fd[1], "argument %s\n", argv[i]);
  55        }
  56        packet_flush(fd[1]);
  57
  58        len = packet_read_line(fd[0], buf, sizeof(buf));
  59        if (!len)
  60                die("git-archive: expected ACK/NAK, got EOF");
  61        if (buf[len-1] == '\n')
  62                buf[--len] = 0;
  63        if (strcmp(buf, "ACK")) {
  64                if (len > 5 && !prefixcmp(buf, "NACK "))
  65                        die("git-archive: NACK %s", buf + 5);
  66                die("git-archive: protocol error");
  67        }
  68
  69        len = packet_read_line(fd[0], buf, sizeof(buf));
  70        if (len)
  71                die("git-archive: expected a flush");
  72
  73        /* Now, start reading from fd[0] and spit it out to stdout */
  74        rv = recv_sideband("archive", fd[0], 1, 2);
  75        close(fd[0]);
  76        close(fd[1]);
  77        rv |= finish_connect(conn);
  78
  79        return !!rv;
  80}
  81
  82static void format_subst(const struct commit *commit,
  83                         const char *src, size_t len,
  84                         struct strbuf *buf)
  85{
  86        char *to_free = NULL;
  87        struct strbuf fmt;
  88
  89        if (src == buf->buf)
  90                to_free = strbuf_detach(buf, NULL);
  91        strbuf_init(&fmt, 0);
  92        for (;;) {
  93                const char *b, *c;
  94
  95                b = memmem(src, len, "$Format:", 8);
  96                if (!b || src + len < b + 9)
  97                        break;
  98                c = memchr(b + 8, '$', len - 8);
  99                if (!c)
 100                        break;
 101
 102                strbuf_reset(&fmt);
 103                strbuf_add(&fmt, b + 8, c - b - 8);
 104
 105                strbuf_add(buf, src, b - src);
 106                format_commit_message(commit, fmt.buf, buf);
 107                len -= c + 1 - src;
 108                src  = c + 1;
 109        }
 110        strbuf_add(buf, src, len);
 111        strbuf_release(&fmt);
 112        free(to_free);
 113}
 114
 115static int convert_to_archive(const char *path,
 116                              const void *src, size_t len,
 117                              struct strbuf *buf,
 118                              const struct commit *commit)
 119{
 120        static struct git_attr *attr_export_subst;
 121        struct git_attr_check check[1];
 122
 123        if (!commit)
 124                return 0;
 125
 126        if (!attr_export_subst)
 127                attr_export_subst = git_attr("export-subst", 12);
 128
 129        check[0].attr = attr_export_subst;
 130        if (git_checkattr(path, ARRAY_SIZE(check), check))
 131                return 0;
 132        if (!ATTR_TRUE(check[0].value))
 133                return 0;
 134
 135        format_subst(commit, src, len, buf);
 136        return 1;
 137}
 138
 139void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
 140                           unsigned int mode, enum object_type *type,
 141                           unsigned long *sizep,
 142                           const struct commit *commit)
 143{
 144        void *buffer;
 145
 146        buffer = read_sha1_file(sha1, type, sizep);
 147        if (buffer && S_ISREG(mode)) {
 148                struct strbuf buf;
 149
 150                strbuf_init(&buf, 0);
 151                strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
 152                convert_to_working_tree(path, buf.buf, buf.len, &buf);
 153                convert_to_archive(path, buf.buf, buf.len, &buf, commit);
 154                buffer = strbuf_detach(&buf, sizep);
 155        }
 156
 157        return buffer;
 158}
 159
 160static int init_archiver(const char *name, struct archiver *ar)
 161{
 162        int rv = -1, i;
 163
 164        for (i = 0; i < ARRAY_SIZE(archivers); i++) {
 165                if (!strcmp(name, archivers[i].name)) {
 166                        memset(ar, 0, sizeof(*ar));
 167                        ar->name = archivers[i].name;
 168                        ar->write_archive = archivers[i].write_archive;
 169                        ar->parse_extra = archivers[i].parse_extra;
 170                        rv = 0;
 171                        break;
 172                }
 173        }
 174        return rv;
 175}
 176
 177void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
 178{
 179        ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
 180}
 181
 182void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
 183                       const char *prefix)
 184{
 185        const char *name = argv[0];
 186        const unsigned char *commit_sha1;
 187        time_t archive_time;
 188        struct tree *tree;
 189        const struct commit *commit;
 190        unsigned char sha1[20];
 191
 192        if (get_sha1(name, sha1))
 193                die("Not a valid object name");
 194
 195        commit = lookup_commit_reference_gently(sha1, 1);
 196        if (commit) {
 197                commit_sha1 = commit->object.sha1;
 198                archive_time = commit->date;
 199        } else {
 200                commit_sha1 = NULL;
 201                archive_time = time(NULL);
 202        }
 203
 204        tree = parse_tree_indirect(sha1);
 205        if (tree == NULL)
 206                die("not a tree object");
 207
 208        if (prefix) {
 209                unsigned char tree_sha1[20];
 210                unsigned int mode;
 211                int err;
 212
 213                err = get_tree_entry(tree->object.sha1, prefix,
 214                                     tree_sha1, &mode);
 215                if (err || !S_ISDIR(mode))
 216                        die("current working directory is untracked");
 217
 218                tree = parse_tree_indirect(tree_sha1);
 219        }
 220        ar_args->tree = tree;
 221        ar_args->commit_sha1 = commit_sha1;
 222        ar_args->commit = commit;
 223        ar_args->time = archive_time;
 224}
 225
 226int parse_archive_args(int argc, const char **argv, struct archiver *ar)
 227{
 228        const char *extra_argv[MAX_EXTRA_ARGS];
 229        int extra_argc = 0;
 230        const char *format = "tar";
 231        const char *base = "";
 232        int verbose = 0;
 233        int i;
 234
 235        for (i = 1; i < argc; i++) {
 236                const char *arg = argv[i];
 237
 238                if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
 239                        for (i = 0; i < ARRAY_SIZE(archivers); i++)
 240                                printf("%s\n", archivers[i].name);
 241                        exit(0);
 242                }
 243                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 244                        verbose = 1;
 245                        continue;
 246                }
 247                if (!prefixcmp(arg, "--format=")) {
 248                        format = arg + 9;
 249                        continue;
 250                }
 251                if (!prefixcmp(arg, "--prefix=")) {
 252                        base = arg + 9;
 253                        continue;
 254                }
 255                if (!strcmp(arg, "--")) {
 256                        i++;
 257                        break;
 258                }
 259                if (arg[0] == '-') {
 260                        if (extra_argc > MAX_EXTRA_ARGS - 1)
 261                                die("Too many extra options");
 262                        extra_argv[extra_argc++] = arg;
 263                        continue;
 264                }
 265                break;
 266        }
 267
 268        /* We need at least one parameter -- tree-ish */
 269        if (argc - 1 < i)
 270                usage(archive_usage);
 271        if (init_archiver(format, ar) < 0)
 272                die("Unknown archive format '%s'", format);
 273
 274        if (extra_argc) {
 275                if (!ar->parse_extra)
 276                        die("'%s' format does not handle %s",
 277                            ar->name, extra_argv[0]);
 278                ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
 279        }
 280        ar->args.verbose = verbose;
 281        ar->args.base = base;
 282
 283        return i;
 284}
 285
 286static const char *extract_remote_arg(int *ac, const char **av)
 287{
 288        int ix, iy, cnt = *ac;
 289        int no_more_options = 0;
 290        const char *remote = NULL;
 291
 292        for (ix = iy = 1; ix < cnt; ix++) {
 293                const char *arg = av[ix];
 294                if (!strcmp(arg, "--"))
 295                        no_more_options = 1;
 296                if (!no_more_options) {
 297                        if (!prefixcmp(arg, "--remote=")) {
 298                                if (remote)
 299                                        die("Multiple --remote specified");
 300                                remote = arg + 9;
 301                                continue;
 302                        }
 303                        if (arg[0] != '-')
 304                                no_more_options = 1;
 305                }
 306                if (ix != iy)
 307                        av[iy] = arg;
 308                iy++;
 309        }
 310        if (remote) {
 311                av[--cnt] = NULL;
 312                *ac = cnt;
 313        }
 314        return remote;
 315}
 316
 317int cmd_archive(int argc, const char **argv, const char *prefix)
 318{
 319        struct archiver ar;
 320        int tree_idx;
 321        const char *remote = NULL;
 322
 323        remote = extract_remote_arg(&argc, argv);
 324        if (remote)
 325                return run_remote_archiver(remote, argc, argv);
 326
 327        setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
 328
 329        memset(&ar, 0, sizeof(ar));
 330        tree_idx = parse_archive_args(argc, argv, &ar);
 331        if (prefix == NULL)
 332                prefix = setup_git_directory();
 333
 334        argv += tree_idx;
 335        parse_treeish_arg(argv, &ar.args, prefix);
 336        parse_pathspec_arg(argv + 1, &ar.args);
 337
 338        return ar.write_archive(&ar.args);
 339}