Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/send-pack.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "config.h"
3
#include "commit.h"
4
#include "date.h"
5
#include "gettext.h"
6
#include "hex.h"
7
#include "odb.h"
8
#include "pkt-line.h"
9
#include "sideband.h"
10
#include "run-command.h"
11
#include "remote.h"
12
#include "connect.h"
13
#include "send-pack.h"
14
#include "transport.h"
15
#include "version.h"
16
#include "oid-array.h"
17
#include "gpg-interface.h"
18
#include "shallow.h"
19
#include "parse-options.h"
20
#include "trace2.h"
21
#include "write-or-die.h"
22
23
int option_parse_push_signed(const struct option *opt,
24
           const char *arg, int unset)
25
0
{
26
0
  if (unset) {
27
0
    *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
28
0
    return 0;
29
0
  }
30
0
  switch (git_parse_maybe_bool(arg)) {
31
0
  case 1:
32
0
    *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
33
0
    return 0;
34
0
  case 0:
35
0
    *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
36
0
    return 0;
37
0
  }
38
0
  if (!strcasecmp("if-asked", arg)) {
39
0
    *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
40
0
    return 0;
41
0
  }
42
0
  die("bad %s argument: %s", opt->long_name, arg);
43
0
}
44
45
static void feed_object(struct repository *r,
46
      const struct object_id *oid, FILE *fh, int negative)
47
0
{
48
0
  if (negative && !odb_has_object(r->objects, oid, 0))
49
0
    return;
50
51
0
  if (negative)
52
0
    putc('^', fh);
53
0
  fputs(oid_to_hex(oid), fh);
54
0
  putc('\n', fh);
55
0
}
56
57
/*
58
 * Make a pack stream and spit it out into file descriptor fd
59
 */
60
static int pack_objects(struct repository *r,
61
      int fd, struct ref *refs, struct oid_array *advertised,
62
      struct oid_array *negotiated,
63
      struct send_pack_args *args)
64
0
{
65
  /*
66
   * The child becomes pack-objects --revs; we feed
67
   * the revision parameters to it via its stdin and
68
   * let its stdout go back to the other end.
69
   */
70
0
  struct child_process po = CHILD_PROCESS_INIT;
71
0
  FILE *po_in;
72
0
  int rc;
73
74
0
  trace2_region_enter("send_pack", "pack_objects", r);
75
0
  strvec_push(&po.args, "pack-objects");
76
0
  strvec_push(&po.args, "--all-progress-implied");
77
0
  strvec_push(&po.args, "--revs");
78
0
  strvec_push(&po.args, "--stdout");
79
0
  if (args->use_thin_pack)
80
0
    strvec_push(&po.args, "--thin");
81
0
  if (args->use_ofs_delta)
82
0
    strvec_push(&po.args, "--delta-base-offset");
83
0
  if (args->quiet || !args->progress)
84
0
    strvec_push(&po.args, "-q");
85
0
  if (args->progress)
86
0
    strvec_push(&po.args, "--progress");
87
0
  if (is_repository_shallow(r))
88
0
    strvec_push(&po.args, "--shallow");
89
0
  if (args->disable_bitmaps)
90
0
    strvec_push(&po.args, "--no-use-bitmap-index");
91
0
  po.in = -1;
92
0
  po.out = args->stateless_rpc ? -1 : fd;
93
0
  po.git_cmd = 1;
94
0
  po.clean_on_exit = 1;
95
0
  if (start_command(&po))
96
0
    die_errno("git pack-objects failed");
97
98
  /*
99
   * We feed the pack-objects we just spawned with revision
100
   * parameters by writing to the pipe.
101
   */
102
0
  po_in = xfdopen(po.in, "w");
103
0
  for (size_t i = 0; i < advertised->nr; i++)
104
0
    feed_object(r, &advertised->oid[i], po_in, 1);
105
0
  for (size_t i = 0; i < negotiated->nr; i++)
106
0
    feed_object(r, &negotiated->oid[i], po_in, 1);
107
108
0
  while (refs) {
109
0
    if (!is_null_oid(&refs->old_oid))
110
0
      feed_object(r, &refs->old_oid, po_in, 1);
111
0
    if (!is_null_oid(&refs->new_oid))
112
0
      feed_object(r, &refs->new_oid, po_in, 0);
113
0
    refs = refs->next;
114
0
  }
115
116
0
  fflush(po_in);
117
0
  if (ferror(po_in))
118
0
    die_errno("error writing to pack-objects");
119
0
  fclose(po_in);
120
121
0
  if (args->stateless_rpc) {
122
0
    char *buf = xmalloc(LARGE_PACKET_MAX);
123
0
    while (1) {
124
0
      ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
125
0
      if (n <= 0)
126
0
        break;
127
0
      send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
128
0
    }
129
0
    free(buf);
130
0
    close(po.out);
131
0
    po.out = -1;
132
0
  }
133
134
0
  rc = finish_command(&po);
135
0
  if (rc) {
136
    /*
137
     * For a normal non-zero exit, we assume pack-objects wrote
138
     * something useful to stderr. For death by signal, though,
139
     * we should mention it to the user. The exception is SIGPIPE
140
     * (141), because that's a normal occurrence if the remote end
141
     * hangs up (and we'll report that by trying to read the unpack
142
     * status).
143
     */
144
0
    if (rc > 128 && rc != 141)
145
0
      error("pack-objects died of signal %d", rc - 128);
146
0
    trace2_region_leave("send_pack", "pack_objects", r);
147
0
    return -1;
148
0
  }
149
0
  trace2_region_leave("send_pack", "pack_objects", r);
150
0
  return 0;
151
0
}
152
153
static int receive_unpack_status(struct packet_reader *reader)
154
0
{
155
0
  if (packet_reader_read(reader) != PACKET_READ_NORMAL)
156
0
    return error(_("unexpected flush packet while reading remote unpack status"));
157
0
  if (!skip_prefix(reader->line, "unpack ", &reader->line))
158
0
    return error(_("unable to parse remote unpack status: %s"), reader->line);
159
0
  if (strcmp(reader->line, "ok"))
160
0
    return error(_("remote unpack failed: %s"), reader->line);
161
0
  return 0;
162
0
}
163
164
static int receive_status(struct repository *r,
165
        struct packet_reader *reader, struct ref *refs)
