Coverage Report

Created: 2024-09-16 06:10

/src/git/upload-pack.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "config.h"
5
#include "environment.h"
6
#include "gettext.h"
7
#include "hex.h"
8
#include "refs.h"
9
#include "pkt-line.h"
10
#include "sideband.h"
11
#include "repository.h"
12
#include "object-store-ll.h"
13
#include "oid-array.h"
14
#include "object.h"
15
#include "commit.h"
16
#include "diff.h"
17
#include "revision.h"
18
#include "list-objects-filter-options.h"
19
#include "run-command.h"
20
#include "connect.h"
21
#include "sigchain.h"
22
#include "version.h"
23
#include "string-list.h"
24
#include "strvec.h"
25
#include "trace2.h"
26
#include "protocol.h"
27
#include "upload-pack.h"
28
#include "commit-graph.h"
29
#include "commit-reach.h"
30
#include "shallow.h"
31
#include "write-or-die.h"
32
#include "json-writer.h"
33
#include "strmap.h"
34
35
/* Remember to update object flag allocation in object.h */
36
0
#define THEY_HAVE (1u << 11)
37
0
#define OUR_REF   (1u << 12)
38
0
#define WANTED    (1u << 13)
39
0
#define COMMON_KNOWN  (1u << 14)
40
41
0
#define SHALLOW   (1u << 16)
42
0
#define NOT_SHALLOW (1u << 17)
43
0
#define CLIENT_SHALLOW  (1u << 18)
44
0
#define HIDDEN_REF  (1u << 19)
45
46
0
#define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
47
0
    NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
48
49
/* Enum for allowed unadvertised object request (UOR) */
50
enum allow_uor {
51
  /* Allow specifying sha1 if it is a ref tip. */
52
  ALLOW_TIP_SHA1 = 0x01,
53
  /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54
  ALLOW_REACHABLE_SHA1 = 0x02,
55
  /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56
  ALLOW_ANY_SHA1 = 0x07
57
};
58
59
/*
60
 * Please annotate, and if possible group together, fields used only
61
 * for protocol v0 or only for protocol v2.
62
 */
63
struct upload_pack_data {
64
  struct string_list symref;        /* v0 only */
65
  struct object_array want_obj;
66
  struct object_array have_obj;
67
  struct strmap wanted_refs;        /* v2 only */
68
  struct strvec hidden_refs;
69
70
  struct object_array shallows;
71
  struct oidset deepen_not;
72
  struct object_array extra_edge_obj;
73
  int depth;
74
  timestamp_t deepen_since;
75
  int deepen_rev_list;
76
  int deepen_relative;
77
  int keepalive;
78
  int shallow_nr;
79
  timestamp_t oldest_have;
80
81
  unsigned int timeout;         /* v0 only */
82
  enum {
83
    NO_MULTI_ACK = 0,
84
    MULTI_ACK = 1,
85
    MULTI_ACK_DETAILED = 2
86
  } multi_ack;            /* v0 only */
87
88
  /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
89
  int use_sideband;
90
91
  struct string_list uri_protocols;
92
  enum allow_uor allow_uor;
93
94
  struct list_objects_filter_options filter_options;
95
  struct string_list allowed_filters;
96
97
  struct packet_writer writer;
98
99
  char *pack_objects_hook;
100
101
  unsigned stateless_rpc : 1;       /* v0 only */
102
  unsigned no_done : 1;         /* v0 only */
103
  unsigned daemon_mode : 1;       /* v0 only */
104
  unsigned filter_capability_requested : 1;   /* v0 only */
105
106
  unsigned use_thin_pack : 1;
107
  unsigned use_ofs_delta : 1;
108
  unsigned no_progress : 1;
109
  unsigned use_include_tag : 1;
110
  unsigned wait_for_done : 1;
111
  unsigned allow_filter : 1;
112
  unsigned allow_filter_fallback : 1;
113
  unsigned long tree_filter_max_depth;
114
115
  unsigned done : 1;          /* v2 only */
116
  unsigned allow_ref_in_want : 1;       /* v2 only */
117
  unsigned allow_sideband_all : 1;      /* v2 only */
118
  unsigned seen_haves : 1;        /* v2 only */
119
  unsigned allow_packfile_uris : 1;     /* v2 only */
120
  unsigned advertise_sid : 1;
121
  unsigned sent_capabilities : 1;
122
};
123
124
static void upload_pack_data_init(struct upload_pack_data *data)
125
0
{
126
0
  struct string_list symref = STRING_LIST_INIT_DUP;
127
0
  struct strmap wanted_refs = STRMAP_INIT;
128
0
  struct strvec hidden_refs = STRVEC_INIT;
129
0
  struct object_array want_obj = OBJECT_ARRAY_INIT;
130
0
  struct object_array have_obj = OBJECT_ARRAY_INIT;
131
0
  struct object_array shallows = OBJECT_ARRAY_INIT;
132
0
  struct oidset deepen_not = OID_ARRAY_INIT;
133
0
  struct string_list uri_protocols = STRING_LIST_INIT_DUP;
134
0
  struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
135
0
  struct string_list allowed_filters = STRING_LIST_INIT_DUP;
136
137
0
  memset(data, 0, sizeof(*data));
138
0
  data->symref = symref;
139
0
  data->wanted_refs = wanted_refs;
140
0
  data->hidden_refs = hidden_refs;
141
0
  data->want_obj = want_obj;
142
0
  data->have_obj = have_obj;
143
0
  data->shallows = shallows;
144
0
  data->deepen_not = deepen_not;
145
0
  data->uri_protocols = uri_protocols;
146
0
  data->extra_edge_obj = extra_edge_obj;
147
0
  data->allowed_filters = allowed_filters;
148
0
  data->allow_filter_fallback = 1;
149
0
  data->tree_filter_max_depth = ULONG_MAX;
150
0
  packet_writer_init(&data->writer, 1);
151
0
  list_objects_filter_init(&data->filter_options);
152
153
0
  data->keepalive = 5;
154
0
  data->advertise_sid = 0;
155
0
}
156
157
static void upload_pack_data_clear(struct upload_pack_data *data)
158
0
{
159
0
  string_list_clear(&data->symref, 1);
160
0
  strmap_clear(&data->wanted_refs, 1);
161
0
  strvec_clear(&data->hidden_refs);
162
0
  object_array_clear(&data->want_obj);
163
0
  object_array_clear(&data->have_obj);
164
0
  object_array_clear(&data->shallows);
165
0
  oidset_clear(&data->deepen_not);
166
0
  object_array_clear(&data->extra_edge_obj);
167
0
  list_objects_filter_release(&data->filter_options);
168
0
  string_list_clear(&data->allowed_filters, 0);
169
170
0
  free((char *)data->pack_objects_hook);
171
0
}
172
173
static void reset_timeout(unsigned int timeout)
174
0
{
175
0
  alarm(timeout);
176
0
}
177
178
static void send_client_data(int fd, const char *data, ssize_t sz,
179
           int use_sideband)
180
0
{
181
0
  if (use_sideband) {
182
0
    send_sideband(1, fd, data, sz, use_sideband);
183
0
    return;
184
0
  }
185
0
  if (fd == 3)
186
    /* emergency quit */
187
0
    fd = 2;
188
0
  if (fd == 2) {
189
    /* XXX: are we happy to lose stuff here? */
190
0
    xwrite(fd, data, sz);
191
0
    return;
192
0
  }
193
0
  write_or_die(fd, data, sz);
194
0
}
195
196
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
197
0
{
198
0
  FILE *fp = cb_data;
199
0
  if (graft->nr_parent == -1)
200
0
    fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
201
0
  return 0;
202
0
}
203
204
struct output_state {
205
  /*
206
   * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
207
   * sideband-64k the band designator takes up 1 byte of space. Because
208
   * relay_pack_data keeps the last byte to itself, we make the buffer 1
209
   * byte bigger than the intended maximum write size.
210
   */
211
  char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
212
  int used;
213
  unsigned packfile_uris_started : 1;
214
  unsigned packfile_started : 1;
215
};
216
217
static int relay_pack_data(int pack_objects_out, struct output_state *os,
218
         int use_sideband, int write_packfile_line)
