clone-pack: new option --keep tells it not to explode the pack.
With new option --keep, or a configuration item clone.keeppack (we need a better name, or start allowing dash,"clone.keep-pack"), the packed data downloaded while cloning is saved as a pack in .git/objects/pack/ locally, with index generated for it with git-index-pack. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 12, 2005 at 18:12 UTC
11dcec07a4815b7a37e009df1e8007d7ec08becf
1 file changed
+179
-9
clone-pack.c
+179
-9
@@ -4,6 +4,7 @@
4
#include <sys/wait.h>
5
6
static int quiet;
7
+static int keep_pack;
8
static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
9
static const char *exec = "git-upload-pack";
10
@@ -106,18 +107,11 @@ static void write_refs(struct ref *ref)
107
free(head_path);
108
}
109
109
-static int clone_pack(int fd[2], int nr_match, char **match)
110
+static int clone_by_unpack(int fd[2])
111
{
111
- struct ref *refs;
112
int status;
113
pid_t pid;
114
115
- get_remote_heads(fd[0], &refs, nr_match, match);
116
- if (!refs) {
117
- packet_flush(fd[1]);
118
- die("no matching remote head");
119
- }
120
- clone_handshake(fd, refs);
115
pid = fork();
116
if (pid < 0)
117
die("git-clone-pack: unable to fork off git-unpack-objects");
@@ -139,7 +133,6 @@ static int clone_pack(int fd[2], int nr_match, char **match)
133
int code = WEXITSTATUS(status);
134
if (code)
135
die("git-unpack-objects died with error code %d", code);
142
- write_refs(refs);
136
return 0;
137
}
138
if (WIFSIGNALED(status)) {
@@ -149,6 +142,178 @@ static int clone_pack(int fd[2], int nr_match, char **match)
142
die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
143
}
144
145
+static int finish_pack(const char *pack_tmp_name)
146
+{
147
+ int pipe_fd[2];
148
+ pid_t pid;
149
+ char idx[PATH_MAX];
150
+ char final[PATH_MAX];
151
+ char hash[41];
152
+ unsigned char sha1[20];
153
+ char *cp;
154
+ int err = 0;
155
+
156
+ if (pipe(pipe_fd) < 0)
157
+ die("git-clone-pack: unable to set up pipe");
158
+
159
+ strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
160
+ cp = strrchr(idx, '/');
161
+ memcpy(cp, "/pidx", 5);
162
+
163
+ pid = fork();
164
+ if (pid < 0)
165
+ die("git-clone-pack: unable to fork off git-index-pack");
166
+ if (!pid) {
167
+ close(0);
168
+ dup2(pipe_fd[1], 1);
169
+ close(pipe_fd[0]);
170
+ close(pipe_fd[1]);
171
+ execlp("git-index-pack","git-index-pack",
172
+ "-o", idx, pack_tmp_name, NULL);
173
+ error("cannot exec git-index-pack <%s> <%s>",
174
+ idx, pack_tmp_name);
175
+ exit(1);
176
+ }
177
+ close(pipe_fd[1]);
178
+ if (read(pipe_fd[0], hash, 40) != 40) {
179
+ error("git-clone-pack: unable to read from git-index-pack");
180
+ err = 1;
181
+ }
182
+ close(pipe_fd[0]);
183
+
184
+ for (;;) {
185
+ int status, code;
186
+ int retval = waitpid(pid, &status, 0);
187
+
188
+ if (retval < 0) {
189
+ if (errno == EINTR)
190
+ continue;
191
+ error("waitpid failed (%s)", strerror(retval));
192
+ goto error_die;
193
+ }
194
+ if (WIFSIGNALED(status)) {
195
+ int sig = WTERMSIG(status);
196
+ error("git-index-pack died of signal %d", sig);
197
+ goto error_die;
198
+ }
199
+ if (!WIFEXITED(status)) {
200
+ error("git-index-pack died of unnatural causes %d",
201
+ status);
202
+ goto error_die;
203
+ }
204
+ code = WEXITSTATUS(status);
205
+ if (code) {
206
+ error("git-index-pack died with error code %d", code);
207
+ goto error_die;
208
+ }
209
+ if (err)
210
+ goto error_die;
211
+ break;
212
+ }
213
+ hash[40] = 0;
214
+ if (get_sha1_hex(hash, sha1)) {
215
+ error("git-index-pack reported nonsense '%s'", hash);
216
+ goto error_die;
217
+ }
218
+ /* Now we have pack in pack_tmp_name[], and
219
+ * idx in idx[]; rename them to their final names.
220
+ */
221
+ snprintf(final, sizeof(final),
222
+ "%s/pack/pack-%s.pack", get_object_directory(), hash);
223
+ move_temp_to_file(pack_tmp_name, final);
224
+ snprintf(final, sizeof(final),
225
+ "%s/pack/pack-%s.idx", get_object_directory(), hash);
226
+ move_temp_to_file(idx, final);
227
+ return 0;
228
+
229
+ error_die:
230
+ unlink(idx);
231
+ unlink(pack_tmp_name);
232
+ exit(1);
233
+}
234
+
235
+static int clone_without_unpack(int fd[2])
236
+{
237
+ char tmpfile[PATH_MAX];
238
+ int ofd, ifd;
239
+
240
+ ifd = fd[0];
241
+ snprintf(tmpfile, sizeof(tmpfile),
242
+ "%s/pack-XXXXXX", get_object_directory());
243
+ ofd = mkstemp(tmpfile);
244
+ if (ofd < 0)
245
+ return error("unable to create temporary file %s", tmpfile);
246
+
247
+ while (1) {
248
+ char buf[8192];
249
+ ssize_t sz, wsz, pos;
250
+ sz = read(ifd, buf, sizeof(buf));
251
+ if (sz == 0)
252
+ break;
253
+ if (sz < 0) {
254
+ error("error reading pack (%s)", strerror(errno));
255
+ close(ofd);
256
+ unlink(tmpfile);
257
+ return -1;
258
+ }
259
+ pos = 0;
260
+ while (pos < sz) {
261
+ wsz = write(ofd, buf + pos, sz - pos);
262
+ if (wsz < 0) {
263
+ error("error writing pack (%s)",
264
+ strerror(errno));
265
+ close(ofd);
266
+ unlink(tmpfile);
267
+ return -1;
268
+ }
269
+ pos += wsz;
270
+ }
271
+ }
272
+ close(ofd);
273
+ return finish_pack(tmpfile);
274
+}
275
+
276
+static int clone_pack(int fd[2], int nr_match, char **match)
277
+{
278
+ struct ref *refs;
279
+ int status;
280
+
281
+ get_remote_heads(fd[0], &refs, nr_match, match);
282
+ if (!refs) {
283
+ packet_flush(fd[1]);
284
+ die("no matching remote head");
285
+ }
286
+ clone_handshake(fd, refs);
287
+
288
+ if (keep_pack)
289
+ status = clone_without_unpack(fd);
290
+ else
291
+ status = clone_by_unpack(fd);
292
+
293
+ if (!status)
294
+ write_refs(refs);
295
+ return status;
296
+}
297
+
298
+static int clone_options(const char *var, const char *value)
299
+{
300
+ if (!strcmp("clone.keeppack", var)) {
301
+ keep_pack = git_config_bool(var, value);
302
+ return 0;
303
+ }
304
+ if (!strcmp("clone.quiet", var)) {
305
+ quiet = git_config_bool(var, value);
306
+ return 0;
307
+ }
308
+ /*
309
+ * Put other local option parsing for this program
310
+ * here ...
311
+ */
312
+
313
+ /* Fall back on the default ones */
314
+ return git_default_config(var, value);
315
+}
316
+
317
int main(int argc, char **argv)
318
{
319
int i, ret, nr_heads;
@@ -156,6 +321,7 @@ int main(int argc, char **argv)
321
int fd[2];
322
pid_t pid;
323
324
+ git_config(clone_options);
325
nr_heads = 0;
326
heads = NULL;
327
for (i = 1; i < argc; i++) {
@@ -170,6 +336,10 @@ int main(int argc, char **argv)
336
exec = arg + 7;
337
continue;
338
}
339
+ if (!strcmp("--keep", arg)) {
340
+ keep_pack = 1;
341
+ continue;
342
+ }
343
usage(clone_pack_usage);
344
}
345
dest = arg;