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