219
0
{
220
  /*
221
   * We keep the last byte to ourselves
222
   * in case we detect broken rev-list, so that we
223
   * can leave the stream corrupted.  This is
224
   * unfortunate -- unpack-objects would happily
225
   * accept a valid packdata with trailing garbage,
226
   * so appending garbage after we pass all the
227
   * pack data is not good enough to signal
228
   * breakage to downstream.
229
   */
230
0
  ssize_t readsz;
231
232
0
  readsz = xread(pack_objects_out, os->buffer + os->used,
233
0
           sizeof(os->buffer) - os->used);
234
0
  if (readsz < 0) {
235
0
    return readsz;
236
0
  }
237
0
  os->used += readsz;
238
239
0
  while (!os->packfile_started) {
240
0
    char *p;
241
0
    if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
242
0
      os->packfile_started = 1;
243
0
      if (write_packfile_line) {
244
0
        if (os->packfile_uris_started)
245
0
          packet_delim(1);
246
0
        packet_write_fmt(1, "\1packfile\n");
247
0
      }
248
0
      break;
249
0
    }
250
0
    if ((p = memchr(os->buffer, '\n', os->used))) {
251
0
      if (!os->packfile_uris_started) {
252
0
        os->packfile_uris_started = 1;
253
0
        if (!write_packfile_line)
254
0
          BUG("packfile_uris requires sideband-all");
255
0
        packet_write_fmt(1, "\1packfile-uris\n");
256
0
      }
257
0
      *p = '\0';
258
0
      packet_write_fmt(1, "\1%s\n", os->buffer);
259
260
0
      os->used -= p - os->buffer + 1;
261
0
      memmove(os->buffer, p + 1, os->used);
262
0
    } else {
263
      /*
264
       * Incomplete line.
265
       */
266
0
      return readsz;
267
0
    }
268
0
  }
269
270
0
  if (os->used > 1) {
271
0
    send_client_data(1, os->buffer, os->used - 1, use_sideband);
272
0
    os->buffer[0] = os->buffer[os->used - 1];
273
0
    os->used = 1;
274
0
  } else {
275
0
    send_client_data(1, os->buffer, os->used, use_sideband);
276
0
    os->used = 0;
277
0
  }
278
279
0
  return readsz;
280
0
}
281
282
static void create_pack_file(struct upload_pack_data *pack_data,
283
           const struct string_list *uri_protocols)
284
0
{
285
0
  struct child_process pack_objects = CHILD_PROCESS_INIT;
286
0
  struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
287
0
  char progress[128];
288
0
  char abort_msg[] = "aborting due to possible repository "
289
0
    "corruption on the remote side.";
290
0
  ssize_t sz;
291
0
  int i;
292
0
  FILE *pipe_fd;
293
294
0
  if (!pack_data->pack_objects_hook)
295
0
    pack_objects.git_cmd = 1;
296
0
  else {
297
0
    strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
298
0
    strvec_push(&pack_objects.args, "git");
299
0
    pack_objects.use_shell = 1;
300
0
  }
301
302
0
  if (pack_data->shallow_nr) {
303
0
    strvec_push(&pack_objects.args, "--shallow-file");
304
0
    strvec_push(&pack_objects.args, "");
305
0
  }
306
0
  strvec_push(&pack_objects.args, "pack-objects");
307
0
  strvec_push(&pack_objects.args, "--revs");
308
0
  if (pack_data->use_thin_pack)
309
0
    strvec_push(&pack_objects.args, "--thin");
310
311
0
  strvec_push(&pack_objects.args, "--stdout");
312
0
  if (pack_data->shallow_nr)
313
0
    strvec_push(&pack_objects.args, "--shallow");
314
0
  if (!pack_data->no_progress)
315
0
    strvec_push(&pack_objects.args, "--progress");
316
0
  if (pack_data->use_ofs_delta)
317
0
    strvec_push(&pack_objects.args, "--delta-base-offset");
318
0
  if (pack_data->use_include_tag)
319
0
    strvec_push(&pack_objects.args, "--include-tag");
320
0
  if (pack_data->filter_options.choice) {
321
0
    const char *spec =
322
0
      expand_list_objects_filter_spec(&pack_data->filter_options);
323
0
    strvec_pushf(&pack_objects.args, "--filter=%s", spec);
324
0
  }
325
0
  if (uri_protocols) {
326
0
    for (i = 0; i < uri_protocols->nr; i++)
327
0
      strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
328
0
           uri_protocols->items[i].string);
329
0
  }
330
331
0
  pack_objects.in = -1;
332
0
  pack_objects.out = -1;
333
0
  pack_objects.err = -1;
334
0
  pack_objects.clean_on_exit = 1;
335
336
0
  if (start_command(&pack_objects))
337
0
    die("git upload-pack: unable to fork git-pack-objects");
338
339
0
  pipe_fd = xfdopen(pack_objects.in, "w");
340
341
0
  if (pack_data->shallow_nr)
342
0
    for_each_commit_graft(write_one_shallow, pipe_fd);
343
344
0
  for (i = 0; i < pack_data->want_obj.nr; i++)
345
0
    fprintf(pipe_fd, "%s\n",
346
0
      oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
347
0
  fprintf(pipe_fd, "--not\n");
348
0
  for (i = 0; i < pack_data->have_obj.nr; i++)
349
0
    fprintf(pipe_fd, "%s\n",
350
0
      oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
351
0
  for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
352
0
    fprintf(pipe_fd, "%s\n",
353
0
      oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
354
0
  fprintf(pipe_fd, "\n");
355
0
  fflush(pipe_fd);
356
0
  fclose(pipe_fd);
357
358
  /* We read from pack_objects.err to capture stderr output for
359
   * progress bar, and pack_objects.out to capture the pack data.
360
   */
361
362
0
  while (1) {
363
0
    struct pollfd pfd[2];
364
0
    int pe, pu, pollsize, polltimeout;
365
0
    int ret;
366
367
0
    reset_timeout(pack_data->timeout);
368
369
0
    pollsize = 0;
370
0
    pe = pu = -1;
371
372
0
    if (0 <= pack_objects.out) {
373
0
      pfd[pollsize].fd = pack_objects.out;
374
0
      pfd[pollsize].events = POLLIN;
375
0
      pu = pollsize;
376
0
      pollsize++;
377
0
    }
378
0
    if (0 <= pack_objects.err) {
379
0
      pfd[pollsize].fd = pack_objects.err;
380
0
      pfd[pollsize].events = POLLIN;
381
0
      pe = pollsize;
382
0
      pollsize++;
383
0
    }
384
385
0
    if (!pollsize)
386
0
      break;
387
388
0
    polltimeout = pack_data->keepalive < 0
389
0
      ? -1
390
0
      : 1000 * pack_data->keepalive;
391
392
0
    ret = poll(pfd, pollsize, polltimeout);
393
394
0
    if (ret < 0) {
395
0
      if (errno != EINTR) {
396
0
        error_errno("poll failed, resuming");
397
0
        sleep(1);
398
0
      }
399
0
      continue;
400
0
    }
401
0
    if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
402
      /* Status ready; we ship that in the side-band
403
       * or dump to the standard error.
404
       */
405
0
      sz = xread(pack_objects.err, progress,
406
0
          sizeof(progress));
407
0
      if (0 < sz)
408
0
        send_client_data(2, progress, sz,
409
0
             pack_data->use_sideband);
410
0
      else if (sz == 0) {
411
0
        close(pack_objects.err);
412
0
        pack_objects.err = -1;
413
0
      }
414
0
      else
415
0
        goto fail;
416
      /* give priority to status messages */
417
0
      continue;
418
0
    }
419
0
    if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
420
0
      int result = relay_pack_data(pack_objects.out,
421
0
                 output_state,
422
0
                 pack_data->use_sideband,
423
0
                 !!uri_protocols);
424
425
0
      if (result == 0) {
426
0
        close(pack_objects.out);
427
0
        pack_objects.out = -1;
428
0
      } else if (result < 0) {
429
0
        goto fail;
430
0
      }
431
0
    }
432
433
    /*
434
     * We hit the keepalive timeout without saying anything; send
435
     * an empty message on the data sideband just to let the other
436
     * side know we're still working on it, but don't have any data
437
     * yet.
438
     *
439
     * If we don't have a sideband channel, there's no room in the
440
     * protocol to say anything, so those clients are just out of
441
     * luck.
442
     */
443
0
    if (!ret && pack_data->use_sideband) {
444
0
      static const char buf[] = "0005\1";
445
0
      write_or_die(1, buf, 5);
446
0
    }
447
0
  }
448
449
0
  if (finish_command(&pack_objects)) {
450
0
    error("git upload-pack: git-pack-objects died with error.");
451
0
    goto fail;
452
0
  }
453
454
  /* flush the data */
455
0
  if (output_state->used > 0) {
456
0
    send_client_data(1, output_state->buffer, output_state->used,
457
0
         pack_data->use_sideband);
458
0
    fprintf(stderr, "flushed.\n");
459
0
  }
460
0
  free(output_state);
461
0
  if (pack_data->use_sideband)
462
0
    packet_flush(1);
463
0
  return;
464
465
0
 fail:
466
0
  free(output_state);
467
0
  send_client_data(3, abort_msg, strlen(abort_msg),
468
0
       pack_data->use_sideband);
469
0
  die("git upload-pack: %s", abort_msg);
470
0
}
471
472
static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
473
0
{
474
0
  int we_knew_they_have = 0;
475
0
  struct object *o = parse_object_with_flags(the_repository, oid,
476
0
               PARSE_OBJECT_SKIP_HASH_CHECK |
477
0
               PARSE_OBJECT_DISCARD_TREE);
478
479
0
  if (!o)
480
0
    die("oops (%s)", oid_to_hex(oid));
481
0
  if (o->type == OBJ_COMMIT) {
482
0
    struct commit_list *parents;
483
0
    struct commit *commit = (struct commit *)o;
484
0
    if (o->flags & THEY_HAVE)
485
0
      we_knew_they_have = 1;
486
0
    else
487
0
      o->flags |= THEY_HAVE;
488
0
    if (!data->oldest_have || (commit->date < data->oldest_have))
489
0
      data->oldest_have = commit->date;
490
0
    for (parents = commit->parents;
491
0
         parents;
492
0
         parents = parents->next)
493
0
      parents->item->object.flags |= THEY_HAVE;
494
0
  }
495
0
  if (!we_knew_they_have) {
496
0
    add_object_array(o, NULL, &data->have_obj);
497
0
    return 1;
498
0
  }
499
0
  return 0;
500
0
}
501
502
static int got_oid(struct upload_pack_data *data,
503
       const char *hex, struct object_id *oid)
