6 static const char clone_pack_usage
[] = "git-clone-pack [host:]directory [heads]*";
7 static const char *exec
= "git-upload-pack";
11 unsigned char sha1
[20];
15 static struct ref
*get_remote_refs(int fd
, int nr_match
, char **match
)
17 struct ref
*ref_list
= NULL
, **next_ref
= &ref_list
;
20 static char line
[1000];
21 unsigned char sha1
[20];
26 len
= packet_read_line(fd
, line
, sizeof(line
));
29 if (line
[len
-1] == '\n')
31 if (len
< 42 || get_sha1_hex(line
, sha1
))
32 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s¤'", line
);
35 if (nr_match
&& !path_match(refname
, nr_match
, match
))
37 ref
= xmalloc(sizeof(struct ref
) + len
);
39 memcpy(ref
->sha1
, sha1
, 20);
40 memcpy(ref
->name
, refname
, len
);
42 next_ref
= &ref
->next
;
47 static void clone_handshake(int fd
[2], struct ref
*ref
)
49 unsigned char sha1
[20];
52 packet_write(fd
[1], "want %s\n", sha1_to_hex(ref
->sha1
));
57 /* We don't have nuttin' */
58 packet_write(fd
[1], "done\n");
59 if (get_ack(fd
[0], sha1
))
60 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1
));
63 static int is_master(struct ref
*ref
)
65 return !strcmp(ref
->name
, "refs/heads/master");
68 static void write_one_ref(struct ref
*ref
)
70 char *path
= git_path(ref
->name
);
74 if (safe_create_leading_directories(path
))
75 die("unable to create leading directory for %s", ref
->name
);
76 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
78 die("unable to create ref %s", ref
->name
);
79 hex
= sha1_to_hex(ref
->sha1
);
81 if (write(fd
, hex
, 41) != 41)
82 die("unable to write ref %s", ref
->name
);
86 static void write_refs(struct ref
*ref
)
88 struct ref
*head
= NULL
, *head_ptr
, *master_ref
;
91 if (!strcmp(ref
->name
, "HEAD")) {
100 if (head
&& !memcmp(ref
->sha1
, head
->sha1
, 20)) {
101 if (!head_ptr
|| ref
== master_ref
)
110 head_path
= git_path("HEAD");
113 * If we had a master ref, and it wasn't HEAD, we need to undo the
114 * symlink, and write a standalone HEAD. Give a warning, because that's
115 * really really wrong.
118 error("HEAD doesn't point to any refs! Making standalone HEAD");
125 /* We reset to the master branch if it's available */
130 * Uhhuh. Other end didn't have master. We start HEAD off with
131 * the first branch with the same value.
134 if (symlink(head_ptr
->name
, head_path
) < 0)
135 die("unable to link HEAD to %s", head_ptr
->name
);
138 static int clone_pack(int fd
[2], int nr_match
, char **match
)
144 refs
= get_remote_refs(fd
[0], nr_match
, match
);
147 die("no matching remote head");
149 clone_handshake(fd
, refs
);
152 die("git-clone-pack: unable to fork off git-unpack-objects");
157 execlp("git-unpack-objects", "git-unpack-objects", NULL
);
158 die("git-unpack-objects exec failed");
162 while (waitpid(pid
, &status
, 0) < 0) {
164 die("waiting for git-unpack-objects: %s", strerror(errno
));
166 if (WIFEXITED(status
)) {
167 int code
= WEXITSTATUS(status
);
169 die("git-unpack-objects died with error code %d", code
);
173 if (WIFSIGNALED(status
)) {
174 int sig
= WTERMSIG(status
);
175 die("git-unpack-objects died of signal %d", sig
);
177 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
180 int main(int argc
, char **argv
)
182 int i
, ret
, nr_heads
;
183 char *dest
= NULL
, **heads
;
189 for (i
= 1; i
< argc
; i
++) {
193 /* Arguments go here */
194 usage(clone_pack_usage
);
197 heads
= argv
+ i
+ 1;
198 nr_heads
= argc
- i
- 1;
202 usage(clone_pack_usage
);
203 pid
= git_connect(fd
, dest
, exec
);
206 ret
= clone_pack(fd
, nr_heads
, heads
);