166
0
{
167
0
  struct ref *hint;
168
0
  int ret;
169
0
  struct ref_push_report *report = NULL;
170
0
  int new_report = 0;
171
0
  int once = 0;
172
173
0
  trace2_region_enter("send_pack", "receive_status", r);
174
0
  hint = NULL;
175
0
  ret = receive_unpack_status(reader);
176
0
  while (1) {
177
0
    struct object_id old_oid, new_oid;
178
0
    const char *head;
179
0
    const char *refname;
180
0
    char *p;
181
0
    if (packet_reader_read(reader) != PACKET_READ_NORMAL)
182
0
      break;
183
0
    head = reader->line;
184
0
    p = strchr(head, ' ');
185
0
    if (!p) {
186
0
      error("invalid status line from remote: %s", reader->line);
187
0
      ret = -1;
188
0
      break;
189
0
    }
190
0
    *p++ = '\0';
191
192
0
    if (!strcmp(head, "option")) {
193
0
      const char *key, *val;
194
195
0
      if (!hint || !(report || new_report)) {
196
0
        if (!once++)
197
0
          error("'option' without a matching 'ok/ng' directive");
198
0
        ret = -1;
199
0
        continue;
200
0
      }
201
0
      if (new_report) {
202
0
        if (!hint->report) {
203
0
          CALLOC_ARRAY(hint->report, 1);
204
0
          report = hint->report;
205
0
        } else {
206
0
          report = hint->report;
207
0
          while (report->next)
208
0
            report = report->next;
209
0
          CALLOC_ARRAY(report->next, 1);
210
0
          report = report->next;
211
0
        }
212
0
        new_report = 0;
213
0
      }
214
0
      key = p;
215
0
      p = strchr(key, ' ');
216
0
      if (p)
217
0
        *p++ = '\0';
218
0
      val = p;
219
0
      if (!strcmp(key, "refname"))
220
0
        report->ref_name = xstrdup_or_null(val);
221
0
      else if (!strcmp(key, "old-oid") && val &&
222
0
         !parse_oid_hex_algop(val, &old_oid, &val, r->hash_algo))
223
0
        report->old_oid = oiddup(&old_oid);
224
0
      else if (!strcmp(key, "new-oid") && val &&
225
0
         !parse_oid_hex_algop(val, &new_oid, &val, r->hash_algo))
226
0
        report->new_oid = oiddup(&new_oid);
227
0
      else if (!strcmp(key, "forced-update"))
228
0
        report->forced_update = 1;
229
0
      continue;
230
0
    }
231
232
0
    report = NULL;
233
0
    new_report = 0;
234
0
    if (strcmp(head, "ok") && strcmp(head, "ng")) {
235
0
      error("invalid ref status from remote: %s", head);
236
0
      ret = -1;
237
0
      break;
238
0
    }
239
0
    refname = p;
240
0
    p = strchr(refname, ' ');
241
0
    if (p)
242
0
      *p++ = '\0';
243
    /* first try searching at our hint, falling back to all refs */
244
0
    if (hint)
245
0
      hint = find_ref_by_name(hint, refname);
246
0
    if (!hint)
247
0
      hint = find_ref_by_name(refs, refname);
248
0
    if (!hint) {
249
0
      warning("remote reported status on unknown ref: %s",
250
0
        refname);
251
0
      continue;
252
0
    }
253
0
    if (hint->status != REF_STATUS_EXPECTING_REPORT &&
254
0
        hint->status != REF_STATUS_OK &&
255
0
        hint->status != REF_STATUS_REMOTE_REJECT) {
256
0
      warning("remote reported status on unexpected ref: %s",
257
0
        refname);
258
0
      continue;
259
0
    }
260
261
    /*
262
     * Clients sending duplicate refs can cause the same value
263
     * to be overridden, causing a memory leak.
264
     */
265
0
    free(hint->remote_status);
266
267
0
    if (!strcmp(head, "ng")) {
268
0
      hint->status = REF_STATUS_REMOTE_REJECT;
269
0
      if (p)
270
0
        hint->remote_status = xstrdup(p);
271
0
      else
272
0
        hint->remote_status = xstrdup("failed");
273
0
    } else {
274
0
      hint->status = REF_STATUS_OK;
275
0
      hint->remote_status = xstrdup_or_null(p);
276
0
      new_report = 1;
277
0
    }
278
0
  }
279
0
  trace2_region_leave("send_pack", "receive_status", r);
280
0
  return ret;
281
0
}
282
283
static int sideband_demux(int in UNUSED, int out, void *data)
284
0
{
285
0
  int *fd = data, ret;
286
0
  if (async_with_fork())
287
0
    close(fd[1]);
288
0
  ret = recv_sideband("send-pack", fd[0], out);
289
0
  close(out);
290
0
  return ret;
291
0
}
292
293
static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
294
0
{
295
0
  struct strbuf *sb = cb;
296
0
  if (graft->nr_parent == -1)
297
0
    packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
298
0
  return 0;
299
0
}
300
301
static void advertise_shallow_grafts_buf(struct repository *r, struct strbuf *sb)
302
0
{
303
0
  if (!is_repository_shallow(r))
304
0
    return;
305
0
  for_each_commit_graft(advertise_shallow_grafts_cb, sb);
306
0
}
307
308
0
#define CHECK_REF_NO_PUSH -1
309
0
#define CHECK_REF_STATUS_REJECTED -2
310
0
#define CHECK_REF_UPTODATE -3
311
static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
312
0
{
313
0
  if (!ref->peer_ref && !args->send_mirror)
314
0
    return CHECK_REF_NO_PUSH;
315
316
  /* Check for statuses set by set_ref_status_for_push() */
317
0
  switch (ref->status) {
318
0
  case REF_STATUS_REJECT_NONFASTFORWARD:
319
0
  case REF_STATUS_REJECT_ALREADY_EXISTS:
320
0
  case REF_STATUS_REJECT_FETCH_FIRST:
321
0
  case REF_STATUS_REJECT_NEEDS_FORCE:
322
0
  case REF_STATUS_REJECT_STALE:
323
0
  case REF_STATUS_REJECT_REMOTE_UPDATED:
324
0
  case REF_STATUS_REJECT_NODELETE:
325
0
    return CHECK_REF_STATUS_REJECTED;
326
0
  case REF_STATUS_UPTODATE:
327
0
    return CHECK_REF_UPTODATE;
328
329
0
  default:
330
0
  case REF_STATUS_EXPECTING_REPORT:
331
    /* already passed checks on the local side */
332
0
  case REF_STATUS_OK:
333
    /* of course this is OK */
334
0
    return 0;
335
0
  }
336
0
}
337
338
/*
339
 * the beginning of the next line, or the end of buffer.
340
 *
341
 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
342
 * convert many similar uses found by "git grep -A4 memchr".
343
 */
