2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
10 #include "tree-walk.h"
14 static const char archive_usage
[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
17 struct archiver archivers
[] = {
20 .write_archive
= write_tar_archive
,
24 .write_archive
= write_zip_archive
,
25 .parse_extra
= parse_extra_zip_args
,
29 static int run_remote_archiver(const char *remote
, int argc
,
33 int fd
[2], i
, len
, rv
;
36 sprintf(buf
, "git-upload-archive");
38 url
= xstrdup(remote
);
39 pid
= git_connect(fd
, url
, buf
);
43 for (i
= 1; i
< argc
; i
++)
44 packet_write(fd
[1], "argument %s\n", argv
[i
]);
47 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
49 die("git-archive: expected ACK/NAK, got EOF");
50 if (buf
[len
-1] == '\n')
52 if (strcmp(buf
, "ACK")) {
53 if (len
> 5 && !strncmp(buf
, "NACK ", 5))
54 die("git-archive: NACK %s", buf
+ 5);
55 die("git-archive: protocol error");
58 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
60 die("git-archive: expected a flush");
62 /* Now, start reading from fd[0] and spit it out to stdout */
63 rv
= copy_fd(fd
[0], 1);
66 rv
|= finish_connect(pid
);
71 static int init_archiver(const char *name
, struct archiver
*ar
)
75 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
76 if (!strcmp(name
, archivers
[i
].name
)) {
77 memcpy(ar
, &archivers
[i
], sizeof(struct archiver
));
85 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
87 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
90 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
93 const char *name
= argv
[0];
94 const unsigned char *commit_sha1
;
97 struct commit
*commit
;
98 unsigned char sha1
[20];
100 if (get_sha1(name
, sha1
))
101 die("Not a valid object name");
103 commit
= lookup_commit_reference_gently(sha1
, 1);
105 commit_sha1
= commit
->object
.sha1
;
106 archive_time
= commit
->date
;
109 archive_time
= time(NULL
);
112 tree
= parse_tree_indirect(sha1
);
114 die("not a tree object");
117 unsigned char tree_sha1
[20];
121 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
123 if (err
|| !S_ISDIR(mode
))
124 die("current working directory is untracked");
127 tree
= parse_tree_indirect(tree_sha1
);
129 ar_args
->tree
= tree
;
130 ar_args
->commit_sha1
= commit_sha1
;
131 ar_args
->time
= archive_time
;
134 static const char *default_parse_extra(struct archiver
*ar
,
139 snprintf(msg
, sizeof(msg
) - 4, "'%s' format does not handle %s",
142 return strcat(msg
, "...");
145 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
147 const char *extra_argv
[MAX_EXTRA_ARGS
];
149 const char *format
= NULL
; /* might want to default to "tar" */
150 const char *base
= "";
153 for (i
= 1; i
< argc
; i
++) {
154 const char *arg
= argv
[i
];
156 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
157 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
158 printf("%s\n", archivers
[i
].name
);
161 if (!strncmp(arg
, "--format=", 9)) {
165 if (!strncmp(arg
, "--prefix=", 9)) {
169 if (!strcmp(arg
, "--")) {
174 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
175 die("Too many extra options");
176 extra_argv
[extra_argc
++] = arg
;
182 /* We need at least one parameter -- tree-ish */
184 usage(archive_usage
);
186 die("You must specify an archive format");
187 if (init_archiver(format
, ar
) < 0)
188 die("Unknown archive format '%s'", format
);
191 if (!ar
->parse_extra
)
192 die("%s", default_parse_extra(ar
, extra_argv
));
193 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
195 ar
->args
.base
= base
;
200 static const char *remote_request(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 (!strncmp(arg
, "--remote=", 9)) {
213 die("Multiple --remote specified");
231 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
235 const char *remote
= NULL
;
237 remote
= remote_request(&argc
, argv
);
239 return run_remote_archiver(remote
, argc
, argv
);
241 memset(&ar
, 0, sizeof(ar
));
242 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
244 prefix
= setup_git_directory();
247 parse_treeish_arg(argv
, &ar
.args
, prefix
);
248 parse_pathspec_arg(argv
+ 1, &ar
.args
);
250 return ar
.write_archive(&ar
.args
);