7 static const char archive_usage
[] = \
8 "git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
10 #define USES_ZLIB_COMPRESSION 1
12 const struct archiver
{
14 write_archive_fn_t write_archive
;
17 { "tar", write_tar_archive
},
18 { "zip", write_zip_archive
, USES_ZLIB_COMPRESSION
},
21 static void format_subst(const struct commit
*commit
,
22 const char *src
, size_t len
,
29 to_free
= strbuf_detach(buf
, NULL
);
34 b
= memmem(src
, len
, "$Format:", 8);
37 c
= memchr(b
+ 8, '$', (src
+ len
) - b
- 8);
42 strbuf_add(&fmt
, b
+ 8, c
- b
- 8);
44 strbuf_add(buf
, src
, b
- src
);
45 format_commit_message(commit
, fmt
.buf
, buf
);
49 strbuf_add(buf
, src
, len
);
54 static 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
)
60 buffer
= read_sha1_file(sha1
, type
, sizep
);
61 if (buffer
&& S_ISREG(mode
)) {
66 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
67 convert_to_working_tree(path
, buf
.buf
, buf
.len
, &buf
);
69 format_subst(commit
, buf
.buf
, buf
.len
, &buf
);
70 buffer
= strbuf_detach(&buf
, &size
);
77 static void setup_archive_check(struct git_attr_check
*check
)
79 static struct git_attr
*attr_export_ignore
;
80 static struct git_attr
*attr_export_subst
;
82 if (!attr_export_ignore
) {
83 attr_export_ignore
= git_attr("export-ignore", 13);
84 attr_export_subst
= git_attr("export-subst", 12);
86 check
[0].attr
= attr_export_ignore
;
87 check
[1].attr
= attr_export_subst
;
90 struct archiver_context
{
91 struct archiver_args
*args
;
92 write_archive_entry_fn_t write_entry
;
95 static int write_archive_entry(const unsigned char *sha1
, const char *base
,
96 int baselen
, const char *filename
, unsigned mode
, int stage
,
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
;
107 enum object_type type
;
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
;
117 setup_archive_check(check
);
118 if (!git_checkattr(path_without_prefix
, ARRAY_SIZE(check
), check
)) {
119 if (ATTR_TRUE(check
[0].value
))
121 convert
= ATTR_TRUE(check
[1].value
);
124 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
125 strbuf_addch(&path
, '/');
127 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
128 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, NULL
, 0);
131 return READ_TREE_RECURSIVE
;
134 buffer
= sha1_file_to_archive(path_without_prefix
, sha1
, mode
,
135 &type
, &size
, convert
? args
->commit
: NULL
);
137 return error("cannot read %s", sha1_to_hex(sha1
));
139 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
140 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, buffer
, size
);
145 int write_archive_entries(struct archiver_args
*args
,
146 write_archive_entry_fn_t write_entry
)
148 struct archiver_context context
;
151 if (args
->baselen
> 0 && args
->base
[args
->baselen
- 1] == '/') {
152 size_t len
= args
->baselen
;
154 while (len
> 1 && args
->base
[len
- 2] == '/')
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);
165 context
.write_entry
= write_entry
;
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
)
174 static const struct archiver
*lookup_archiver(const char *name
)
178 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
179 if (!strcmp(name
, archivers
[i
].name
))
180 return &archivers
[i
];
185 static void parse_pathspec_arg(const char **pathspec
,
186 struct archiver_args
*ar_args
)
188 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
191 static void parse_treeish_arg(const char **argv
,
192 struct archiver_args
*ar_args
, const char *prefix
)
194 const char *name
= argv
[0];
195 const unsigned char *commit_sha1
;
198 const struct commit
*commit
;
199 unsigned char sha1
[20];
201 if (get_sha1(name
, sha1
))
202 die("Not a valid object name");
204 commit
= lookup_commit_reference_gently(sha1
, 1);
206 commit_sha1
= commit
->object
.sha1
;
207 archive_time
= commit
->date
;
210 archive_time
= time(NULL
);
213 tree
= parse_tree_indirect(sha1
);
215 die("not a tree object");
218 unsigned char tree_sha1
[20];
222 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
224 if (err
|| !S_ISDIR(mode
))
225 die("current working directory is untracked");
227 tree
= parse_tree_indirect(tree_sha1
);
229 ar_args
->tree
= tree
;
230 ar_args
->commit_sha1
= commit_sha1
;
231 ar_args
->commit
= commit
;
232 ar_args
->time
= archive_time
;
235 static int parse_archive_args(int argc
, const char **argv
,
236 const struct archiver
**ar
, struct archiver_args
*args
)
238 const char *format
= "tar";
239 const char *base
= "";
240 int compression_level
= -1;
244 for (i
= 1; i
< argc
; i
++) {
245 const char *arg
= argv
[i
];
247 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
248 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
249 printf("%s\n", archivers
[i
].name
);
252 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
256 if (!prefixcmp(arg
, "--format=")) {
260 if (!prefixcmp(arg
, "--prefix=")) {
264 if (!strcmp(arg
, "--")) {
268 if (arg
[0] == '-' && isdigit(arg
[1]) && arg
[2] == '\0') {
269 compression_level
= arg
[1] - '0';
273 die("Unknown argument: %s", arg
);
277 /* We need at least one parameter -- tree-ish */
279 usage(archive_usage
);
280 *ar
= lookup_archiver(format
);
282 die("Unknown archive format '%s'", format
);
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
;
289 die("Argument not supported for format '%s': -%d",
290 format
, compression_level
);
293 args
->verbose
= verbose
;
295 args
->baselen
= strlen(base
);
300 int write_archive(int argc
, const char **argv
, const char *prefix
,
303 const struct archiver
*ar
= NULL
;
304 struct archiver_args args
;
307 tree_idx
= parse_archive_args(argc
, argv
, &ar
, &args
);
308 if (setup_prefix
&& prefix
== NULL
)
309 prefix
= setup_git_directory();
312 parse_treeish_arg(argv
, &args
, prefix
);
313 parse_pathspec_arg(argv
+ 1, &args
);
315 return ar
->write_archive(&args
);