]> git.ipfire.org Git - thirdparty/git.git/blame - archive.c
parseopt: make usage optional
[thirdparty/git.git] / archive.c
CommitLineData
18125644
LH
1#include "cache.h"
2#include "commit.h"
c0885435 3#include "tree-walk.h"
18125644 4#include "attr.h"
562e25ab 5#include "archive.h"
4fac1d3a
RS
6#include "parse-options.h"
7
8static 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};
c0885435
RS
15
16#define USES_ZLIB_COMPRESSION 1
17
120a385a 18static const struct archiver {
f15f736d
RS
19 const char *name;
20 write_archive_fn_t write_archive;
21 unsigned int flags;
22} archivers[] = {
c0885435
RS
23 { "tar", write_tar_archive },
24 { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
25};
26
18125644
LH
27static void format_subst(const struct commit *commit,
28 const char *src, size_t len,
29 struct strbuf *buf)
30{
31 char *to_free = NULL;
f285a2d7 32 struct strbuf fmt = STRBUF_INIT;
18125644
LH
33
34 if (src == buf->buf)
35 to_free = strbuf_detach(buf, NULL);
18125644
LH
36 for (;;) {
37 const char *b, *c;
38
39 b = memmem(src, len, "$Format:", 8);
75b7dfbd 40 if (!b)
18125644 41 break;
75b7dfbd 42 c = memchr(b + 8, '$', (src + len) - b - 8);
18125644
LH
43 if (!c)
44 break;
45
46 strbuf_reset(&fmt);
47 strbuf_add(&fmt, b + 8, c - b - 8);
48
49 strbuf_add(buf, src, b - src);
d36f8679 50 format_commit_message(commit, fmt.buf, buf, DATE_NORMAL);
18125644
LH
51 len -= c + 1 - src;
52 src = c + 1;
53 }
54 strbuf_add(buf, src, len);
55 strbuf_release(&fmt);
56 free(to_free);
57}
58
1d11d5bb
RS
59static void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
60 unsigned int mode, enum object_type *type,
61 unsigned long *sizep, const struct commit *commit)
18125644
LH
62{
63 void *buffer;
64
65 buffer = read_sha1_file(sha1, type, sizep);
66 if (buffer && S_ISREG(mode)) {
f285a2d7 67 struct strbuf buf = STRBUF_INIT;
18125644
LH
68 size_t size = 0;
69
18125644
LH
70 strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
71 convert_to_working_tree(path, buf.buf, buf.len, &buf);
1d11d5bb
RS
72 if (commit)
73 format_subst(commit, buf.buf, buf.len, &buf);
18125644
LH
74 buffer = strbuf_detach(&buf, &size);
75 *sizep = size;
76 }
77
78 return buffer;
79}
80
1d11d5bb 81static void setup_archive_check(struct git_attr_check *check)
008d896d
RS
82{
83 static struct git_attr *attr_export_ignore;
1d11d5bb 84 static struct git_attr *attr_export_subst;
008d896d 85
1d11d5bb 86 if (!attr_export_ignore) {
008d896d 87 attr_export_ignore = git_attr("export-ignore", 13);
1d11d5bb
RS
88 attr_export_subst = git_attr("export-subst", 12);
89 }
008d896d 90 check[0].attr = attr_export_ignore;
1d11d5bb 91 check[1].attr = attr_export_subst;
008d896d 92}
562e25ab
RS
93
94struct archiver_context {
95 struct archiver_args *args;
96 write_archive_entry_fn_t write_entry;
97};
98
99static int write_archive_entry(const unsigned char *sha1, const char *base,
100 int baselen, const char *filename, unsigned mode, int stage,
101 void *context)
102{
103 static struct strbuf path = STRBUF_INIT;
104 struct archiver_context *c = context;
105 struct archiver_args *args = c->args;
106 write_archive_entry_fn_t write_entry = c->write_entry;
1d11d5bb
RS
107 struct git_attr_check check[2];
108 const char *path_without_prefix;
109 int convert = 0;
562e25ab
RS
110 int err;
111 enum object_type type;
112 unsigned long size;
113 void *buffer;
114
115 strbuf_reset(&path);
116 strbuf_grow(&path, PATH_MAX);
117 strbuf_add(&path, base, baselen);
118 strbuf_addstr(&path, filename);
1d11d5bb 119 path_without_prefix = path.buf + args->baselen;
562e25ab 120
1d11d5bb
RS
121 setup_archive_check(check);
122 if (!git_checkattr(path_without_prefix, ARRAY_SIZE(check), check)) {
123 if (ATTR_TRUE(check[0].value))
124 return 0;
125 convert = ATTR_TRUE(check[1].value);
126 }
562e25ab
RS
127
128 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
129 strbuf_addch(&path, '/');
130 if (args->verbose)
131 fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
132 err = write_entry(args, sha1, path.buf, path.len, mode, NULL, 0);
133 if (err)
134 return err;
d3bee161 135 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
562e25ab
RS
136 }
137
1d11d5bb
RS
138 buffer = sha1_file_to_archive(path_without_prefix, sha1, mode,
139 &type, &size, convert ? args->commit : NULL);
562e25ab
RS
140 if (!buffer)
141 return error("cannot read %s", sha1_to_hex(sha1));
142 if (args->verbose)
143 fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
144 err = write_entry(args, sha1, path.buf, path.len, mode, buffer, size);
145 free(buffer);
146 return err;
147}
148
149int write_archive_entries(struct archiver_args *args,
150 write_archive_entry_fn_t write_entry)
151{
152 struct archiver_context context;
153 int err;
154
155 if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
156 size_t len = args->baselen;
157
158 while (len > 1 && args->base[len - 2] == '/')
159 len--;
160 if (args->verbose)
161 fprintf(stderr, "%.*s\n", (int)len, args->base);
162 err = write_entry(args, args->tree->object.sha1, args->base,
163 len, 040777, NULL, 0);
164 if (err)
165 return err;
166 }
167
168 context.args = args;
169 context.write_entry = write_entry;
170
171 err = read_tree_recursive(args->tree, args->base, args->baselen, 0,
172 args->pathspec, write_archive_entry, &context);
173 if (err == READ_TREE_RECURSIVE)
174 err = 0;
175 return err;
176}
6e94e683 177
c0885435
RS
178static const struct archiver *lookup_archiver(const char *name)
179{
180 int i;
181
4fac1d3a
RS
182 if (!name)
183 return NULL;
184
c0885435
RS
185 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
186 if (!strcmp(name, archivers[i].name))
187 return &archivers[i];
188 }
189 return NULL;
190}
191
192static void parse_pathspec_arg(const char **pathspec,
193 struct archiver_args *ar_args)
194{
195 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
196}
197
198static void parse_treeish_arg(const char **argv,
199 struct archiver_args *ar_args, const char *prefix)
200{
201 const char *name = argv[0];
202 const unsigned char *commit_sha1;
203 time_t archive_time;
204 struct tree *tree;
205 const struct commit *commit;
206 unsigned char sha1[20];
207
208 if (get_sha1(name, sha1))
209 die("Not a valid object name");
210
211 commit = lookup_commit_reference_gently(sha1, 1);
212 if (commit) {
213 commit_sha1 = commit->object.sha1;
214 archive_time = commit->date;
215 } else {
216 commit_sha1 = NULL;
217 archive_time = time(NULL);
218 }
219
220 tree = parse_tree_indirect(sha1);
221 if (tree == NULL)
222 die("not a tree object");
223
224 if (prefix) {
225 unsigned char tree_sha1[20];
226 unsigned int mode;
227 int err;
228
229 err = get_tree_entry(tree->object.sha1, prefix,
230 tree_sha1, &mode);
231 if (err || !S_ISDIR(mode))
232 die("current working directory is untracked");
233
234 tree = parse_tree_indirect(tree_sha1);
235 }
236 ar_args->tree = tree;
237 ar_args->commit_sha1 = commit_sha1;
238 ar_args->commit = commit;
239 ar_args->time = archive_time;
240}
241
aec0c1bb
CMDV
242static void create_output_file(const char *output_file)
243{
244 int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
245 if (output_fd < 0)
246 die("could not create archive file: %s ", output_file);
247 if (output_fd != 1) {
248 if (dup2(output_fd, 1) < 0)
249 die("could not redirect output");
250 else
251 close(output_fd);
252 }
253}
254
4fac1d3a
RS
255#define OPT__COMPR(s, v, h, p) \
256 { OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
257 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }
258#define OPT__COMPR_HIDDEN(s, v, p) \
259 { OPTION_SET_INT, (s), NULL, (v), NULL, "", \
260 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_HIDDEN, NULL, (p) }
261
c0885435
RS
262static int parse_archive_args(int argc, const char **argv,
263 const struct archiver **ar, struct archiver_args *args)
264{
265 const char *format = "tar";
4fac1d3a
RS
266 const char *base = NULL;
267 const char *remote = NULL;
268 const char *exec = NULL;
aec0c1bb 269 const char *output = NULL;
c0885435
RS
270 int compression_level = -1;
271 int verbose = 0;
272 int i;
4fac1d3a
RS
273 int list = 0;
274 struct option opts[] = {
275 OPT_GROUP(""),
276 OPT_STRING(0, "format", &format, "fmt", "archive format"),
277 OPT_STRING(0, "prefix", &base, "prefix",
278 "prepend prefix to each pathname in the archive"),
aec0c1bb
CMDV
279 OPT_STRING(0, "output", &output, "file",
280 "write the archive to this file"),
4fac1d3a
RS
281 OPT__VERBOSE(&verbose),
282 OPT__COMPR('0', &compression_level, "store only", 0),
283 OPT__COMPR('1', &compression_level, "compress faster", 1),
284 OPT__COMPR_HIDDEN('2', &compression_level, 2),
285 OPT__COMPR_HIDDEN('3', &compression_level, 3),
286 OPT__COMPR_HIDDEN('4', &compression_level, 4),
287 OPT__COMPR_HIDDEN('5', &compression_level, 5),
288 OPT__COMPR_HIDDEN('6', &compression_level, 6),
289 OPT__COMPR_HIDDEN('7', &compression_level, 7),
290 OPT__COMPR_HIDDEN('8', &compression_level, 8),
291 OPT__COMPR('9', &compression_level, "compress better", 9),
292 OPT_GROUP(""),
293 OPT_BOOLEAN('l', "list", &list,
294 "list supported archive formats"),
295 OPT_GROUP(""),
296 OPT_STRING(0, "remote", &remote, "repo",
297 "retrieve the archive from remote repository <repo>"),
298 OPT_STRING(0, "exec", &exec, "cmd",
299 "path to the remote git-upload-archive command"),
300 OPT_END()
301 };
302
303 argc = parse_options(argc, argv, opts, archive_usage, 0);
304
305 if (remote)
306 die("Unexpected option --remote");
307 if (exec)
308 die("Option --exec can only be used together with --remote");
309
310 if (!base)
311 base = "";
312
aec0c1bb
CMDV
313 if (output)
314 create_output_file(output);
315
4fac1d3a
RS
316 if (list) {
317 for (i = 0; i < ARRAY_SIZE(archivers); i++)
318 printf("%s\n", archivers[i].name);
319 exit(0);
c0885435
RS
320 }
321
322 /* We need at least one parameter -- tree-ish */
4fac1d3a
RS
323 if (argc < 1)
324 usage_with_options(archive_usage, opts);
c0885435
RS
325 *ar = lookup_archiver(format);
326 if (!*ar)
327 die("Unknown archive format '%s'", format);
328
329 args->compression_level = Z_DEFAULT_COMPRESSION;
330 if (compression_level != -1) {
331 if ((*ar)->flags & USES_ZLIB_COMPRESSION)
332 args->compression_level = compression_level;
333 else {
334 die("Argument not supported for format '%s': -%d",
335 format, compression_level);
336 }
337 }
338 args->verbose = verbose;
339 args->base = base;
340 args->baselen = strlen(base);
341
4fac1d3a 342 return argc;
c0885435
RS
343}
344
6e94e683
RS
345int write_archive(int argc, const char **argv, const char *prefix,
346 int setup_prefix)
347{
348 const struct archiver *ar = NULL;
349 struct archiver_args args;
6e94e683 350
4fac1d3a 351 argc = parse_archive_args(argc, argv, &ar, &args);
6e94e683
RS
352 if (setup_prefix && prefix == NULL)
353 prefix = setup_git_directory();
354
6e94e683
RS
355 parse_treeish_arg(argv, &args, prefix);
356 parse_pathspec_arg(argv + 1, &args);
357
ddff8563
CB
358 git_config(git_default_config, NULL);
359
6e94e683
RS
360 return ar->write_archive(&args);
361}