8 static const char clone_pack_usage[] =
9 "git-clone-pack [-q] [--keep] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
10 static const char *exec = "git-upload-pack";
12 static void clone_handshake(int fd[2], struct ref *ref)
14 unsigned char sha1[20];
17 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
22 /* We don't have nuttin' */
23 packet_write(fd[1], "done\n");
24 if (get_ack(fd[0], sha1))
25 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
28 static int is_master(struct ref *ref)
30 return !strcmp(ref->name, "refs/heads/master");
33 static void write_one_ref(struct ref *ref)
35 char *path = git_path("%s", ref->name);
39 if (safe_create_leading_directories(path))
40 die("unable to create leading directory for %s", ref->name);
41 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
43 die("unable to create ref %s", ref->name);
44 hex = sha1_to_hex(ref->old_sha1);
46 if (write(fd, hex, 41) != 41)
47 die("unable to write ref %s", ref->name);
51 static void write_refs(struct ref *ref)
53 struct ref *head = NULL, *head_ptr, *master_ref;
56 /* Upload-pack must report HEAD first */
57 if (!strcmp(ref->name, "HEAD")) {
67 !memcmp(ref->old_sha1, head->old_sha1, 20) &&
68 !strncmp(ref->name, "refs/heads/",11) &&
69 (!head_ptr || ref == master_ref))
76 fprintf(stderr, "No HEAD in remote.\n");
80 head_path = strdup(git_path("HEAD"));
83 * If we had a master ref, and it wasn't HEAD, we need to undo the
84 * symlink, and write a standalone HEAD. Give a warning, because that's
85 * really really wrong.
88 error("HEAD doesn't point to any refs! Making standalone HEAD");
96 /* We reset to the master branch if it's available */
100 fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
103 * Uhhuh. Other end didn't have master. We start HEAD off with
104 * the first branch with the same value.
106 if (create_symref(head_path, head_ptr->name) < 0)
107 die("unable to link HEAD to %s", head_ptr->name);
111 static int clone_by_unpack(int fd[2])
118 die("git-clone-pack: unable to fork off git-unpack-objects");
123 execlp("git-unpack-objects", "git-unpack-objects",
124 quiet ? "-q" : NULL, NULL);
125 die("git-unpack-objects exec failed");
129 while (waitpid(pid, &status, 0) < 0) {
131 die("waiting for git-unpack-objects: %s", strerror(errno));
133 if (WIFEXITED(status)) {
134 int code = WEXITSTATUS(status);
136 die("git-unpack-objects died with error code %d", code);
139 if (WIFSIGNALED(status)) {
140 int sig = WTERMSIG(status);
141 die("git-unpack-objects died of signal %d", sig);
143 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
146 static int finish_pack(const char *pack_tmp_name)
151 char final[PATH_MAX];
153 unsigned char sha1[20];
157 if (pipe(pipe_fd) < 0)
158 die("git-clone-pack: unable to set up pipe");
160 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
161 cp = strrchr(idx, '/');
162 memcpy(cp, "/pidx", 5);
166 die("git-clone-pack: unable to fork off git-index-pack");
172 execlp("git-index-pack","git-index-pack",
173 "-o", idx, pack_tmp_name, NULL);
174 error("cannot exec git-index-pack <%s> <%s>",
179 if (read(pipe_fd[0], hash, 40) != 40) {
180 error("git-clone-pack: unable to read from git-index-pack");
187 int retval = waitpid(pid, &status, 0);
192 error("waitpid failed (%s)", strerror(retval));
195 if (WIFSIGNALED(status)) {
196 int sig = WTERMSIG(status);
197 error("git-index-pack died of signal %d", sig);
200 if (!WIFEXITED(status)) {
201 error("git-index-pack died of unnatural causes %d",
205 code = WEXITSTATUS(status);
207 error("git-index-pack died with error code %d", code);
215 if (get_sha1_hex(hash, sha1)) {
216 error("git-index-pack reported nonsense '%s'", hash);
219 /* Now we have pack in pack_tmp_name[], and
220 * idx in idx[]; rename them to their final names.
222 snprintf(final, sizeof(final),
223 "%s/pack/pack-%s.pack", get_object_directory(), hash);
224 move_temp_to_file(pack_tmp_name, final);
226 snprintf(final, sizeof(final),
227 "%s/pack/pack-%s.idx", get_object_directory(), hash);
228 move_temp_to_file(idx, final);
234 unlink(pack_tmp_name);
238 static int clone_without_unpack(int fd[2])
240 char tmpfile[PATH_MAX];
244 snprintf(tmpfile, sizeof(tmpfile),
245 "%s/pack-XXXXXX", get_object_directory());
246 ofd = mkstemp(tmpfile);
248 return error("unable to create temporary file %s", tmpfile);
252 ssize_t sz, wsz, pos;
253 sz = read(ifd, buf, sizeof(buf));
257 error("error reading pack (%s)", strerror(errno));
264 wsz = write(ofd, buf + pos, sz - pos);
266 error("error writing pack (%s)",
276 return finish_pack(tmpfile);
279 static int clone_pack(int fd[2], int nr_match, char **match)
284 get_remote_heads(fd[0], &refs, nr_match, match);
287 die("no matching remote head");
289 clone_handshake(fd, refs);
292 status = clone_without_unpack(fd);
294 status = clone_by_unpack(fd);
301 static int clone_options(const char *var, const char *value)
303 if (!strcmp("clone.keeppack", var)) {
304 keep_pack = git_config_bool(var, value);
307 if (!strcmp("clone.quiet", var)) {
308 quiet = git_config_bool(var, value);
312 * Put other local option parsing for this program
316 /* Fall back on the default ones */
317 return git_default_config(var, value);
320 int main(int argc, char **argv)
322 int i, ret, nr_heads;
323 char *dest = NULL, **heads;
327 git_config(clone_options);
330 for (i = 1; i < argc; i++) {
334 if (!strcmp("-q", arg)) {
338 if (!strncmp("--exec=", arg, 7)) {
342 if (!strcmp("--keep", arg)) {
346 usage(clone_pack_usage);
349 heads = argv + i + 1;
350 nr_heads = argc - i - 1;
354 usage(clone_pack_usage);
355 pid = git_connect(fd, dest, exec);
358 ret = clone_pack(fd, nr_heads, heads);