Coverage Report

Created: 2024-09-16 06:10

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