4 #include "object-store.h"
9 #include "parse-options.h"
10 #include "unpack-trees.h"
13 static char const * const archive_usage
[] = {
14 N_("git archive [<options>] <tree-ish> [<path>...]"),
15 N_("git archive --list"),
16 N_("git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]"),
17 N_("git archive --remote <repo> [--exec <cmd>] --list"),
21 static const struct archiver
**archivers
;
22 static int nr_archivers
;
23 static int alloc_archivers
;
24 static int remote_allow_unreachable
;
26 void register_archiver(struct archiver
*ar
)
28 ALLOC_GROW(archivers
, nr_archivers
+ 1, alloc_archivers
);
29 archivers
[nr_archivers
++] = ar
;
32 void init_archivers(void)
38 static void format_subst(const struct commit
*commit
,
39 const char *src
, size_t len
,
43 struct strbuf fmt
= STRBUF_INIT
;
44 struct pretty_print_context ctx
= {0};
45 ctx
.date_mode
.type
= DATE_NORMAL
;
46 ctx
.abbrev
= DEFAULT_ABBREV
;
49 to_free
= strbuf_detach(buf
, NULL
);
53 b
= memmem(src
, len
, "$Format:", 8);
56 c
= memchr(b
+ 8, '$', (src
+ len
) - b
- 8);
61 strbuf_add(&fmt
, b
+ 8, c
- b
- 8);
63 strbuf_add(buf
, src
, b
- src
);
64 format_commit_message(commit
, fmt
.buf
, buf
, &ctx
);
68 strbuf_add(buf
, src
, len
);
73 void *object_file_to_archive(const struct archiver_args
*args
,
74 const char *path
, const struct object_id
*oid
,
75 unsigned int mode
, enum object_type
*type
,
79 const struct commit
*commit
= args
->convert
? args
->commit
: NULL
;
80 struct checkout_metadata meta
;
82 init_checkout_metadata(&meta
, args
->refname
,
83 args
->commit_oid
? args
->commit_oid
:
84 (args
->tree
? &args
->tree
->object
.oid
: NULL
), oid
);
86 path
+= args
->baselen
;
87 buffer
= read_object_file(oid
, type
, sizep
);
88 if (buffer
&& S_ISREG(mode
)) {
89 struct strbuf buf
= STRBUF_INIT
;
92 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
93 convert_to_working_tree(args
->repo
->index
, path
, buf
.buf
, buf
.len
, &buf
, &meta
);
95 format_subst(commit
, buf
.buf
, buf
.len
, &buf
);
96 buffer
= strbuf_detach(&buf
, &size
);
104 struct directory
*up
;
105 struct object_id oid
;
109 char path
[FLEX_ARRAY
];
112 struct archiver_context
{
113 struct archiver_args
*args
;
114 write_archive_entry_fn_t write_entry
;
115 struct directory
*bottom
;
118 static const struct attr_check
*get_archive_attrs(struct index_state
*istate
,
121 static struct attr_check
*check
;
123 check
= attr_check_initl("export-ignore", "export-subst", NULL
);
124 git_check_attr(istate
, path
, check
);
128 static int check_attr_export_ignore(const struct attr_check
*check
)
130 return check
&& ATTR_TRUE(check
->items
[0].value
);
133 static int check_attr_export_subst(const struct attr_check
*check
)
135 return check
&& ATTR_TRUE(check
->items
[1].value
);
138 static int write_archive_entry(const struct object_id
*oid
, const char *base
,
139 int baselen
, const char *filename
, unsigned mode
, int stage
,
142 static struct strbuf path
= STRBUF_INIT
;
143 struct archiver_context
*c
= context
;
144 struct archiver_args
*args
= c
->args
;
145 write_archive_entry_fn_t write_entry
= c
->write_entry
;
147 const char *path_without_prefix
;
151 strbuf_grow(&path
, PATH_MAX
);
152 strbuf_add(&path
, args
->base
, args
->baselen
);
153 strbuf_add(&path
, base
, baselen
);
154 strbuf_addstr(&path
, filename
);
155 if (S_ISDIR(mode
) || S_ISGITLINK(mode
))
156 strbuf_addch(&path
, '/');
157 path_without_prefix
= path
.buf
+ args
->baselen
;
159 if (!S_ISDIR(mode
)) {
160 const struct attr_check
*check
;
161 check
= get_archive_attrs(args
->repo
->index
, path_without_prefix
);
162 if (check_attr_export_ignore(check
))
164 args
->convert
= check_attr_export_subst(check
);
167 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
169 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
170 err
= write_entry(args
, oid
, path
.buf
, path
.len
, mode
);
173 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
177 fprintf(stderr
, "%.*s\n", (int)path
.len
, path
.buf
);
178 return write_entry(args
, oid
, path
.buf
, path
.len
, mode
);
181 static void queue_directory(const unsigned char *sha1
,
182 struct strbuf
*base
, const char *filename
,
183 unsigned mode
, int stage
, struct archiver_context
*c
)
186 size_t len
= st_add4(base
->len
, 1, strlen(filename
), 1);
187 d
= xmalloc(st_add(sizeof(*d
), len
));
189 d
->baselen
= base
->len
;
193 d
->len
= xsnprintf(d
->path
, len
, "%.*s%s/", (int)base
->len
, base
->buf
, filename
);
194 hashcpy(d
->oid
.hash
, sha1
);
197 static int write_directory(struct archiver_context
*c
)
199 struct directory
*d
= c
->bottom
;
205 d
->path
[d
->len
- 1] = '\0'; /* no trailing slash */
207 write_directory(c
) ||
208 write_archive_entry(&d
->oid
, d
->path
, d
->baselen
,
209 d
->path
+ d
->baselen
, d
->mode
,
210 d
->stage
, c
) != READ_TREE_RECURSIVE
;
215 static int queue_or_write_archive_entry(const struct object_id
*oid
,
216 struct strbuf
*base
, const char *filename
,
217 unsigned mode
, int stage
, void *context
)
219 struct archiver_context
*c
= context
;
222 !(base
->len
>= c
->bottom
->len
&&
223 !strncmp(base
->buf
, c
->bottom
->path
, c
->bottom
->len
))) {
224 struct directory
*next
= c
->bottom
->up
;
230 size_t baselen
= base
->len
;
231 const struct attr_check
*check
;
233 /* Borrow base, but restore its original value when done. */
234 strbuf_addstr(base
, filename
);
235 strbuf_addch(base
, '/');
236 check
= get_archive_attrs(c
->args
->repo
->index
, base
->buf
);
237 strbuf_setlen(base
, baselen
);
239 if (check_attr_export_ignore(check
))
241 queue_directory(oid
->hash
, base
, filename
,
243 return READ_TREE_RECURSIVE
;
246 if (write_directory(c
))
248 return write_archive_entry(oid
, base
->buf
, base
->len
, filename
, mode
,
252 int write_archive_entries(struct archiver_args
*args
,
253 write_archive_entry_fn_t write_entry
)
255 struct archiver_context context
;
256 struct unpack_trees_options opts
;
260 if (args
->baselen
> 0 && args
->base
[args
->baselen
- 1] == '/') {
261 size_t len
= args
->baselen
;
263 while (len
> 1 && args
->base
[len
- 2] == '/')
266 fprintf(stderr
, "%.*s\n", (int)len
, args
->base
);
267 err
= write_entry(args
, &args
->tree
->object
.oid
, args
->base
,
273 memset(&context
, 0, sizeof(context
));
275 context
.write_entry
= write_entry
;
278 * Setup index and instruct attr to read index only
280 if (!args
->worktree_attributes
) {
281 memset(&opts
, 0, sizeof(opts
));
284 opts
.src_index
= args
->repo
->index
;
285 opts
.dst_index
= args
->repo
->index
;
286 opts
.fn
= oneway_merge
;
287 init_tree_desc(&t
, args
->tree
->buffer
, args
->tree
->size
);
288 if (unpack_trees(1, &t
, &opts
))
290 git_attr_set_direction(GIT_ATTR_INDEX
);
293 err
= read_tree_recursive(args
->repo
, args
->tree
, "",
294 0, 0, &args
->pathspec
,
295 queue_or_write_archive_entry
,
297 if (err
== READ_TREE_RECURSIVE
)
299 while (context
.bottom
) {
300 struct directory
*next
= context
.bottom
->up
;
301 free(context
.bottom
);
302 context
.bottom
= next
;
307 static const struct archiver
*lookup_archiver(const char *name
)
314 for (i
= 0; i
< nr_archivers
; i
++) {
315 if (!strcmp(name
, archivers
[i
]->name
))
321 struct path_exists_context
{
322 struct pathspec pathspec
;
323 struct archiver_args
*args
;
326 static int reject_entry(const struct object_id
*oid
, struct strbuf
*base
,
327 const char *filename
, unsigned mode
,
328 int stage
, void *context
)
331 struct path_exists_context
*ctx
= context
;
334 struct strbuf sb
= STRBUF_INIT
;
335 strbuf_addbuf(&sb
, base
);
336 strbuf_addstr(&sb
, filename
);
337 if (!match_pathspec(ctx
->args
->repo
->index
,
339 sb
.buf
, sb
.len
, 0, NULL
, 1))
340 ret
= READ_TREE_RECURSIVE
;
346 static int path_exists(struct archiver_args
*args
, const char *path
)
348 const char *paths
[] = { path
, NULL
};
349 struct path_exists_context ctx
;
353 parse_pathspec(&ctx
.pathspec
, 0, 0, "", paths
);
354 ctx
.pathspec
.recursive
= 1;
355 ret
= read_tree_recursive(args
->repo
, args
->tree
, "",
358 clear_pathspec(&ctx
.pathspec
);
362 static void parse_pathspec_arg(const char **pathspec
,
363 struct archiver_args
*ar_args
)
366 * must be consistent with parse_pathspec in path_exists()
367 * Also if pathspec patterns are dependent, we're in big
368 * trouble as we test each one separately
370 parse_pathspec(&ar_args
->pathspec
, 0,
371 PATHSPEC_PREFER_FULL
,
373 ar_args
->pathspec
.recursive
= 1;
376 if (**pathspec
&& !path_exists(ar_args
, *pathspec
))
377 die(_("pathspec '%s' did not match any files"), *pathspec
);
383 static void parse_treeish_arg(const char **argv
,
384 struct archiver_args
*ar_args
, const char *prefix
,
387 const char *name
= argv
[0];
388 const struct object_id
*commit_oid
;
391 const struct commit
*commit
;
392 struct object_id oid
;
395 /* Remotes are only allowed to fetch actual refs */
396 if (remote
&& !remote_allow_unreachable
) {
397 const char *colon
= strchrnul(name
, ':');
398 int refnamelen
= colon
- name
;
400 if (!dwim_ref(name
, refnamelen
, &oid
, &ref
, 0))
401 die(_("no such ref: %.*s"), refnamelen
, name
);
403 dwim_ref(name
, strlen(name
), &oid
, &ref
, 0);
406 if (get_oid(name
, &oid
))
407 die(_("not a valid object name: %s"), name
);
409 commit
= lookup_commit_reference_gently(ar_args
->repo
, &oid
, 1);
411 commit_oid
= &commit
->object
.oid
;
412 archive_time
= commit
->date
;
415 archive_time
= time(NULL
);
418 tree
= parse_tree_indirect(&oid
);
420 die(_("not a tree object: %s"), oid_to_hex(&oid
));
423 struct object_id tree_oid
;
427 err
= get_tree_entry(ar_args
->repo
,
431 if (err
|| !S_ISDIR(mode
))
432 die(_("current working directory is untracked"));
434 tree
= parse_tree_indirect(&tree_oid
);
436 ar_args
->refname
= ref
;
437 ar_args
->tree
= tree
;
438 ar_args
->commit_oid
= commit_oid
;
439 ar_args
->commit
= commit
;
440 ar_args
->time
= archive_time
;
443 #define OPT__COMPR(s, v, h, p) \
444 OPT_SET_INT_F(s, NULL, v, h, p, PARSE_OPT_NONEG)
445 #define OPT__COMPR_HIDDEN(s, v, p) \
446 OPT_SET_INT_F(s, NULL, v, "", p, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN)
448 static int parse_archive_args(int argc
, const char **argv
,
449 const struct archiver
**ar
, struct archiver_args
*args
,
450 const char *name_hint
, int is_remote
)
452 const char *format
= NULL
;
453 const char *base
= NULL
;
454 const char *remote
= NULL
;
455 const char *exec
= NULL
;
456 const char *output
= NULL
;
457 int compression_level
= -1;
461 int worktree_attributes
= 0;
462 struct option opts
[] = {
464 OPT_STRING(0, "format", &format
, N_("fmt"), N_("archive format")),
465 OPT_STRING(0, "prefix", &base
, N_("prefix"),
466 N_("prepend prefix to each pathname in the archive")),
467 OPT_STRING('o', "output", &output
, N_("file"),
468 N_("write the archive to this file")),
469 OPT_BOOL(0, "worktree-attributes", &worktree_attributes
,
470 N_("read .gitattributes in working directory")),
471 OPT__VERBOSE(&verbose
, N_("report archived files on stderr")),
472 OPT__COMPR('0', &compression_level
, N_("store only"), 0),
473 OPT__COMPR('1', &compression_level
, N_("compress faster"), 1),
474 OPT__COMPR_HIDDEN('2', &compression_level
, 2),
475 OPT__COMPR_HIDDEN('3', &compression_level
, 3),
476 OPT__COMPR_HIDDEN('4', &compression_level
, 4),
477 OPT__COMPR_HIDDEN('5', &compression_level
, 5),
478 OPT__COMPR_HIDDEN('6', &compression_level
, 6),
479 OPT__COMPR_HIDDEN('7', &compression_level
, 7),
480 OPT__COMPR_HIDDEN('8', &compression_level
, 8),
481 OPT__COMPR('9', &compression_level
, N_("compress better"), 9),
483 OPT_BOOL('l', "list", &list
,
484 N_("list supported archive formats")),
486 OPT_STRING(0, "remote", &remote
, N_("repo"),
487 N_("retrieve the archive from remote repository <repo>")),
488 OPT_STRING(0, "exec", &exec
, N_("command"),
489 N_("path to the remote git-upload-archive command")),
493 argc
= parse_options(argc
, argv
, NULL
, opts
, archive_usage
, 0);
496 die(_("Unexpected option --remote"));
498 die(_("Option --exec can only be used together with --remote"));
500 die(_("Unexpected option --output"));
506 for (i
= 0; i
< nr_archivers
; i
++)
507 if (!is_remote
|| archivers
[i
]->flags
& ARCHIVER_REMOTE
)
508 printf("%s\n", archivers
[i
]->name
);
512 if (!format
&& name_hint
)
513 format
= archive_format_from_filename(name_hint
);
517 /* We need at least one parameter -- tree-ish */
519 usage_with_options(archive_usage
, opts
);
520 *ar
= lookup_archiver(format
);
521 if (!*ar
|| (is_remote
&& !((*ar
)->flags
& ARCHIVER_REMOTE
)))
522 die(_("Unknown archive format '%s'"), format
);
524 args
->compression_level
= Z_DEFAULT_COMPRESSION
;
525 if (compression_level
!= -1) {
526 if ((*ar
)->flags
& ARCHIVER_WANT_COMPRESSION_LEVELS
)
527 args
->compression_level
= compression_level
;
529 die(_("Argument not supported for format '%s': -%d"),
530 format
, compression_level
);
533 args
->verbose
= verbose
;
535 args
->baselen
= strlen(base
);
536 args
->worktree_attributes
= worktree_attributes
;
541 int write_archive(int argc
, const char **argv
, const char *prefix
,
542 struct repository
*repo
,
543 const char *name_hint
, int remote
)
545 const struct archiver
*ar
= NULL
;
546 struct archiver_args args
;
548 git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable
);
549 git_config(git_default_config
, NULL
);
552 argc
= parse_archive_args(argc
, argv
, &ar
, &args
, name_hint
, remote
);
553 if (!startup_info
->have_repository
) {
555 * We know this will die() with an error, so we could just
556 * die ourselves; but its error message will be more specific
557 * than what we could write here.
559 setup_git_directory();
562 parse_treeish_arg(argv
, &args
, prefix
, remote
);
563 parse_pathspec_arg(argv
+ 1, &args
);
565 return ar
->write_archive(ar
, &args
);
568 static int match_extension(const char *filename
, const char *ext
)
570 int prefixlen
= strlen(filename
) - strlen(ext
);
573 * We need 1 character for the '.', and 1 character to ensure that the
574 * prefix is non-empty (k.e., we don't match .tar.gz with no actual
577 if (prefixlen
< 2 || filename
[prefixlen
- 1] != '.')
579 return !strcmp(filename
+ prefixlen
, ext
);
582 const char *archive_format_from_filename(const char *filename
)
586 for (i
= 0; i
< nr_archivers
; i
++)
587 if (match_extension(filename
, archivers
[i
]->name
))
588 return archivers
[i
]->name
;