344
static const char *next_line(const char *line, size_t len)
345
0
{
346
0
  const char *nl = memchr(line, '\n', len);
347
0
  if (!nl)
348
0
    return line + len; /* incomplete line */
349
0
  return nl + 1;
350
0
}
351
352
static int generate_push_cert(struct strbuf *req_buf,
353
            const struct ref *remote_refs,
354
            struct send_pack_args *args,
355
            const char *cap_string,
356
            const char *push_cert_nonce)
357
0
{
358
0
  const struct ref *ref;
359
0
  struct string_list_item *item;
360
0
  char *signing_key_id = get_signing_key_id();
361
0
  char *signing_key = get_signing_key();
362
0
  const char *cp, *np;
363
0
  struct strbuf cert = STRBUF_INIT;
364
0
  int update_seen = 0;
365
366
0
  strbuf_addstr(&cert, "certificate version 0.1\n");
367
0
  strbuf_addf(&cert, "pusher %s ", signing_key_id);
368
0
  datestamp(&cert);
369
0
  strbuf_addch(&cert, '\n');
370
0
  if (args->url && *args->url) {
371
0
    char *anon_url = transport_anonymize_url(args->url);
372
0
    strbuf_addf(&cert, "pushee %s\n", anon_url);
373
0
    free(anon_url);
374
0
  }
375
0
  if (push_cert_nonce[0])
376
0
    strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
377
0
  if (args->push_options)
378
0
    for_each_string_list_item(item, args->push_options)
379
0
      strbuf_addf(&cert, "push-option %s\n", item->string);
380
0
  strbuf_addstr(&cert, "\n");
381
382
0
  for (ref = remote_refs; ref; ref = ref->next) {
383
0
    if (check_to_send_update(ref, args) < 0)
384
0
      continue;
385
0
    update_seen = 1;
386
0
    strbuf_addf(&cert, "%s %s %s\n",
387
0
          oid_to_hex(&ref->old_oid),
388
0
          oid_to_hex(&ref->new_oid),
389
0
          ref->name);
390
0
  }
391
0
  if (!update_seen)
392
0
    goto free_return;
393
394
0
  if (sign_buffer(&cert, &cert, signing_key))
395
0
    die(_("failed to sign the push certificate"));
396
397
0
  packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
398
0
  for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
399
0
    np = next_line(cp, cert.buf + cert.len - cp);
400
0
    packet_buf_write(req_buf,
401
0
         "%.*s", (int)(np - cp), cp);
402
0
  }
