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 const struct archiver archivers
[] = {
19 { "tar", write_tar_archive
, NULL
},
20 { "zip", write_zip_archive
, parse_extra_zip_args
},
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 *extra_argv
[MAX_EXTRA_ARGS
];
142 const char *format
= "tar";
143 const char *base
= "";
147 for (i
= 1; i
< argc
; i
++) {
148 const char *arg
= argv
[i
];
150 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
151 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
152 printf("%s\n", archivers
[i
].name
);
155 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
159 if (!prefixcmp(arg
, "--format=")) {
163 if (!prefixcmp(arg
, "--prefix=")) {
167 if (!strcmp(arg
, "--")) {
172 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
173 die("Too many extra options");
174 extra_argv
[extra_argc
++] = arg
;
180 /* We need at least one parameter -- tree-ish */
182 usage(archive_usage
);
183 *ar
= lookup_archiver(format
);
185 die("Unknown archive format '%s'", format
);
188 if (!(*ar
)->parse_extra
)
189 die("'%s' format does not handle %s",
190 (*ar
)->name
, extra_argv
[0]);
191 args
->extra
= (*ar
)->parse_extra(extra_argc
, extra_argv
);
193 args
->verbose
= verbose
;
195 args
->baselen
= strlen(base
);
200 static const char *extract_remote_arg(int *ac
, const char **av
)
202 int ix
, iy
, cnt
= *ac
;
203 int no_more_options
= 0;
204 const char *remote
= NULL
;
206 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
207 const char *arg
= av
[ix
];
208 if (!strcmp(arg
, "--"))
210 if (!no_more_options
) {
211 if (!prefixcmp(arg
, "--remote=")) {
213 die("Multiple --remote specified");
231 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
233 const struct archiver
*ar
= NULL
;
234 struct archiver_args args
;
236 const char *remote
= NULL
;
238 remote
= extract_remote_arg(&argc
, argv
);
240 return run_remote_archiver(remote
, argc
, argv
);
242 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
244 tree_idx
= parse_archive_args(argc
, argv
, &ar
, &args
);
246 prefix
= setup_git_directory();
249 parse_treeish_arg(argv
, &args
, prefix
);
250 parse_pathspec_arg(argv
+ 1, &args
);
252 return ar
->write_archive(&args
);