5 static const char clone_pack_usage[] =
6 "git-clone-pack [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
7 static const char *exec = "git-upload-pack";
11 static void clone_handshake(int fd[2], struct ref *ref)
13 unsigned char sha1[20];
16 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
21 /* We don't have nuttin' */
22 packet_write(fd[1], "done\n");
23 if (get_ack(fd[0], sha1))
24 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
27 static int is_master(struct ref *ref)
29 return !strcmp(ref->name, "refs/heads/master");
32 static void write_one_ref(struct ref *ref)
34 char *path = git_path("%s", ref->name);
38 if (!strncmp(ref->name, "refs/", 5) &&
39 check_ref_format(ref->name + 5)) {
40 error("refusing to create funny ref '%s' locally", ref->name);
44 if (safe_create_leading_directories(path))
45 die("unable to create leading directory for %s", ref->name);
46 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
48 die("unable to create ref %s", ref->name);
49 hex = sha1_to_hex(ref->old_sha1);
51 if (write(fd, hex, 41) != 41)
52 die("unable to write ref %s", ref->name);
56 static void write_refs(struct ref *ref)
58 struct ref *head = NULL, *head_ptr, *master_ref;
61 /* Upload-pack must report HEAD first */
62 if (!strcmp(ref->name, "HEAD")) {
72 !memcmp(ref->old_sha1, head->old_sha1, 20) &&
73 !strncmp(ref->name, "refs/heads/",11) &&
74 (!head_ptr || ref == master_ref))
81 fprintf(stderr, "No HEAD in remote.\n");
85 head_path = strdup(git_path("HEAD"));
88 * If we had a master ref, and it wasn't HEAD, we need to undo the
89 * symlink, and write a standalone HEAD. Give a warning, because that's
90 * really really wrong.
93 error("HEAD doesn't point to any refs! Making standalone HEAD");
101 /* We reset to the master branch if it's available */
105 fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
108 * Uhhuh. Other end didn't have master. We start HEAD off with
109 * the first branch with the same value.
111 if (create_symref(head_path, head_ptr->name) < 0)
112 die("unable to link HEAD to %s", head_ptr->name);
116 static int clone_pack(int fd[2], int nr_match, char **match)
121 get_remote_heads(fd[0], &refs, nr_match, match, 1);
124 die("no matching remote head");
126 clone_handshake(fd, refs);
128 status = receive_keep_pack(fd, "git-clone-pack", quiet);
130 fprintf(stderr, "\n");
138 sha1_to_hex(refs->old_sha1),
146 int main(int argc, char **argv)
148 int i, ret, nr_heads;
149 char *dest = NULL, **heads;
153 setup_git_directory();
157 for (i = 1; i < argc; i++) {
161 if (!strcmp("-q", arg)) {
165 if (!strncmp("--exec=", arg, 7)) {
169 usage(clone_pack_usage);
172 heads = argv + i + 1;
173 nr_heads = argc - i - 1;
177 usage(clone_pack_usage);
178 pid = git_connect(fd, dest, exec);
181 ret = clone_pack(fd, nr_heads, heads);