403
0
  packet_buf_write(req_buf, "push-cert-end\n");
404
405
0
free_return:
406
0
  free(signing_key_id);
407
0
  free(signing_key);
408
0
  strbuf_release(&cert);
409
0
  return update_seen;
410
0
}
411
412
0
#define NONCE_LEN_LIMIT 256
413
414
static void reject_invalid_nonce(const char *nonce, int len)
415
0
{
416
0
  int i = 0;
417
418
0
  if (NONCE_LEN_LIMIT <= len)
419
0
    die("the receiving end asked to sign an invalid nonce <%.*s>",
420
0
        len, nonce);
421
422
0
  for (i = 0; i < len; i++) {
423
0
    int ch = nonce[i] & 0xFF;
424
0
    if (isalnum(ch) ||
425
0
        ch == '-' || ch == '.' ||
426
0
        ch == '/' || ch == '+' ||
427
0
        ch == '=' || ch == '_')
428
0
      continue;
429
0
    die("the receiving end asked to sign an invalid nonce <%.*s>",
430
0
        len, nonce);
431
0
  }
432
0
}
433
434
static void get_commons_through_negotiation(struct repository *r,
435
              const char *url,
436
              const struct ref *remote_refs,
437
              struct oid_array *commons)
438
0
{
439
0
  struct child_process child = CHILD_PROCESS_INIT;
440
0
  const struct ref *ref;
441
0
  int len = r->hash_algo->hexsz + 1; /* hash + NL */
442
0
  int nr_negotiation_tip = 0;
443
444
0
  child.git_cmd = 1;
445
0
  child.no_stdin = 1;
446
0
  child.out = -1;
447
0
  strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
448
0
  for (ref = remote_refs; ref; ref = ref->next) {
449
0
    if (!is_null_oid(&ref->new_oid)) {
450
0
      strvec_pushf(&child.args, "--negotiation-tip=%s",
451
0
             oid_to_hex(&ref->new_oid));
452
0
      nr_negotiation_tip++;
453
0
    }
454
0
  }
455
0
  strvec_push(&child.args, url);
456
457
0
  if (!nr_negotiation_tip) {
458
0
    child_process_clear(&child);
459
0
    return;
460
0
  }
461
462
0
  if (start_command(&child))
463
0
    die(_("send-pack: unable to fork off fetch subprocess"));
464
465
0
  do {
466
0
    char hex_hash[GIT_MAX_HEXSZ + 1];
467
0
    int read_len = read_in_full(child.out, hex_hash, len);
468
0
    struct object_id oid;
469
0
    const char *end;
470
471
0
    if (!read_len)
472
0
      break;
473
0
    if (read_len != len)
474
0
      die("invalid length read %d", read_len);
475
0
    if (parse_oid_hex_algop(hex_hash, &oid, &end, r->hash_algo) || *end != '\n')
476
0
      die("invalid hash");
477
0
    oid_array_append(commons, &oid);
478
0
  } while (1);
479
480
0
  if (finish_command(&child)) {
481
    /*
482
     * The information that push negotiation provides is useful but
483
     * not mandatory.
484
     */
485
0
    warning(_("push negotiation failed; proceeding anyway with push"));
486
0
  }
487
0
}
488
489
int send_pack(struct repository *r,
490
        struct send_pack_args *args,
491
        int fd[], struct child_process *conn,
492
        struct ref *remote_refs,
493
        struct oid_array *extra_have)
