6 static const char clone_pack_usage
[] =
7 "git-clone-pack [--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("%s", ref
->name
);
37 if (!strncmp(ref
->name
, "refs/", 5) &&
38 check_ref_format(ref
->name
+ 5)) {
39 error("refusing to create funny ref '%s' locally", ref
->name
);
43 if (safe_create_leading_directories(path
))
44 die("unable to create leading directory for %s", ref
->name
);
45 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
47 die("unable to create ref %s", ref
->name
);
48 hex
= sha1_to_hex(ref
->old_sha1
);
50 if (write(fd
, hex
, 41) != 41)
51 die("unable to write ref %s", ref
->name
);
55 static void write_refs(struct ref
*ref
)
57 struct ref
*head
= NULL
, *head_ptr
, *master_ref
;
60 /* Upload-pack must report HEAD first */
61 if (!strcmp(ref
->name
, "HEAD")) {
71 !memcmp(ref
->old_sha1
, head
->old_sha1
, 20) &&
72 !strncmp(ref
->name
, "refs/heads/",11) &&
73 (!head_ptr
|| ref
== master_ref
))
80 fprintf(stderr
, "No HEAD in remote.\n");
84 head_path
= strdup(git_path("HEAD"));
87 * If we had a master ref, and it wasn't HEAD, we need to undo the
88 * symlink, and write a standalone HEAD. Give a warning, because that's
89 * really really wrong.
92 error("HEAD doesn't point to any refs! Making standalone HEAD");
100 /* We reset to the master branch if it's available */
104 fprintf(stderr
, "Setting HEAD to %s\n", head_ptr
->name
);
107 * Uhhuh. Other end didn't have master. We start HEAD off with
108 * the first branch with the same value.
110 if (create_symref(head_path
, head_ptr
->name
) < 0)
111 die("unable to link HEAD to %s", head_ptr
->name
);
115 static int finish_pack(const char *pack_tmp_name
)
120 char final
[PATH_MAX
];
122 unsigned char sha1
[20];
126 if (pipe(pipe_fd
) < 0)
127 die("git-clone-pack: unable to set up pipe");
129 strcpy(idx
, pack_tmp_name
); /* ".git/objects/pack-XXXXXX" */
130 cp
= strrchr(idx
, '/');
131 memcpy(cp
, "/pidx", 5);
135 die("git-clone-pack: unable to fork off git-index-pack");
141 execlp("git-index-pack","git-index-pack",
142 "-o", idx
, pack_tmp_name
, NULL
);
143 error("cannot exec git-index-pack <%s> <%s>",
148 if (read(pipe_fd
[0], hash
, 40) != 40) {
149 error("git-clone-pack: unable to read from git-index-pack");
156 int retval
= waitpid(pid
, &status
, 0);
161 error("waitpid failed (%s)", strerror(retval
));
164 if (WIFSIGNALED(status
)) {
165 int sig
= WTERMSIG(status
);
166 error("git-index-pack died of signal %d", sig
);
169 if (!WIFEXITED(status
)) {
170 error("git-index-pack died of unnatural causes %d",
174 code
= WEXITSTATUS(status
);
176 error("git-index-pack died with error code %d", code
);
184 if (get_sha1_hex(hash
, sha1
)) {
185 error("git-index-pack reported nonsense '%s'", hash
);
188 /* Now we have pack in pack_tmp_name[], and
189 * idx in idx[]; rename them to their final names.
191 snprintf(final
, sizeof(final
),
192 "%s/pack/pack-%s.pack", get_object_directory(), hash
);
193 move_temp_to_file(pack_tmp_name
, final
);
195 snprintf(final
, sizeof(final
),
196 "%s/pack/pack-%s.idx", get_object_directory(), hash
);
197 move_temp_to_file(idx
, final
);
203 unlink(pack_tmp_name
);
207 static int clone_without_unpack(int fd
[2])
209 char tmpfile
[PATH_MAX
];
213 snprintf(tmpfile
, sizeof(tmpfile
),
214 "%s/pack/tmp-XXXXXX", get_object_directory());
215 ofd
= mkstemp(tmpfile
);
217 return error("unable to create temporary file %s", tmpfile
);
221 ssize_t sz
, wsz
, pos
;
222 sz
= read(ifd
, buf
, sizeof(buf
));
226 error("error reading pack (%s)", strerror(errno
));
233 wsz
= write(ofd
, buf
+ pos
, sz
- pos
);
235 error("error writing pack (%s)",
245 return finish_pack(tmpfile
);
248 static int clone_pack(int fd
[2], int nr_match
, char **match
)
253 get_remote_heads(fd
[0], &refs
, nr_match
, match
, 1);
256 die("no matching remote head");
258 clone_handshake(fd
, refs
);
260 status
= clone_without_unpack(fd
);
267 int main(int argc
, char **argv
)
269 int i
, ret
, nr_heads
;
270 char *dest
= NULL
, **heads
;
274 setup_git_directory();
278 for (i
= 1; i
< argc
; i
++) {
282 if (!strcmp("-q", arg
))
284 if (!strncmp("--exec=", arg
, 7)) {
288 if (!strcmp("--keep", arg
))
290 usage(clone_pack_usage
);
293 heads
= argv
+ i
+ 1;
294 nr_heads
= argc
- i
- 1;
298 usage(clone_pack_usage
);
299 pid
= git_connect(fd
, dest
, exec
);
302 ret
= clone_pack(fd
, nr_heads
, heads
);