504
0
{
505
0
  if (get_oid_hex(hex, oid))
506
0
    die("git upload-pack: expected SHA1 object, got '%s'", hex);
507
0
  if (!repo_has_object_file_with_flags(the_repository, oid,
508
0
               OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
509
0
    return -1;
510
0
  return do_got_oid(data, oid);
511
0
}
512
513
static int ok_to_give_up(struct upload_pack_data *data)
514
0
{
515
0
  timestamp_t min_generation = GENERATION_NUMBER_ZERO;
516
517
0
  if (!data->have_obj.nr)
518
0
    return 0;
519
520
0
  return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
521
0
              COMMON_KNOWN, data->oldest_have,
522
0
              min_generation);
523
0
}
524
525
static int get_common_commits(struct upload_pack_data *data,
526
            struct packet_reader *reader)
527
0
{
528
0
  struct object_id oid;
529
0
  char last_hex[GIT_MAX_HEXSZ + 1];
530
0
  int got_common = 0;
531
0
  int got_other = 0;
532
0
  int sent_ready = 0;
533
534
0
  for (;;) {
535
0
    const char *arg;
536
537
0
    reset_timeout(data->timeout);
538
539
0
    if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
540
0
      if (data->multi_ack == MULTI_ACK_DETAILED
541
0
          && got_common
542
0
          && !got_other
543
0
          && ok_to_give_up(data)) {
544
0
        sent_ready = 1;
545
0
        packet_write_fmt(1, "ACK %s ready\n", last_hex);
546
0
      }
547
0
      if (data->have_obj.nr == 0 || data->multi_ack)
548
0
        packet_write_fmt(1, "NAK\n");
549
550
0
      if (data->no_done && sent_ready) {
551
0
        packet_write_fmt(1, "ACK %s\n", last_hex);
552
0
        return 0;
553
0
      }
554
0
      if (data->stateless_rpc)
555
0
        exit(0);
556
0
      got_common = 0;
557
0
      got_other = 0;
558
0
      continue;
559
0
    }
560
0
    if (skip_prefix(reader->line, "have ", &arg)) {
561
0
      switch (got_oid(data, arg, &oid)) {
562
0
      case -1: /* they have what we do not */
563
0
        got_other = 1;
564
0
        if (data->multi_ack
565
0
            && ok_to_give_up(data)) {
566
0
          const char *hex = oid_to_hex(&oid);
567
0
          if (data->multi_ack == MULTI_ACK_DETAILED) {
568
0
            sent_ready = 1;
569
0
            packet_write_fmt(1, "ACK %s ready\n", hex);
570
0
          } else
571
0
            packet_write_fmt(1, "ACK %s continue\n", hex);
572
0
        }
573
0
        break;
574
0
      default:
575
0
        got_common = 1;
576
0
        oid_to_hex_r(last_hex, &oid);
577
0
        if (data->multi_ack == MULTI_ACK_DETAILED)
578
0
          packet_write_fmt(1, "ACK %s common\n", last_hex);
579
0
        else if (data->multi_ack)
580
0
          packet_write_fmt(1, "ACK %s continue\n", last_hex);
581
0
        else if (data->have_obj.nr == 1)
582
0
          packet_write_fmt(1, "ACK %s\n", last_hex);
583
0
        break;
584
0
      }
585
0
      continue;
586
0
    }
587
0
    if (!strcmp(reader->line, "done")) {
588
0
      if (data->have_obj.nr > 0) {
589
0
        if (data->multi_ack)
590
0
          packet_write_fmt(1, "ACK %s\n", last_hex);
591
0
        return 0;
592
0
      }
593
0
      packet_write_fmt(1, "NAK\n");
594
0
      return -1;
595
0
    }
596
0
    die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
597
0
  }
598
0
}
599
600
static int allow_hidden_refs(enum allow_uor allow_uor)
601
0
{
602
0
  if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
603
0
    return 1;
604
0
  return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
605
0
}
606
607
static void for_each_namespaced_ref_1(each_ref_fn fn,
608
              struct upload_pack_data *data)
609
0
{
610
0
  const char **excludes = NULL;
611
  /*
612
   * If `data->allow_uor` allows fetching hidden refs, we need to
613
   * mark all references (including hidden ones), to check in
614
   * `is_our_ref()` below.
615
   *
616
   * Otherwise, we only care about whether each reference's object
617
   * has the OUR_REF bit set or not, so do not need to visit
618
   * hidden references.
619
   */
620
0
  if (allow_hidden_refs(data->allow_uor))
621
0
    excludes = hidden_refs_to_excludes(&data->hidden_refs);
622
623
0
  refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
624
0
             excludes, fn, data);
625
0
}
626
627
628
static int is_our_ref(struct object *o, enum allow_uor allow_uor)
629
0
{
630
0
  return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
631
0
}
632
633
/*
634
 * on successful case, it's up to the caller to close cmd->out
635
 */
636
static int do_reachable_revlist(struct child_process *cmd,
637
        struct object_array *src,
638
        struct object_array *reachable,
639
        enum allow_uor allow_uor)
640
0
{
641
0
  struct object *o;
642
0
  FILE *cmd_in = NULL;
643
0
  int i;
644
645
0
  strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
646
0
  cmd->git_cmd = 1;
647
0
  cmd->no_stderr = 1;
648
0
  cmd->in = -1;
649
0
  cmd->out = -1;
650
651
  /*
652
   * If the next rev-list --stdin encounters an unknown commit,
653
   * it terminates, which will cause SIGPIPE in the write loop
654
   * below.
655
   */
656
0
  sigchain_push(SIGPIPE, SIG_IGN);
657
658
0
  if (start_command(cmd))
659
0
    goto error;
660
661
0
  cmd_in = xfdopen(cmd->in, "w");
662
663
0
  for (i = get_max_object_index(); 0 < i; ) {
664
0
    o = get_indexed_object(--i);
665
0
    if (!o)
666
0
      continue;
667
0
    if (reachable && o->type == OBJ_COMMIT)
668
0
      o->flags &= ~TMP_MARK;
669
0
    if (!is_our_ref(o, allow_uor))
670
0
      continue;
671
0
    if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
672
0
      goto error;
673
0
  }
674
0
  for (i = 0; i < src->nr; i++) {
675
0
    o = src->objects[i].item;
676
0
    if (is_our_ref(o, allow_uor)) {
677
0
      if (reachable)
678
0
        add_object_array(o, NULL, reachable);
679
0
      continue;
680
0
    }
681
0
    if (reachable && o->type == OBJ_COMMIT)
682
0
      o->flags |= TMP_MARK;
683
0
    if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
684
0
      goto error;
685
0
  }
686
0
  if (ferror(cmd_in) || fflush(cmd_in))
687
0
    goto error;
688
0
  fclose(cmd_in);
689
0
  cmd->in = -1;
690
0
  sigchain_pop(SIGPIPE);
691
692
0
  return 0;
693
694
0
error:
695
0
  sigchain_pop(SIGPIPE);
696
697
0
  if (cmd_in)
698
0
    fclose(cmd_in);
699
0
  if (cmd->out >= 0)
700
0
    close(cmd->out);
701
0
  return -1;
702
0
}
703
704
static int get_reachable_list(struct upload_pack_data *data,
705
            struct object_array *reachable)
