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