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
;
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 pid
= git_connect(fd
, url
, exec
, 0);
53 for (i
= 1; i
< argc
; i
++) {
56 packet_write(fd
[1], "argument %s\n", argv
[i
]);
60 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
62 die("git-archive: expected ACK/NAK, got EOF");
63 if (buf
[len
-1] == '\n')
65 if (strcmp(buf
, "ACK")) {
66 if (len
> 5 && !prefixcmp(buf
, "NACK "))
67 die("git-archive: NACK %s", buf
+ 5);
68 die("git-archive: protocol error");
71 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
73 die("git-archive: expected a flush");
75 /* Now, start reading from fd[0] and spit it out to stdout */
76 rv
= recv_sideband("archive", fd
[0], 1, 2);
79 rv
|= finish_connect(pid
);
84 static void *format_subst(const struct commit
*commit
, const char *format
,
87 unsigned long len
= *sizep
, result_len
= 0;
88 const char *a
= format
;
93 char *fmt
, *formatted
= NULL
;
94 unsigned long a_len
, fmt_len
, formatted_len
, allocated
= 0;
96 b
= memmem(a
, len
, "$Format:", 8);
97 if (!b
|| a
+ len
< b
+ 9)
99 c
= memchr(b
+ 8, '$', len
- 8);
105 fmt
= xmalloc(fmt_len
+ 1);
106 memcpy(fmt
, b
+ 8, fmt_len
);
109 formatted_len
= format_commit_message(commit
, fmt
, &formatted
,
112 result
= xrealloc(result
, result_len
+ a_len
+ formatted_len
);
113 memcpy(result
+ result_len
, a
, a_len
);
114 memcpy(result
+ result_len
+ a_len
, formatted
, formatted_len
);
115 result_len
+= a_len
+ formatted_len
;
121 result
= xrealloc(result
, result_len
+ len
);
122 memcpy(result
+ result_len
, a
, len
);
131 static void *convert_to_archive(const char *path
,
132 const void *src
, unsigned long *sizep
,
133 const struct commit
*commit
)
135 static struct git_attr
*attr_export_subst
;
136 struct git_attr_check check
[1];
141 if (!attr_export_subst
)
142 attr_export_subst
= git_attr("export-subst", 12);
144 check
[0].attr
= attr_export_subst
;
145 if (git_checkattr(path
, ARRAY_SIZE(check
), check
))
147 if (!ATTR_TRUE(check
[0].value
))
150 return format_subst(commit
, src
, sizep
);
153 void *sha1_file_to_archive(const char *path
, const unsigned char *sha1
,
154 unsigned int mode
, enum object_type
*type
,
156 const struct commit
*commit
)
158 void *buffer
, *converted
;
160 buffer
= read_sha1_file(sha1
, type
, size
);
161 if (buffer
&& S_ISREG(mode
)) {
162 converted
= convert_to_working_tree(path
, buffer
, size
);
168 converted
= convert_to_archive(path
, buffer
, size
, commit
);
178 static int init_archiver(const char *name
, struct archiver
*ar
)
182 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
183 if (!strcmp(name
, archivers
[i
].name
)) {
184 memset(ar
, 0, sizeof(*ar
));
185 ar
->name
= archivers
[i
].name
;
186 ar
->write_archive
= archivers
[i
].write_archive
;
187 ar
->parse_extra
= archivers
[i
].parse_extra
;
195 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
197 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
200 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
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 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
246 const char *extra_argv
[MAX_EXTRA_ARGS
];
248 const char *format
= "tar";
249 const char *base
= "";
253 for (i
= 1; i
< argc
; i
++) {
254 const char *arg
= argv
[i
];
256 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
257 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
258 printf("%s\n", archivers
[i
].name
);
261 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
265 if (!prefixcmp(arg
, "--format=")) {
269 if (!prefixcmp(arg
, "--prefix=")) {
273 if (!strcmp(arg
, "--")) {
278 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
279 die("Too many extra options");
280 extra_argv
[extra_argc
++] = arg
;
286 /* We need at least one parameter -- tree-ish */
288 usage(archive_usage
);
289 if (init_archiver(format
, ar
) < 0)
290 die("Unknown archive format '%s'", format
);
293 if (!ar
->parse_extra
)
294 die("'%s' format does not handle %s",
295 ar
->name
, extra_argv
[0]);
296 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
298 ar
->args
.verbose
= verbose
;
299 ar
->args
.base
= base
;
304 static const char *extract_remote_arg(int *ac
, const char **av
)
306 int ix
, iy
, cnt
= *ac
;
307 int no_more_options
= 0;
308 const char *remote
= NULL
;
310 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
311 const char *arg
= av
[ix
];
312 if (!strcmp(arg
, "--"))
314 if (!no_more_options
) {
315 if (!prefixcmp(arg
, "--remote=")) {
317 die("Multiple --remote specified");
335 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
339 const char *remote
= NULL
;
341 remote
= extract_remote_arg(&argc
, argv
);
343 return run_remote_archiver(remote
, argc
, argv
);
345 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
347 memset(&ar
, 0, sizeof(ar
));
348 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
350 prefix
= setup_git_directory();
353 parse_treeish_arg(argv
, &ar
.args
, prefix
);
354 parse_pathspec_arg(argv
+ 1, &ar
.args
);
356 return ar
.write_archive(&ar
.args
);