8 static const char clone_pack_usage
[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
9 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 (safe_create_leading_directories(path
))
39 die("unable to create leading directory for %s", ref
->name
);
40 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
42 die("unable to create ref %s", ref
->name
);
43 hex
= sha1_to_hex(ref
->old_sha1
);
45 if (write(fd
, hex
, 41) != 41)
46 die("unable to write ref %s", ref
->name
);
50 static void write_refs(struct ref
*ref
)
52 struct ref
*head
= NULL
, *head_ptr
, *master_ref
;
55 /* Upload-pack must report HEAD first */
56 if (!strcmp(ref
->name
, "HEAD")) {
66 !memcmp(ref
->old_sha1
, head
->old_sha1
, 20) &&
67 !strncmp(ref
->name
, "refs/heads/",11) &&
68 (!head_ptr
|| ref
== master_ref
))
75 fprintf(stderr
, "No HEAD in remote.\n");
79 head_path
= strdup(git_path("HEAD"));
82 * If we had a master ref, and it wasn't HEAD, we need to undo the
83 * symlink, and write a standalone HEAD. Give a warning, because that's
84 * really really wrong.
87 error("HEAD doesn't point to any refs! Making standalone HEAD");
95 /* We reset to the master branch if it's available */
99 fprintf(stderr
, "Setting HEAD to %s\n", head_ptr
->name
);
102 * Uhhuh. Other end didn't have master. We start HEAD off with
103 * the first branch with the same value.
105 if (create_symref(head_path
, head_ptr
->name
) < 0)
106 die("unable to link HEAD to %s", head_ptr
->name
);
110 static int clone_by_unpack(int fd
[2])
117 die("git-clone-pack: unable to fork off git-unpack-objects");
122 execlp("git-unpack-objects", "git-unpack-objects",
123 quiet
? "-q" : NULL
, NULL
);
124 die("git-unpack-objects exec failed");
128 while (waitpid(pid
, &status
, 0) < 0) {
130 die("waiting for git-unpack-objects: %s", strerror(errno
));
132 if (WIFEXITED(status
)) {
133 int code
= WEXITSTATUS(status
);
135 die("git-unpack-objects died with error code %d", code
);
138 if (WIFSIGNALED(status
)) {
139 int sig
= WTERMSIG(status
);
140 die("git-unpack-objects died of signal %d", sig
);
142 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
145 static int finish_pack(const char *pack_tmp_name
)
150 char final
[PATH_MAX
];
152 unsigned char sha1
[20];
156 if (pipe(pipe_fd
) < 0)
157 die("git-clone-pack: unable to set up pipe");
159 strcpy(idx
, pack_tmp_name
); /* ".git/objects/pack-XXXXXX" */
160 cp
= strrchr(idx
, '/');
161 memcpy(cp
, "/pidx", 5);
165 die("git-clone-pack: unable to fork off git-index-pack");
171 execlp("git-index-pack","git-index-pack",
172 "-o", idx
, pack_tmp_name
, NULL
);
173 error("cannot exec git-index-pack <%s> <%s>",
178 if (read(pipe_fd
[0], hash
, 40) != 40) {
179 error("git-clone-pack: unable to read from git-index-pack");
186 int retval
= waitpid(pid
, &status
, 0);
191 error("waitpid failed (%s)", strerror(retval
));
194 if (WIFSIGNALED(status
)) {
195 int sig
= WTERMSIG(status
);
196 error("git-index-pack died of signal %d", sig
);
199 if (!WIFEXITED(status
)) {
200 error("git-index-pack died of unnatural causes %d",
204 code
= WEXITSTATUS(status
);
206 error("git-index-pack died with error code %d", code
);
214 if (get_sha1_hex(hash
, sha1
)) {
215 error("git-index-pack reported nonsense '%s'", hash
);
218 /* Now we have pack in pack_tmp_name[], and
219 * idx in idx[]; rename them to their final names.
221 snprintf(final
, sizeof(final
),
222 "%s/pack/pack-%s.pack", get_object_directory(), hash
);
223 move_temp_to_file(pack_tmp_name
, final
);
224 snprintf(final
, sizeof(final
),
225 "%s/pack/pack-%s.idx", get_object_directory(), hash
);
226 move_temp_to_file(idx
, final
);
231 unlink(pack_tmp_name
);
235 static int clone_without_unpack(int fd
[2])
237 char tmpfile
[PATH_MAX
];
241 snprintf(tmpfile
, sizeof(tmpfile
),
242 "%s/pack-XXXXXX", get_object_directory());
243 ofd
= mkstemp(tmpfile
);
245 return error("unable to create temporary file %s", tmpfile
);
249 ssize_t sz
, wsz
, pos
;
250 sz
= read(ifd
, buf
, sizeof(buf
));
254 error("error reading pack (%s)", strerror(errno
));
261 wsz
= write(ofd
, buf
+ pos
, sz
- pos
);
263 error("error writing pack (%s)",
273 return finish_pack(tmpfile
);
276 static int clone_pack(int fd
[2], int nr_match
, char **match
)
281 get_remote_heads(fd
[0], &refs
, nr_match
, match
);
284 die("no matching remote head");
286 clone_handshake(fd
, refs
);
289 status
= clone_without_unpack(fd
);
291 status
= clone_by_unpack(fd
);
298 static int clone_options(const char *var
, const char *value
)
300 if (!strcmp("clone.keeppack", var
)) {
301 keep_pack
= git_config_bool(var
, value
);
304 if (!strcmp("clone.quiet", var
)) {
305 quiet
= git_config_bool(var
, value
);
309 * Put other local option parsing for this program
313 /* Fall back on the default ones */
314 return git_default_config(var
, value
);
317 int main(int argc
, char **argv
)
319 int i
, ret
, nr_heads
;
320 char *dest
= NULL
, **heads
;
324 git_config(clone_options
);
327 for (i
= 1; i
< argc
; i
++) {
331 if (!strcmp("-q", arg
)) {
335 if (!strncmp("--exec=", arg
, 7)) {
339 if (!strcmp("--keep", arg
)) {
343 usage(clone_pack_usage
);
346 heads
= argv
+ i
+ 1;
347 nr_heads
= argc
- i
- 1;
351 usage(clone_pack_usage
);
352 pid
= git_connect(fd
, dest
, exec
);
355 ret
= clone_pack(fd
, nr_heads
, heads
);