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";
12 unsigned char sha1[20];
16 static struct ref *get_remote_refs(int fd, int nr_match, char **match)
18 struct ref *ref_list = NULL, **next_ref = &ref_list;
21 static char line[1000];
22 unsigned char sha1[20];
27 len = packet_read_line(fd, line, sizeof(line));
30 if (line[len-1] == '\n')
32 if (len < 42 || get_sha1_hex(line, sha1))
33 die("git-clone-pack: protocol error - expected ref descriptor, got '%s'", line);
36 if (nr_match && !path_match(refname, nr_match, match))
38 ref = xmalloc(sizeof(struct ref) + len);
40 memcpy(ref->sha1, sha1, 20);
41 memcpy(ref->name, refname, len);
43 next_ref = &ref->next;
48 static void clone_handshake(int fd[2], struct ref *ref)
50 unsigned char sha1[20];
53 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->sha1));
58 /* We don't have nuttin' */
59 packet_write(fd[1], "done\n");
60 if (get_ack(fd[0], sha1))
61 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
64 static int is_master(struct ref *ref)
66 return !strcmp(ref->name, "refs/heads/master");
69 static void write_one_ref(struct ref *ref)
71 char *path = git_path(ref->name);
75 if (safe_create_leading_directories(path))
76 die("unable to create leading directory for %s", ref->name);
77 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
79 die("unable to create ref %s", ref->name);
80 hex = sha1_to_hex(ref->sha1);
82 if (write(fd, hex, 41) != 41)
83 die("unable to write ref %s", ref->name);
87 static void write_refs(struct ref *ref)
89 struct ref *head = NULL, *head_ptr, *master_ref;
92 if (!strcmp(ref->name, "HEAD")) {
101 if (head && !memcmp(ref->sha1, head->sha1, 20)) {
102 if (!head_ptr || ref == master_ref)
111 head_path = git_path("HEAD");
114 * If we had a master ref, and it wasn't HEAD, we need to undo the
115 * symlink, and write a standalone HEAD. Give a warning, because that's
116 * really really wrong.
119 error("HEAD doesn't point to any refs! Making standalone HEAD");
126 /* We reset to the master branch if it's available */
131 * Uhhuh. Other end didn't have master. We start HEAD off with
132 * the first branch with the same value.
135 if (symlink(head_ptr->name, head_path) < 0)
136 die("unable to link HEAD to %s", head_ptr->name);
139 static int clone_pack(int fd[2], int nr_match, char **match)
145 refs = get_remote_refs(fd[0], nr_match, match);
148 die("no matching remote head");
150 clone_handshake(fd, refs);
153 die("git-clone-pack: unable to fork off git-unpack-objects");
158 execlp("git-unpack-objects", "git-unpack-objects",
159 quiet ? "-q" : NULL, NULL);
160 die("git-unpack-objects exec failed");
164 while (waitpid(pid, &status, 0) < 0) {
166 die("waiting for git-unpack-objects: %s", strerror(errno));
168 if (WIFEXITED(status)) {
169 int code = WEXITSTATUS(status);
171 die("git-unpack-objects died with error code %d", code);
175 if (WIFSIGNALED(status)) {
176 int sig = WTERMSIG(status);
177 die("git-unpack-objects died of signal %d", sig);
179 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
182 int main(int argc, char **argv)
184 int i, ret, nr_heads;
185 char *dest = NULL, **heads;
191 for (i = 1; i < argc; i++) {
195 if (!strcmp("-q", arg)) {
199 if (!strncmp("--exec=", arg, 7)) {
203 usage(clone_pack_usage);
206 heads = argv + i + 1;
207 nr_heads = argc - i - 1;
211 usage(clone_pack_usage);
212 pid = git_connect(fd, dest, exec);
215 ret = clone_pack(fd, nr_heads, heads);