2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
15 static const char archive_usage
[] = \
16 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
18 static struct archiver_desc
21 write_archive_fn_t write_archive
;
22 parse_extra_args_fn_t parse_extra
;
24 { "tar", write_tar_archive
, NULL
},
25 { "zip", write_zip_archive
, parse_extra_zip_args
},
28 static int run_remote_archiver(const char *remote
, int argc
,
31 char *url
, buf
[LARGE_PACKET_MAX
];
32 int fd
[2], i
, len
, rv
;
33 struct child_process
*conn
;
34 const char *exec
= "git-upload-archive";
37 for (i
= 1; i
< argc
; i
++) {
38 const char *arg
= argv
[i
];
39 if (!prefixcmp(arg
, "--exec=")) {
41 die("multiple --exec specified");
48 url
= xstrdup(remote
);
49 conn
= git_connect(fd
, url
, exec
, 0);
51 for (i
= 1; i
< argc
; i
++) {
54 packet_write(fd
[1], "argument %s\n", argv
[i
]);
58 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
60 die("git-archive: expected ACK/NAK, got EOF");
61 if (buf
[len
-1] == '\n')
63 if (strcmp(buf
, "ACK")) {
64 if (len
> 5 && !prefixcmp(buf
, "NACK "))
65 die("git-archive: NACK %s", buf
+ 5);
66 die("git-archive: protocol error");
69 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
71 die("git-archive: expected a flush");
73 /* Now, start reading from fd[0] and spit it out to stdout */
74 rv
= recv_sideband("archive", fd
[0], 1, 2);
77 rv
|= finish_connect(conn
);
82 static void format_subst(const struct commit
*commit
,
83 const char *src
, size_t len
,
90 to_free
= strbuf_detach(buf
, NULL
);
95 b
= memmem(src
, len
, "$Format:", 8);
96 if (!b
|| src
+ len
< b
+ 9)
98 c
= memchr(b
+ 8, '$', len
- 8);
103 strbuf_add(&fmt
, b
+ 8, c
- b
- 8);
105 strbuf_add(buf
, src
, b
- src
);
106 format_commit_message(commit
, fmt
.buf
, buf
);
110 strbuf_add(buf
, src
, len
);
111 strbuf_release(&fmt
);
115 static int convert_to_archive(const char *path
,
116 const void *src
, size_t len
,
118 const struct commit
*commit
)
120 static struct git_attr
*attr_export_subst
;
121 struct git_attr_check check
[1];
126 if (!attr_export_subst
)
127 attr_export_subst
= git_attr("export-subst", 12);
129 check
[0].attr
= attr_export_subst
;
130 if (git_checkattr(path
, ARRAY_SIZE(check
), check
))
132 if (!ATTR_TRUE(check
[0].value
))
135 format_subst(commit
, src
, len
, buf
);
139 void *sha1_file_to_archive(const char *path
, const unsigned char *sha1
,
140 unsigned int mode
, enum object_type
*type
,
141 unsigned long *sizep
,
142 const struct commit
*commit
)
146 buffer
= read_sha1_file(sha1
, type
, sizep
);
147 if (buffer
&& S_ISREG(mode
)) {
151 strbuf_init(&buf
, 0);
152 strbuf_attach(&buf
, buffer
, *sizep
, *sizep
+ 1);
153 convert_to_working_tree(path
, buf
.buf
, buf
.len
, &buf
);
154 convert_to_archive(path
, buf
.buf
, buf
.len
, &buf
, commit
);
155 buffer
= strbuf_detach(&buf
, &size
);
162 static int init_archiver(const char *name
, struct archiver
*ar
)
166 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
167 if (!strcmp(name
, archivers
[i
].name
)) {
168 memset(ar
, 0, sizeof(*ar
));
169 ar
->name
= archivers
[i
].name
;
170 ar
->write_archive
= archivers
[i
].write_archive
;
171 ar
->parse_extra
= archivers
[i
].parse_extra
;
179 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
181 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
184 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
187 const char *name
= argv
[0];
188 const unsigned char *commit_sha1
;
191 const struct commit
*commit
;
192 unsigned char sha1
[20];
194 if (get_sha1(name
, sha1
))
195 die("Not a valid object name");
197 commit
= lookup_commit_reference_gently(sha1
, 1);
199 commit_sha1
= commit
->object
.sha1
;
200 archive_time
= commit
->date
;
203 archive_time
= time(NULL
);
206 tree
= parse_tree_indirect(sha1
);
208 die("not a tree object");
211 unsigned char tree_sha1
[20];
215 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
217 if (err
|| !S_ISDIR(mode
))
218 die("current working directory is untracked");
220 tree
= parse_tree_indirect(tree_sha1
);
222 ar_args
->tree
= tree
;
223 ar_args
->commit_sha1
= commit_sha1
;
224 ar_args
->commit
= commit
;
225 ar_args
->time
= archive_time
;
228 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
230 const char *extra_argv
[MAX_EXTRA_ARGS
];
232 const char *format
= "tar";
233 const char *base
= "";
237 for (i
= 1; i
< argc
; i
++) {
238 const char *arg
= argv
[i
];
240 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
241 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
242 printf("%s\n", archivers
[i
].name
);
245 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
249 if (!prefixcmp(arg
, "--format=")) {
253 if (!prefixcmp(arg
, "--prefix=")) {
257 if (!strcmp(arg
, "--")) {
262 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
263 die("Too many extra options");
264 extra_argv
[extra_argc
++] = arg
;
270 /* We need at least one parameter -- tree-ish */
272 usage(archive_usage
);
273 if (init_archiver(format
, ar
) < 0)
274 die("Unknown archive format '%s'", format
);
277 if (!ar
->parse_extra
)
278 die("'%s' format does not handle %s",
279 ar
->name
, extra_argv
[0]);
280 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
282 ar
->args
.verbose
= verbose
;
283 ar
->args
.base
= base
;
288 static const char *extract_remote_arg(int *ac
, const char **av
)
290 int ix
, iy
, cnt
= *ac
;
291 int no_more_options
= 0;
292 const char *remote
= NULL
;
294 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
295 const char *arg
= av
[ix
];
296 if (!strcmp(arg
, "--"))
298 if (!no_more_options
) {
299 if (!prefixcmp(arg
, "--remote=")) {
301 die("Multiple --remote specified");
319 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
323 const char *remote
= NULL
;
325 remote
= extract_remote_arg(&argc
, argv
);
327 return run_remote_archiver(remote
, argc
, argv
);
329 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
331 memset(&ar
, 0, sizeof(ar
));
332 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
334 prefix
= setup_git_directory();
337 parse_treeish_arg(argv
, &ar
.args
, prefix
);
338 parse_pathspec_arg(argv
+ 1, &ar
.args
);
340 return ar
.write_archive(&ar
.args
);