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