2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
13 static const char archive_usage
[] = \
14 "git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
16 #define USES_ZLIB_COMPRESSION 1
18 const struct archiver archivers
[] = {
19 { "tar", write_tar_archive
},
20 { "zip", write_zip_archive
, USES_ZLIB_COMPRESSION
},
23 static int run_remote_archiver(const char *remote
, int argc
,
26 char *url
, buf
[LARGE_PACKET_MAX
];
27 int fd
[2], i
, len
, rv
;
28 struct child_process
*conn
;
29 const char *exec
= "git-upload-archive";
32 for (i
= 1; i
< argc
; i
++) {
33 const char *arg
= argv
[i
];
34 if (!prefixcmp(arg
, "--exec=")) {
36 die("multiple --exec specified");
43 url
= xstrdup(remote
);
44 conn
= git_connect(fd
, url
, exec
, 0);
46 for (i
= 1; i
< argc
; i
++) {
49 packet_write(fd
[1], "argument %s\n", argv
[i
]);
53 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
55 die("git-archive: expected ACK/NAK, got EOF");
56 if (buf
[len
-1] == '\n')
58 if (strcmp(buf
, "ACK")) {
59 if (len
> 5 && !prefixcmp(buf
, "NACK "))
60 die("git-archive: NACK %s", buf
+ 5);
61 die("git-archive: protocol error");
64 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
66 die("git-archive: expected a flush");
68 /* Now, start reading from fd[0] and spit it out to stdout */
69 rv
= recv_sideband("archive", fd
[0], 1, 2);
72 rv
|= finish_connect(conn
);
77 static const struct archiver
*lookup_archiver(const char *name
)
81 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
82 if (!strcmp(name
, archivers
[i
].name
))
88 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
90 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
93 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
96 const char *name
= argv
[0];
97 const unsigned char *commit_sha1
;
100 const struct commit
*commit
;
101 unsigned char sha1
[20];
103 if (get_sha1(name
, sha1
))
104 die("Not a valid object name");
106 commit
= lookup_commit_reference_gently(sha1
, 1);
108 commit_sha1
= commit
->object
.sha1
;
109 archive_time
= commit
->date
;
112 archive_time
= time(NULL
);
115 tree
= parse_tree_indirect(sha1
);
117 die("not a tree object");
120 unsigned char tree_sha1
[20];
124 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
126 if (err
|| !S_ISDIR(mode
))
127 die("current working directory is untracked");
129 tree
= parse_tree_indirect(tree_sha1
);
131 ar_args
->tree
= tree
;
132 ar_args
->commit_sha1
= commit_sha1
;
133 ar_args
->commit
= commit
;
134 ar_args
->time
= archive_time
;
137 int parse_archive_args(int argc
, const char **argv
, const struct archiver
**ar
,
138 struct archiver_args
*args
)
140 const char *format
= "tar";
141 const char *base
= "";
142 int compression_level
= -1;
146 for (i
= 1; i
< argc
; i
++) {
147 const char *arg
= argv
[i
];
149 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
150 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
151 printf("%s\n", archivers
[i
].name
);
154 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
158 if (!prefixcmp(arg
, "--format=")) {
162 if (!prefixcmp(arg
, "--prefix=")) {
166 if (!strcmp(arg
, "--")) {
170 if (arg
[0] == '-' && isdigit(arg
[1]) && arg
[2] == '\0') {
171 compression_level
= arg
[1] - '0';
175 die("Unknown argument: %s", arg
);
179 /* We need at least one parameter -- tree-ish */
181 usage(archive_usage
);
182 *ar
= lookup_archiver(format
);
184 die("Unknown archive format '%s'", format
);
186 args
->compression_level
= Z_DEFAULT_COMPRESSION
;
187 if (compression_level
!= -1) {
188 if ((*ar
)->flags
& USES_ZLIB_COMPRESSION
)
189 args
->compression_level
= compression_level
;
191 die("Argument not supported for format '%s': -%d",
192 format
, compression_level
);
195 args
->verbose
= verbose
;
197 args
->baselen
= strlen(base
);
202 static const char *extract_remote_arg(int *ac
, const char **av
)
204 int ix
, iy
, cnt
= *ac
;
205 int no_more_options
= 0;
206 const char *remote
= NULL
;
208 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
209 const char *arg
= av
[ix
];
210 if (!strcmp(arg
, "--"))
212 if (!no_more_options
) {
213 if (!prefixcmp(arg
, "--remote=")) {
215 die("Multiple --remote specified");
233 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
235 const struct archiver
*ar
= NULL
;
236 struct archiver_args args
;
238 const char *remote
= NULL
;
240 remote
= extract_remote_arg(&argc
, argv
);
242 return run_remote_archiver(remote
, argc
, argv
);
244 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
246 tree_idx
= parse_archive_args(argc
, argv
, &ar
, &args
);
248 prefix
= setup_git_directory();
251 parse_treeish_arg(argv
, &args
, prefix
);
252 parse_pathspec_arg(argv
+ 1, &args
);
254 return ar
->write_archive(&args
);