6 #include "parse-options.h"
8 static char const * const archive_usage
[] = {
9 "git archive [options] <tree-ish> [path...]",
11 "git archive --remote <repo> [--exec <cmd>] [options] <tree-ish> [path...]",
12 "git archive --remote <repo> [--exec <cmd>] --list",
16 #define USES_ZLIB_COMPRESSION 1
18 const struct archiver
{
20 write_archive_fn_t write_archive
;
23 { "tar", write_tar_archive
},
24 { "zip", write_zip_archive
, USES_ZLIB_COMPRESSION
},
27 static void format_subst(const struct commit
*commit
,
28 const char *src
, size_t len
,
35 to_free
= strbuf_detach(buf
, NULL
);
40 b
= memmem(src
, len
, "$Format:", 8);
43 c
= memchr(b
+ 8, '$', (src
+ len
) - b
- 8);
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
);
55 strbuf_add(buf
, src
, len
);
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
)
66 buffer
= read_sha1_file(sha1
, type
, sizep
);
67 if (buffer
&& S_ISREG(mode
)) {
72 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
73 convert_to_working_tree(path
, buf
.buf
, buf
.len
, &buf
);
75 format_subst(commit
, buf
.buf
, buf
.len
, &buf
);
76 buffer
= strbuf_detach(&buf
, &size
);
83 static void setup_archive_check(struct git_attr_check
*check
)
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);
92 check
[0].attr
= attr_export_ignore
;
93 check
[1].attr
= attr_export_subst
;
96 struct archiver_context
{
97 struct archiver_args
*args
;
98 write_archive_entry_fn_t write_entry
;
101 static int write_archive_entry(const unsigned char *sha1
, const char *base
,
102 int baselen
, const char *filename
, unsigned mode
, int stage
,
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
;
113 enum object_type type
;
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
))
127 convert
= ATTR_TRUE(check
[1].value
);
130 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
131 strbuf_addch(&path
, '/');
133 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
134 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, NULL
, 0);
137 return READ_TREE_RECURSIVE
;
140 buffer
= sha1_file_to_archive(path_without_prefix
, sha1
, mode
,
141 &type
, &size
, convert
? args
->commit
: NULL
);
143 return error("cannot read %s", sha1_to_hex(sha1
));
145 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
146 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, buffer
, size
);
151 int write_archive_entries(struct archiver_args
*args
,
152 write_archive_entry_fn_t write_entry
)
154 struct archiver_context context
;
157 if (args
->baselen
> 0 && args
->base
[args
->baselen
- 1] == '/') {
158 size_t len
= args
->baselen
;
160 while (len
> 1 && args
->base
[len
- 2] == '/')
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);
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
)
180 static const struct archiver
*lookup_archiver(const char *name
)
187 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
188 if (!strcmp(name
, archivers
[i
].name
))
189 return &archivers
[i
];
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
;
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);
215 commit_sha1
= commit
->object
.sha1
;
216 archive_time
= commit
->date
;
219 archive_time
= time(NULL
);
222 tree
= parse_tree_indirect(sha1
);
224 die("not a tree object");
227 unsigned char tree_sha1
[20];
231 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
233 if (err
|| !S_ISDIR(mode
))
234 die("current working directory is untracked");
236 tree
= parse_tree_indirect(tree_sha1
);
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;
262 struct option opts
[] = {
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),
279 OPT_BOOLEAN('l', "list", &list
,
280 "list supported archive formats"),
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"),
289 argc
= parse_options(argc
, argv
, opts
, archive_usage
, 0);
292 die("Unexpected option --remote");
294 die("Option --exec can only be used together with --remote");
300 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
301 printf("%s\n", archivers
[i
].name
);
305 /* We need at least one parameter -- tree-ish */
307 usage_with_options(archive_usage
, opts
);
308 *ar
= lookup_archiver(format
);
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
;
317 die("Argument not supported for format '%s': -%d",
318 format
, compression_level
);
321 args
->verbose
= verbose
;
323 args
->baselen
= strlen(base
);
328 int write_archive(int argc
, const char **argv
, const char *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
);