Merge branch 'sg/subtree-signed-commits' into pu
[git] / upload-pack.c
1 #include "cache.h"
2 #include "config.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "tag.h"
7 #include "object.h"
8 #include "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 #include "list-objects-filter.h"
13 #include "list-objects-filter-options.h"
14 #include "run-command.h"
15 #include "connect.h"
16 #include "sigchain.h"
17 #include "version.h"
18 #include "string-list.h"
19 #include "argv-array.h"
20 #include "prio-queue.h"
21 #include "protocol.h"
22 #include "quote.h"
23 #include "upload-pack.h"
24 #include "serve.h"
25
26 /* Remember to update object flag allocation in object.h */
27 #define THEY_HAVE       (1u << 11)
28 #define OUR_REF         (1u << 12)
29 #define WANTED          (1u << 13)
30 #define COMMON_KNOWN    (1u << 14)
31 #define REACHABLE       (1u << 15)
32
33 #define SHALLOW         (1u << 16)
34 #define NOT_SHALLOW     (1u << 17)
35 #define CLIENT_SHALLOW  (1u << 18)
36 #define HIDDEN_REF      (1u << 19)
37
38 static timestamp_t oldest_have;
39
40 static int deepen_relative;
41 static int multi_ack;
42 static int no_done;
43 static int use_thin_pack, use_ofs_delta, use_include_tag;
44 static int no_progress, daemon_mode;
45 /* Allow specifying sha1 if it is a ref tip. */
46 #define ALLOW_TIP_SHA1  01
47 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
48 #define ALLOW_REACHABLE_SHA1    02
49 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
50 #define ALLOW_ANY_SHA1  07
51 static unsigned int allow_unadvertised_object_request;
52 static int shallow_nr;
53 static struct object_array have_obj;
54 static struct object_array want_obj;
55 static struct object_array extra_edge_obj;
56 static unsigned int timeout;
57 static int keepalive = 5;
58 /* 0 for no sideband,
59  * otherwise maximum packet size (up to 65520 bytes).
60  */
61 static int use_sideband;
62 static int stateless_rpc;
63 static const char *pack_objects_hook;
64
65 static int filter_capability_requested;
66 static int filter_advertise;
67 static struct list_objects_filter_options filter_options;
68
69 static void reset_timeout(void)
70 {
71         alarm(timeout);
72 }
73
74 static void send_client_data(int fd, const char *data, ssize_t sz)
75 {
76         if (use_sideband) {
77                 send_sideband(1, fd, data, sz, use_sideband);
78                 return;
79         }
80         if (fd == 3)
81                 /* emergency quit */
82                 fd = 2;
83         if (fd == 2) {
84                 /* XXX: are we happy to lose stuff here? */
85                 xwrite(fd, data, sz);
86                 return;
87         }
88         write_or_die(fd, data, sz);
89 }
90
91 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
92 {
93         FILE *fp = cb_data;
94         if (graft->nr_parent == -1)
95                 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
96         return 0;
97 }
98
99 static void create_pack_file(void)
100 {
101         struct child_process pack_objects = CHILD_PROCESS_INIT;
102         char data[8193], progress[128];
103         char abort_msg[] = "aborting due to possible repository "
104                 "corruption on the remote side.";
105         int buffered = -1;
106         ssize_t sz;
107         int i;
108         FILE *pipe_fd;
109
110         if (!pack_objects_hook)
111                 pack_objects.git_cmd = 1;
112         else {
113                 argv_array_push(&pack_objects.args, pack_objects_hook);
114                 argv_array_push(&pack_objects.args, "git");
115                 pack_objects.use_shell = 1;
116         }
117
118         if (shallow_nr) {
119                 argv_array_push(&pack_objects.args, "--shallow-file");
120                 argv_array_push(&pack_objects.args, "");
121         }
122         argv_array_push(&pack_objects.args, "pack-objects");
123         argv_array_push(&pack_objects.args, "--revs");
124         if (use_thin_pack)
125                 argv_array_push(&pack_objects.args, "--thin");
126
127         argv_array_push(&pack_objects.args, "--stdout");
128         if (shallow_nr)
129                 argv_array_push(&pack_objects.args, "--shallow");
130         if (!no_progress)
131                 argv_array_push(&pack_objects.args, "--progress");
132         if (use_ofs_delta)
133                 argv_array_push(&pack_objects.args, "--delta-base-offset");
134         if (use_include_tag)
135                 argv_array_push(&pack_objects.args, "--include-tag");
136         if (filter_options.filter_spec) {
137                 if (pack_objects.use_shell) {
138                         struct strbuf buf = STRBUF_INIT;
139                         sq_quote_buf(&buf, filter_options.filter_spec);
140                         argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
141                         strbuf_release(&buf);
142                 } else {
143                         argv_array_pushf(&pack_objects.args, "--filter=%s",
144                                          filter_options.filter_spec);
145                 }
146         }
147
148         pack_objects.in = -1;
149         pack_objects.out = -1;
150         pack_objects.err = -1;
151
152         if (start_command(&pack_objects))
153                 die("git upload-pack: unable to fork git-pack-objects");
154
155         pipe_fd = xfdopen(pack_objects.in, "w");
156
157         if (shallow_nr)
158                 for_each_commit_graft(write_one_shallow, pipe_fd);
159
160         for (i = 0; i < want_obj.nr; i++)
161                 fprintf(pipe_fd, "%s\n",
162                         oid_to_hex(&want_obj.objects[i].item->oid));
163         fprintf(pipe_fd, "--not\n");
164         for (i = 0; i < have_obj.nr; i++)
165                 fprintf(pipe_fd, "%s\n",
166                         oid_to_hex(&have_obj.objects[i].item->oid));
167         for (i = 0; i < extra_edge_obj.nr; i++)
168                 fprintf(pipe_fd, "%s\n",
169                         oid_to_hex(&extra_edge_obj.objects[i].item->oid));
170         fprintf(pipe_fd, "\n");
171         fflush(pipe_fd);
172         fclose(pipe_fd);
173
174         /* We read from pack_objects.err to capture stderr output for
175          * progress bar, and pack_objects.out to capture the pack data.
176          */
177
178         while (1) {
179                 struct pollfd pfd[2];
180                 int pe, pu, pollsize;
181                 int ret;
182
183                 reset_timeout();
184
185                 pollsize = 0;
186                 pe = pu = -1;
187
188                 if (0 <= pack_objects.out) {
189                         pfd[pollsize].fd = pack_objects.out;
190                         pfd[pollsize].events = POLLIN;
191                         pu = pollsize;
192                         pollsize++;
193                 }
194                 if (0 <= pack_objects.err) {
195                         pfd[pollsize].fd = pack_objects.err;
196                         pfd[pollsize].events = POLLIN;
197                         pe = pollsize;
198                         pollsize++;
199                 }
200
201                 if (!pollsize)
202                         break;
203
204                 ret = poll(pfd, pollsize,
205                         keepalive < 0 ? -1 : 1000 * keepalive);
206
207                 if (ret < 0) {
208                         if (errno != EINTR) {
209                                 error_errno("poll failed, resuming");
210                                 sleep(1);
211                         }
212                         continue;
213                 }
214                 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
215                         /* Status ready; we ship that in the side-band
216                          * or dump to the standard error.
217                          */
218                         sz = xread(pack_objects.err, progress,
219                                   sizeof(progress));
220                         if (0 < sz)
221                                 send_client_data(2, progress, sz);
222                         else if (sz == 0) {
223                                 close(pack_objects.err);
224                                 pack_objects.err = -1;
225                         }
226                         else
227                                 goto fail;
228                         /* give priority to status messages */
229                         continue;
230                 }
231                 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
232                         /* Data ready; we keep the last byte to ourselves
233                          * in case we detect broken rev-list, so that we
234                          * can leave the stream corrupted.  This is
235                          * unfortunate -- unpack-objects would happily
236                          * accept a valid packdata with trailing garbage,
237                          * so appending garbage after we pass all the
238                          * pack data is not good enough to signal
239                          * breakage to downstream.
240                          */
241                         char *cp = data;
242                         ssize_t outsz = 0;
243                         if (0 <= buffered) {
244                                 *cp++ = buffered;
245                                 outsz++;
246                         }
247                         sz = xread(pack_objects.out, cp,
248                                   sizeof(data) - outsz);
249                         if (0 < sz)
250                                 ;
251                         else if (sz == 0) {
252                                 close(pack_objects.out);
253                                 pack_objects.out = -1;
254                         }
255                         else
256                                 goto fail;
257                         sz += outsz;
258                         if (1 < sz) {
259                                 buffered = data[sz-1] & 0xFF;
260                                 sz--;
261                         }
262                         else
263                                 buffered = -1;
264                         send_client_data(1, data, sz);
265                 }
266
267                 /*
268                  * We hit the keepalive timeout without saying anything; send
269                  * an empty message on the data sideband just to let the other
270                  * side know we're still working on it, but don't have any data
271                  * yet.
272                  *
273                  * If we don't have a sideband channel, there's no room in the
274                  * protocol to say anything, so those clients are just out of
275                  * luck.
276                  */
277                 if (!ret && use_sideband) {
278                         static const char buf[] = "0005\1";
279                         write_or_die(1, buf, 5);
280                 }
281         }
282
283         if (finish_command(&pack_objects)) {
284                 error("git upload-pack: git-pack-objects died with error.");
285                 goto fail;
286         }
287
288         /* flush the data */
289         if (0 <= buffered) {
290                 data[0] = buffered;
291                 send_client_data(1, data, 1);
292                 fprintf(stderr, "flushed.\n");
293         }
294         if (use_sideband)
295                 packet_flush(1);
296         return;
297
298  fail:
299         send_client_data(3, abort_msg, sizeof(abort_msg));
300         die("git upload-pack: %s", abort_msg);
301 }
302
303 static int got_oid(const char *hex, struct object_id *oid)
304 {
305         struct object *o;
306         int we_knew_they_have = 0;
307
308         if (get_oid_hex(hex, oid))
309                 die("git upload-pack: expected SHA1 object, got '%s'", hex);
310         if (!has_object_file(oid))
311                 return -1;
312
313         o = parse_object(oid);
314         if (!o)
315                 die("oops (%s)", oid_to_hex(oid));
316         if (o->type == OBJ_COMMIT) {
317                 struct commit_list *parents;
318                 struct commit *commit = (struct commit *)o;
319                 if (o->flags & THEY_HAVE)
320                         we_knew_they_have = 1;
321                 else
322                         o->flags |= THEY_HAVE;
323                 if (!oldest_have || (commit->date < oldest_have))
324                         oldest_have = commit->date;
325                 for (parents = commit->parents;
326                      parents;
327                      parents = parents->next)
328                         parents->item->object.flags |= THEY_HAVE;
329         }
330         if (!we_knew_they_have) {
331                 add_object_array(o, NULL, &have_obj);
332                 return 1;
333         }
334         return 0;
335 }
336
337 static int reachable(struct commit *want)
338 {
339         struct prio_queue work = { compare_commits_by_commit_date };
340
341         prio_queue_put(&work, want);
342         while (work.nr) {
343                 struct commit_list *list;
344                 struct commit *commit = prio_queue_get(&work);
345
346                 if (commit->object.flags & THEY_HAVE) {
347                         want->object.flags |= COMMON_KNOWN;
348                         break;
349                 }
350                 if (!commit->object.parsed)
351                         parse_object(&commit->object.oid);
352                 if (commit->object.flags & REACHABLE)
353                         continue;
354                 commit->object.flags |= REACHABLE;
355                 if (commit->date < oldest_have)
356                         continue;
357                 for (list = commit->parents; list; list = list->next) {
358                         struct commit *parent = list->item;
359                         if (!(parent->object.flags & REACHABLE))
360                                 prio_queue_put(&work, parent);
361                 }
362         }
363         want->object.flags |= REACHABLE;
364         clear_commit_marks(want, REACHABLE);
365         clear_prio_queue(&work);
366         return (want->object.flags & COMMON_KNOWN);
367 }
368
369 static int ok_to_give_up(void)
370 {
371         int i;
372
373         if (!have_obj.nr)
374                 return 0;
375
376         for (i = 0; i < want_obj.nr; i++) {
377                 struct object *want = want_obj.objects[i].item;
378
379                 if (want->flags & COMMON_KNOWN)
380                         continue;
381                 want = deref_tag(want, "a want line", 0);
382                 if (!want || want->type != OBJ_COMMIT) {
383                         /* no way to tell if this is reachable by
384                          * looking at the ancestry chain alone, so
385                          * leave a note to ourselves not to worry about
386                          * this object anymore.
387                          */
388                         want_obj.objects[i].item->flags |= COMMON_KNOWN;
389                         continue;
390                 }
391                 if (!reachable((struct commit *)want))
392                         return 0;
393         }
394         return 1;
395 }
396
397 static int get_common_commits(void)
398 {
399         struct object_id oid;
400         char last_hex[GIT_MAX_HEXSZ + 1];
401         int got_common = 0;
402         int got_other = 0;
403         int sent_ready = 0;
404
405         save_commit_buffer = 0;
406
407         for (;;) {
408                 char *line = packet_read_line(0, NULL);
409                 const char *arg;
410
411                 reset_timeout();
412
413                 if (!line) {
414                         if (multi_ack == 2 && got_common
415                             && !got_other && ok_to_give_up()) {
416                                 sent_ready = 1;
417                                 packet_write_fmt(1, "ACK %s ready\n", last_hex);
418                         }
419                         if (have_obj.nr == 0 || multi_ack)
420                                 packet_write_fmt(1, "NAK\n");
421
422                         if (no_done && sent_ready) {
423                                 packet_write_fmt(1, "ACK %s\n", last_hex);
424                                 return 0;
425                         }
426                         if (stateless_rpc)
427                                 exit(0);
428                         got_common = 0;
429                         got_other = 0;
430                         continue;
431                 }
432                 if (skip_prefix(line, "have ", &arg)) {
433                         switch (got_oid(arg, &oid)) {
434                         case -1: /* they have what we do not */
435                                 got_other = 1;
436                                 if (multi_ack && ok_to_give_up()) {
437                                         const char *hex = oid_to_hex(&oid);
438                                         if (multi_ack == 2) {
439                                                 sent_ready = 1;
440                                                 packet_write_fmt(1, "ACK %s ready\n", hex);
441                                         } else
442                                                 packet_write_fmt(1, "ACK %s continue\n", hex);
443                                 }
444                                 break;
445                         default:
446                                 got_common = 1;
447                                 memcpy(last_hex, oid_to_hex(&oid), 41);
448                                 if (multi_ack == 2)
449                                         packet_write_fmt(1, "ACK %s common\n", last_hex);
450                                 else if (multi_ack)
451                                         packet_write_fmt(1, "ACK %s continue\n", last_hex);
452                                 else if (have_obj.nr == 1)
453                                         packet_write_fmt(1, "ACK %s\n", last_hex);
454                                 break;
455                         }
456                         continue;
457                 }
458                 if (!strcmp(line, "done")) {
459                         if (have_obj.nr > 0) {
460                                 if (multi_ack)
461                                         packet_write_fmt(1, "ACK %s\n", last_hex);
462                                 return 0;
463                         }
464                         packet_write_fmt(1, "NAK\n");
465                         return -1;
466                 }
467                 die("git upload-pack: expected SHA1 list, got '%s'", line);
468         }
469 }
470
471 static int is_our_ref(struct object *o)
472 {
473         int allow_hidden_ref = (allow_unadvertised_object_request &
474                         (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
475         return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
476 }
477
478 /*
479  * on successful case, it's up to the caller to close cmd->out
480  */
481 static int do_reachable_revlist(struct child_process *cmd,
482                                 struct object_array *src,
483                                 struct object_array *reachable)
484 {
485         static const char *argv[] = {
486                 "rev-list", "--stdin", NULL,
487         };
488         struct object *o;
489         char namebuf[42]; /* ^ + SHA-1 + LF */
490         int i;
491
492         cmd->argv = argv;
493         cmd->git_cmd = 1;
494         cmd->no_stderr = 1;
495         cmd->in = -1;
496         cmd->out = -1;
497
498         /*
499          * If the next rev-list --stdin encounters an unknown commit,
500          * it terminates, which will cause SIGPIPE in the write loop
501          * below.
502          */
503         sigchain_push(SIGPIPE, SIG_IGN);
504
505         if (start_command(cmd))
506                 goto error;
507
508         namebuf[0] = '^';
509         namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
510         for (i = get_max_object_index(); 0 < i; ) {
511                 o = get_indexed_object(--i);
512                 if (!o)
513                         continue;
514                 if (reachable && o->type == OBJ_COMMIT)
515                         o->flags &= ~TMP_MARK;
516                 if (!is_our_ref(o))
517                         continue;
518                 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
519                 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
520                         goto error;
521         }
522         namebuf[GIT_SHA1_HEXSZ] = '\n';
523         for (i = 0; i < src->nr; i++) {
524                 o = src->objects[i].item;
525                 if (is_our_ref(o)) {
526                         if (reachable)
527                                 add_object_array(o, NULL, reachable);
528                         continue;
529                 }
530                 if (reachable && o->type == OBJ_COMMIT)
531                         o->flags |= TMP_MARK;
532                 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
533                 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
534                         goto error;
535         }
536         close(cmd->in);
537         cmd->in = -1;
538         sigchain_pop(SIGPIPE);
539
540         return 0;
541
542 error:
543         sigchain_pop(SIGPIPE);
544
545         if (cmd->in >= 0)
546                 close(cmd->in);
547         if (cmd->out >= 0)
548                 close(cmd->out);
549         return -1;
550 }
551
552 static int get_reachable_list(struct object_array *src,
553                               struct object_array *reachable)
554 {
555         struct child_process cmd = CHILD_PROCESS_INIT;
556         int i;
557         struct object *o;
558         char namebuf[42]; /* ^ + SHA-1 + LF */
559
560         if (do_reachable_revlist(&cmd, src, reachable) < 0)
561                 return -1;
562
563         while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
564                 struct object_id sha1;
565
566                 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
567                         break;
568
569                 o = lookup_object(sha1.hash);
570                 if (o && o->type == OBJ_COMMIT) {
571                         o->flags &= ~TMP_MARK;
572                 }
573         }
574         for (i = get_max_object_index(); 0 < i; i--) {
575                 o = get_indexed_object(i - 1);
576                 if (o && o->type == OBJ_COMMIT &&
577                     (o->flags & TMP_MARK)) {
578                         add_object_array(o, NULL, reachable);
579                                 o->flags &= ~TMP_MARK;
580                 }
581         }
582         close(cmd.out);
583
584         if (finish_command(&cmd))
585                 return -1;
586
587         return 0;
588 }
589
590 static int has_unreachable(struct object_array *src)
591 {
592         struct child_process cmd = CHILD_PROCESS_INIT;
593         char buf[1];
594         int i;
595
596         if (do_reachable_revlist(&cmd, src, NULL) < 0)
597                 return 1;
598
599         /*
600          * The commits out of the rev-list are not ancestors of
601          * our ref.
602          */
603         i = read_in_full(cmd.out, buf, 1);
604         if (i)
605                 goto error;
606         close(cmd.out);
607         cmd.out = -1;
608
609         /*
610          * rev-list may have died by encountering a bad commit
611          * in the history, in which case we do want to bail out
612          * even when it showed no commit.
613          */
614         if (finish_command(&cmd))
615                 goto error;
616
617         /* All the non-tip ones are ancestors of what we advertised */
618         return 0;
619
620 error:
621         sigchain_pop(SIGPIPE);
622         if (cmd.out >= 0)
623                 close(cmd.out);
624         return 1;
625 }
626
627 static void check_non_tip(void)
628 {
629         int i;
630
631         /*
632          * In the normal in-process case without
633          * uploadpack.allowReachableSHA1InWant,
634          * non-tip requests can never happen.
635          */
636         if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
637                 goto error;
638         if (!has_unreachable(&want_obj))
639                 /* All the non-tip ones are ancestors of what we advertised */
640                 return;
641
642 error:
643         /* Pick one of them (we know there at least is one) */
644         for (i = 0; i < want_obj.nr; i++) {
645                 struct object *o = want_obj.objects[i].item;
646                 if (!is_our_ref(o))
647                         die("git upload-pack: not our ref %s",
648                             oid_to_hex(&o->oid));
649         }
650 }
651
652 static void send_shallow(struct commit_list *result)
653 {
654         while (result) {
655                 struct object *object = &result->item->object;
656                 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
657                         packet_write_fmt(1, "shallow %s",
658                                          oid_to_hex(&object->oid));
659                         register_shallow(&object->oid);
660                         shallow_nr++;
661                 }
662                 result = result->next;
663         }
664 }
665
666 static void send_unshallow(const struct object_array *shallows)
667 {
668         int i;
669
670         for (i = 0; i < shallows->nr; i++) {
671                 struct object *object = shallows->objects[i].item;
672                 if (object->flags & NOT_SHALLOW) {
673                         struct commit_list *parents;
674                         packet_write_fmt(1, "unshallow %s",
675                                          oid_to_hex(&object->oid));
676                         object->flags &= ~CLIENT_SHALLOW;
677                         /*
678                          * We want to _register_ "object" as shallow, but we
679                          * also need to traverse object's parents to deepen a
680                          * shallow clone. Unregister it for now so we can
681                          * parse and add the parents to the want list, then
682                          * re-register it.
683                          */
684                         unregister_shallow(&object->oid);
685                         object->parsed = 0;
686                         parse_commit_or_die((struct commit *)object);
687                         parents = ((struct commit *)object)->parents;
688                         while (parents) {
689                                 add_object_array(&parents->item->object,
690                                                  NULL, &want_obj);
691                                 parents = parents->next;
692                         }
693                         add_object_array(object, NULL, &extra_edge_obj);
694                 }
695                 /* make sure commit traversal conforms to client */
696                 register_shallow(&object->oid);
697         }
698 }
699
700 static void deepen(int depth, int deepen_relative,
701                    struct object_array *shallows)
702 {
703         if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
704                 int i;
705
706                 for (i = 0; i < shallows->nr; i++) {
707                         struct object *object = shallows->objects[i].item;
708                         object->flags |= NOT_SHALLOW;
709                 }
710         } else if (deepen_relative) {
711                 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
712                 struct commit_list *result;
713
714                 get_reachable_list(shallows, &reachable_shallows);
715                 result = get_shallow_commits(&reachable_shallows,
716                                              depth + 1,
717                                              SHALLOW, NOT_SHALLOW);
718                 send_shallow(result);
719                 free_commit_list(result);
720                 object_array_clear(&reachable_shallows);
721         } else {
722                 struct commit_list *result;
723
724                 result = get_shallow_commits(&want_obj, depth,
725                                              SHALLOW, NOT_SHALLOW);
726                 send_shallow(result);
727                 free_commit_list(result);
728         }
729
730         send_unshallow(shallows);
731 }
732
733 static void deepen_by_rev_list(int ac, const char **av,
734                                struct object_array *shallows)
735 {
736         struct commit_list *result;
737
738         result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
739         send_shallow(result);
740         free_commit_list(result);
741         send_unshallow(shallows);
742 }
743
744 static int send_shallow_list(int depth, int deepen_rev_list,
745                              timestamp_t deepen_since,
746                              struct string_list *deepen_not,
747                              struct object_array *shallows)
748 {
749         int ret = 0;
750
751         if (depth > 0 && deepen_rev_list)
752                 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
753         if (depth > 0) {
754                 deepen(depth, deepen_relative, shallows);
755                 ret = 1;
756         } else if (deepen_rev_list) {
757                 struct argv_array av = ARGV_ARRAY_INIT;
758                 int i;
759
760                 argv_array_push(&av, "rev-list");
761                 if (deepen_since)
762                         argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
763                 if (deepen_not->nr) {
764                         argv_array_push(&av, "--not");
765                         for (i = 0; i < deepen_not->nr; i++) {
766                                 struct string_list_item *s = deepen_not->items + i;
767                                 argv_array_push(&av, s->string);
768                         }
769                         argv_array_push(&av, "--not");
770                 }
771                 for (i = 0; i < want_obj.nr; i++) {
772                         struct object *o = want_obj.objects[i].item;
773                         argv_array_push(&av, oid_to_hex(&o->oid));
774                 }
775                 deepen_by_rev_list(av.argc, av.argv, shallows);
776                 argv_array_clear(&av);
777                 ret = 1;
778         } else {
779                 if (shallows->nr > 0) {
780                         int i;
781                         for (i = 0; i < shallows->nr; i++)
782                                 register_shallow(&shallows->objects[i].item->oid);
783                 }
784         }
785
786         shallow_nr += shallows->nr;
787         return ret;
788 }
789
790 static int process_shallow(const char *line, struct object_array *shallows)
791 {
792         const char *arg;
793         if (skip_prefix(line, "shallow ", &arg)) {
794                 struct object_id oid;
795                 struct object *object;
796                 if (get_oid_hex(arg, &oid))
797                         die("invalid shallow line: %s", line);
798                 object = parse_object(&oid);
799                 if (!object)
800                         return 1;
801                 if (object->type != OBJ_COMMIT)
802                         die("invalid shallow object %s", oid_to_hex(&oid));
803                 if (!(object->flags & CLIENT_SHALLOW)) {
804                         object->flags |= CLIENT_SHALLOW;
805                         add_object_array(object, NULL, shallows);
806                 }
807                 return 1;
808         }
809
810         return 0;
811 }
812
813 static int process_deepen(const char *line, int *depth)
814 {
815         const char *arg;
816         if (skip_prefix(line, "deepen ", &arg)) {
817                 char *end = NULL;
818                 *depth = (int)strtol(arg, &end, 0);
819                 if (!end || *end || *depth <= 0)
820                         die("Invalid deepen: %s", line);
821                 return 1;
822         }
823
824         return 0;
825 }
826
827 static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
828 {
829         const char *arg;
830         if (skip_prefix(line, "deepen-since ", &arg)) {
831                 char *end = NULL;
832                 *deepen_since = parse_timestamp(arg, &end, 0);
833                 if (!end || *end || !deepen_since ||
834                     /* revisions.c's max_age -1 is special */
835                     *deepen_since == -1)
836                         die("Invalid deepen-since: %s", line);
837                 *deepen_rev_list = 1;
838                 return 1;
839         }
840         return 0;
841 }
842
843 static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
844 {
845         const char *arg;
846         if (skip_prefix(line, "deepen-not ", &arg)) {
847                 char *ref = NULL;
848                 struct object_id oid;
849                 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
850                         die("git upload-pack: ambiguous deepen-not: %s", line);
851                 string_list_append(deepen_not, ref);
852                 free(ref);
853                 *deepen_rev_list = 1;
854                 return 1;
855         }
856         return 0;
857 }
858
859 static void receive_needs(void)
860 {
861         struct object_array shallows = OBJECT_ARRAY_INIT;
862         struct string_list deepen_not = STRING_LIST_INIT_DUP;
863         int depth = 0;
864         int has_non_tip = 0;
865         timestamp_t deepen_since = 0;
866         int deepen_rev_list = 0;
867
868         shallow_nr = 0;
869         for (;;) {
870                 struct object *o;
871                 const char *features;
872                 struct object_id oid_buf;
873                 char *line = packet_read_line(0, NULL);
874                 const char *arg;
875
876                 reset_timeout();
877                 if (!line)
878                         break;
879
880                 if (process_shallow(line, &shallows))
881                         continue;
882                 if (process_deepen(line, &depth))
883                         continue;
884                 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
885                         continue;
886                 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
887                         continue;
888
889                 if (skip_prefix(line, "filter ", &arg)) {
890                         if (!filter_capability_requested)
891                                 die("git upload-pack: filtering capability not negotiated");
892                         parse_list_objects_filter(&filter_options, arg);
893                         continue;
894                 }
895
896                 if (!skip_prefix(line, "want ", &arg) ||
897                     get_oid_hex(arg, &oid_buf))
898                         die("git upload-pack: protocol error, "
899                             "expected to get sha, not '%s'", line);
900
901                 features = arg + 40;
902
903                 if (parse_feature_request(features, "deepen-relative"))
904                         deepen_relative = 1;
905                 if (parse_feature_request(features, "multi_ack_detailed"))
906                         multi_ack = 2;
907                 else if (parse_feature_request(features, "multi_ack"))
908                         multi_ack = 1;
909                 if (parse_feature_request(features, "no-done"))
910                         no_done = 1;
911                 if (parse_feature_request(features, "thin-pack"))
912                         use_thin_pack = 1;
913                 if (parse_feature_request(features, "ofs-delta"))
914                         use_ofs_delta = 1;
915                 if (parse_feature_request(features, "side-band-64k"))
916                         use_sideband = LARGE_PACKET_MAX;
917                 else if (parse_feature_request(features, "side-band"))
918                         use_sideband = DEFAULT_PACKET_MAX;
919                 if (parse_feature_request(features, "no-progress"))
920                         no_progress = 1;
921                 if (parse_feature_request(features, "include-tag"))
922                         use_include_tag = 1;
923                 if (parse_feature_request(features, "filter"))
924                         filter_capability_requested = 1;
925
926                 o = parse_object(&oid_buf);
927                 if (!o) {
928                         packet_write_fmt(1,
929                                          "ERR upload-pack: not our ref %s",
930                                          oid_to_hex(&oid_buf));
931                         die("git upload-pack: not our ref %s",
932                             oid_to_hex(&oid_buf));
933                 }
934                 if (!(o->flags & WANTED)) {
935                         o->flags |= WANTED;
936                         if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
937                               || is_our_ref(o)))
938                                 has_non_tip = 1;
939                         add_object_array(o, NULL, &want_obj);
940                 }
941         }
942
943         /*
944          * We have sent all our refs already, and the other end
945          * should have chosen out of them. When we are operating
946          * in the stateless RPC mode, however, their choice may
947          * have been based on the set of older refs advertised
948          * by another process that handled the initial request.
949          */
950         if (has_non_tip)
951                 check_non_tip();
952
953         if (!use_sideband && daemon_mode)
954                 no_progress = 1;
955
956         if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
957                 return;
958
959         if (send_shallow_list(depth, deepen_rev_list, deepen_since,
960                               &deepen_not, &shallows))
961                 packet_flush(1);
962         object_array_clear(&shallows);
963 }
964
965 /* return non-zero if the ref is hidden, otherwise 0 */
966 static int mark_our_ref(const char *refname, const char *refname_full,
967                         const struct object_id *oid)
968 {
969         struct object *o = lookup_unknown_object(oid->hash);
970
971         if (ref_is_hidden(refname, refname_full)) {
972                 o->flags |= HIDDEN_REF;
973                 return 1;
974         }
975         o->flags |= OUR_REF;
976         return 0;
977 }
978
979 static int check_ref(const char *refname_full, const struct object_id *oid,
980                      int flag, void *cb_data)
981 {
982         const char *refname = strip_namespace(refname_full);
983
984         mark_our_ref(refname, refname_full, oid);
985         return 0;
986 }
987
988 static void format_symref_info(struct strbuf *buf, struct string_list *symref)
989 {
990         struct string_list_item *item;
991
992         if (!symref->nr)
993                 return;
994         for_each_string_list_item(item, symref)
995                 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
996 }
997
998 static int send_ref(const char *refname, const struct object_id *oid,
999                     int flag, void *cb_data)
1000 {
1001         static const char *capabilities = "multi_ack thin-pack side-band"
1002                 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1003                 " deepen-relative no-progress include-tag multi_ack_detailed";
1004         const char *refname_nons = strip_namespace(refname);
1005         struct object_id peeled;
1006
1007         if (mark_our_ref(refname_nons, refname, oid))
1008                 return 0;
1009
1010         if (capabilities) {
1011                 struct strbuf symref_info = STRBUF_INIT;
1012
1013                 format_symref_info(&symref_info, cb_data);
1014                 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1015                              oid_to_hex(oid), refname_nons,
1016                              0, capabilities,
1017                              (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1018                                      " allow-tip-sha1-in-want" : "",
1019                              (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1020                                      " allow-reachable-sha1-in-want" : "",
1021                              stateless_rpc ? " no-done" : "",
1022                              symref_info.buf,
1023                              filter_advertise ? " filter" : "",
1024                              git_user_agent_sanitized());
1025                 strbuf_release(&symref_info);
1026         } else {
1027                 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1028         }
1029         capabilities = NULL;
1030         if (!peel_ref(refname, &peeled))
1031                 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1032         return 0;
1033 }
1034
1035 static int find_symref(const char *refname, const struct object_id *oid,
1036                        int flag, void *cb_data)
1037 {
1038         const char *symref_target;
1039         struct string_list_item *item;
1040
1041         if ((flag & REF_ISSYMREF) == 0)
1042                 return 0;
1043         symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1044         if (!symref_target || (flag & REF_ISSYMREF) == 0)
1045                 die("'%s' is a symref but it is not?", refname);
1046         item = string_list_append(cb_data, refname);
1047         item->util = xstrdup(symref_target);
1048         return 0;
1049 }
1050
1051 static int upload_pack_config(const char *var, const char *value, void *unused)
1052 {
1053         if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1054                 if (git_config_bool(var, value))
1055                         allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1056                 else
1057                         allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1058         } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1059                 if (git_config_bool(var, value))
1060                         allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1061                 else
1062                         allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1063         } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1064                 if (git_config_bool(var, value))
1065                         allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1066                 else
1067                         allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1068         } else if (!strcmp("uploadpack.keepalive", var)) {
1069                 keepalive = git_config_int(var, value);
1070                 if (!keepalive)
1071                         keepalive = -1;
1072         } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1073                 if (!strcmp("uploadpack.packobjectshook", var))
1074                         return git_config_string(&pack_objects_hook, var, value);
1075         } else if (!strcmp("uploadpack.allowfilter", var)) {
1076                 filter_advertise = git_config_bool(var, value);
1077         }
1078         return parse_hide_refs_config(var, value, "uploadpack");
1079 }
1080
1081 void upload_pack(struct upload_pack_options *options)
1082 {
1083         struct string_list symref = STRING_LIST_INIT_DUP;
1084
1085         stateless_rpc = options->stateless_rpc;
1086         timeout = options->timeout;
1087         daemon_mode = options->daemon_mode;
1088
1089         git_config(upload_pack_config, NULL);
1090
1091         head_ref_namespaced(find_symref, &symref);
1092
1093         if (options->advertise_refs || !stateless_rpc) {
1094                 reset_timeout();
1095                 head_ref_namespaced(send_ref, &symref);
1096                 for_each_namespaced_ref(send_ref, &symref);
1097                 advertise_shallow_grafts(1);
1098                 packet_flush(1);
1099         } else {
1100                 head_ref_namespaced(check_ref, NULL);
1101                 for_each_namespaced_ref(check_ref, NULL);
1102         }
1103         string_list_clear(&symref, 1);
1104         if (options->advertise_refs)
1105                 return;
1106
1107         receive_needs();
1108         if (want_obj.nr) {
1109                 get_common_commits();
1110                 create_pack_file();
1111         }
1112 }
1113
1114 struct upload_pack_data {
1115         struct object_array wants;
1116         struct oid_array haves;
1117
1118         struct object_array shallows;
1119         struct string_list deepen_not;
1120         int depth;
1121         timestamp_t deepen_since;
1122         int deepen_rev_list;
1123         int deepen_relative;
1124
1125         unsigned stateless_rpc : 1;
1126
1127         unsigned use_thin_pack : 1;
1128         unsigned use_ofs_delta : 1;
1129         unsigned no_progress : 1;
1130         unsigned use_include_tag : 1;
1131         unsigned done : 1;
1132 };
1133
1134 #define UPLOAD_PACK_DATA_INIT { OBJECT_ARRAY_INIT, OID_ARRAY_INIT, OBJECT_ARRAY_INIT, STRING_LIST_INIT_DUP, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
1135
1136 static void upload_pack_data_clear(struct upload_pack_data *data)
1137 {
1138         object_array_clear(&data->wants);
1139         oid_array_clear(&data->haves);
1140         object_array_clear(&data->shallows);
1141         string_list_clear(&data->deepen_not, 0);
1142 }
1143
1144 static int parse_want(const char *line)
1145 {
1146         const char *arg;
1147         if (skip_prefix(line, "want ", &arg)) {
1148                 struct object_id oid;
1149                 struct object *o;
1150
1151                 if (get_oid_hex(arg, &oid))
1152                         die("git upload-pack: protocol error, "
1153                             "expected to get oid, not '%s'", line);
1154
1155                 o = parse_object(&oid);
1156                 if (!o) {
1157                         packet_write_fmt(1,
1158                                          "ERR upload-pack: not our ref %s",
1159                                          oid_to_hex(&oid));
1160                         die("git upload-pack: not our ref %s",
1161                             oid_to_hex(&oid));
1162                 }
1163
1164                 if (!(o->flags & WANTED)) {
1165                         o->flags |= WANTED;
1166                         add_object_array(o, NULL, &want_obj);
1167                 }
1168
1169                 return 1;
1170         }
1171
1172         return 0;
1173 }
1174
1175 static int parse_have(const char *line, struct oid_array *haves)
1176 {
1177         const char *arg;
1178         if (skip_prefix(line, "have ", &arg)) {
1179                 struct object_id oid;
1180
1181                 if (get_oid_hex(arg, &oid))
1182                         die("git upload-pack: expected SHA1 object, got '%s'", arg);
1183                 oid_array_append(haves, &oid);
1184                 return 1;
1185         }
1186
1187         return 0;
1188 }
1189
1190 static void process_args(struct argv_array *args, struct upload_pack_data *data)
1191 {
1192         int i;
1193
1194         for (i = 0; i < args->argc; i++) {
1195                 const char *arg = args->argv[i];
1196
1197                 /* process want */
1198                 if (parse_want(arg))
1199                         continue;
1200                 /* process have line */
1201                 if (parse_have(arg, &data->haves))
1202                         continue;
1203
1204                 /* process args like thin-pack */
1205                 if (!strcmp(arg, "thin-pack")) {
1206                         use_thin_pack = 1;
1207                         continue;
1208                 }
1209                 if (!strcmp(arg, "ofs-delta")) {
1210                         use_ofs_delta = 1;
1211                         continue;
1212                 }
1213                 if (!strcmp(arg, "no-progress")) {
1214                         no_progress = 1;
1215                         continue;
1216                 }
1217                 if (!strcmp(arg, "include-tag")) {
1218                         use_include_tag = 1;
1219                         continue;
1220                 }
1221                 if (!strcmp(arg, "done")) {
1222                         data->done = 1;
1223                         continue;
1224                 }
1225
1226                 /* Shallow related arguments */
1227                 if (process_shallow(arg, &data->shallows))
1228                         continue;
1229                 if (process_deepen(arg, &data->depth))
1230                         continue;
1231                 if (process_deepen_since(arg, &data->deepen_since,
1232                                          &data->deepen_rev_list))
1233                         continue;
1234                 if (process_deepen_not(arg, &data->deepen_not,
1235                                        &data->deepen_rev_list))
1236                         continue;
1237                 if (!strcmp(arg, "deepen-relative")) {
1238                         data->deepen_relative = 1;
1239                         continue;
1240                 }
1241
1242                 /* ignore unknown lines maybe? */
1243                 die("unexpect line: '%s'", arg);
1244         }
1245 }
1246
1247 static void read_haves(struct upload_pack_data *data)
1248 {
1249         struct packet_reader reader;
1250         packet_reader_init(&reader, 0, NULL, 0,
1251                            PACKET_READ_CHOMP_NEWLINE);
1252
1253         while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
1254
1255                 if (parse_have(reader.line, &data->haves))
1256                         continue;
1257                 if (!strcmp(reader.line, "done")) {
1258                         data->done = 1;
1259                         continue;
1260                 }
1261         }
1262         if (reader.status != PACKET_READ_FLUSH)
1263                 die("ERROR");
1264 }
1265
1266 static int process_haves(struct oid_array *haves, struct oid_array *common)
1267 {
1268         int i;
1269
1270         /* Process haves */
1271         for (i = 0; i < haves->nr; i++) {
1272                 const struct object_id *oid = &haves->oid[i];
1273                 struct object *o;
1274                 int we_knew_they_have = 0;
1275
1276                 if (!has_object_file(oid))
1277                         continue;
1278
1279                 oid_array_append(common, oid);
1280
1281                 o = parse_object(oid);
1282                 if (!o)
1283                         die("oops (%s)", oid_to_hex(oid));
1284                 if (o->type == OBJ_COMMIT) {
1285                         struct commit_list *parents;
1286                         struct commit *commit = (struct commit *)o;
1287                         if (o->flags & THEY_HAVE)
1288                                 we_knew_they_have = 1;
1289                         else
1290                                 o->flags |= THEY_HAVE;
1291                         if (!oldest_have || (commit->date < oldest_have))
1292                                 oldest_have = commit->date;
1293                         for (parents = commit->parents;
1294                              parents;
1295                              parents = parents->next)
1296                                 parents->item->object.flags |= THEY_HAVE;
1297                 }
1298                 if (!we_knew_they_have)
1299                         add_object_array(o, NULL, &have_obj);
1300         }
1301
1302         return 0;
1303 }
1304
1305 static int send_acks(struct oid_array *acks, struct strbuf *response)
1306 {
1307         int i;
1308
1309         packet_buf_write(response, "acknowledgments\n");
1310
1311         /* Send Acks */
1312         if (!acks->nr)
1313                 packet_buf_write(response, "NAK\n");
1314
1315         for (i = 0; i < acks->nr; i++) {
1316                 packet_buf_write(response, "ACK %s\n",
1317                                  oid_to_hex(&acks->oid[i]));
1318         }
1319
1320         if (ok_to_give_up()) {
1321                 /* Send Ready */
1322                 packet_buf_write(response, "ready\n");
1323                 return 1;
1324         }
1325
1326         return 0;
1327 }
1328
1329 static int process_haves_and_send_acks(struct upload_pack_data *data)
1330 {
1331         struct oid_array common = OID_ARRAY_INIT;
1332         struct strbuf response = STRBUF_INIT;
1333         int ret = 0;
1334
1335         process_haves(&data->haves, &common);
1336         if (data->done) {
1337                 ret = 1;
1338         } else if (send_acks(&common, &response)) {
1339                 packet_buf_delim(&response);
1340                 ret = 1;
1341         } else {
1342                 /* Add Flush */
1343                 packet_buf_flush(&response);
1344                 ret = 0;
1345         }
1346
1347         /* Send response */
1348         write_or_die(1, response.buf, response.len);
1349         strbuf_release(&response);
1350
1351         oid_array_clear(&data->haves);
1352         oid_array_clear(&common);
1353         return ret;
1354 }
1355
1356 static void send_shallow_info(struct upload_pack_data *data)
1357 {
1358         /* No shallow info needs to be sent */
1359         if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1360             !is_repository_shallow())
1361                 return;
1362
1363         packet_write_fmt(1, "shallow-info\n");
1364
1365         if (!send_shallow_list(data->depth, data->deepen_rev_list,
1366                                data->deepen_since, &data->deepen_not,
1367                                &data->shallows) && is_repository_shallow())
1368                 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1369
1370         packet_delim(1);
1371 }
1372
1373 enum fetch_state {
1374         FETCH_PROCESS_ARGS = 0,
1375         FETCH_READ_HAVES,
1376         FETCH_SEND_ACKS,
1377         FETCH_SEND_PACK,
1378         FETCH_DONE,
1379 };
1380
1381 int upload_pack_v2(struct repository *r, struct argv_array *keys,
1382                    struct argv_array *args)
1383 {
1384         enum fetch_state state = FETCH_PROCESS_ARGS;
1385         struct upload_pack_data data = UPLOAD_PACK_DATA_INIT;
1386         use_sideband = LARGE_PACKET_MAX;
1387
1388         while (state != FETCH_DONE) {
1389                 switch (state) {
1390                 case FETCH_PROCESS_ARGS:
1391                         process_args(args, &data);
1392
1393                         if (!want_obj.nr) {
1394                                 /*
1395                                  * Request didn't contain any 'want' lines,
1396                                  * guess they didn't want anything.
1397                                  */
1398                                 state = FETCH_DONE;
1399                         } else if (data.haves.nr) {
1400                                 /*
1401                                  * Request had 'have' lines, so lets ACK them.
1402                                  */
1403                                 state = FETCH_SEND_ACKS;
1404                         } else {
1405                                 /*
1406                                  * Request had 'want's but no 'have's so we can
1407                                  * immedietly go to construct and send a pack.
1408                                  */
1409                                 state = FETCH_SEND_PACK;
1410                         }
1411                         break;
1412                 case FETCH_READ_HAVES:
1413                         read_haves(&data);
1414                         state = FETCH_SEND_ACKS;
1415                         break;
1416                 case FETCH_SEND_ACKS:
1417                         if (process_haves_and_send_acks(&data))
1418                                 state = FETCH_SEND_PACK;
1419                         else
1420                                 state = FETCH_DONE;
1421                         break;
1422                 case FETCH_SEND_PACK:
1423                         send_shallow_info(&data);
1424
1425                         packet_write_fmt(1, "packfile\n");
1426                         create_pack_file();
1427                         state = FETCH_DONE;
1428                         break;
1429                 case FETCH_DONE:
1430                         continue;
1431                 }
1432         }
1433
1434         upload_pack_data_clear(&data);
1435         return 0;
1436 }
1437
1438 int upload_pack_advertise(struct repository *r,
1439                           struct strbuf *value)
1440 {
1441         if (value)
1442                 strbuf_addstr(value, "shallow");
1443         return 1;
1444 }