2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
14 static const char archive_usage
[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
17 static struct archiver_desc
20 write_archive_fn_t write_archive
;
21 parse_extra_args_fn_t parse_extra
;
23 { "tar", write_tar_archive
, NULL
},
24 { "zip", write_zip_archive
, parse_extra_zip_args
},
27 static int run_remote_archiver(const char *remote
, int argc
,
30 char *url
, buf
[LARGE_PACKET_MAX
];
31 int fd
[2], i
, len
, rv
;
33 const char *exec
= "git-upload-archive";
36 for (i
= 1; i
< argc
; i
++) {
37 const char *arg
= argv
[i
];
38 if (!prefixcmp(arg
, "--exec=")) {
40 die("multiple --exec specified");
47 url
= xstrdup(remote
);
48 pid
= git_connect(fd
, url
, exec
, 0);
52 for (i
= 1; i
< argc
; i
++) {
55 packet_write(fd
[1], "argument %s\n", argv
[i
]);
59 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
61 die("git-archive: expected ACK/NAK, got EOF");
62 if (buf
[len
-1] == '\n')
64 if (strcmp(buf
, "ACK")) {
65 if (len
> 5 && !prefixcmp(buf
, "NACK "))
66 die("git-archive: NACK %s", buf
+ 5);
67 die("git-archive: protocol error");
70 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
72 die("git-archive: expected a flush");
74 /* Now, start reading from fd[0] and spit it out to stdout */
75 rv
= recv_sideband("archive", fd
[0], 1, 2);
78 rv
|= finish_connect(pid
);
83 static int init_archiver(const char *name
, struct archiver
*ar
)
87 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
88 if (!strcmp(name
, archivers
[i
].name
)) {
89 memset(ar
, 0, sizeof(*ar
));
90 ar
->name
= archivers
[i
].name
;
91 ar
->write_archive
= archivers
[i
].write_archive
;
92 ar
->parse_extra
= archivers
[i
].parse_extra
;
100 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
102 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
105 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
108 const char *name
= argv
[0];
109 const unsigned char *commit_sha1
;
112 struct commit
*commit
;
113 unsigned char sha1
[20];
115 if (get_sha1(name
, sha1
))
116 die("Not a valid object name");
118 commit
= lookup_commit_reference_gently(sha1
, 1);
120 commit_sha1
= commit
->object
.sha1
;
121 archive_time
= commit
->date
;
124 archive_time
= time(NULL
);
127 tree
= parse_tree_indirect(sha1
);
129 die("not a tree object");
132 unsigned char tree_sha1
[20];
136 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
138 if (err
|| !S_ISDIR(mode
))
139 die("current working directory is untracked");
141 tree
= parse_tree_indirect(tree_sha1
);
143 ar_args
->tree
= tree
;
144 ar_args
->commit_sha1
= commit_sha1
;
145 ar_args
->time
= archive_time
;
148 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
150 const char *extra_argv
[MAX_EXTRA_ARGS
];
152 const char *format
= "tar";
153 const char *base
= "";
157 for (i
= 1; i
< argc
; i
++) {
158 const char *arg
= argv
[i
];
160 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
161 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
162 printf("%s\n", archivers
[i
].name
);
165 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
169 if (!prefixcmp(arg
, "--format=")) {
173 if (!prefixcmp(arg
, "--prefix=")) {
177 if (!strcmp(arg
, "--")) {
182 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
183 die("Too many extra options");
184 extra_argv
[extra_argc
++] = arg
;
190 /* We need at least one parameter -- tree-ish */
192 usage(archive_usage
);
193 if (init_archiver(format
, ar
) < 0)
194 die("Unknown archive format '%s'", format
);
197 if (!ar
->parse_extra
)
198 die("'%s' format does not handle %s",
199 ar
->name
, extra_argv
[0]);
200 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
202 ar
->args
.verbose
= verbose
;
203 ar
->args
.base
= base
;
208 static const char *extract_remote_arg(int *ac
, const char **av
)
210 int ix
, iy
, cnt
= *ac
;
211 int no_more_options
= 0;
212 const char *remote
= NULL
;
214 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
215 const char *arg
= av
[ix
];
216 if (!strcmp(arg
, "--"))
218 if (!no_more_options
) {
219 if (!prefixcmp(arg
, "--remote=")) {
221 die("Multiple --remote specified");
239 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
243 const char *remote
= NULL
;
245 remote
= extract_remote_arg(&argc
, argv
);
247 return run_remote_archiver(remote
, argc
, argv
);
249 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
251 memset(&ar
, 0, sizeof(ar
));
252 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
254 prefix
= setup_git_directory();
257 parse_treeish_arg(argv
, &ar
.args
, prefix
);
258 parse_pathspec_arg(argv
+ 1, &ar
.args
);
260 return ar
.write_archive(&ar
.args
);