Coverage Report

Created: 2023-11-19 07:08

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