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