Code

everyday: use the dashless form of git-init
[git.git] / archive.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tree-walk.h"
4 #include "attr.h"
5 #include "archive.h"
6 #include "parse-options.h"
8 static char const * const archive_usage[] = {
9         "git archive [options] <tree-ish> [path...]",
10         "git archive --list",
11         "git archive --remote <repo> [--exec <cmd>] [options] <tree-ish> [path...]",
12         "git archive --remote <repo> [--exec <cmd>] --list",
13         NULL
14 };
16 #define USES_ZLIB_COMPRESSION 1
18 const struct archiver {
19         const char *name;
20         write_archive_fn_t write_archive;
21         unsigned int flags;
22 } archivers[] = {
23         { "tar", write_tar_archive },
24         { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
25 };
27 static void format_subst(const struct commit *commit,
28                          const char *src, size_t len,
29                          struct strbuf *buf)
30 {
31         char *to_free = NULL;
32         struct strbuf fmt;
34         if (src == buf->buf)
35                 to_free = strbuf_detach(buf, NULL);
36         strbuf_init(&fmt, 0);
37         for (;;) {
38                 const char *b, *c;
40                 b = memmem(src, len, "$Format:", 8);
41                 if (!b)
42                         break;
43                 c = memchr(b + 8, '$', (src + len) - b - 8);
44                 if (!c)
45                         break;
47                 strbuf_reset(&fmt);
48                 strbuf_add(&fmt, b + 8, c - b - 8);
50                 strbuf_add(buf, src, b - src);
51                 format_commit_message(commit, fmt.buf, buf, DATE_NORMAL);
52                 len -= c + 1 - src;
53                 src  = c + 1;
54         }
55         strbuf_add(buf, src, len);
56         strbuf_release(&fmt);
57         free(to_free);
58 }
60 static void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
61                 unsigned int mode, enum object_type *type,
62                 unsigned long *sizep, const struct commit *commit)
63 {
64         void *buffer;
66         buffer = read_sha1_file(sha1, type, sizep);
67         if (buffer && S_ISREG(mode)) {
68                 struct strbuf buf;
69                 size_t size = 0;
71                 strbuf_init(&buf, 0);
72                 strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
73                 convert_to_working_tree(path, buf.buf, buf.len, &buf);
74                 if (commit)
75                         format_subst(commit, buf.buf, buf.len, &buf);
76                 buffer = strbuf_detach(&buf, &size);
77                 *sizep = size;
78         }
80         return buffer;
81 }
83 static void setup_archive_check(struct git_attr_check *check)
84 {
85         static struct git_attr *attr_export_ignore;
86         static struct git_attr *attr_export_subst;
88         if (!attr_export_ignore) {
89                 attr_export_ignore = git_attr("export-ignore", 13);
90                 attr_export_subst = git_attr("export-subst", 12);
91         }
92         check[0].attr = attr_export_ignore;
93         check[1].attr = attr_export_subst;
94 }
96 struct archiver_context {
97         struct archiver_args *args;
98         write_archive_entry_fn_t write_entry;
99 };
101 static int write_archive_entry(const unsigned char *sha1, const char *base,
102                 int baselen, const char *filename, unsigned mode, int stage,
103                 void *context)
105         static struct strbuf path = STRBUF_INIT;
106         struct archiver_context *c = context;
107         struct archiver_args *args = c->args;
108         write_archive_entry_fn_t write_entry = c->write_entry;
109         struct git_attr_check check[2];
110         const char *path_without_prefix;
111         int convert = 0;
112         int err;
113         enum object_type type;
114         unsigned long size;
115         void *buffer;
117         strbuf_reset(&path);
118         strbuf_grow(&path, PATH_MAX);
119         strbuf_add(&path, base, baselen);
120         strbuf_addstr(&path, filename);
121         path_without_prefix = path.buf + args->baselen;
123         setup_archive_check(check);
124         if (!git_checkattr(path_without_prefix, ARRAY_SIZE(check), check)) {
125                 if (ATTR_TRUE(check[0].value))
126                         return 0;
127                 convert = ATTR_TRUE(check[1].value);
128         }
130         if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
131                 strbuf_addch(&path, '/');
132                 if (args->verbose)
133                         fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
134                 err = write_entry(args, sha1, path.buf, path.len, mode, NULL, 0);
135                 if (err)
136                         return err;
137                 return READ_TREE_RECURSIVE;
138         }
140         buffer = sha1_file_to_archive(path_without_prefix, sha1, mode,
141                         &type, &size, convert ? args->commit : NULL);
142         if (!buffer)
143                 return error("cannot read %s", sha1_to_hex(sha1));
144         if (args->verbose)
145                 fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
146         err = write_entry(args, sha1, path.buf, path.len, mode, buffer, size);
147         free(buffer);
148         return err;
151 int write_archive_entries(struct archiver_args *args,
152                 write_archive_entry_fn_t write_entry)
154         struct archiver_context context;
155         int err;
157         if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
158                 size_t len = args->baselen;
160                 while (len > 1 && args->base[len - 2] == '/')
161                         len--;
162                 if (args->verbose)
163                         fprintf(stderr, "%.*s\n", (int)len, args->base);
164                 err = write_entry(args, args->tree->object.sha1, args->base,
165                                 len, 040777, NULL, 0);
166                 if (err)
167                         return err;
168         }
170         context.args = args;
171         context.write_entry = write_entry;
173         err =  read_tree_recursive(args->tree, args->base, args->baselen, 0,
174                         args->pathspec, write_archive_entry, &context);
175         if (err == READ_TREE_RECURSIVE)
176                 err = 0;
177         return err;
180 static const struct archiver *lookup_archiver(const char *name)
182         int i;
184         if (!name)
185                 return NULL;
187         for (i = 0; i < ARRAY_SIZE(archivers); i++) {
188                 if (!strcmp(name, archivers[i].name))
189                         return &archivers[i];
190         }
191         return NULL;
194 static void parse_pathspec_arg(const char **pathspec,
195                 struct archiver_args *ar_args)
197         ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
200 static void parse_treeish_arg(const char **argv,
201                 struct archiver_args *ar_args, const char *prefix)
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];
210         if (get_sha1(name, sha1))
211                 die("Not a valid object name");
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         }
222         tree = parse_tree_indirect(sha1);
223         if (tree == NULL)
224                 die("not a tree object");
226         if (prefix) {
227                 unsigned char tree_sha1[20];
228                 unsigned int mode;
229                 int err;
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");
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;
244 #define OPT__COMPR(s, v, h, p) \
245         { OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
246           PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }
247 #define OPT__COMPR_HIDDEN(s, v, p) \
248         { OPTION_SET_INT, (s), NULL, (v), NULL, "", \
249           PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_HIDDEN, NULL, (p) }
251 static int parse_archive_args(int argc, const char **argv,
252                 const struct archiver **ar, struct archiver_args *args)
254         const char *format = "tar";
255         const char *base = NULL;
256         const char *remote = NULL;
257         const char *exec = NULL;
258         int compression_level = -1;
259         int verbose = 0;
260         int i;
261         int list = 0;
262         struct option opts[] = {
263                 OPT_GROUP(""),
264                 OPT_STRING(0, "format", &format, "fmt", "archive format"),
265                 OPT_STRING(0, "prefix", &base, "prefix",
266                         "prepend prefix to each pathname in the archive"),
267                 OPT__VERBOSE(&verbose),
268                 OPT__COMPR('0', &compression_level, "store only", 0),
269                 OPT__COMPR('1', &compression_level, "compress faster", 1),
270                 OPT__COMPR_HIDDEN('2', &compression_level, 2),
271                 OPT__COMPR_HIDDEN('3', &compression_level, 3),
272                 OPT__COMPR_HIDDEN('4', &compression_level, 4),
273                 OPT__COMPR_HIDDEN('5', &compression_level, 5),
274                 OPT__COMPR_HIDDEN('6', &compression_level, 6),
275                 OPT__COMPR_HIDDEN('7', &compression_level, 7),
276                 OPT__COMPR_HIDDEN('8', &compression_level, 8),
277                 OPT__COMPR('9', &compression_level, "compress better", 9),
278                 OPT_GROUP(""),
279                 OPT_BOOLEAN('l', "list", &list,
280                         "list supported archive formats"),
281                 OPT_GROUP(""),
282                 OPT_STRING(0, "remote", &remote, "repo",
283                         "retrieve the archive from remote repository <repo>"),
284                 OPT_STRING(0, "exec", &exec, "cmd",
285                         "path to the remote git-upload-archive command"),
286                 OPT_END()
287         };
289         argc = parse_options(argc, argv, opts, archive_usage, 0);
291         if (remote)
292                 die("Unexpected option --remote");
293         if (exec)
294                 die("Option --exec can only be used together with --remote");
296         if (!base)
297                 base = "";
299         if (list) {
300                 for (i = 0; i < ARRAY_SIZE(archivers); i++)
301                         printf("%s\n", archivers[i].name);
302                 exit(0);
303         }
305         /* We need at least one parameter -- tree-ish */
306         if (argc < 1)
307                 usage_with_options(archive_usage, opts);
308         *ar = lookup_archiver(format);
309         if (!*ar)
310                 die("Unknown archive format '%s'", format);
312         args->compression_level = Z_DEFAULT_COMPRESSION;
313         if (compression_level != -1) {
314                 if ((*ar)->flags & USES_ZLIB_COMPRESSION)
315                         args->compression_level = compression_level;
316                 else {
317                         die("Argument not supported for format '%s': -%d",
318                                         format, compression_level);
319                 }
320         }
321         args->verbose = verbose;
322         args->base = base;
323         args->baselen = strlen(base);
325         return argc;
328 int write_archive(int argc, const char **argv, const char *prefix,
329                 int setup_prefix)
331         const struct archiver *ar = NULL;
332         struct archiver_args args;
334         argc = parse_archive_args(argc, argv, &ar, &args);
335         if (setup_prefix && prefix == NULL)
336                 prefix = setup_git_directory();
338         parse_treeish_arg(argv, &args, prefix);
339         parse_pathspec_arg(argv + 1, &args);
341         git_config(git_default_config, NULL);
343         return ar->write_archive(&args);