7 static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
8 static const char *exec = "git-upload-pack";
10 static void clone_handshake(int fd[2], struct ref *ref)
12 unsigned char sha1[20];
15 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
20 /* We don't have nuttin' */
21 packet_write(fd[1], "done\n");
22 if (get_ack(fd[0], sha1))
23 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
26 static int is_master(struct ref *ref)
28 return !strcmp(ref->name, "refs/heads/master");
31 static void write_one_ref(struct ref *ref)
33 char *path = git_path(ref->name);
37 if (safe_create_leading_directories(path))
38 die("unable to create leading directory for %s", ref->name);
39 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
41 die("unable to create ref %s", ref->name);
42 hex = sha1_to_hex(ref->old_sha1);
44 if (write(fd, hex, 41) != 41)
45 die("unable to write ref %s", ref->name);
49 static void write_refs(struct ref *ref)
51 struct ref *head = NULL, *head_ptr, *master_ref;
54 if (!strcmp(ref->name, "HEAD")) {
63 if (head && !memcmp(ref->old_sha1, head->old_sha1, 20)) {
64 if (!head_ptr || ref == master_ref)
73 head_path = git_path("HEAD");
76 * If we had a master ref, and it wasn't HEAD, we need to undo the
77 * symlink, and write a standalone HEAD. Give a warning, because that's
78 * really really wrong.
81 error("HEAD doesn't point to any refs! Making standalone HEAD");
88 /* We reset to the master branch if it's available */
93 * Uhhuh. Other end didn't have master. We start HEAD off with
94 * the first branch with the same value.
97 if (symlink(head_ptr->name, head_path) < 0)
98 die("unable to link HEAD to %s", head_ptr->name);
101 static int clone_pack(int fd[2], int nr_match, char **match)
107 get_remote_heads(fd[0], &refs, nr_match, match);
110 die("no matching remote head");
112 clone_handshake(fd, refs);
115 die("git-clone-pack: unable to fork off git-unpack-objects");
120 execlp("git-unpack-objects", "git-unpack-objects",
121 quiet ? "-q" : NULL, NULL);
122 die("git-unpack-objects exec failed");
126 while (waitpid(pid, &status, 0) < 0) {
128 die("waiting for git-unpack-objects: %s", strerror(errno));
130 if (WIFEXITED(status)) {
131 int code = WEXITSTATUS(status);
133 die("git-unpack-objects died with error code %d", code);
137 if (WIFSIGNALED(status)) {
138 int sig = WTERMSIG(status);
139 die("git-unpack-objects died of signal %d", sig);
141 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
144 int main(int argc, char **argv)
146 int i, ret, nr_heads;
147 char *dest = NULL, **heads;
153 for (i = 1; i < argc; i++) {
157 if (!strcmp("-q", arg)) {
161 if (!strncmp("--exec=", arg, 7)) {
165 usage(clone_pack_usage);
168 heads = argv + i + 1;
169 nr_heads = argc - i - 1;
173 usage(clone_pack_usage);
174 pid = git_connect(fd, dest, exec);
177 ret = clone_pack(fd, nr_heads, heads);