706
0
{
707
0
  struct child_process cmd = CHILD_PROCESS_INIT;
708
0
  int i;
709
0
  struct object *o;
710
0
  char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
711
0
  const unsigned hexsz = the_hash_algo->hexsz;
712
0
  int ret;
713
714
0
  if (do_reachable_revlist(&cmd, &data->shallows, reachable,
715
0
         data->allow_uor) < 0) {
716
0
    ret = -1;
717
0
    goto out;
718
0
  }
719
720
0
  while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
721
0
    struct object_id oid;
722
0
    const char *p;
723
724
0
    if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
725
0
      break;
726
727
0
    o = lookup_object(the_repository, &oid);
728
0
    if (o && o->type == OBJ_COMMIT) {
729
0
      o->flags &= ~TMP_MARK;
730
0
    }
731
0
  }
732
0
  for (i = get_max_object_index(); 0 < i; i--) {
733
0
    o = get_indexed_object(i - 1);
734
0
    if (o && o->type == OBJ_COMMIT &&
735
0
        (o->flags & TMP_MARK)) {
736
0
      add_object_array(o, NULL, reachable);
737
0
        o->flags &= ~TMP_MARK;
738
0
    }
739
0
  }
740
0
  close(cmd.out);
741
742
0
  if (finish_command(&cmd)) {
743
0
    ret = -1;
744
0
    goto out;
745
0
  }
746
747
0
  ret = 0;
748
749
0
out:
750
0
  child_process_clear(&cmd);
751
0
  return ret;
752
0
}
753
754
static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
755
0
{
756
0
  struct child_process cmd = CHILD_PROCESS_INIT;
757
0
  char buf[1];
758
0
  int i;
759
760
0
  if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
761
0
    goto error;
762
763
  /*
764
   * The commits out of the rev-list are not ancestors of
765
   * our ref.
766
   */
767
0
  i = read_in_full(cmd.out, buf, 1);
768
0
  if (i)
769
0
    goto error;
770
0
  close(cmd.out);
771
0
  cmd.out = -1;
772
773
  /*
774
   * rev-list may have died by encountering a bad commit
775
   * in the history, in which case we do want to bail out
776
   * even when it showed no commit.
777
   */
778
0
  if (finish_command(&cmd))
779
0
    goto error;
780
781
  /* All the non-tip ones are ancestors of what we advertised */
782
0
  return 0;
783
784
0
error:
785
0
  if (cmd.out >= 0)
786
0
    close(cmd.out);
787
0
  child_process_clear(&cmd);
788
0
  return 1;
789
0
}
790
791
static void check_non_tip(struct upload_pack_data *data)
792
0
{
793
0
  int i;
794
795
  /*
796
   * In the normal in-process case without
797
   * uploadpack.allowReachableSHA1InWant,
798
   * non-tip requests can never happen.
799
   */
800
0
  if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
801
0
    goto error;
802
0
  if (!has_unreachable(&data->want_obj, data->allow_uor))
803
    /* All the non-tip ones are ancestors of what we advertised */
804
0
    return;
805
806
0
error:
807
  /* Pick one of them (we know there at least is one) */
808
0
  for (i = 0; i < data->want_obj.nr; i++) {
809
0
    struct object *o = data->want_obj.objects[i].item;
810
0
    if (!is_our_ref(o, data->allow_uor)) {
811
0
      error("git upload-pack: not our ref %s",
812
0
            oid_to_hex(&o->oid));
813
0
      packet_writer_error(&data->writer,
814
0
              "upload-pack: not our ref %s",
815
0
              oid_to_hex(&o->oid));
816
0
      exit(128);
817
0
    }
818
0
  }
819
0
}
820
821
static void send_shallow(struct upload_pack_data *data,
822
       struct commit_list *result)
823
0
{
824
0
  while (result) {
825
0
    struct object *object = &result->item->object;
826
0
    if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
827
0
      packet_writer_write(&data->writer, "shallow %s",
828
0
              oid_to_hex(&object->oid));
829
0
      register_shallow(the_repository, &object->oid);
830
0
      data->shallow_nr++;
831
0
    }
832
0
    result = result->next;
833
0
  }
834
0
}
835
836
static void send_unshallow(struct upload_pack_data *data)
837
0
{
838
0
  int i;
839
840
0
  for (i = 0; i < data->shallows.nr; i++) {
841
0
    struct object *object = data->shallows.objects[i].item;
842
0
    if (object->flags & NOT_SHALLOW) {
843
0
      struct commit_list *parents;
844
0
      packet_writer_write(&data->writer, "unshallow %s",
845
0
              oid_to_hex(&object->oid));
846
0
      object->flags &= ~CLIENT_SHALLOW;
847
      /*
848
       * We want to _register_ "object" as shallow, but we
849
       * also need to traverse object's parents to deepen a
850
       * shallow clone. Unregister it for now so we can
851
       * parse and add the parents to the want list, then
852
       * re-register it.
853
       */
854
0
      unregister_shallow(&object->oid);
855
0
      object->parsed = 0;
856
0
      parse_commit_or_die((struct commit *)object);
857
0
      parents = ((struct commit *)object)->parents;
858
0
      while (parents) {
859
0
        add_object_array(&parents->item->object,
860
0
             NULL, &data->want_obj);
861
0
        parents = parents->next;
862
0
      }
863
0
      add_object_array(object, NULL, &data->extra_edge_obj);
864
0
    }
865
    /* make sure commit traversal conforms to client */
866
0
    register_shallow(the_repository, &object->oid);
867
0
  }
868
0
}
869
870
static int check_ref(const char *refname_full, const char *referent UNUSED, const struct object_id *oid,
871
         int flag, void *cb_data);
872
static void deepen(struct upload_pack_data *data, int depth)
873
0
{
874
0
  if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
875
0
    int i;
876
877
0
    for (i = 0; i < data->shallows.nr; i++) {
878
0
      struct object *object = data->shallows.objects[i].item;
879
0
      object->flags |= NOT_SHALLOW;
880
0
    }
881
0
  } else if (data->deepen_relative) {
882
0
    struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
883
0
    struct commit_list *result;
884
885
    /*
886
     * Checking for reachable shallows requires that our refs be
887
     * marked with OUR_REF.
888
     */
889
0
    refs_head_ref_namespaced(get_main_ref_store(the_repository),
890
0
           check_ref, data);
891
0
    for_each_namespaced_ref_1(check_ref, data);
892
893
0
    get_reachable_list(data, &reachable_shallows);
894
0
    result = get_shallow_commits(&reachable_shallows,
895
0
               depth + 1,
896
0
               SHALLOW, NOT_SHALLOW);
897
0
    send_shallow(data, result);
898
0
    free_commit_list(result);
899
0
    object_array_clear(&reachable_shallows);
900
0
  } else {
901
0
    struct commit_list *result;
902
903
0
    result = get_shallow_commits(&data->want_obj, depth,
904
0
               SHALLOW, NOT_SHALLOW);
905
0
    send_shallow(data, result);
906
0
    free_commit_list(result);
907
0
  }
908
909
0
  send_unshallow(data);
910
0
}
911
912
static void deepen_by_rev_list(struct upload_pack_data *data,
913
             int ac,
914
             const char **av)
