]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
remote-helpers: allow all tests running from any dir
[thirdparty/git.git] / upload-pack.c
CommitLineData
def88e9a
LT
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
958c24b1 4#include "sideband.h"
f6b42a81
JH
5#include "tag.h"
6#include "object.h"
f0243f26 7#include "commit.h"
77cb17e9 8#include "exec_cmd.h"
9b8dc263
JS
9#include "diff.h"
10#include "revision.h"
11#include "list-objects.h"
cc41fa8d 12#include "run-command.h"
47a59185 13#include "connect.h"
051e4005 14#include "sigchain.h"
ff5effdf 15#include "version.h"
daebaa78 16#include "string-list.h"
def88e9a 17
62b4698e 18static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
def88e9a 19
937a515a
JH
20/* bits #0..7 in revision.h, #8..10 in commit.c */
21#define THEY_HAVE (1u << 11)
22#define OUR_REF (1u << 12)
23#define WANTED (1u << 13)
24#define COMMON_KNOWN (1u << 14)
25#define REACHABLE (1u << 15)
26
f53514bc
JS
27#define SHALLOW (1u << 16)
28#define NOT_SHALLOW (1u << 17)
29#define CLIENT_SHALLOW (1u << 18)
390eb36b 30#define HIDDEN_REF (1u << 19)
f53514bc 31
3fbe2d54 32static unsigned long oldest_have;
937a515a 33
3f1da57f 34static int multi_ack;
4e10cf9a 35static int no_done;
348e390b 36static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 37static int no_progress, daemon_mode;
390eb36b 38static int allow_tip_sha1_in_want;
f0cea83f 39static int shallow_nr;
b1e9fff7
JH
40static struct object_array have_obj;
41static struct object_array want_obj;
6523078b 42static struct object_array extra_edge_obj;
96f1e58f 43static unsigned int timeout;
115dedd7 44static int keepalive = 5;
d47f3db7
JH
45/* 0 for no sideband,
46 * otherwise maximum packet size (up to 65520 bytes).
47 */
96f1e58f 48static int use_sideband;
42526b47
SP
49static int advertise_refs;
50static int stateless_rpc;
960deccb
PA
51
52static void reset_timeout(void)
53{
54 alarm(timeout);
55}
fb9040cc 56
583b7ea3
JH
57static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
58{
958c24b1 59 if (use_sideband)
d47f3db7 60 return send_sideband(1, fd, data, sz, use_sideband);
958c24b1
JH
61 if (fd == 3)
62 /* emergency quit */
63 fd = 2;
64 if (fd == 2) {
93822c22 65 /* XXX: are we happy to lose stuff here? */
958c24b1
JH
66 xwrite(fd, data, sz);
67 return sz;
583b7ea3 68 }
cdf4fb8e
JK
69 write_or_die(fd, data, sz);
70 return sz;
583b7ea3
JH
71}
72
fb9040cc
LT
73static void create_pack_file(void)
74{
cc41fa8d 75 struct child_process pack_objects;
363b7817 76 char data[8193], progress[128];
583b7ea3
JH
77 char abort_msg[] = "aborting due to possible repository "
78 "corruption on the remote side.";
b1c71b72 79 int buffered = -1;
1456b043 80 ssize_t sz;
cdab4858
NTND
81 const char *argv[12];
82 int i, arg = 0;
83 FILE *pipe_fd;
0179c945 84 const char *shallow_file = NULL;
75bfc6c2 85
cdab4858 86 if (shallow_nr) {
1a30f5a2 87 shallow_file = setup_temporary_shallow(NULL);
cdab4858
NTND
88 argv[arg++] = "--shallow-file";
89 argv[arg++] = shallow_file;
f0cea83f 90 }
cdab4858
NTND
91 argv[arg++] = "pack-objects";
92 argv[arg++] = "--revs";
93 if (use_thin_pack)
94 argv[arg++] = "--thin";
75bfc6c2 95
cc41fa8d
JS
96 argv[arg++] = "--stdout";
97 if (!no_progress)
98 argv[arg++] = "--progress";
99 if (use_ofs_delta)
100 argv[arg++] = "--delta-base-offset";
348e390b
SP
101 if (use_include_tag)
102 argv[arg++] = "--include-tag";
cc41fa8d
JS
103 argv[arg++] = NULL;
104
105 memset(&pack_objects, 0, sizeof(pack_objects));
b9612197 106 pack_objects.in = -1;
cc41fa8d
JS
107 pack_objects.out = -1;
108 pack_objects.err = -1;
109 pack_objects.git_cmd = 1;
110 pack_objects.argv = argv;
21edd3f1 111
4c324c00 112 if (start_command(&pack_objects))
7e44c935 113 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 114
cdab4858
NTND
115 pipe_fd = xfdopen(pack_objects.in, "w");
116
117 for (i = 0; i < want_obj.nr; i++)
118 fprintf(pipe_fd, "%s\n",
119 sha1_to_hex(want_obj.objects[i].item->sha1));
120 fprintf(pipe_fd, "--not\n");
121 for (i = 0; i < have_obj.nr; i++)
122 fprintf(pipe_fd, "%s\n",
123 sha1_to_hex(have_obj.objects[i].item->sha1));
124 for (i = 0; i < extra_edge_obj.nr; i++)
125 fprintf(pipe_fd, "%s\n",
126 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
127 fprintf(pipe_fd, "\n");
128 fflush(pipe_fd);
129 fclose(pipe_fd);
f0cea83f 130
cc41fa8d
JS
131 /* We read from pack_objects.err to capture stderr output for
132 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 133 */
b1c71b72
JH
134
135 while (1) {
b1c71b72 136 struct pollfd pfd[2];
363b7817 137 int pe, pu, pollsize;
05e95155 138 int ret;
b1c71b72 139
0d516ada
ML
140 reset_timeout();
141
b1c71b72 142 pollsize = 0;
363b7817 143 pe = pu = -1;
b1c71b72 144
cc41fa8d
JS
145 if (0 <= pack_objects.out) {
146 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
147 pfd[pollsize].events = POLLIN;
148 pu = pollsize;
149 pollsize++;
150 }
cc41fa8d
JS
151 if (0 <= pack_objects.err) {
152 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
153 pfd[pollsize].events = POLLIN;
154 pe = pollsize;
155 pollsize++;
156 }
b1c71b72 157
4c324c00
JS
158 if (!pollsize)
159 break;
160
05e95155
JK
161 ret = poll(pfd, pollsize, 1000 * keepalive);
162 if (ret < 0) {
4c324c00
JS
163 if (errno != EINTR) {
164 error("poll failed, resuming: %s",
165 strerror(errno));
166 sleep(1);
b1c71b72 167 }
4c324c00
JS
168 continue;
169 }
6b59f51b
NP
170 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
171 /* Status ready; we ship that in the side-band
172 * or dump to the standard error.
173 */
174 sz = xread(pack_objects.err, progress,
175 sizeof(progress));
176 if (0 < sz)
177 send_client_data(2, progress, sz);
178 else if (sz == 0) {
179 close(pack_objects.err);
180 pack_objects.err = -1;
181 }
182 else
183 goto fail;
184 /* give priority to status messages */
185 continue;
186 }
4c324c00
JS
187 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
188 /* Data ready; we keep the last byte to ourselves
189 * in case we detect broken rev-list, so that we
190 * can leave the stream corrupted. This is
191 * unfortunate -- unpack-objects would happily
192 * accept a valid packdata with trailing garbage,
193 * so appending garbage after we pass all the
194 * pack data is not good enough to signal
195 * breakage to downstream.
196 */
197 char *cp = data;
198 ssize_t outsz = 0;
199 if (0 <= buffered) {
200 *cp++ = buffered;
201 outsz++;
b1c71b72 202 }
4c324c00
JS
203 sz = xread(pack_objects.out, cp,
204 sizeof(data) - outsz);
205 if (0 < sz)
1456b043 206 ;
4c324c00
JS
207 else if (sz == 0) {
208 close(pack_objects.out);
209 pack_objects.out = -1;
363b7817 210 }
4c324c00
JS
211 else
212 goto fail;
213 sz += outsz;
214 if (1 < sz) {
215 buffered = data[sz-1] & 0xFF;
216 sz--;
b1c71b72 217 }
4c324c00
JS
218 else
219 buffered = -1;
220 sz = send_client_data(1, data, sz);
221 if (sz < 0)
b1c71b72 222 goto fail;
4c324c00 223 }
05e95155
JK
224
225 /*
226 * We hit the keepalive timeout without saying anything; send
227 * an empty message on the data sideband just to let the other
228 * side know we're still working on it, but don't have any data
229 * yet.
230 *
231 * If we don't have a sideband channel, there's no room in the
232 * protocol to say anything, so those clients are just out of
233 * luck.
234 */
235 if (!ret && use_sideband) {
236 static const char buf[] = "0005\1";
237 write_or_die(1, buf, 5);
238 }
4c324c00 239 }
b1c71b72 240
4c324c00 241 if (finish_command(&pack_objects)) {
7e44c935 242 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
243 goto fail;
244 }
b1c71b72 245
4c324c00
JS
246 /* flush the data */
247 if (0 <= buffered) {
248 data[0] = buffered;
249 sz = send_client_data(1, data, 1);
250 if (sz < 0)
251 goto fail;
252 fprintf(stderr, "flushed.\n");
b1c71b72 253 }
4c324c00
JS
254 if (use_sideband)
255 packet_flush(1);
256 return;
257
b1c71b72 258 fail:
583b7ea3 259 send_client_data(3, abort_msg, sizeof(abort_msg));
7e44c935 260 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
261}
262
def88e9a
LT
263static int got_sha1(char *hex, unsigned char *sha1)
264{
b1e9fff7 265 struct object *o;
937a515a 266 int we_knew_they_have = 0;
b1e9fff7 267
def88e9a 268 if (get_sha1_hex(hex, sha1))
7e44c935 269 die("git upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc 270 if (!has_sha1_file(sha1))
937a515a 271 return -1;
b1e9fff7 272
a6eec126 273 o = parse_object(sha1);
b1e9fff7
JH
274 if (!o)
275 die("oops (%s)", sha1_to_hex(sha1));
182a8dab 276 if (o->type == OBJ_COMMIT) {
b1e9fff7 277 struct commit_list *parents;
937a515a 278 struct commit *commit = (struct commit *)o;
b1e9fff7 279 if (o->flags & THEY_HAVE)
937a515a
JH
280 we_knew_they_have = 1;
281 else
282 o->flags |= THEY_HAVE;
283 if (!oldest_have || (commit->date < oldest_have))
284 oldest_have = commit->date;
285 for (parents = commit->parents;
b1e9fff7
JH
286 parents;
287 parents = parents->next)
288 parents->item->object.flags |= THEY_HAVE;
fb9040cc 289 }
937a515a
JH
290 if (!we_knew_they_have) {
291 add_object_array(o, NULL, &have_obj);
292 return 1;
293 }
294 return 0;
295}
296
297static int reachable(struct commit *want)
298{
299 struct commit_list *work = NULL;
300
47e44ed1 301 commit_list_insert_by_date(want, &work);
937a515a
JH
302 while (work) {
303 struct commit_list *list = work->next;
304 struct commit *commit = work->item;
305 free(work);
306 work = list;
307
308 if (commit->object.flags & THEY_HAVE) {
309 want->object.flags |= COMMON_KNOWN;
310 break;
311 }
312 if (!commit->object.parsed)
313 parse_object(commit->object.sha1);
314 if (commit->object.flags & REACHABLE)
315 continue;
316 commit->object.flags |= REACHABLE;
317 if (commit->date < oldest_have)
318 continue;
319 for (list = commit->parents; list; list = list->next) {
320 struct commit *parent = list->item;
321 if (!(parent->object.flags & REACHABLE))
47e44ed1 322 commit_list_insert_by_date(parent, &work);
937a515a
JH
323 }
324 }
325 want->object.flags |= REACHABLE;
326 clear_commit_marks(want, REACHABLE);
327 free_commit_list(work);
328 return (want->object.flags & COMMON_KNOWN);
329}
330
331static int ok_to_give_up(void)
332{
333 int i;
334
335 if (!have_obj.nr)
336 return 0;
337
338 for (i = 0; i < want_obj.nr; i++) {
339 struct object *want = want_obj.objects[i].item;
340
341 if (want->flags & COMMON_KNOWN)
342 continue;
343 want = deref_tag(want, "a want line", 0);
344 if (!want || want->type != OBJ_COMMIT) {
345 /* no way to tell if this is reachable by
346 * looking at the ancestry chain alone, so
347 * leave a note to ourselves not to worry about
348 * this object anymore.
349 */
350 want_obj.objects[i].item->flags |= COMMON_KNOWN;
351 continue;
352 }
353 if (!reachable((struct commit *)want))
354 return 0;
355 }
fb9040cc 356 return 1;
def88e9a
LT
357}
358
359static int get_common_commits(void)
360{
c04c4e57 361 unsigned char sha1[20];
78affc49 362 char last_hex[41];
49bee717
SP
363 int got_common = 0;
364 int got_other = 0;
4e10cf9a 365 int sent_ready = 0;
def88e9a 366
f0243f26
JS
367 save_commit_buffer = 0;
368
eeefa7c9 369 for (;;) {
74543a04 370 char *line = packet_read_line(0, NULL);
960deccb 371 reset_timeout();
def88e9a 372
74543a04 373 if (!line) {
49bee717 374 if (multi_ack == 2 && got_common
4e10cf9a
JH
375 && !got_other && ok_to_give_up()) {
376 sent_ready = 1;
49bee717 377 packet_write(1, "ACK %s ready\n", last_hex);
4e10cf9a 378 }
b1e9fff7 379 if (have_obj.nr == 0 || multi_ack)
1bd8c8f0 380 packet_write(1, "NAK\n");
4e10cf9a
JH
381
382 if (no_done && sent_ready) {
383 packet_write(1, "ACK %s\n", last_hex);
384 return 0;
385 }
42526b47
SP
386 if (stateless_rpc)
387 exit(0);
49bee717
SP
388 got_common = 0;
389 got_other = 0;
def88e9a
LT
390 continue;
391 }
59556548 392 if (starts_with(line, "have ")) {
937a515a
JH
393 switch (got_sha1(line+5, sha1)) {
394 case -1: /* they have what we do not */
49bee717 395 got_other = 1;
78affc49
SP
396 if (multi_ack && ok_to_give_up()) {
397 const char *hex = sha1_to_hex(sha1);
4e10cf9a
JH
398 if (multi_ack == 2) {
399 sent_ready = 1;
78affc49 400 packet_write(1, "ACK %s ready\n", hex);
4e10cf9a 401 } else
78affc49
SP
402 packet_write(1, "ACK %s continue\n", hex);
403 }
937a515a
JH
404 break;
405 default:
49bee717 406 got_common = 1;
78affc49
SP
407 memcpy(last_hex, sha1_to_hex(sha1), 41);
408 if (multi_ack == 2)
409 packet_write(1, "ACK %s common\n", last_hex);
410 else if (multi_ack)
411 packet_write(1, "ACK %s continue\n", last_hex);
c04c4e57 412 else if (have_obj.nr == 1)
78affc49 413 packet_write(1, "ACK %s\n", last_hex);
937a515a 414 break;
af2d3aa4 415 }
def88e9a
LT
416 continue;
417 }
418 if (!strcmp(line, "done")) {
b1e9fff7 419 if (have_obj.nr > 0) {
1bd8c8f0 420 if (multi_ack)
c04c4e57 421 packet_write(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
422 return 0;
423 }
def88e9a
LT
424 packet_write(1, "NAK\n");
425 return -1;
426 }
7e44c935 427 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 428 }
def88e9a
LT
429}
430
390eb36b
JH
431static int is_our_ref(struct object *o)
432{
433 return o->flags &
434 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
435}
436
051e4005
JH
437static void check_non_tip(void)
438{
439 static const char *argv[] = {
440 "rev-list", "--stdin", NULL,
441 };
442 static struct child_process cmd;
443 struct object *o;
444 char namebuf[42]; /* ^ + SHA-1 + LF */
445 int i;
446
447 /* In the normal in-process case non-tip request can never happen */
448 if (!stateless_rpc)
449 goto error;
450
451 cmd.argv = argv;
452 cmd.git_cmd = 1;
453 cmd.no_stderr = 1;
454 cmd.in = -1;
455 cmd.out = -1;
456
457 if (start_command(&cmd))
458 goto error;
459
460 /*
461 * If rev-list --stdin encounters an unknown commit, it
462 * terminates, which will cause SIGPIPE in the write loop
463 * below.
464 */
465 sigchain_push(SIGPIPE, SIG_IGN);
466
467 namebuf[0] = '^';
468 namebuf[41] = '\n';
469 for (i = get_max_object_index(); 0 < i; ) {
470 o = get_indexed_object(--i);
2a745324
BH
471 if (!o)
472 continue;
390eb36b 473 if (!is_our_ref(o))
051e4005
JH
474 continue;
475 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
476 if (write_in_full(cmd.in, namebuf, 42) < 0)
477 goto error;
478 }
479 namebuf[40] = '\n';
480 for (i = 0; i < want_obj.nr; i++) {
481 o = want_obj.objects[i].item;
390eb36b 482 if (is_our_ref(o))
051e4005
JH
483 continue;
484 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
485 if (write_in_full(cmd.in, namebuf, 41) < 0)
486 goto error;
487 }
488 close(cmd.in);
489
490 sigchain_pop(SIGPIPE);
491
492 /*
493 * The commits out of the rev-list are not ancestors of
494 * our ref.
495 */
496 i = read_in_full(cmd.out, namebuf, 1);
497 if (i)
498 goto error;
499 close(cmd.out);
500
501 /*
502 * rev-list may have died by encountering a bad commit
503 * in the history, in which case we do want to bail out
504 * even when it showed no commit.
505 */
506 if (finish_command(&cmd))
507 goto error;
508
509 /* All the non-tip ones are ancestors of what we advertised */
510 return;
511
512error:
513 /* Pick one of them (we know there at least is one) */
514 for (i = 0; i < want_obj.nr; i++) {
515 o = want_obj.objects[i].item;
390eb36b 516 if (!is_our_ref(o))
051e4005
JH
517 die("git upload-pack: not our ref %s",
518 sha1_to_hex(o->sha1));
519 }
520}
521
b1e9fff7 522static void receive_needs(void)
fb9040cc 523{
3cd47459 524 struct object_array shallows = OBJECT_ARRAY_INIT;
74543a04 525 int depth = 0;
051e4005 526 int has_non_tip = 0;
fb9040cc 527
f0cea83f 528 shallow_nr = 0;
fb9040cc 529 for (;;) {
565ebbf7 530 struct object *o;
f47182c8 531 const char *features;
6ece0d30 532 unsigned char sha1_buf[20];
74543a04 533 char *line = packet_read_line(0, NULL);
960deccb 534 reset_timeout();
74543a04 535 if (!line)
ed09aef0 536 break;
e091eb93 537
59556548 538 if (starts_with(line, "shallow ")) {
ed09aef0
JS
539 unsigned char sha1[20];
540 struct object *object;
b7b02170 541 if (get_sha1_hex(line + 8, sha1))
ed09aef0
JS
542 die("invalid shallow line: %s", line);
543 object = parse_object(sha1);
544 if (!object)
af04fa2a 545 continue;
6293ded3
NTND
546 if (object->type != OBJ_COMMIT)
547 die("invalid shallow object %s", sha1_to_hex(sha1));
e58e57e4
JK
548 if (!(object->flags & CLIENT_SHALLOW)) {
549 object->flags |= CLIENT_SHALLOW;
550 add_object_array(object, NULL, &shallows);
551 }
ed09aef0
JS
552 continue;
553 }
59556548 554 if (starts_with(line, "deepen ")) {
016e6ccb
JS
555 char *end;
556 depth = strtol(line + 7, &end, 0);
557 if (end == line + 7 || depth <= 0)
558 die("Invalid deepen: %s", line);
559 continue;
560 }
59556548 561 if (!starts_with(line, "want ") ||
6ece0d30 562 get_sha1_hex(line+5, sha1_buf))
7e44c935 563 die("git upload-pack: protocol error, "
e091eb93 564 "expected to get sha, not '%s'", line);
f47182c8
JH
565
566 features = line + 45;
567
568 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 569 multi_ack = 2;
f47182c8 570 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 571 multi_ack = 1;
f47182c8 572 if (parse_feature_request(features, "no-done"))
4e10cf9a 573 no_done = 1;
f47182c8 574 if (parse_feature_request(features, "thin-pack"))
b19696c2 575 use_thin_pack = 1;
f47182c8 576 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 577 use_ofs_delta = 1;
f47182c8 578 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 579 use_sideband = LARGE_PACKET_MAX;
f47182c8 580 else if (parse_feature_request(features, "side-band"))
d47f3db7 581 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 582 if (parse_feature_request(features, "no-progress"))
b0e90897 583 no_progress = 1;
f47182c8 584 if (parse_feature_request(features, "include-tag"))
348e390b 585 use_include_tag = 1;
565ebbf7 586
f59de5d1 587 o = parse_object(sha1_buf);
051e4005 588 if (!o)
9f9aa761
EN
589 die("git upload-pack: not our ref %s",
590 sha1_to_hex(sha1_buf));
565ebbf7
JH
591 if (!(o->flags & WANTED)) {
592 o->flags |= WANTED;
390eb36b 593 if (!is_our_ref(o))
051e4005 594 has_non_tip = 1;
b1e9fff7 595 add_object_array(o, NULL, &want_obj);
565ebbf7 596 }
fb9040cc 597 }
9462e3f5 598
051e4005
JH
599 /*
600 * We have sent all our refs already, and the other end
601 * should have chosen out of them. When we are operating
602 * in the stateless RPC mode, however, their choice may
603 * have been based on the set of older refs advertised
604 * by another process that handled the initial request.
605 */
606 if (has_non_tip)
607 check_non_tip();
608
9462e3f5
JS
609 if (!use_sideband && daemon_mode)
610 no_progress = 1;
611
f53514bc
JS
612 if (depth == 0 && shallows.nr == 0)
613 return;
016e6ccb 614 if (depth > 0) {
4dcb167f 615 struct commit_list *result = NULL, *backup = NULL;
f53514bc 616 int i;
79d3a236 617 if (depth == INFINITE_DEPTH && !is_repository_shallow())
4dcb167f
NTND
618 for (i = 0; i < shallows.nr; i++) {
619 struct object *object = shallows.objects[i].item;
620 object->flags |= NOT_SHALLOW;
621 }
622 else
623 backup = result =
624 get_shallow_commits(&want_obj, depth,
625 SHALLOW, NOT_SHALLOW);
016e6ccb 626 while (result) {
f53514bc 627 struct object *object = &result->item->object;
1f2de769 628 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
f53514bc
JS
629 packet_write(1, "shallow %s",
630 sha1_to_hex(object->sha1));
631 register_shallow(object->sha1);
f0cea83f 632 shallow_nr++;
f53514bc 633 }
016e6ccb
JS
634 result = result->next;
635 }
636 free_commit_list(backup);
f53514bc
JS
637 for (i = 0; i < shallows.nr; i++) {
638 struct object *object = shallows.objects[i].item;
639 if (object->flags & NOT_SHALLOW) {
640 struct commit_list *parents;
641 packet_write(1, "unshallow %s",
642 sha1_to_hex(object->sha1));
643 object->flags &= ~CLIENT_SHALLOW;
644 /* make sure the real parents are parsed */
645 unregister_shallow(object->sha1);
176d45cb 646 object->parsed = 0;
367068e0 647 parse_commit_or_die((struct commit *)object);
f53514bc
JS
648 parents = ((struct commit *)object)->parents;
649 while (parents) {
650 add_object_array(&parents->item->object,
651 NULL, &want_obj);
652 parents = parents->next;
653 }
6523078b 654 add_object_array(object, NULL, &extra_edge_obj);
f53514bc
JS
655 }
656 /* make sure commit traversal conforms to client */
657 register_shallow(object->sha1);
658 }
659 packet_flush(1);
660 } else
661 if (shallows.nr > 0) {
662 int i;
663 for (i = 0; i < shallows.nr; i++)
664 register_shallow(shallows.objects[i].item->sha1);
665 }
f0cea83f
NE
666
667 shallow_nr += shallows.nr;
f53514bc 668 free(shallows.objects);
fb9040cc
LT
669}
670
daebaa78 671/* return non-zero if the ref is hidden, otherwise 0 */
cbbe50db
JH
672static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
673{
674 struct object *o = lookup_unknown_object(sha1);
daebaa78 675
390eb36b
JH
676 if (ref_is_hidden(refname)) {
677 o->flags |= HIDDEN_REF;
daebaa78 678 return 1;
390eb36b 679 }
cbbe50db
JH
680 if (!o)
681 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
3f1da57f 682 o->flags |= OUR_REF;
cbbe50db
JH
683 return 0;
684}
685
7171d8c1
JH
686static void format_symref_info(struct strbuf *buf, struct string_list *symref)
687{
688 struct string_list_item *item;
689
690 if (!symref->nr)
691 return;
692 for_each_string_list_item(item, symref)
693 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
694}
695
8da19775 696static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
def88e9a 697{
ed09aef0 698 static const char *capabilities = "multi_ack thin-pack side-band"
348e390b 699 " side-band-64k ofs-delta shallow no-progress"
78affc49 700 " include-tag multi_ack_detailed";
6b01ecfe 701 const char *refname_nons = strip_namespace(refname);
435c8332 702 unsigned char peeled[20];
b5b16990 703
a4d695de 704 if (mark_our_ref(refname, sha1, flag, NULL))
daebaa78 705 return 0;
cbbe50db 706
7171d8c1
JH
707 if (capabilities) {
708 struct strbuf symref_info = STRBUF_INIT;
709
710 format_symref_info(&symref_info, cb_data);
711 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
ff5effdf 712 sha1_to_hex(sha1), refname_nons,
cf2ad8e6 713 0, capabilities,
390eb36b 714 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
ff5effdf 715 stateless_rpc ? " no-done" : "",
7171d8c1 716 symref_info.buf,
ff5effdf 717 git_user_agent_sanitized());
7171d8c1
JH
718 strbuf_release(&symref_info);
719 } else {
6b01ecfe 720 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
7171d8c1 721 }
1f5881bb 722 capabilities = NULL;
435c8332
JK
723 if (!peel_ref(refname, peeled))
724 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
def88e9a
LT
725 return 0;
726}
727
7171d8c1
JH
728static int find_symref(const char *refname, const unsigned char *sha1, int flag,
729 void *cb_data)
730{
731 const char *symref_target;
732 struct string_list_item *item;
733 unsigned char unused[20];
734
735 if ((flag & REF_ISSYMREF) == 0)
736 return 0;
737 symref_target = resolve_ref_unsafe(refname, unused, 0, &flag);
738 if (!symref_target || (flag & REF_ISSYMREF) == 0)
739 die("'%s' is a symref but it is not?", refname);
740 item = string_list_append(cb_data, refname);
741 item->util = xstrdup(symref_target);
742 return 0;
743}
744
59076eba 745static void upload_pack(void)
def88e9a 746{
7171d8c1
JH
747 struct string_list symref = STRING_LIST_INIT_DUP;
748
749 head_ref_namespaced(find_symref, &symref);
750
42526b47
SP
751 if (advertise_refs || !stateless_rpc) {
752 reset_timeout();
7171d8c1
JH
753 head_ref_namespaced(send_ref, &symref);
754 for_each_namespaced_ref(send_ref, &symref);
ad491366 755 advertise_shallow_grafts(1);
42526b47
SP
756 packet_flush(1);
757 } else {
6b01ecfe
JT
758 head_ref_namespaced(mark_our_ref, NULL);
759 for_each_namespaced_ref(mark_our_ref, NULL);
42526b47 760 }
7171d8c1 761 string_list_clear(&symref, 1);
42526b47
SP
762 if (advertise_refs)
763 return;
764
b1e9fff7 765 receive_needs();
59076eba
DR
766 if (want_obj.nr) {
767 get_common_commits();
768 create_pack_file();
769 }
def88e9a
LT
770}
771
daebaa78
JH
772static int upload_pack_config(const char *var, const char *value, void *unused)
773{
390eb36b
JH
774 if (!strcmp("uploadpack.allowtipsha1inwant", var))
775 allow_tip_sha1_in_want = git_config_bool(var, value);
05e95155
JK
776 else if (!strcmp("uploadpack.keepalive", var)) {
777 keepalive = git_config_int(var, value);
778 if (!keepalive)
779 keepalive = -1;
780 }
daebaa78
JH
781 return parse_hide_refs_config(var, value, "uploadpack");
782}
783
def88e9a
LT
784int main(int argc, char **argv)
785{
8d630132 786 char *dir;
960deccb
PA
787 int i;
788 int strict = 0;
789
5e9637c6
ÆAB
790 git_setup_gettext();
791
bbc30f99 792 packet_trace_identity("upload-pack");
2fb3f6db 793 git_extract_argv0_path(argv[0]);
dae556bd 794 read_replace_refs = 0;
2fb3f6db 795
960deccb
PA
796 for (i = 1; i < argc; i++) {
797 char *arg = argv[i];
798
799 if (arg[0] != '-')
800 break;
42526b47
SP
801 if (!strcmp(arg, "--advertise-refs")) {
802 advertise_refs = 1;
803 continue;
804 }
805 if (!strcmp(arg, "--stateless-rpc")) {
806 stateless_rpc = 1;
807 continue;
808 }
960deccb
PA
809 if (!strcmp(arg, "--strict")) {
810 strict = 1;
811 continue;
812 }
59556548 813 if (starts_with(arg, "--timeout=")) {
960deccb 814 timeout = atoi(arg+10);
9462e3f5 815 daemon_mode = 1;
960deccb
PA
816 continue;
817 }
818 if (!strcmp(arg, "--")) {
819 i++;
820 break;
821 }
822 }
a6080a0a 823
960deccb 824 if (i != argc-1)
def88e9a 825 usage(upload_pack_usage);
04b33055 826
e1464ca7 827 setup_path();
04b33055 828
960deccb 829 dir = argv[i];
113b9475 830
8d630132 831 if (!enter_repo(dir, strict))
05ac6b34 832 die("'%s' does not appear to be a git repository", dir);
ad491366 833
daebaa78 834 git_config(upload_pack_config, NULL);
def88e9a
LT
835 upload_pack();
836 return 0;
837}