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