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