Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 442 lines (401 sloc) 9.871 kb
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
f6b42a81 »
2005-10-13 Show peeled onion from upload-pack and server-info.
4 #include "tag.h"
5 #include "object.h"
f0243f26 »
2005-10-28 git-upload-pack: More efficient usage of the has_sha1 array
6 #include "commit.h"
77cb17e9 »
2006-01-10 Exec git programs without using PATH.
7 #include "exec_cmd.h"
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
8 #include <signal.h>
9 #include <sys/poll.h>
10 #include <sys/wait.h>
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
11
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
12 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
13
f0243f26 »
2005-10-28 git-upload-pack: More efficient usage of the has_sha1 array
14 #define THEY_HAVE (1U << 0)
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
15 #define OUR_REF (1U << 1)
16 #define WANTED (1U << 2)
6b32884a »
2005-10-22 upload-pack: Increase MAX_HAS.
17 #define MAX_HAS 256
18 #define MAX_NEEDS 256
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
19 static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
20 static int use_thin_pack = 0;
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
21 static unsigned char has_sha1[MAX_HAS][20];
22 static unsigned char needs_sha1[MAX_NEEDS][20];
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
23 static unsigned int timeout = 0;
24
25 static void reset_timeout(void)
26 {
27 alarm(timeout);
28 }
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
29
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
30 static int strip(char *line, int len)
31 {
32 if (len && line[len-1] == '\n')
33 line[--len] = 0;
34 return len;
35 }
36
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
37 static void create_pack_file(void)
38 {
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
39 /* Pipes between rev-list to pack-objects and pack-objects to us. */
40 int lp_pipe[2], pu_pipe[2];
41 pid_t pid_rev_list, pid_pack_objects;
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
42 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
43 char data[8193];
44 int buffered = -1;
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
45
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
46 if (pipe(lp_pipe) < 0)
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
47 die("git-upload-pack: unable to create pipe");
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
48 pid_rev_list = fork();
49 if (pid_rev_list < 0)
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
50 die("git-upload-pack: unable to fork git-rev-list");
51
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
52 if (!pid_rev_list) {
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
53 int i;
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
54 int args;
9201c707 »
2006-03-05 Const tightening.
55 const char **argv;
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
56 char *buf;
57 char **p;
58
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
59 if (create_full_pack) {
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
60 args = 10;
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
61 use_thin_pack = 0; /* no point doing it */
62 }
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
63 else
64 args = nr_has + nr_needs + 5;
9201c707 »
2006-03-05 Const tightening.
65 p = xmalloc(args * sizeof(char *));
66 argv = (const char **) p;
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
67 buf = xmalloc(args * 45);
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
68
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
69 dup2(lp_pipe[1], 1);
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
70 close(0);
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
71 close(lp_pipe[0]);
72 close(lp_pipe[1]);
77cb17e9 »
2006-01-10 Exec git programs without using PATH.
73 *p++ = "rev-list";
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
74 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
b5c367f7 »
2005-10-26 Fix cloning (memory corruption)
75 if (create_full_pack || MAX_NEEDS <= nr_needs)
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
76 *p++ = "--all";
77 else {
78 for (i = 0; i < nr_needs; i++) {
79 *p++ = buf;
80 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
81 buf += 41;
82 }
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
83 }
b5c367f7 »
2005-10-26 Fix cloning (memory corruption)
84 if (!create_full_pack)
85 for (i = 0; i < nr_has; i++) {
86 *p++ = buf;
87 *buf++ = '^';
88 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
89 buf += 41;
90 }
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
91 *p++ = NULL;
77cb17e9 »
2006-01-10 Exec git programs without using PATH.
92 execv_git_cmd(argv);
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
93 die("git-upload-pack: unable to exec git-rev-list");
94 }
b1c71b72 »
2006-06-20 upload-pack: avoid sending an incomplete pack upon failure
95
96 if (pipe(pu_pipe) < 0)
97 die("git-upload-pack: unable to create pipe");
98 pid_pack_objects = fork();
99 if (pid_pack_objects < 0) {
100 /* daemon sets things up to ignore TERM */
101 kill(pid_rev_list, SIGKILL);
102 die("git-upload-pack: unable to fork git-pack-objects");
103 }
104 if (!pid_pack_objects) {
105 dup2(lp_pipe[0], 0);
106 dup2(pu_pipe[1], 1);
107
108 close(lp_pipe[0]);
109 close(lp_pipe[1]);
110 close(pu_pipe[0]);
111 close(pu_pipe[1]);
112 execl_git_cmd("pack-objects", "--stdout", NULL);
113 kill(pid_rev_list, SIGKILL);
114 die("git-upload-pack: unable to exec git-pack-objects");
115 }
116
117 close(lp_pipe[0]);
118 close(lp_pipe[1]);
119
120 /* We read from pu_pipe[0] to capture the pack data.
121 */
122 close(pu_pipe[1]);
123
124 while (1) {
125 const char *who;
126 struct pollfd pfd[2];
127 pid_t pid;
128 int status;
129 ssize_t sz;
130 int pu, pollsize;
131
132 pollsize = 0;
133 pu = -1;
134
135 if (0 <= pu_pipe[0]) {
136 pfd[pollsize].fd = pu_pipe[0];
137 pfd[pollsize].events = POLLIN;
138 pu = pollsize;
139 pollsize++;
140 }
141
142 if (pollsize) {
143 if (poll(pfd, pollsize, -1) < 0) {
144 if (errno != EINTR) {
145 error("poll failed, resuming: %s",
146 strerror(errno));
147 sleep(1);
148 }
149 continue;
150 }
151 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
152 /* Data ready; we keep the last byte
153 * to ourselves in case we detect
154 * broken rev-list, so that we can
155 * leave the stream corrupted. This
156 * is unfortunate -- unpack-objects
157 * would happily accept a valid pack
158 * data with trailing garbage, so
159 * appending garbage after we pass all
160 * the pack data is not good enough to
161 * signal breakage to downstream.
162 */
163 char *cp = data;
164 ssize_t outsz = 0;
165 if (0 <= buffered) {
166 *cp++ = buffered;
167 outsz++;
168 }
169 sz = read(pu_pipe[0], cp,
170 sizeof(data) - outsz);
171 if (0 < sz)
172 ;
173 else if (sz == 0) {
174 close(pu_pipe[0]);
175 pu_pipe[0] = -1;
176 }
177 else
178 goto fail;
179 sz += outsz;
180 if (1 < sz) {
181 buffered = data[sz-1] & 0xFF;
182 sz--;
183 }
184 else
185 buffered = -1;
186 sz = xwrite(1, data, sz);
187 if (sz < 0)
188 goto fail;
189 }
190 }
191
192 /* See if the children are still there */
193 if (pid_rev_list || pid_pack_objects) {
194 pid = waitpid(-1, &status, WNOHANG);
195 if (!pid)
196 continue;
197 who = ((pid == pid_rev_list) ? "git-rev-list" :
198 (pid == pid_pack_objects) ? "git-pack-objects" :
199 NULL);
200 if (!who) {
201 if (pid < 0) {
202 error("git-upload-pack: %s",
203 strerror(errno));
204 goto fail;
205 }
206 error("git-upload-pack: we weren't "
207 "waiting for %d", pid);
208 continue;
209 }
210 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
211 error("git-upload-pack: %s died with error.",
212 who);
213 goto fail;
214 }
215 if (pid == pid_rev_list)
216 pid_rev_list = 0;
217 if (pid == pid_pack_objects)
218 pid_pack_objects = 0;
219 if (pid_rev_list || pid_pack_objects)
220 continue;
221 }
222
223 /* both died happily */
224 if (pollsize)
225 continue;
226
227 /* flush the data */
228 if (0 <= buffered) {
229 data[0] = buffered;
230 sz = xwrite(1, data, 1);
231 if (sz < 0)
232 goto fail;
233 fprintf(stderr, "flushed.\n");
234 }
235 return;
236 }
237 fail:
238 if (pid_pack_objects)
239 kill(pid_pack_objects, SIGKILL);
240 if (pid_rev_list)
241 kill(pid_rev_list, SIGKILL);
242 die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
243 }
244
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
245 static int got_sha1(char *hex, unsigned char *sha1)
246 {
247 if (get_sha1_hex(hex, sha1))
248 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
249 if (!has_sha1_file(sha1))
250 return 0;
f0243f26 »
2005-10-28 git-upload-pack: More efficient usage of the has_sha1 array
251 if (nr_has < MAX_HAS) {
252 struct object *o = lookup_object(sha1);
253 if (!(o && o->parsed))
254 o = parse_object(sha1);
255 if (!o)
256 die("oops (%s)", sha1_to_hex(sha1));
885a86ab »
2006-06-14 Shrink "struct object" a bit
257 if (o->type == TYPE_COMMIT) {
f0243f26 »
2005-10-28 git-upload-pack: More efficient usage of the has_sha1 array
258 struct commit_list *parents;
259 if (o->flags & THEY_HAVE)
260 return 0;
261 o->flags |= THEY_HAVE;
262 for (parents = ((struct commit*)o)->parents;
263 parents;
264 parents = parents->next)
265 parents->item->object.flags |= THEY_HAVE;
266 }
267 memcpy(has_sha1[nr_has++], sha1, 20);
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
268 }
269 return 1;
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
270 }
271
272 static int get_common_commits(void)
273 {
274 static char line[1000];
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
275 unsigned char sha1[20], last_sha1[20];
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
276 int len;
277
f0243f26 »
2005-10-28 git-upload-pack: More efficient usage of the has_sha1 array
278 track_object_refs = 0;
279 save_commit_buffer = 0;
280
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
281 for(;;) {
282 len = packet_read_line(0, line, sizeof(line));
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
283 reset_timeout();
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
284
285 if (!len) {
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
286 if (nr_has == 0 || multi_ack)
287 packet_write(1, "NAK\n");
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
288 continue;
289 }
75bfc6c2 »
2005-07-04 Make git-fetch-pack actually do all the unpacking etc.
290 len = strip(line, len);
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
291 if (!strncmp(line, "have ", 5)) {
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
292 if (got_sha1(line+5, sha1) &&
293 (multi_ack || nr_has == 1)) {
294 if (nr_has >= MAX_HAS)
295 multi_ack = 0;
296 packet_write(1, "ACK %s%s\n",
297 sha1_to_hex(sha1),
298 multi_ack ? " continue" : "");
299 if (multi_ack)
300 memcpy(last_sha1, sha1, 20);
af2d3aa4 »
2005-10-25 Revert recent fetch-pack/upload-pack updates.
301 }
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
302 continue;
303 }
304 if (!strcmp(line, "done")) {
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
305 if (nr_has > 0) {
306 if (multi_ack)
307 packet_write(1, "ACK %s\n",
308 sha1_to_hex(last_sha1));
309 return 0;
310 }
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
311 packet_write(1, "NAK\n");
312 return -1;
313 }
314 die("git-upload-pack: expected SHA1 list, got '%s'", line);
315 }
316 }
317
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
318 static int receive_needs(void)
319 {
320 static char line[1000];
321 int len, needs;
322
323 needs = 0;
324 for (;;) {
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
325 struct object *o;
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
326 unsigned char dummy[20], *sha1_buf;
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
327 len = packet_read_line(0, line, sizeof(line));
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
328 reset_timeout();
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
329 if (!len)
330 return needs;
331
e091eb93 »
2005-10-05 upload-pack: Do not choke on too many heads request.
332 sha1_buf = dummy;
333 if (needs == MAX_NEEDS) {
334 fprintf(stderr,
335 "warning: supporting only a max of %d requests. "
336 "sending everything instead.\n",
337 MAX_NEEDS);
338 }
339 else if (needs < MAX_NEEDS)
340 sha1_buf = needs_sha1[needs];
341
342 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
343 die("git-upload-pack: protocol error, "
344 "expected to get sha, not '%s'", line);
1bd8c8f0 »
2005-10-28 git-upload-pack: Support the multi_ack protocol
345 if (strstr(line+45, "multi_ack"))
346 multi_ack = 1;
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
347 if (strstr(line+45, "thin-pack"))
348 use_thin_pack = 1;
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
349
350 /* We have sent all our refs already, and the other end
351 * should have chosen out of them; otherwise they are
352 * asking for nonsense.
353 *
354 * Hmph. We may later want to allow "want" line that
355 * asks for something like "master~10" (symbolic)...
356 * would it make sense? I don't know.
357 */
358 o = lookup_object(sha1_buf);
359 if (!o || !(o->flags & OUR_REF))
360 die("git-upload-pack: not our ref %s", line+5);
361 if (!(o->flags & WANTED)) {
362 o->flags |= WANTED;
363 needs++;
364 }
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
365 }
366 }
367
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
368 static int send_ref(const char *refname, const unsigned char *sha1)
369 {
b19696c2 »
2006-02-20 Use thin pack transfer in "git fetch".
370 static char *capabilities = "multi_ack thin-pack";
f6b42a81 »
2005-10-13 Show peeled onion from upload-pack and server-info.
371 struct object *o = parse_object(sha1);
372
b5b16990 »
2006-02-17 Prevent git-upload-pack segfault if object cannot be found
373 if (!o)
374 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
375
1f5881bb »
2005-10-28 fix multi_ack.
376 if (capabilities)
377 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
378 0, capabilities);
379 else
380 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
381 capabilities = NULL;
565ebbf7 »
2005-10-24 upload-pack: tighten request validation.
382 if (!(o->flags & OUR_REF)) {
383 o->flags |= OUR_REF;
384 nr_our_refs++;
385 }
885a86ab »
2006-06-14 Shrink "struct object" a bit
386 if (o->type == TYPE_TAG) {
9534f40b »
2005-11-02 Be careful when dereferencing tags.
387 o = deref_tag(o, refname, 0);
f6b42a81 »
2005-10-13 Show peeled onion from upload-pack and server-info.
388 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
389 }
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
390 return 0;
391 }
392
393 static int upload_pack(void)
394 {
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
395 reset_timeout();
723c31fe »
2005-07-05 Add "git_path()" and "head_ref()" helper functions.
396 head_ref(send_ref);
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
397 for_each_ref(send_ref);
398 packet_flush(1);
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
399 nr_needs = receive_needs();
400 if (!nr_needs)
401 return 0;
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
402 get_common_commits();
fb9040cc »
2005-07-04 Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
403 create_pack_file();
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
404 return 0;
405 }
406
407 int main(int argc, char **argv)
408 {
8d630132 »
2005-11-17 Server-side support for user-relative paths.
409 char *dir;
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
410 int i;
411 int strict = 0;
412
413 for (i = 1; i < argc; i++) {
414 char *arg = argv[i];
415
416 if (arg[0] != '-')
417 break;
418 if (!strcmp(arg, "--strict")) {
419 strict = 1;
420 continue;
421 }
422 if (!strncmp(arg, "--timeout=", 10)) {
423 timeout = atoi(arg+10);
424 continue;
425 }
426 if (!strcmp(arg, "--")) {
427 i++;
428 break;
429 }
430 }
431
432 if (i != argc-1)
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
433 usage(upload_pack_usage);
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
434 dir = argv[i];
113b9475 »
2005-07-08 Make "upload-pack" match git-fetch-pack usage
435
8d630132 »
2005-11-17 Server-side support for user-relative paths.
436 if (!enter_repo(dir, strict))
437 die("'%s': unable to chdir or not a git archive", dir);
960deccb »
2005-10-19 git-daemon: timeout, eliminate double DWIM
438
def88e9a »
2005-07-04 Commit first cut at "git-fetch-pack"
439 upload_pack();
440 return 0;
441 }
Something went wrong with that request. Please try again.