8 static const char clone_pack_usage
[] =
9 "git-clone-pack [-q] [--keep] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
10 static const char *exec
= "git-upload-pack";
12 static void clone_handshake(int fd
[2], struct ref
*ref
)
14 unsigned char sha1
[20];
17 packet_write(fd
[1], "want %s\n", sha1_to_hex(ref
->old_sha1
));
22 /* We don't have nuttin' */
23 packet_write(fd
[1], "done\n");
24 if (get_ack(fd
[0], sha1
))
25 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1
));
28 static int is_master(struct ref
*ref
)
30 return !strcmp(ref
->name
, "refs/heads/master");
33 static void write_one_ref(struct ref
*ref
)
35 char *path
= git_path("%s", ref
->name
);
39 if (!strncmp(ref
->name
, "refs/", 5) &&
40 check_ref_format(ref
->name
+ 5)) {
41 error("refusing to create funny ref '%s' locally", ref
->name
);
45 if (safe_create_leading_directories(path
))
46 die("unable to create leading directory for %s", ref
->name
);
47 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
49 die("unable to create ref %s", ref
->name
);
50 hex
= sha1_to_hex(ref
->old_sha1
);
52 if (write(fd
, hex
, 41) != 41)
53 die("unable to write ref %s", ref
->name
);
57 static void write_refs(struct ref
*ref
)
59 struct ref
*head
= NULL
, *head_ptr
, *master_ref
;
62 /* Upload-pack must report HEAD first */
63 if (!strcmp(ref
->name
, "HEAD")) {
73 !memcmp(ref
->old_sha1
, head
->old_sha1
, 20) &&
74 !strncmp(ref
->name
, "refs/heads/",11) &&
75 (!head_ptr
|| ref
== master_ref
))
82 fprintf(stderr
, "No HEAD in remote.\n");
86 head_path
= strdup(git_path("HEAD"));
89 * If we had a master ref, and it wasn't HEAD, we need to undo the
90 * symlink, and write a standalone HEAD. Give a warning, because that's
91 * really really wrong.
94 error("HEAD doesn't point to any refs! Making standalone HEAD");
102 /* We reset to the master branch if it's available */
106 fprintf(stderr
, "Setting HEAD to %s\n", head_ptr
->name
);
109 * Uhhuh. Other end didn't have master. We start HEAD off with
110 * the first branch with the same value.
112 if (create_symref(head_path
, head_ptr
->name
) < 0)
113 die("unable to link HEAD to %s", head_ptr
->name
);
117 static int clone_by_unpack(int fd
[2])
124 die("git-clone-pack: unable to fork off git-unpack-objects");
129 execlp("git-unpack-objects", "git-unpack-objects",
130 quiet
? "-q" : NULL
, NULL
);
131 die("git-unpack-objects exec failed");
135 while (waitpid(pid
, &status
, 0) < 0) {
137 die("waiting for git-unpack-objects: %s", strerror(errno
));
139 if (WIFEXITED(status
)) {
140 int code
= WEXITSTATUS(status
);
142 die("git-unpack-objects died with error code %d", code
);
145 if (WIFSIGNALED(status
)) {
146 int sig
= WTERMSIG(status
);
147 die("git-unpack-objects died of signal %d", sig
);
149 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
152 static int finish_pack(const char *pack_tmp_name
)
157 char final
[PATH_MAX
];
159 unsigned char sha1
[20];
163 if (pipe(pipe_fd
) < 0)
164 die("git-clone-pack: unable to set up pipe");
166 strcpy(idx
, pack_tmp_name
); /* ".git/objects/pack-XXXXXX" */
167 cp
= strrchr(idx
, '/');
168 memcpy(cp
, "/pidx", 5);
172 die("git-clone-pack: unable to fork off git-index-pack");
178 execlp("git-index-pack","git-index-pack",
179 "-o", idx
, pack_tmp_name
, NULL
);
180 error("cannot exec git-index-pack <%s> <%s>",
185 if (read(pipe_fd
[0], hash
, 40) != 40) {
186 error("git-clone-pack: unable to read from git-index-pack");
193 int retval
= waitpid(pid
, &status
, 0);
198 error("waitpid failed (%s)", strerror(retval
));
201 if (WIFSIGNALED(status
)) {
202 int sig
= WTERMSIG(status
);
203 error("git-index-pack died of signal %d", sig
);
206 if (!WIFEXITED(status
)) {
207 error("git-index-pack died of unnatural causes %d",
211 code
= WEXITSTATUS(status
);
213 error("git-index-pack died with error code %d", code
);
221 if (get_sha1_hex(hash
, sha1
)) {
222 error("git-index-pack reported nonsense '%s'", hash
);
225 /* Now we have pack in pack_tmp_name[], and
226 * idx in idx[]; rename them to their final names.
228 snprintf(final
, sizeof(final
),
229 "%s/pack/pack-%s.pack", get_object_directory(), hash
);
230 move_temp_to_file(pack_tmp_name
, final
);
232 snprintf(final
, sizeof(final
),
233 "%s/pack/pack-%s.idx", get_object_directory(), hash
);
234 move_temp_to_file(idx
, final
);
240 unlink(pack_tmp_name
);
244 static int clone_without_unpack(int fd
[2])
246 char tmpfile
[PATH_MAX
];
250 snprintf(tmpfile
, sizeof(tmpfile
),
251 "%s/pack-XXXXXX", get_object_directory());
252 ofd
= mkstemp(tmpfile
);
254 return error("unable to create temporary file %s", tmpfile
);
258 ssize_t sz
, wsz
, pos
;
259 sz
= read(ifd
, buf
, sizeof(buf
));
263 error("error reading pack (%s)", strerror(errno
));
270 wsz
= write(ofd
, buf
+ pos
, sz
- pos
);
272 error("error writing pack (%s)",
282 return finish_pack(tmpfile
);
285 static int clone_pack(int fd
[2], int nr_match
, char **match
)
290 get_remote_heads(fd
[0], &refs
, nr_match
, match
, 1);
293 die("no matching remote head");
295 clone_handshake(fd
, refs
);
298 status
= clone_without_unpack(fd
);
300 status
= clone_by_unpack(fd
);
307 static int clone_options(const char *var
, const char *value
)
309 if (!strcmp("clone.keeppack", var
)) {
310 keep_pack
= git_config_bool(var
, value
);
313 if (!strcmp("clone.quiet", var
)) {
314 quiet
= git_config_bool(var
, value
);
318 * Put other local option parsing for this program
322 /* Fall back on the default ones */
323 return git_default_config(var
, value
);
326 int main(int argc
, char **argv
)
328 int i
, ret
, nr_heads
;
329 char *dest
= NULL
, **heads
;
333 git_config(clone_options
);
336 for (i
= 1; i
< argc
; i
++) {
340 if (!strcmp("-q", arg
)) {
344 if (!strncmp("--exec=", arg
, 7)) {
348 if (!strcmp("--keep", arg
)) {
352 usage(clone_pack_usage
);
355 heads
= argv
+ i
+ 1;
356 nr_heads
= argc
- i
- 1;
360 usage(clone_pack_usage
);
361 pid
= git_connect(fd
, dest
, exec
);
364 ret
= clone_pack(fd
, nr_heads
, heads
);