915
0
{
916
0
  struct commit_list *result;
917
918
0
  disable_commit_graph(the_repository);
919
0
  result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
920
0
  send_shallow(data, result);
921
0
  free_commit_list(result);
922
0
  send_unshallow(data);
923
0
}
924
925
/* Returns 1 if a shallow list is sent or 0 otherwise */
926
static int send_shallow_list(struct upload_pack_data *data)
927
0
{
928
0
  int ret = 0;
929
930
0
  if (data->depth > 0 && data->deepen_rev_list)
931
0
    die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
932
0
  if (data->depth > 0) {
933
0
    deepen(data, data->depth);
934
0
    ret = 1;
935
0
  } else if (data->deepen_rev_list) {
936
0
    struct strvec av = STRVEC_INIT;
937
0
    int i;
938
939
0
    strvec_push(&av, "rev-list");
940
0
    if (data->deepen_since)
941
0
      strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
942
0
    if (oidset_size(&data->deepen_not)) {
943
0
      const struct object_id *oid;
944
0
      struct oidset_iter iter;
945
0
      strvec_push(&av, "--not");
946
0
      oidset_iter_init(&data->deepen_not, &iter);
947
0
      while ((oid = oidset_iter_next(&iter)))
948
0
        strvec_push(&av, oid_to_hex(oid));
949
0
      strvec_push(&av, "--not");
950
0
    }
951
0
    for (i = 0; i < data->want_obj.nr; i++) {
952
0
      struct object *o = data->want_obj.objects[i].item;
953
0
      strvec_push(&av, oid_to_hex(&o->oid));
954
0
    }
955
0
    deepen_by_rev_list(data, av.nr, av.v);
956
0
    strvec_clear(&av);
957
0
    ret = 1;
958
0
  } else {
959
0
    if (data->shallows.nr > 0) {
960
0
      int i;
961
0
      for (i = 0; i < data->shallows.nr; i++)
962
0
        register_shallow(the_repository,
963
0
             &data->shallows.objects[i].item->oid);
964
0
    }
965
0
  }
966
967
0
  data->shallow_nr += data->shallows.nr;
968
0
  return ret;
969
0
}
970
971
static int process_shallow(const char *line, struct object_array *shallows)
972
0
{
973
0
  const char *arg;
974
0
  if (skip_prefix(line, "shallow ", &arg)) {
975
0
    struct object_id oid;
976
0
    struct object *object;
977
0
    if (get_oid_hex(arg, &oid))
978
0
      die("invalid shallow line: %s", line);
979
0
    object = parse_object(the_repository, &oid);
980
0
    if (!object)
981
0
      return 1;
982
0
    if (object->type != OBJ_COMMIT)
983
0
      die("invalid shallow object %s", oid_to_hex(&oid));
984
0
    if (!(object->flags & CLIENT_SHALLOW)) {
985
0
      object->flags |= CLIENT_SHALLOW;
986
0
      add_object_array(object, NULL, shallows);
987
0
    }
988
0
    return 1;
989
0
  }
990
991
0
  return 0;
992
0
}
993
994
static int process_deepen(const char *line, int *depth)
995
0
{
996
0
  const char *arg;
997
0
  if (skip_prefix(line, "deepen ", &arg)) {
998
0
    char *end = NULL;
999
0
    *depth = (int)strtol(arg, &end, 0);
1000
0
    if (!end || *end || *depth <= 0)
1001
0
      die("Invalid deepen: %s", line);
1002
0
    return 1;
1003
0
  }
1004
1005
0
  return 0;
1006
0
}
1007
1008
static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
1009
0
{
1010
0
  const char *arg;
1011
0
  if (skip_prefix(line, "deepen-since ", &arg)) {
1012
0
    char *end = NULL;
1013
0
    *deepen_since = parse_timestamp(arg, &end, 0);
1014
0
    if (!end || *end || !deepen_since ||
1015
        /* revisions.c's max_age -1 is special */
1016
0
        *deepen_since == -1)
1017
0
      die("Invalid deepen-since: %s", line);
1018
0
    *deepen_rev_list = 1;
1019
0
    return 1;
1020
0
  }
1021
0
  return 0;
1022
0
}
1023
1024
static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
1025
0
{
1026
0
  const char *arg;
1027
0
  if (skip_prefix(line, "deepen-not ", &arg)) {
1028
0
    char *ref = NULL;
1029
0
    struct object_id oid;
1030
0
    if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1031
0
      die("git upload-pack: ambiguous deepen-not: %s", line);
1032
0
    oidset_insert(deepen_not, &oid);
1033
0
    free(ref);
1034
0
    *deepen_rev_list = 1;
1035
0
    return 1;
1036
0
  }
1037
0
  return 0;
1038
0
}
1039
1040
NORETURN __attribute__((format(printf,2,3)))
1041
static void send_err_and_die(struct upload_pack_data *data,
1042
           const char *fmt, ...)
1043
0
{
1044
0
  struct strbuf buf = STRBUF_INIT;
1045
0
  va_list ap;
1046
1047
0
  va_start(ap, fmt);
1048
0
  strbuf_vaddf(&buf, fmt, ap);
1049
0
  va_end(ap);
1050
1051
0
  packet_writer_error(&data->writer, "%s", buf.buf);
1052
0
  die("%s", buf.buf);
1053
0
}
1054
1055
static void check_one_filter(struct upload_pack_data *data,
1056
           struct list_objects_filter_options *opts)
1057
0
{
1058
0
  const char *key = list_object_filter_config_name(opts->choice);
1059
0
  struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1060
0
                 key);
1061
0
  int allowed;
1062
1063
0
  if (item)
1064
0
    allowed = (intptr_t)item->util;
1065
0
  else
1066
0
    allowed = data->allow_filter_fallback;
1067
1068
0
  if (!allowed)
1069
0
    send_err_and_die(data, "filter '%s' not supported", key);
1070
1071
0
  if (opts->choice == LOFC_TREE_DEPTH &&
1072
0
      opts->tree_exclude_depth > data->tree_filter_max_depth)
1073
0
    send_err_and_die(data,
1074
0
         "tree filter allows max depth %lu, but got %lu",
1075
0
         data->tree_filter_max_depth,
1076
0
         opts->tree_exclude_depth);
1077
0
}
1078
1079
static void check_filter_recurse(struct upload_pack_data *data,
1080
         struct list_objects_filter_options *opts)
1081
0
{
1082
0
  size_t i;
1083
1084
0
  check_one_filter(data, opts);
1085
0
  if (opts->choice != LOFC_COMBINE)
1086
0
    return;
1087
1088
0
  for (i = 0; i < opts->sub_nr; i++)
1089
0
    check_filter_recurse(data, &opts->sub[i]);
1090
0
}
1091
1092
static void die_if_using_banned_filter(struct upload_pack_data *data)
1093
0
{
1094
0
  check_filter_recurse(data, &data->filter_options);
1095
0
}
1096
1097
static void receive_needs(struct upload_pack_data *data,
1098
        struct packet_reader *reader)
1099
0
{
1100
0
  int has_non_tip = 0;
1101
1102
0
  data->shallow_nr = 0;
1103
0
  for (;;) {
1104
0
    struct object *o;
1105
0
    const char *features;
1106
0
    struct object_id oid_buf;
1107
0
    const char *arg;
1108
0
    size_t feature_len;
1109
1110
0
    reset_timeout(data->timeout);
1111
0
    if (packet_reader_read(reader) != PACKET_READ_NORMAL)
1112
0
      break;
1113
1114
0
    if (process_shallow(reader->line, &data->shallows))
1115
0
      continue;
1116
0
    if (process_deepen(reader->line, &data->depth))
1117
0
      continue;
1118
0
    if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
1119
0
      continue;
1120
0
    if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
1121
0
      continue;
1122
1123
0
    if (skip_prefix(reader->line, "filter ", &arg)) {
1124
0
      if (!data->filter_capability_requested)
1125
0
        die("git upload-pack: filtering capability not negotiated");
1126
0
      list_objects_filter_die_if_populated(&data->filter_options);
1127
0
      parse_list_objects_filter(&data->filter_options, arg);
1128
0
      die_if_using_banned_filter(data);
1129
0
      continue;
1130
0
    }
1131
1132
0
    if (!skip_prefix(reader->line, "want ", &arg) ||
1133
0
        parse_oid_hex(arg, &oid_buf, &features))
1134
0
      die("git upload-pack: protocol error, "
1135
0
          "expected to get object ID, not '%s'", reader->line);
1136
1137
0
    if (parse_feature_request(features, "deepen-relative"))
1138
0
      data->deepen_relative = 1;
1139
0
    if (parse_feature_request(features, "multi_ack_detailed"))
1140
0
      data->multi_ack = MULTI_ACK_DETAILED;
1141
0
    else if (parse_feature_request(features, "multi_ack"))
1142
0
      data->multi_ack = MULTI_ACK;
1143
0
    if (parse_feature_request(features, "no-done"))
1144
0
      data->no_done = 1;
1145
0
    if (parse_feature_request(features, "thin-pack"))
1146
0
      data->use_thin_pack = 1;
1147
0
    if (parse_feature_request(features, "ofs-delta"))
1148
0
      data->use_ofs_delta = 1;
1149
0
    if (parse_feature_request(features, "side-band-64k"))
1150
0
      data->use_sideband = LARGE_PACKET_MAX;
1151
0
    else if (parse_feature_request(features, "side-band"))
1152
0
      data->use_sideband = DEFAULT_PACKET_MAX;
1153
0
    if (parse_feature_request(features, "no-progress"))
1154
0
      data->no_progress = 1;
1155
0
    if (parse_feature_request(features, "include-tag"))
1156
0
      data->use_include_tag = 1;
1157
0
    if (data->allow_filter &&
1158
0
        parse_feature_request(features, "filter"))
1159
0
      data->filter_capability_requested = 1;
1160
1161
0
    arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1162
0
    if (arg) {
1163
0
      char *client_sid = xstrndup(arg, feature_len);
1164
0
      trace2_data_string("transfer", NULL, "client-sid", client_sid);
1165
0
      free(client_sid);
1166
0
    }
1167
1168
0
    o = parse_object_with_flags(the_repository, &oid_buf,
1169
0
              PARSE_OBJECT_SKIP_HASH_CHECK |
1170
0
              PARSE_OBJECT_DISCARD_TREE);
1171
0
    if (!o) {
1172
0
      packet_writer_error(&data->writer,
1173
0
              "upload-pack: not our ref %s",
1174
0
              oid_to_hex(&oid_buf));
1175
0
      die("git upload-pack: not our ref %s",
1176
0
          oid_to_hex(&oid_buf));
1177
0
    }
1178
0
    if (!(o->flags & WANTED)) {
1179
0
      o->flags |= WANTED;
1180
0
      if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1181
0
            || is_our_ref(o, data->allow_uor)))
1182
0
        has_non_tip = 1;
1183
0
      add_object_array(o, NULL, &data->want_obj);
1184
0
    }
