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";
9 static void clone_handshake(int fd[2], struct ref *ref)
11 unsigned char sha1[20];
14 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
19 /* We don't have nuttin' */
20 packet_write(fd[1], "done\n");
21 if (get_ack(fd[0], sha1))
22 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
25 static int is_master(struct ref *ref)
27 return !strcmp(ref->name, "refs/heads/master");
30 static void write_one_ref(struct ref *ref)
32 char *path = git_path("%s", ref->name);
36 if (!strncmp(ref->name, "refs/", 5) &&
37 check_ref_format(ref->name + 5)) {
38 error("refusing to create funny ref '%s' locally", ref->name);
42 if (safe_create_leading_directories(path))
43 die("unable to create leading directory for %s", ref->name);
44 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
46 die("unable to create ref %s", ref->name);
47 hex = sha1_to_hex(ref->old_sha1);
49 if (write(fd, hex, 41) != 41)
50 die("unable to write ref %s", ref->name);
54 static void write_refs(struct ref *ref)
56 struct ref *head = NULL, *head_ptr, *master_ref;
59 /* Upload-pack must report HEAD first */
60 if (!strcmp(ref->name, "HEAD")) {
70 !memcmp(ref->old_sha1, head->old_sha1, 20) &&
71 !strncmp(ref->name, "refs/heads/",11) &&
72 (!head_ptr || ref == master_ref))
79 fprintf(stderr, "No HEAD in remote.\n");
83 head_path = strdup(git_path("HEAD"));
86 * If we had a master ref, and it wasn't HEAD, we need to undo the
87 * symlink, and write a standalone HEAD. Give a warning, because that's
88 * really really wrong.
91 error("HEAD doesn't point to any refs! Making standalone HEAD");
99 /* We reset to the master branch if it's available */
103 fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
106 * Uhhuh. Other end didn't have master. We start HEAD off with
107 * the first branch with the same value.
109 if (create_symref(head_path, head_ptr->name) < 0)
110 die("unable to link HEAD to %s", head_ptr->name);
114 static int clone_pack(int fd[2], int nr_match, char **match)
119 get_remote_heads(fd[0], &refs, nr_match, match, 1);
122 die("no matching remote head");
124 clone_handshake(fd, refs);
126 status = receive_keep_pack(fd, "git-clone-pack");
134 sha1_to_hex(refs->old_sha1),
142 int main(int argc, char **argv)
144 int i, ret, nr_heads;
145 char *dest = NULL, **heads;
149 setup_git_directory();
153 for (i = 1; i < argc; i++) {
157 if (!strcmp("-q", arg))
159 if (!strncmp("--exec=", arg, 7)) {
163 usage(clone_pack_usage);
166 heads = argv + i + 1;
167 nr_heads = argc - i - 1;
171 usage(clone_pack_usage);
172 pid = git_connect(fd, dest, exec);
175 ret = clone_pack(fd, nr_heads, heads);