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