1185
0
  }
1186
1187
  /*
1188
   * We have sent all our refs already, and the other end
1189
   * should have chosen out of them. When we are operating
1190
   * in the stateless RPC mode, however, their choice may
1191
   * have been based on the set of older refs advertised
1192
   * by another process that handled the initial request.
1193
   */
1194
0
  if (has_non_tip)
1195
0
    check_non_tip(data);
1196
1197
0
  if (!data->use_sideband && data->daemon_mode)
1198
0
    data->no_progress = 1;
1199
1200
0
  if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
1201
0
    return;
1202
1203
0
  if (send_shallow_list(data))
1204
0
    packet_flush(1);
1205
0
}
1206
1207
/* return non-zero if the ref is hidden, otherwise 0 */
1208
static int mark_our_ref(const char *refname, const char *refname_full,
1209
      const struct object_id *oid, const struct strvec *hidden_refs)
1210
0
{
1211
0
  struct object *o = lookup_unknown_object(the_repository, oid);
1212
1213
0
  if (ref_is_hidden(refname, refname_full, hidden_refs)) {
1214
0
    o->flags |= HIDDEN_REF;
1215
0
    return 1;
1216
0
  }
1217
0
  o->flags |= OUR_REF;
1218
0
  return 0;
1219
0
}
1220
1221
static int check_ref(const char *refname_full, const char *referent UNUSED,const struct object_id *oid,
1222
         int flag UNUSED, void *cb_data)
1223
0
{
1224
0
  const char *refname = strip_namespace(refname_full);
1225
0
  struct upload_pack_data *data = cb_data;
1226
1227
0
  mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
1228
0
  return 0;
1229
0
}
1230
1231
static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1232
0
{
1233
0
  struct string_list_item *item;
1234
1235
0
  if (!symref->nr)
1236
0
    return;
1237
0
  for_each_string_list_item(item, symref)
1238
0
    strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1239
0
}
1240
1241
0
static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1242
0
  if (d->advertise_sid)
1243
0
    strbuf_addf(buf, " session-id=%s", trace2_session_id());
1244
0
}
1245
1246
static void write_v0_ref(struct upload_pack_data *data,
1247
      const char *refname, const char *refname_nons,
1248
      const struct object_id *oid)
1249
0
{
1250
0
  static const char *capabilities = "multi_ack thin-pack side-band"
1251
0
    " side-band-64k ofs-delta shallow deepen-since deepen-not"
1252
0
    " deepen-relative no-progress include-tag multi_ack_detailed";
1253
0
  struct object_id peeled;
1254
1255
0
  if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
1256
0
    return;
1257
1258
0
  if (capabilities) {
1259
0
    struct strbuf symref_info = STRBUF_INIT;
1260
0
    struct strbuf session_id = STRBUF_INIT;
1261
1262
0
    format_symref_info(&symref_info, &data->symref);
1263
0
    format_session_id(&session_id, data);
1264
0
    packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
1265
0
           oid_to_hex(oid), refname_nons,
1266
0
           0, capabilities,
1267
0
           (data->allow_uor & ALLOW_TIP_SHA1) ?
1268
0
             " allow-tip-sha1-in-want" : "",
1269
0
           (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
1270
0
             " allow-reachable-sha1-in-want" : "",
1271
0
           data->no_done ? " no-done" : "",
1272
0
           symref_info.buf,
1273
0
           data->allow_filter ? " filter" : "",
1274
0
           session_id.buf,
1275
0
           the_hash_algo->name,
1276
0
           git_user_agent_sanitized());
1277
0
    strbuf_release(&symref_info);
1278
0
    strbuf_release(&session_id);
1279
0
    data->sent_capabilities = 1;
1280
0
  } else {
1281
0
    packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
1282
0
  }
1283
0
  capabilities = NULL;
1284
0
  if (!peel_iterated_oid(the_repository, oid, &peeled))
1285
0
    packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1286
0
  return;
1287
0
}
1288
1289
static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
1290
        int flag UNUSED, void *cb_data)
1291
0
{
1292
0
  write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
1293
0
  return 0;
1294
0
}
1295
1296
static int find_symref(const char *refname, const char *referent UNUSED,
1297
           const struct object_id *oid UNUSED,
1298
           int flag, void *cb_data)
1299
0
{
1300
0
  const char *symref_target;
1301
0
  struct string_list_item *item;
1302
1303
0
  if ((flag & REF_ISSYMREF) == 0)
1304
0
    return 0;
1305
0
  symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1306
0
            refname, 0, NULL, &flag);
1307
0
  if (!symref_target || (flag & REF_ISSYMREF) == 0)
1308
0
    die("'%s' is a symref but it is not?", refname);
1309
0
  item = string_list_append(cb_data, strip_namespace(refname));
1310
0
  item->util = xstrdup(strip_namespace(symref_target));
1311
0
  return 0;
1312
0
}
1313
1314
static int parse_object_filter_config(const char *var, const char *value,
1315
              const struct key_value_info *kvi,
1316
              struct upload_pack_data *data)
1317
0
{
1318
0
  struct strbuf buf = STRBUF_INIT;
1319
0
  const char *sub, *key;
1320
0
  size_t sub_len;
1321
1322
0
  if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1323
0
    return 0;
1324
1325
0
  if (!sub) {
1326
0
    if (!strcmp(key, "allow"))
1327
0
      data->allow_filter_fallback = git_config_bool(var, value);
1328
0
    return 0;
1329
0
  }
1330
1331
0
  strbuf_add(&buf, sub, sub_len);
1332
1333
0
  if (!strcmp(key, "allow"))
1334
0
    string_list_insert(&data->allowed_filters, buf.buf)->util =
1335
0
      (void *)(intptr_t)git_config_bool(var, value);
1336
0
  else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1337
0
    if (!value) {
1338
0
      strbuf_release(&buf);
1339
0
      return config_error_nonbool(var);
1340
0
    }
1341
0
    string_list_insert(&data->allowed_filters, buf.buf)->util =
1342
0
      (void *)(intptr_t)1;
1343
0
    data->tree_filter_max_depth = git_config_ulong(var, value,
1344
0
                     kvi);
1345
0
  }
1346
1347
0
  strbuf_release(&buf);
1348
0
  return 0;
1349
0
}
1350
1351
static int upload_pack_config(const char *var, const char *value,
1352
            const struct config_context *ctx,
1353
            void *cb_data)
1354
0
{
1355
0
  struct upload_pack_data *data = cb_data;
1356
1357
0
  if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1358
0
    if (git_config_bool(var, value))
1359
0
      data->allow_uor |= ALLOW_TIP_SHA1;
1360
0
    else
1361
0
      data->allow_uor &= ~ALLOW_TIP_SHA1;
1362
0
  } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1363
0
    if (git_config_bool(var, value))
1364
0
      data->allow_uor |= ALLOW_REACHABLE_SHA1;
1365
0
    else
1366
0
      data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
1367
0
  } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1368
0
    if (git_config_bool(var, value))
1369
0
      data->allow_uor |= ALLOW_ANY_SHA1;
1370
0
    else
1371
0
      data->allow_uor &= ~ALLOW_ANY_SHA1;
1372
0
  } else if (!strcmp("uploadpack.keepalive", var)) {
1373
0
    data->keepalive = git_config_int(var, value, ctx->kvi);
1374
0
    if (!data->keepalive)
1375
0
      data->keepalive = -1;
1376
0
  } else if (!strcmp("uploadpack.allowfilter", var)) {
1377
0
    data->allow_filter = git_config_bool(var, value);
1378
0
  } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1379
0
    data->allow_ref_in_want = git_config_bool(var, value);
1380
0
  } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1381
0
    data->allow_sideband_all = git_config_bool(var, value);
1382
0
  } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1383
0
    if (value)
1384
0
      data->allow_packfile_uris = 1;
1385
0
  } else if (!strcmp("core.precomposeunicode", var)) {
1386
0
    precomposed_unicode = git_config_bool(var, value);
1387
0
  } else if (!strcmp("transfer.advertisesid", var)) {
1388
0
    data->advertise_sid = git_config_bool(var, value);
1389
0
  }
1390
1391
0
  if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
1392
0
    return -1;
1393
1394
0
  return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
1395
0
}
1396
1397
static int upload_pack_protected_config(const char *var, const char *value,
1398
          const struct config_context *ctx UNUSED,
1399
          void *cb_data)
1400
0
{
1401
0
  struct upload_pack_data *data = cb_data;
1402
1403
0
  if (!strcmp("uploadpack.packobjectshook", var))
1404
0
    return git_config_string(&data->pack_objects_hook, var, value);
1405
0
  return 0;
1406
0
}
1407
1408
static void get_upload_pack_config(struct repository *r,
1409
           struct upload_pack_data *data)
