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