1
+/*
2
+ * Copyright (c) 2006 Franck Bui-Huu
3
+ * Copyright (c) 2006 Rene Scharfe
4
+ */
5
+#include <time.h>
6
+#include "cache.h"
7
+#include "builtin.h"
8
+#include "archive.h"
9
+#include "commit.h"
10
+#include "tree-walk.h"
11
+#include "exec_cmd.h"
12
+#include "pkt-line.h"
13
+
14
+static const char archive_usage[] = \
15
+"git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
16
+
17
+struct archiver archivers[] = {
18
+ { "" /* dummy */ },
19
+};
20
+
21
+static int run_remote_archiver(struct archiver *ar, int argc,
22
+ const char **argv)
23
+{
24
+ char *url, buf[1024];
25
+ int fd[2], i, len, rv;
26
+ pid_t pid;
27
+
28
+ sprintf(buf, "git-upload-archive");
29
+
30
+ url = xstrdup(ar->remote);
31
+ pid = git_connect(fd, url, buf);
32
+ if (pid < 0)
33
+ return pid;
34
+
35
+ for (i = 1; i < argc; i++) {
36
+ if (!strncmp(argv[i], "--remote=", 9))
37
+ continue;
38
+ packet_write(fd[1], "argument %s\n", argv[i]);
39
+ }
40
+ packet_flush(fd[1]);
41
+
42
+ len = packet_read_line(fd[0], buf, sizeof(buf));
43
+ if (!len)
44
+ die("git-archive: expected ACK/NAK, got EOF");
45
+ if (buf[len-1] == '\n')
46
+ buf[--len] = 0;
47
+ if (strcmp(buf, "ACK")) {
48
+ if (len > 5 && !strncmp(buf, "NACK ", 5))
49
+ die("git-archive: NACK %s", buf + 5);
50
+ die("git-archive: protocol error");
51
+ }
52
+
53
+ len = packet_read_line(fd[0], buf, sizeof(buf));
54
+ if (len)
55
+ die("git-archive: expected a flush");
56
+
57
+ /* Now, start reading from fd[0] and spit it out to stdout */
58
+ rv = copy_fd(fd[0], 1);
59
+
60
+ close(fd[0]);
61
+ rv |= finish_connect(pid);
62
+
63
+ return !!rv;
64
+}
65
+
66
+static int init_archiver(const char *name, struct archiver *ar)
67
+{
68
+ int rv = -1, i;
69
+
70
+ for (i = 0; i < ARRAY_SIZE(archivers); i++) {
71
+ if (!strcmp(name, archivers[i].name)) {
72
+ memcpy(ar, &archivers[i], sizeof(struct archiver));
73
+ rv = 0;
74
+ break;
75
+ }
76
+ }
77
+ return rv;
78
+}
79
+
80
+void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
81
+{
82
+ ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
83
+}
84
+
85
+void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
86
+ const char *prefix)
87
+{
88
+ const char *name = argv[0];
89
+ const unsigned char *commit_sha1;
90
+ time_t archive_time;
91
+ struct tree *tree;
92
+ struct commit *commit;
93
+ unsigned char sha1[20];
94
+
95
+ if (get_sha1(name, sha1))
96
+ die("Not a valid object name");
97
+
98
+ commit = lookup_commit_reference_gently(sha1, 1);
99
+ if (commit) {
100
+ commit_sha1 = commit->object.sha1;
101
+ archive_time = commit->date;
102
+ } else {
103
+ commit_sha1 = NULL;
104
+ archive_time = time(NULL);
105
+ }
106
+
107
+ tree = parse_tree_indirect(sha1);
108
+ if (tree == NULL)
109
+ die("not a tree object");
110
+
111
+ if (prefix) {
112
+ unsigned char tree_sha1[20];
113
+ unsigned int mode;
114
+ int err;
115
+
116
+ err = get_tree_entry(tree->object.sha1, prefix,
117
+ tree_sha1, &mode);
118
+ if (err || !S_ISDIR(mode))
119
+ die("current working directory is untracked");
120
+
121
+ free(tree);
122
+ tree = parse_tree_indirect(tree_sha1);
123
+ }
124
+ ar_args->tree = tree;
125
+ ar_args->commit_sha1 = commit_sha1;
126
+ ar_args->time = archive_time;
127
+}
128
+
129
+static const char *default_parse_extra(struct archiver *ar,
130
+ const char **argv)
131
+{
132
+ static char msg[64];
133
+
134
+ snprintf(msg, sizeof(msg) - 4, "'%s' format does not handle %s",
135
+ ar->name, *argv);
136
+
137
+ return strcat(msg, "...");
138
+}
139
+
140
+int parse_archive_args(int argc, const char **argv, struct archiver *ar)
141
+{
142
+ const char *extra_argv[MAX_EXTRA_ARGS];
143
+ int extra_argc = 0;
144
+ const char *format = NULL; /* might want to default to "tar" */
145
+ const char *remote = NULL;
146
+ const char *base = "";
147
+ int list = 0;
148
+ int i;
149
+
150
+ for (i = 1; i < argc; i++) {
151
+ const char *arg = argv[i];
152
+
153
+ if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
154
+ list = 1;
155
+ continue;
156
+ }
157
+ if (!strncmp(arg, "--format=", 9)) {
158
+ format = arg + 9;
159
+ continue;
160
+ }
161
+ if (!strncmp(arg, "--prefix=", 9)) {
162
+ base = arg + 9;
163
+ continue;
164
+ }
165
+ if (!strncmp(arg, "--remote=", 9)) {
166
+ remote = arg + 9;
167
+ continue;
168
+ }
169
+ if (!strcmp(arg, "--")) {
170
+ i++;
171
+ break;
172
+ }
173
+ if (arg[0] == '-') {
174
+ if (extra_argc > MAX_EXTRA_ARGS - 1)
175
+ die("Too many extra options");
176
+ extra_argv[extra_argc++] = arg;
177
+ continue;
178
+ }
179
+ break;
180
+ }
181
+
182
+ if (list) {
183
+ if (!remote) {
184
+ for (i = 0; i < ARRAY_SIZE(archivers); i++)
185
+ printf("%s\n", archivers[i].name);
186
+ exit(0);
187
+ }
188
+ die("--list and --remote are mutually exclusive");
189
+ }
190
+
191
+ if (argc - i < 1)
192
+ usage(archive_usage);
193
+ if (!format)
194
+ die("You must specify an archive format");
195
+ if (init_archiver(format, ar) < 0)
196
+ die("Unknown archive format '%s'", format);
197
+
198
+ if (extra_argc && !remote) {
199
+ if (!ar->parse_extra) {
200
+ die("%s", default_parse_extra(ar, extra_argv));
201
+ }
202
+ ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
203
+ }
204
+ ar->remote = remote;
205
+ ar->args.base = base;
206
+
207
+ return i;
208
+}
209
+
210
+int cmd_archive(int argc, const char **argv, const char *prefix)
211
+{
212
+ struct archiver ar;
213
+ int tree_idx;
214
+
215
+ tree_idx = parse_archive_args(argc, argv, &ar);
216
+
217
+ if (ar.remote)
218
+ return run_remote_archiver(&ar, argc, argv);
219
+
220
+ if (prefix == NULL)
221
+ prefix = setup_git_directory();
222
+
223
+ argv += tree_idx;
224
+ parse_treeish_arg(argv, &ar.args, prefix);
225
+ parse_pathspec_arg(argv + 1, &ar.args);
226
+
227
+ return ar.write_archive(&ar.args);
228
+}