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