494
0
{
495
0
  struct oid_array commons = OID_ARRAY_INIT;
496
0
  int in = fd[0];
497
0
  int out = fd[1];
498
0
  struct strbuf req_buf = STRBUF_INIT;
499
0
  struct strbuf cap_buf = STRBUF_INIT;
500
0
  struct ref *ref;
501
0
  int need_pack_data = 0;
502
0
  int allow_deleting_refs = 0;
503
0
  int status_report = 0;
504
0
  int use_sideband = 0;
505
0
  int quiet_supported = 0;
506
0
  int agent_supported = 0;
507
0
  int advertise_sid = 0;
508
0
  int push_negotiate = 0;
509
0
  int use_atomic = 0;
510
0
  int atomic_supported = 0;
511
0
  int use_push_options = 0;
512
0
  int push_options_supported = 0;
513
0
  int object_format_supported = 0;
514
0
  unsigned cmds_sent = 0;
515
0
  int ret;
516
0
  struct async demux;
517
0
  char *push_cert_nonce = NULL;
518
0
  struct packet_reader reader;
519
0
  int use_bitmaps;
520
521
0
  if (!remote_refs) {
522
0
    fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
523
0
      "Perhaps you should specify a branch.\n");
524
0
    ret = 0;
525
0
    goto out;
526
0
  }
527
528
0
  repo_config_get_bool(r, "push.negotiate", &push_negotiate);
529
0
  if (push_negotiate) {
530
0
    trace2_region_enter("send_pack", "push_negotiate", r);
531
0
    get_commons_through_negotiation(r, args->url, remote_refs, &commons);
532
0
    trace2_region_leave("send_pack", "push_negotiate", r);
533
0
  }
534
535
0
  if (!repo_config_get_bool(r, "push.usebitmaps", &use_bitmaps))
536
0
    args->disable_bitmaps = !use_bitmaps;
537
538
0
  repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid);
539
540
  /* Does the other end support the reporting? */
541
0
  if (server_supports("report-status-v2"))
542
0
    status_report = 2;
543
0
  else if (server_supports("report-status"))
544
0
    status_report = 1;
545
0
  if (server_supports("delete-refs"))
546
0
    allow_deleting_refs = 1;
547
0
  if (server_supports("ofs-delta"))
548
0
    args->use_ofs_delta = 1;
549
0
  if (server_supports("side-band-64k"))
550
0
    use_sideband = 1;
551
0
  if (server_supports("quiet"))
552
0
    quiet_supported = 1;
553
0
  if (server_supports("agent"))
554
0
    agent_supported = 1;
555
0
  if (!server_supports("session-id"))
556
0
    advertise_sid = 0;
557
0
  if (server_supports("no-thin"))
558
0
    args->use_thin_pack = 0;
559
0
  if (server_supports("atomic"))
560
0
    atomic_supported = 1;
561
0
  if (server_supports("push-options"))
562
0
    push_options_supported = 1;
