Fix the "close before dup" bug in clone-pack too
[git/git-svn.git] / clone-pack.c
blob252fb80efd55a4cc4fa679411c4570093500f4ad
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
6 static int quiet;
7 static const char clone_pack_usage[] = "git-clone-pack [host:]directory [heads]*";
8 static const char *exec = "git-upload-pack";
10 struct ref {
11 struct ref *next;
12 unsigned char sha1[20];
13 char name[0];
16 static struct ref *get_remote_refs(int fd, int nr_match, char **match)
18 struct ref *ref_list = NULL, **next_ref = &ref_list;
20 for (;;) {
21 static char line[1000];
22 unsigned char sha1[20];
23 struct ref *ref;
24 char *refname;
25 int len;
27 len = packet_read_line(fd, line, sizeof(line));
28 if (!len)
29 break;
30 if (line[len-1] == '\n')
31 line[--len] = 0;
32 if (len < 42 || get_sha1_hex(line, sha1))
33 die("git-fetch-pack: protocol error - expected ref descriptor, got '%s¤'", line);
34 refname = line+41;
35 len = len-40;
36 if (nr_match && !path_match(refname, nr_match, match))
37 continue;
38 ref = xmalloc(sizeof(struct ref) + len);
39 ref->next = NULL;
40 memcpy(ref->sha1, sha1, 20);
41 memcpy(ref->name, refname, len);
42 *next_ref = ref;
43 next_ref = &ref->next;
45 return ref_list;
48 static void clone_handshake(int fd[2], struct ref *ref)
50 unsigned char sha1[20];
52 while (ref) {
53 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->sha1));
54 ref = ref->next;
56 packet_flush(fd[1]);
58 /* We don't have nuttin' */
59 packet_write(fd[1], "done\n");
60 if (get_ack(fd[0], sha1))
61 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
64 static int is_master(struct ref *ref)
66 return !strcmp(ref->name, "refs/heads/master");
69 static void write_one_ref(struct ref *ref)
71 char *path = git_path(ref->name);
72 int fd;
73 char *hex;
75 if (safe_create_leading_directories(path))
76 die("unable to create leading directory for %s", ref->name);
77 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
78 if (fd < 0)
79 die("unable to create ref %s", ref->name);
80 hex = sha1_to_hex(ref->sha1);
81 hex[40] = '\n';
82 if (write(fd, hex, 41) != 41)
83 die("unable to write ref %s", ref->name);
84 close(fd);
87 static void write_refs(struct ref *ref)
89 struct ref *head = NULL, *head_ptr, *master_ref;
90 char *head_path;
92 if (!strcmp(ref->name, "HEAD")) {
93 head = ref;
94 ref = ref->next;
96 head_ptr = NULL;
97 master_ref = NULL;
98 while (ref) {
99 if (is_master(ref))
100 master_ref = ref;
101 if (head && !memcmp(ref->sha1, head->sha1, 20)) {
102 if (!head_ptr || ref == master_ref)
103 head_ptr = ref;
105 write_one_ref(ref);
106 ref = ref->next;
108 if (!head)
109 return;
111 head_path = git_path("HEAD");
112 if (!head_ptr) {
114 * If we had a master ref, and it wasn't HEAD, we need to undo the
115 * symlink, and write a standalone HEAD. Give a warning, because that's
116 * really really wrong.
118 if (master_ref) {
119 error("HEAD doesn't point to any refs! Making standalone HEAD");
120 unlink(head_path);
122 write_one_ref(head);
123 return;
126 /* We reset to the master branch if it's available */
127 if (master_ref)
128 return;
131 * Uhhuh. Other end didn't have master. We start HEAD off with
132 * the first branch with the same value.
134 unlink(head_path);
135 if (symlink(head_ptr->name, head_path) < 0)
136 die("unable to link HEAD to %s", head_ptr->name);
139 static int clone_pack(int fd[2], int nr_match, char **match)
141 struct ref *refs;
142 int status;
143 pid_t pid;
145 refs = get_remote_refs(fd[0], nr_match, match);
146 if (!refs) {
147 packet_flush(fd[1]);
148 die("no matching remote head");
150 clone_handshake(fd, refs);
151 pid = fork();
152 if (pid < 0)
153 die("git-clone-pack: unable to fork off git-unpack-objects");
154 if (!pid) {
155 dup2(fd[0], 0);
156 close(fd[0]);
157 close(fd[1]);
158 execlp("git-unpack-objects", "git-unpack-objects",
159 quiet ? "-q" : NULL, NULL);
160 die("git-unpack-objects exec failed");
162 close(fd[0]);
163 close(fd[1]);
164 while (waitpid(pid, &status, 0) < 0) {
165 if (errno != EINTR)
166 die("waiting for git-unpack-objects: %s", strerror(errno));
168 if (WIFEXITED(status)) {
169 int code = WEXITSTATUS(status);
170 if (code)
171 die("git-unpack-objects died with error code %d", code);
172 write_refs(refs);
173 return 0;
175 if (WIFSIGNALED(status)) {
176 int sig = WTERMSIG(status);
177 die("git-unpack-objects died of signal %d", sig);
179 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
182 int main(int argc, char **argv)
184 int i, ret, nr_heads;
185 char *dest = NULL, **heads;
186 int fd[2];
187 pid_t pid;
189 nr_heads = 0;
190 heads = NULL;
191 for (i = 1; i < argc; i++) {
192 char *arg = argv[i];
194 if (*arg == '-') {
195 if (!strcmp("-q", arg)) {
196 quiet = 1;
197 continue;
199 usage(clone_pack_usage);
201 dest = arg;
202 heads = argv + i + 1;
203 nr_heads = argc - i - 1;
204 break;
206 if (!dest)
207 usage(clone_pack_usage);
208 pid = git_connect(fd, dest, exec);
209 if (pid < 0)
210 return 1;
211 ret = clone_pack(fd, nr_heads, heads);
212 close(fd[0]);
213 close(fd[1]);
214 finish_connect(pid);
215 return ret;