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 archivers
[] = {
13 { "tar", write_tar_archive
},
14 { "zip", write_zip_archive
, USES_ZLIB_COMPRESSION
},
17 static void format_subst(const struct commit
*commit
,
18 const char *src
, size_t len
,
25 to_free
= strbuf_detach(buf
, NULL
);
30 b
= memmem(src
, len
, "$Format:", 8);
33 c
= memchr(b
+ 8, '$', (src
+ len
) - b
- 8);
38 strbuf_add(&fmt
, b
+ 8, c
- b
- 8);
40 strbuf_add(buf
, src
, b
- src
);
41 format_commit_message(commit
, fmt
.buf
, buf
);
45 strbuf_add(buf
, src
, len
);
50 static 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
)
56 buffer
= read_sha1_file(sha1
, type
, sizep
);
57 if (buffer
&& S_ISREG(mode
)) {
62 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
63 convert_to_working_tree(path
, buf
.buf
, buf
.len
, &buf
);
65 format_subst(commit
, buf
.buf
, buf
.len
, &buf
);
66 buffer
= strbuf_detach(&buf
, &size
);
73 static void setup_archive_check(struct git_attr_check
*check
)
75 static struct git_attr
*attr_export_ignore
;
76 static struct git_attr
*attr_export_subst
;
78 if (!attr_export_ignore
) {
79 attr_export_ignore
= git_attr("export-ignore", 13);
80 attr_export_subst
= git_attr("export-subst", 12);
82 check
[0].attr
= attr_export_ignore
;
83 check
[1].attr
= attr_export_subst
;
86 struct archiver_context
{
87 struct archiver_args
*args
;
88 write_archive_entry_fn_t write_entry
;
91 static int write_archive_entry(const unsigned char *sha1
, const char *base
,
92 int baselen
, const char *filename
, unsigned mode
, int stage
,
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
;
103 enum object_type type
;
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
;
113 setup_archive_check(check
);
114 if (!git_checkattr(path_without_prefix
, ARRAY_SIZE(check
), check
)) {
115 if (ATTR_TRUE(check
[0].value
))
117 convert
= ATTR_TRUE(check
[1].value
);
120 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
121 strbuf_addch(&path
, '/');
123 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
124 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, NULL
, 0);
127 return READ_TREE_RECURSIVE
;
130 buffer
= sha1_file_to_archive(path_without_prefix
, sha1
, mode
,
131 &type
, &size
, convert
? args
->commit
: NULL
);
133 return error("cannot read %s", sha1_to_hex(sha1
));
135 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
136 err
= write_entry(args
, sha1
, path
.buf
, path
.len
, mode
, buffer
, size
);
141 int write_archive_entries(struct archiver_args
*args
,
142 write_archive_entry_fn_t write_entry
)
144 struct archiver_context context
;
147 if (args
->baselen
> 0 && args
->base
[args
->baselen
- 1] == '/') {
148 size_t len
= args
->baselen
;
150 while (len
> 1 && args
->base
[len
- 2] == '/')
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);
161 context
.write_entry
= write_entry
;
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
)
170 static const struct archiver
*lookup_archiver(const char *name
)
174 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
175 if (!strcmp(name
, archivers
[i
].name
))
176 return &archivers
[i
];
181 static void parse_pathspec_arg(const char **pathspec
,
182 struct archiver_args
*ar_args
)
184 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
187 static void parse_treeish_arg(const char **argv
,
188 struct archiver_args
*ar_args
, const char *prefix
)
190 const char *name
= argv
[0];
191 const unsigned char *commit_sha1
;
194 const struct commit
*commit
;
195 unsigned char sha1
[20];
197 if (get_sha1(name
, sha1
))
198 die("Not a valid object name");
200 commit
= lookup_commit_reference_gently(sha1
, 1);
202 commit_sha1
= commit
->object
.sha1
;
203 archive_time
= commit
->date
;
206 archive_time
= time(NULL
);
209 tree
= parse_tree_indirect(sha1
);
211 die("not a tree object");
214 unsigned char tree_sha1
[20];
218 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
220 if (err
|| !S_ISDIR(mode
))
221 die("current working directory is untracked");
223 tree
= parse_tree_indirect(tree_sha1
);
225 ar_args
->tree
= tree
;
226 ar_args
->commit_sha1
= commit_sha1
;
227 ar_args
->commit
= commit
;
228 ar_args
->time
= archive_time
;
231 static int parse_archive_args(int argc
, const char **argv
,
232 const struct archiver
**ar
, struct archiver_args
*args
)
234 const char *format
= "tar";
235 const char *base
= "";
236 int compression_level
= -1;
240 for (i
= 1; i
< argc
; i
++) {
241 const char *arg
= argv
[i
];
243 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
244 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
245 printf("%s\n", archivers
[i
].name
);
248 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
252 if (!prefixcmp(arg
, "--format=")) {
256 if (!prefixcmp(arg
, "--prefix=")) {
260 if (!strcmp(arg
, "--")) {
264 if (arg
[0] == '-' && isdigit(arg
[1]) && arg
[2] == '\0') {
265 compression_level
= arg
[1] - '0';
269 die("Unknown argument: %s", arg
);
273 /* We need at least one parameter -- tree-ish */
275 usage(archive_usage
);
276 *ar
= lookup_archiver(format
);
278 die("Unknown archive format '%s'", format
);
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
;
285 die("Argument not supported for format '%s': -%d",
286 format
, compression_level
);
289 args
->verbose
= verbose
;
291 args
->baselen
= strlen(base
);
296 int write_archive(int argc
, const char **argv
, const char *prefix
,
299 const struct archiver
*ar
= NULL
;
300 struct archiver_args args
;
303 tree_idx
= parse_archive_args(argc
, argv
, &ar
, &args
);
304 if (setup_prefix
&& prefix
== NULL
)
305 prefix
= setup_git_directory();
308 parse_treeish_arg(argv
, &args
, prefix
);
309 parse_pathspec_arg(argv
+ 1, &args
);
311 return ar
->write_archive(&args
);