563
564
0
  if (!server_supports_hash(r->hash_algo->name, &object_format_supported))
565
0
    die(_("the receiving end does not support this repository's hash algorithm"));
566
567
0
  if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
568
0
    size_t len;
569
0
    const char *nonce = server_feature_value("push-cert", &len);
570
571
0
    if (nonce) {
572
0
      reject_invalid_nonce(nonce, len);
573
0
      push_cert_nonce = xmemdupz(nonce, len);
574
0
    } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
575
0
      die(_("the receiving end does not support --signed push"));
576
0
    } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
577
0
      warning(_("not sending a push certificate since the"
578
0
          " receiving end does not support --signed"
579
0
          " push"));
580
0
    }
581
0
  }
582
583
0
  if (args->atomic && !atomic_supported)
584
0
    die(_("the receiving end does not support --atomic push"));
585
586
0
  use_atomic = atomic_supported && args->atomic;
587
588
0
  if (args->push_options && !push_options_supported)
589
0
    die(_("the receiving end does not support push options"));
590
591
0
  use_push_options = push_options_supported && args->push_options;
592
593
0
  if (status_report == 1)
594
0
    strbuf_addstr(&cap_buf, " report-status");
595
0
  else if (status_report == 2)
596
0
    strbuf_addstr(&cap_buf, " report-status-v2");
597
0
  if (use_sideband)
598
0
    strbuf_addstr(&cap_buf, " side-band-64k");
599
0
  if (quiet_supported && (args->quiet || !args->progress))
600
0
    strbuf_addstr(&cap_buf, " quiet");
601
0
  if (use_atomic)
602
0
    strbuf_addstr(&cap_buf, " atomic");
603
0
  if (use_push_options)
604
0
    strbuf_addstr(&cap_buf, " push-options");
605
0
  if (object_format_supported)
606
0
    strbuf_addf(&cap_buf, " object-format=%s", r->hash_algo->name);
607
0
  if (agent_supported)
608
0
    strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
609
0
  if (advertise_sid)
610
0
    strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
611
612
  /*
613
   * NEEDSWORK: why does delete-refs have to be so specific to
614
   * send-pack machinery that set_ref_status_for_push() cannot
615
   * set this bit for us???
616
   */
617
0
  for (ref = remote_refs; ref; ref = ref->next)
618
0
    if (ref->deletion && !allow_deleting_refs)
619
0
      ref->status = REF_STATUS_REJECT_NODELETE;
620
621
  /*
622
   * Clear the status for each ref and see if we need to send
623
   * the pack data.
624
   */
625
0
  for (ref = remote_refs; ref; ref = ref->next) {
626
0
    switch (check_to_send_update(ref, args)) {
627
0
    case 0: /* no error */
628
0
      break;
629
0
    case CHECK_REF_STATUS_REJECTED:
630
      /*
631
       * When we know the server would reject a ref update if
632
       * we were to send it and we're trying to send the refs
633
       * atomically, abort the whole operation.
634
       */
635
0
      if (use_atomic) {
636
0
        reject_atomic_push(remote_refs, args->send_mirror);
637
0
        error("atomic push failed for ref %s. status: %d",
638
0
              ref->name, ref->status);
639
0
        ret = ERROR_SEND_PACK_BAD_REF_STATUS;
640
0
        packet_flush(out);
641
0
        goto out;
642
0
      }
643
      /* else fallthrough */
644
0
    default:
645
0
      continue;
646
0
    }
647
0
    if (!ref->deletion)
648
0
      need_pack_data = 1;
649
650
0
    if (args->dry_run || !status_report)
651
0
      ref->status = REF_STATUS_OK;
652
0
    else
653
0
      ref->status = REF_STATUS_EXPECTING_REPORT;
654
0
  }
655
656
0
  if (!args->dry_run)
657
0
    advertise_shallow_grafts_buf(r, &req_buf);
658
659
  /*
660
   * Finally, tell the other end!
661
   */
662
0
  if (!args->dry_run && push_cert_nonce) {
663
0
    cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
664
0
                 cap_buf.buf, push_cert_nonce);
665
0
    trace2_printf("Generated push certificate");