1410
0
{
1411
0
  repo_config(r, upload_pack_config, data);
1412
0
  git_protected_config(upload_pack_protected_config, data);
1413
1414
0
  data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
1415
0
}
1416
1417
void upload_pack(const int advertise_refs, const int stateless_rpc,
1418
     const int timeout)
1419
0
{
1420
0
  struct packet_reader reader;
1421
0
  struct upload_pack_data data;
1422
1423
0
  upload_pack_data_init(&data);
1424
0
  get_upload_pack_config(the_repository, &data);
1425
1426
0
  data.stateless_rpc = stateless_rpc;
1427
0
  data.timeout = timeout;
1428
0
  if (data.timeout)
1429
0
    data.daemon_mode = 1;
1430
1431
0
  refs_head_ref_namespaced(get_main_ref_store(the_repository),
1432
0
         find_symref, &data.symref);
1433
1434
0
  if (advertise_refs || !data.stateless_rpc) {
1435
0
    reset_timeout(data.timeout);
1436
0
    if (advertise_refs)
1437
0
      data.no_done = 1;
1438
0
    refs_head_ref_namespaced(get_main_ref_store(the_repository),
1439
0
           send_ref, &data);
1440
0
    for_each_namespaced_ref_1(send_ref, &data);
1441
0
    if (!data.sent_capabilities) {
1442
0
      const char *refname = "capabilities^{}";
1443
0
      write_v0_ref(&data, refname, refname, null_oid());
1444
0
    }
1445
    /*
1446
     * fflush stdout before calling advertise_shallow_grafts because send_ref
1447
     * uses stdio.
1448
     */
1449
0
    fflush_or_die(stdout);
1450
0
    advertise_shallow_grafts(1);
1451
0
    packet_flush(1);
1452
0
  } else {
1453
0
    refs_head_ref_namespaced(get_main_ref_store(the_repository),
1454
0
           check_ref, &data);
1455
0
    for_each_namespaced_ref_1(check_ref, &data);
1456
0
  }
1457
1458
0
  if (!advertise_refs) {
1459
0
    packet_reader_init(&reader, 0, NULL, 0,
1460
0
           PACKET_READ_CHOMP_NEWLINE |
1461
0
           PACKET_READ_DIE_ON_ERR_PACKET);
1462
1463
0
    receive_needs(&data, &reader);
1464
1465
    /*
1466
     * An EOF at this exact point in negotiation should be
1467
     * acceptable from stateless clients as they will consume the
1468
     * shallow list before doing subsequent rpc with haves/etc.
1469
     */
1470
0
    if (data.stateless_rpc)
1471
0
      reader.options |= PACKET_READ_GENTLE_ON_EOF;
1472
1473
0
    if (data.want_obj.nr &&
1474
0
        packet_reader_peek(&reader) != PACKET_READ_EOF) {
1475
0
      reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
1476
0
      get_common_commits(&data, &reader);
1477
0
      create_pack_file(&data, NULL);
1478
0
    }
1479
0
  }
1480
1481
0
  upload_pack_data_clear(&data);
1482
0
}
1483
1484
static int parse_want(struct packet_writer *writer, const char *line,
1485
          struct object_array *want_obj)
1486
0
{
1487
0
  const char *arg;
1488
0
  if (skip_prefix(line, "want ", &arg)) {
1489
0
    struct object_id oid;
1490
0
    struct object *o;
1491
1492
0
    if (get_oid_hex(arg, &oid))
1493
0
      die("git upload-pack: protocol error, "
1494
0
          "expected to get oid, not '%s'", line);
1495
1496
0
    o = parse_object_with_flags(the_repository, &oid,
1497
0
              PARSE_OBJECT_SKIP_HASH_CHECK |
1498
0
              PARSE_OBJECT_DISCARD_TREE);
1499
1500
0
    if (!o) {
1501
0
      packet_writer_error(writer,
1502
0
              "upload-pack: not our ref %s",
1503
0
              oid_to_hex(&oid));
1504
0
      die("git upload-pack: not our ref %s",
1505
0
          oid_to_hex(&oid));
1506
0
    }
1507
1508
0
    if (!(o->flags & WANTED)) {
1509
0
      o->flags |= WANTED;
1510
0
      add_object_array(o, NULL, want_obj);
1511
0
    }
1512
1513
0
    return 1;
1514
0
  }
1515
1516
0
  return 0;
1517
0
}
1518
1519
static int parse_want_ref(struct packet_writer *writer, const char *line,
1520
        struct strmap *wanted_refs,
1521
        struct strvec *hidden_refs,
1522
        struct object_array *want_obj)
1523
0
{
1524
0
  const char *refname_nons;
1525
0
  if (skip_prefix(line, "want-ref ", &refname_nons)) {
1526
0
    struct object_id oid;
1527
0
    struct object *o = NULL;
1528
0
    struct strbuf refname = STRBUF_INIT;
1529
1530
0
    strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1531
0
    if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
1532
0
        refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
1533
0
      packet_writer_error(writer, "unknown ref %s", refname_nons);
1534
0
      die("unknown ref %s", refname_nons);
1535
0
    }
1536
0
    strbuf_release(&refname);
1537
1538
0
    if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1539
0
      packet_writer_error(writer, "duplicate want-ref %s",
1540
0
              refname_nons);
1541
0
      die("duplicate want-ref %s", refname_nons);
1542
0
    }
1543
1544
0
    if (!starts_with(refname_nons, "refs/tags/")) {
1545
0
      struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1546
0
      if (commit)
1547
0
        o = &commit->object;
1548
0
    }
1549
1550
0
    if (!o)
1551
0
      o = parse_object_or_die(&oid, refname_nons);
1552
1553
0
    if (!(o->flags & WANTED)) {
1554
0
      o->flags |= WANTED;
1555
0
      add_object_array(o, NULL, want_obj);
1556
0
    }
1557
1558
0
    return 1;
1559
0
  }
1560
1561
0
  return 0;
1562
0
}
1563
1564
static int parse_have(const char *line, struct upload_pack_data *data)
1565
0
{
1566
0
  const char *arg;
1567
0
  if (skip_prefix(line, "have ", &arg)) {
1568
0
    struct object_id oid;
1569
1570
0
    got_oid(data, arg, &oid);
1571
0
    data->seen_haves = 1;
1572
0
    return 1;
1573
0
  }
1574
1575
0
  return 0;
1576
0
}
1577
1578
static void trace2_fetch_info(struct upload_pack_data *data)
1579
0
{
1580
0
  struct json_writer jw = JSON_WRITER_INIT;
1581
1582
0
  jw_object_begin(&jw, 0);
1583
0
  jw_object_intmax(&jw, "haves", data->have_obj.nr);
1584
0
  jw_object_intmax(&jw, "wants", data->want_obj.nr);
1585
0
  jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
1586
0
  jw_object_intmax(&jw, "depth", data->depth);
1587
0
  jw_object_intmax(&jw, "shallows", data->shallows.nr);
1588
0
  jw_object_bool(&jw, "deepen-since", data->deepen_since);
1589
0
  jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
1590
0
  jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1591
0
  if (data->filter_options.choice)
1592
0
    jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1593
0
  else
1594
0
    jw_object_null(&jw, "filter");
1595
0
  jw_end(&jw);
1596
1597
0
  trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1598
1599
0
  jw_release(&jw);
1600
0
}
1601
1602
static void process_args(struct packet_reader *request,
1603
       struct upload_pack_data *data)
