archive.con commit Allow the built-in exec path to be relative to the command invocation path (49fa65a)
   1#include "cache.h"
   2#include "commit.h"
   3#include "tree-walk.h"
   4#include "attr.h"
   5#include "archive.h"
   6
   7static const char archive_usage[] = \
   8"git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
   9
  10#define USES_ZLIB_COMPRESSION 1
  11
  12const struct archiver {
  13        const char *name;
  14        write_archive_fn_t write_archive;
  15        unsigned int flags;
  16} archivers[] = {
  17        { "tar", write_tar_archive },
  18        { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
  19};
  20
  21static void format_subst(const struct commit *commit,
  22                         const char *src, size_t len,
  23                         struct strbuf *buf)
  24{
  25        char *to_free = NULL;
  26        struct strbuf fmt;
  27
  28        if (src == buf->buf)
  29                to_free = strbuf_detach(buf, NULL);
  30        strbuf_init(&fmt, 0);
  31        for (;;) {
  32                const char *b, *c;
  33
  34                b = memmem(src, len, "$Format:", 8);
  35                if (!b)
  36                        break;
  37                c = memchr(b + 8, '$', (src + len) - b - 8);
  38                if (!c)
  39                        break;
  40
  41                strbuf_reset(&fmt);
  42                strbuf_add(&fmt, b + 8, c - b - 8);
  43
  44                strbuf_add(buf, src, b - src);
  45                format_commit_message(commit, fmt.buf, buf);
  46                len -= c + 1 - src;
  47                src  = c + 1;
  48        }
  49        strbuf_add(buf, src, len);
  50        strbuf_release(&fmt);
  51        free(to_free);
  52}
  53
  54static void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
  55                unsigned int mode, enum object_type *type,
  56                unsigned long *sizep, const struct commit *commit)
  57{
  58        void *buffer;
  59
  60        buffer = read_sha1_file(sha1, type, sizep);
  61        if (buffer && S_ISREG(mode)) {
  62                struct strbuf buf;
  63                size_t size = 0;
  64
  65                strbuf_init(&buf, 0);
  66                strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
  67                convert_to_working_tree(path, buf.buf, buf.len, &buf);
  68                if (commit)
  69                        format_subst(commit, buf.buf, buf.len, &buf);
  70                buffer = strbuf_detach(&buf, &size);
  71                *sizep = size;
  72        }
  73
  74        return buffer;
  75}
  76
  77static void setup_archive_check(struct git_attr_check *check)
  78{
  79        static struct git_attr *attr_export_ignore;
  80        static struct git_attr *attr_export_subst;
  81
  82        if (!attr_export_ignore) {
  83                attr_export_ignore = git_attr("export-ignore", 13);
  84                attr_export_subst = git_attr("export-subst", 12);
  85        }
  86        check[0].attr = attr_export_ignore;
  87        check[1].attr = attr_export_subst;
  88}
  89
  90struct archiver_context {
  91        struct archiver_args *args;
  92        write_archive_entry_fn_t write_entry;
  93};
  94
  95static int write_archive_entry(const unsigned char *sha1, const char *base,
  96                int baselen, const char *filename, unsigned mode, int stage,
  97                void *context)
  98{
  99        static struct strbuf path = STRBUF_INIT;
 100        struct archiver_context *c = context;
 101        struct archiver_args *args = c->args;
 102        write_archive_entry_fn_t write_entry = c->write_entry;
 103        struct git_attr_check check[2];
 104        const char *path_without_prefix;
 105        int convert = 0;
 106        int err;
 107        enum object_type type;
 108        unsigned long size;
 109        void *buffer;
 110
 111        strbuf_reset(&path);
 112        strbuf_grow(&path, PATH_MAX);
 113        strbuf_add(&path, base, baselen);
 114        strbuf_addstr(&path, filename);
 115        path_without_prefix = path.buf + args->baselen;
 116
 117        setup_archive_check(check);
 118        if (!git_checkattr(path_without_prefix, ARRAY_SIZE(check), check)) {
 119                if (ATTR_TRUE(check[0].value))
 120                        return 0;
 121                convert = ATTR_TRUE(check[1].value);
 122        }
 123
 124        if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 125                strbuf_addch(&path, '/');
 126                if (args->verbose)
 127                        fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
 128                err = write_entry(args, sha1, path.buf, path.len, mode, NULL, 0);
 129                if (err)
 130                        return err;
 131                return READ_TREE_RECURSIVE;
 132        }
 133
 134        buffer = sha1_file_to_archive(path_without_prefix, sha1, mode,
 135                        &type, &size, convert ? args->commit : NULL);
 136        if (!buffer)
 137                return error("cannot read %s", sha1_to_hex(sha1));
 138        if (args->verbose)
 139                fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
 140        err = write_entry(args, sha1, path.buf, path.len, mode, buffer, size);
 141        free(buffer);
 142        return err;
 143}
 144
 145int write_archive_entries(struct archiver_args *args,
 146                write_archive_entry_fn_t write_entry)
 147{
 148        struct archiver_context context;
 149        int err;
 150
 151        if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
 152                size_t len = args->baselen;
 153
 154                while (len > 1 && args->base[len - 2] == '/')
 155                        len--;
 156                if (args->verbose)
 157                        fprintf(stderr, "%.*s\n", (int)len, args->base);
 158                err = write_entry(args, args->tree->object.sha1, args->base,
 159                                len, 040777, NULL, 0);
 160                if (err)
 161                        return err;
 162        }
 163
 164        context.args = args;
 165        context.write_entry = write_entry;
 166
 167        err =  read_tree_recursive(args->tree, args->base, args->baselen, 0,
 168                        args->pathspec, write_archive_entry, &context);
 169        if (err == READ_TREE_RECURSIVE)
 170                err = 0;
 171        return err;
 172}
 173
 174static const struct archiver *lookup_archiver(const char *name)
 175{
 176        int i;
 177
 178        for (i = 0; i < ARRAY_SIZE(archivers); i++) {
 179                if (!strcmp(name, archivers[i].name))
 180                        return &archivers[i];
 181        }
 182        return NULL;
 183}
 184
 185static void parse_pathspec_arg(const char **pathspec,
 186                struct archiver_args *ar_args)
 187{
 188        ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
 189}
 190
 191static void parse_treeish_arg(const char **argv,
 192                struct archiver_args *ar_args, const char *prefix)
 193{
 194        const char *name = argv[0];
 195        const unsigned char *commit_sha1;
 196        time_t archive_time;
 197        struct tree *tree;
 198        const struct commit *commit;
 199        unsigned char sha1[20];
 200
 201        if (get_sha1(name, sha1))
 202                die("Not a valid object name");
 203
 204        commit = lookup_commit_reference_gently(sha1, 1);
 205        if (commit) {
 206                commit_sha1 = commit->object.sha1;
 207                archive_time = commit->date;
 208        } else {
 209                commit_sha1 = NULL;
 210                archive_time = time(NULL);
 211        }
 212
 213        tree = parse_tree_indirect(sha1);
 214        if (tree == NULL)
 215                die("not a tree object");
 216
 217        if (prefix) {
 218                unsigned char tree_sha1[20];
 219                unsigned int mode;
 220                int err;
 221
 222                err = get_tree_entry(tree->object.sha1, prefix,
 223                                     tree_sha1, &mode);
 224                if (err || !S_ISDIR(mode))
 225                        die("current working directory is untracked");
 226
 227                tree = parse_tree_indirect(tree_sha1);
 228        }
 229        ar_args->tree = tree;
 230        ar_args->commit_sha1 = commit_sha1;
 231        ar_args->commit = commit;
 232        ar_args->time = archive_time;
 233}
 234
 235static int parse_archive_args(int argc, const char **argv,
 236                const struct archiver **ar, struct archiver_args *args)
 237{
 238        const char *format = "tar";
 239        const char *base = "";
 240        int compression_level = -1;
 241        int verbose = 0;
 242        int i;
 243
 244        for (i = 1; i < argc; i++) {
 245                const char *arg = argv[i];
 246
 247                if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
 248                        for (i = 0; i < ARRAY_SIZE(archivers); i++)
 249                                printf("%s\n", archivers[i].name);
 250                        exit(0);
 251                }
 252                if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
 253                        verbose = 1;
 254                        continue;
 255                }
 256                if (!prefixcmp(arg, "--format=")) {
 257                        format = arg + 9;
 258                        continue;
 259                }
 260                if (!prefixcmp(arg, "--prefix=")) {
 261                        base = arg + 9;
 262                        continue;
 263                }
 264                if (!strcmp(arg, "--")) {
 265                        i++;
 266                        break;
 267                }
 268                if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
 269                        compression_level = arg[1] - '0';
 270                        continue;
 271                }
 272                if (arg[0] == '-')
 273                        die("Unknown argument: %s", arg);
 274                break;
 275        }
 276
 277        /* We need at least one parameter -- tree-ish */
 278        if (argc - 1 < i)
 279                usage(archive_usage);
 280        *ar = lookup_archiver(format);
 281        if (!*ar)
 282                die("Unknown archive format '%s'", format);
 283
 284        args->compression_level = Z_DEFAULT_COMPRESSION;
 285        if (compression_level != -1) {
 286                if ((*ar)->flags & USES_ZLIB_COMPRESSION)
 287                        args->compression_level = compression_level;
 288                else {
 289                        die("Argument not supported for format '%s': -%d",
 290                                        format, compression_level);
 291                }
 292        }
 293        args->verbose = verbose;
 294        args->base = base;
 295        args->baselen = strlen(base);
 296
 297        return i;
 298}
 299
 300int write_archive(int argc, const char **argv, const char *prefix,
 301                int setup_prefix)
 302{
 303        const struct archiver *ar = NULL;
 304        struct archiver_args args;
 305        int tree_idx;
 306
 307        tree_idx = parse_archive_args(argc, argv, &ar, &args);
 308        if (setup_prefix && prefix == NULL)
 309                prefix = setup_git_directory();
 310
 311        argv += tree_idx;
 312        parse_treeish_arg(argv, &args, prefix);
 313        parse_pathspec_arg(argv + 1, &args);
 314
 315        return ar->write_archive(&args);
 316}