666
0
  } else if (!args->dry_run) {
667
0
    for (ref = remote_refs; ref; ref = ref->next) {
668
0
      char *old_hex, *new_hex;
669
670
0
      if (check_to_send_update(ref, args) < 0)
671
0
        continue;
672
673
0
      old_hex = oid_to_hex(&ref->old_oid);
674
0
      new_hex = oid_to_hex(&ref->new_oid);
675
0
      if (!cmds_sent) {
676
0
        packet_buf_write(&req_buf,
677
0
             "%s %s %s%c%s",
678
0
             old_hex, new_hex, ref->name, 0,
679
0
             cap_buf.buf);
680
0
        cmds_sent = 1;
681
0
      } else {
682
0
        packet_buf_write(&req_buf, "%s %s %s",
683
0
             old_hex, new_hex, ref->name);
684
0
      }
685
0
    }
686
0
  }
687
688
0
  if (use_push_options) {
689
0
    struct string_list_item *item;
690
691
0
    packet_buf_flush(&req_buf);
692
0
    for_each_string_list_item(item, args->push_options)
693
0
      packet_buf_write(&req_buf, "%s", item->string);
694
0
  }
695
696
0
  if (args->stateless_rpc) {
697
0
    if (!args->dry_run && (cmds_sent || is_repository_shallow(r))) {
698
0
      packet_buf_flush(&req_buf);
699
0
      send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
700
0
    }
701
0
  } else {
702
0
    write_or_die(out, req_buf.buf, req_buf.len);
703
0
    packet_flush(out);
704
0
  }
705
706
0
  if (use_sideband && cmds_sent) {
707
0
    memset(&demux, 0, sizeof(demux));
708
0
    demux.proc = sideband_demux;
709
0
    demux.data = fd;
710
0
    demux.out = -1;
711
0
    demux.isolate_sigpipe = 1;
712
0
    if (start_async(&demux))
713
0
      die("send-pack: unable to fork off sideband demultiplexer");
714
0
    in = demux.out;
715
0
  }
716
717
0
  packet_reader_init(&reader, in, NULL, 0,
718
0
         PACKET_READ_CHOMP_NEWLINE |
719
0
         PACKET_READ_DIE_ON_ERR_PACKET);
720
721
0
  if (need_pack_data && cmds_sent) {
722
0
    if (pack_objects(r, out, remote_refs, extra_have, &commons, args) < 0) {
723
0
      if (args->stateless_rpc)
724
0
        close(out);
725
0
      if (git_connection_is_socket(conn))
726
0
        shutdown(fd[0], SHUT_WR);
727
728
      /*
729
       * Do not even bother with the return value; we know we
730
       * are failing, and just want the error() side effects,
731
       * as well as marking refs with their remote status (if
732
       * we get one).
733
       */
734
0
      if (status_report)
735
0
        receive_status(r, &reader, remote_refs);
736
737
0
      if (use_sideband) {
738
0
        close(demux.out);
739
0
        finish_async(&demux);
740
0
      }
741
0
      fd[1] = -1;
742
743
0
      ret = -1;
744
0
      goto out;
745
0
    }
746
0
    if (!args->stateless_rpc)
747
      /* Closed by pack_objects() via start_command() */
748
0
      fd[1] = -1;
749
0
  }
750
0
  if (args->stateless_rpc && cmds_sent)
751
0
    packet_flush(out);
752
753
0
  if (status_report && cmds_sent)
754
0
    ret = receive_status(r, &reader, remote_refs);
755
0
  else
756
0
    ret = 0;
757
0
  if (args->stateless_rpc)
758
0
    packet_flush(out);
759
760
0
  if (use_sideband && cmds_sent) {
761
0
    close(demux.out);
762
0
    if (finish_async(&demux)) {
763
0
      error("error in sideband demultiplexer");
764
0
      ret = -1;
765
0
    }
766
0
  }
767
768
0
  if (ret < 0)
769
0
    goto out;
770
771
0
  for (ref = remote_refs; ref; ref = ref->next) {
772
0
    switch (ref->status) {
773
0
    case REF_STATUS_NONE:
774
0
    case REF_STATUS_UPTODATE:
775
0
    case REF_STATUS_OK:
776
0
      break;
777
0
    default:
778
0
      ret = ERROR_SEND_PACK_BAD_REF_STATUS;
779
0
      goto out;
780
0
    }
781
0
  }
782
783
0
  ret = 0;
784
785
0
out:
786
0
  oid_array_clear(&commons);
787
0
  strbuf_release(&req_buf);
788
0
  strbuf_release(&cap_buf);
789
0
  free(push_cert_nonce);
790
0
  return ret;
791
0
}