1604
0
{
1605
0
  while (packet_reader_read(request) == PACKET_READ_NORMAL) {
1606
0
    const char *arg = request->line;
1607
0
    const char *p;
1608
1609
    /* process want */
1610
0
    if (parse_want(&data->writer, arg, &data->want_obj))
1611
0
      continue;
1612
0
    if (data->allow_ref_in_want &&
1613
0
        parse_want_ref(&data->writer, arg, &data->wanted_refs,
1614
0
           &data->hidden_refs, &data->want_obj))
1615
0
      continue;
1616
    /* process have line */
1617
0
    if (parse_have(arg, data))
1618
0
      continue;
1619
1620
    /* process args like thin-pack */
1621
0
    if (!strcmp(arg, "thin-pack")) {
1622
0
      data->use_thin_pack = 1;
1623
0
      continue;
1624
0
    }
1625
0
    if (!strcmp(arg, "ofs-delta")) {
1626
0
      data->use_ofs_delta = 1;
1627
0
      continue;
1628
0
    }
1629
0
    if (!strcmp(arg, "no-progress")) {
1630
0
      data->no_progress = 1;
1631
0
      continue;
1632
0
    }
1633
0
    if (!strcmp(arg, "include-tag")) {
1634
0
      data->use_include_tag = 1;
1635
0
      continue;
1636
0
    }
1637
0
    if (!strcmp(arg, "done")) {
1638
0
      data->done = 1;
1639
0
      continue;
1640
0
    }
1641
0
    if (!strcmp(arg, "wait-for-done")) {
1642
0
      data->wait_for_done = 1;
1643
0
      continue;
1644
0
    }
1645
1646
    /* Shallow related arguments */
1647
0
    if (process_shallow(arg, &data->shallows))
1648
0
      continue;
1649
0
    if (process_deepen(arg, &data->depth))
1650
0
      continue;
1651
0
    if (process_deepen_since(arg, &data->deepen_since,
1652
0
           &data->deepen_rev_list))
1653
0
      continue;
1654
0
    if (process_deepen_not(arg, &data->deepen_not,
1655
0
               &data->deepen_rev_list))
1656
0
      continue;
1657
0
    if (!strcmp(arg, "deepen-relative")) {
1658
0
      data->deepen_relative = 1;
1659
0
      continue;
1660
0
    }
1661
1662
0
    if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
1663
0
      list_objects_filter_die_if_populated(&data->filter_options);
1664
0
      parse_list_objects_filter(&data->filter_options, p);
1665
0
      die_if_using_banned_filter(data);
1666
0
      continue;
1667
0
    }
1668
1669
0
    if (data->allow_sideband_all &&
1670
0
        !strcmp(arg, "sideband-all")) {
1671
0
      data->writer.use_sideband = 1;
1672
0
      continue;
1673
0
    }
1674
1675
0
    if (data->allow_packfile_uris &&
1676
0
        skip_prefix(arg, "packfile-uris ", &p)) {
1677
0
      if (data->uri_protocols.nr)
1678
0
        send_err_and_die(data,
1679
0
             "multiple packfile-uris lines forbidden");
1680
0
      string_list_split(&data->uri_protocols, p, ',', -1);
1681
0
      continue;
1682
0
    }
1683
1684
    /* ignore unknown lines maybe? */
1685
0
    die("unexpected line: '%s'", arg);
1686
0
  }
1687
1688
0
  if (data->uri_protocols.nr && !data->writer.use_sideband)
1689
0
    string_list_clear(&data->uri_protocols, 0);
1690
1691
0
  if (request->status != PACKET_READ_FLUSH)
1692
0
    die(_("expected flush after fetch arguments"));
1693
1694
0
  if (trace2_is_enabled())
1695
0
    trace2_fetch_info(data);
1696
0
}
1697
1698
static int send_acks(struct upload_pack_data *data, struct object_array *acks)
1699
0
{
1700
0
  int i;
1701
1702
0
  packet_writer_write(&data->writer, "acknowledgments\n");
1703
1704
  /* Send Acks */
1705
0
  if (!acks->nr)
1706
0
    packet_writer_write(&data->writer, "NAK\n");
1707
1708
0
  for (i = 0; i < acks->nr; i++) {
1709
0
    packet_writer_write(&data->writer, "ACK %s\n",
1710
0
            oid_to_hex(&acks->objects[i].item->oid));
1711
0
  }
1712
1713
0
  if (!data->wait_for_done && ok_to_give_up(data)) {
1714
    /* Send Ready */
1715
0
    packet_writer_write(&data->writer, "ready\n");
1716
0
    return 1;
1717
0
  }
1718
1719
0
  return 0;
1720
0
}
1721
1722
static int process_haves_and_send_acks(struct upload_pack_data *data)
1723
0
{
1724
0
  int ret = 0;
1725
1726
0
  if (data->done) {
1727
0
    ret = 1;
1728
0
  } else if (send_acks(data, &data->have_obj)) {
1729
0
    packet_writer_delim(&data->writer);
1730
0
    ret = 1;
1731
0
  } else {
1732
    /* Add Flush */
1733
0
    packet_writer_flush(&data->writer);
1734
0
    ret = 0;
1735
0
  }
1736
1737
0
  return ret;
1738
0
}
1739
1740
static void send_wanted_ref_info(struct upload_pack_data *data)
1741
0
{
1742
0
  struct hashmap_iter iter;
1743
0
  const struct strmap_entry *e;
1744
1745
0
  if (strmap_empty(&data->wanted_refs))
1746
0
    return;
1747
1748
0
  packet_writer_write(&data->writer, "wanted-refs\n");
1749
1750
0
  strmap_for_each_entry(&data->wanted_refs, &iter, e) {
1751
0
    packet_writer_write(&data->writer, "%s %s\n",
1752
0
            oid_to_hex(e->value),
1753
0
            e->key);
1754
0
  }
1755
1756
0
  packet_writer_delim(&data->writer);
1757
0
}
1758
1759
static void send_shallow_info(struct upload_pack_data *data)
1760
0
{
1761
  /* No shallow info needs to be sent */
1762
0
  if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1763
0
      !is_repository_shallow(the_repository))
1764
0
    return;
1765
1766
0
  packet_writer_write(&data->writer, "shallow-info\n");
1767
1768
0
  if (!send_shallow_list(data) &&
1769
0
      is_repository_shallow(the_repository))
1770
0
    deepen(data, INFINITE_DEPTH);
1771
1772
0
  packet_delim(1);
1773
0
}
1774
1775
enum fetch_state {
1776
  FETCH_PROCESS_ARGS = 0,
1777
  FETCH_SEND_ACKS,
1778
  FETCH_SEND_PACK,
1779
  FETCH_DONE,
1780
};
1781
1782
int upload_pack_v2(struct repository *r, struct packet_reader *request)
1783
0
{
1784
0
  enum fetch_state state = FETCH_PROCESS_ARGS;
1785
0
  struct upload_pack_data data;
1786
1787
0
  clear_object_flags(ALL_FLAGS);
1788
1789
0
  upload_pack_data_init(&data);
1790
0
  data.use_sideband = LARGE_PACKET_MAX;
1791
0
  get_upload_pack_config(r, &data);
1792
1793
0
  while (state != FETCH_DONE) {
1794
0
    switch (state) {
1795
0
    case FETCH_PROCESS_ARGS:
1796
0
      process_args(request, &data);
1797
1798
0
      if (!data.want_obj.nr && !data.wait_for_done) {
1799
        /*
1800
         * Request didn't contain any 'want' lines (and
1801
         * the request does not contain
1802
         * "wait-for-done", in which it is reasonable
1803
         * to just send 'have's without 'want's); guess
1804
         * they didn't want anything.
1805
         */
1806
0
        state = FETCH_DONE;
1807
0
      } else if (data.seen_haves) {
1808
        /*
1809
         * Request had 'have' lines, so lets ACK them.
1810
         */
1811
0
        state = FETCH_SEND_ACKS;
1812
0
      } else {
1813
        /*
1814
         * Request had 'want's but no 'have's so we can
1815
         * immedietly go to construct and send a pack.
1816
         */
1817
0
        state = FETCH_SEND_PACK;
1818
0
      }
1819
0
      break;
1820
0
    case FETCH_SEND_ACKS:
1821
0
      if (process_haves_and_send_acks(&data))
1822
0
        state = FETCH_SEND_PACK;
1823
0
      else
1824
0
        state = FETCH_DONE;
1825
0
      break;
1826
0
    case FETCH_SEND_PACK:
1827
0
      send_wanted_ref_info(&data);
1828
0
      send_shallow_info(&data);
1829
1830
0
      if (data.uri_protocols.nr) {
1831
0
        create_pack_file(&data, &data.uri_protocols);
1832
0
      } else {
1833
0
        packet_writer_write(&data.writer, "packfile\n");
1834
0
        create_pack_file(&data, NULL);
1835
0
      }
1836
0
      state = FETCH_DONE;
1837
0
      break;
1838
0
    case FETCH_DONE:
1839
0
      continue;
1840
0
    }
1841
0
  }
1842
1843
0
  upload_pack_data_clear(&data);
1844
0
  return 0;
1845
0
}
1846
1847
int upload_pack_advertise(struct repository *r,
1848
        struct strbuf *value)
1849
0
{
1850
0
  struct upload_pack_data data;
1851
1852
0
  upload_pack_data_init(&data);
1853
0
  get_upload_pack_config(r, &data);
1854
1855
0
  if (value) {
1856
0
    strbuf_addstr(value, "shallow wait-for-done");
1857
1858
0
    if (data.allow_filter)
1859
0
      strbuf_addstr(value, " filter");
1860
1861
0
    if (data.allow_ref_in_want)
1862
0
      strbuf_addstr(value, " ref-in-want");
1863
1864
0
    if (data.allow_sideband_all)
1865
0
      strbuf_addstr(value, " sideband-all");
1866
1867
0
    if (data.allow_packfile_uris)
1868
0
      strbuf_addstr(value, " packfile-uris");
1869
0
  }
1870
1871
0
  upload_pack_data_clear(&data);
1872
1873
0
  return 1;
1874
0
}