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