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