Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/receive-pack.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "abspath.h"
3
#include "repository.h"
4
#include "config.h"
5
#include "environment.h"
6
#include "gettext.h"
7
#include "hex.h"
8
#include "lockfile.h"
9
#include "pack.h"
10
#include "refs.h"
11
#include "pkt-line.h"
12
#include "sideband.h"
13
#include "run-command.h"
14
#include "hook.h"
15
#include "exec-cmd.h"
16
#include "commit.h"
17
#include "object.h"
18
#include "remote.h"
19
#include "connect.h"
20
#include "string-list.h"
21
#include "oid-array.h"
22
#include "connected.h"
23
#include "strvec.h"
24
#include "version.h"
25
#include "gpg-interface.h"
26
#include "sigchain.h"
27
#include "fsck.h"
28
#include "tmp-objdir.h"
29
#include "oidset.h"
30
#include "packfile.h"
31
#include "object-name.h"
32
#include "object-store-ll.h"
33
#include "path.h"
34
#include "protocol.h"
35
#include "commit-reach.h"
36
#include "server-info.h"
37
#include "trace.h"
38
#include "trace2.h"
39
#include "worktree.h"
40
#include "shallow.h"
41
#include "parse-options.h"
42
43
static const char * const receive_pack_usage[] = {
44
  N_("git receive-pack <git-dir>"),
45
  NULL
46
};
47
48
enum deny_action {
49
  DENY_UNCONFIGURED,
50
  DENY_IGNORE,
51
  DENY_WARN,
52
  DENY_REFUSE,
53
  DENY_UPDATE_INSTEAD
54
};
55
56
static int deny_deletes;
57
static int deny_non_fast_forwards;
58
static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
59
static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
60
static int receive_fsck_objects = -1;
61
static int transfer_fsck_objects = -1;
62
static struct strbuf fsck_msg_types = STRBUF_INIT;
63
static int receive_unpack_limit = -1;
64
static int transfer_unpack_limit = -1;
65
static int advertise_atomic_push = 1;
66
static int advertise_push_options;
67
static int advertise_sid;
68
static int unpack_limit = 100;
69
static off_t max_input_size;
70
static int report_status;
71
static int report_status_v2;
72
static int use_sideband;
73
static int use_atomic;
74
static int use_push_options;
75
static int quiet;
76
static int prefer_ofs_delta = 1;
77
static int auto_update_server_info;
78
static int auto_gc = 1;
79
static int reject_thin;
80
static int stateless_rpc;
81
static const char *service_dir;
82
static const char *head_name;
83
static void *head_name_to_free;
84
static int sent_capabilities;
85
static int shallow_update;
86
static const char *alt_shallow_file;
87
static struct strbuf push_cert = STRBUF_INIT;
88
static struct object_id push_cert_oid;
89
static struct signature_check sigcheck;
90
static const char *push_cert_nonce;
91
static char *cert_nonce_seed;
92
static struct strvec hidden_refs = STRVEC_INIT;
93
94
static const char *NONCE_UNSOLICITED = "UNSOLICITED";
95
static const char *NONCE_BAD = "BAD";
96
static const char *NONCE_MISSING = "MISSING";
97
static const char *NONCE_OK = "OK";
98
static const char *NONCE_SLOP = "SLOP";
99
static const char *nonce_status;
100
static long nonce_stamp_slop;
101
static timestamp_t nonce_stamp_slop_limit;
102
static struct ref_transaction *transaction;
103
104
static enum {
105
  KEEPALIVE_NEVER = 0,
106
  KEEPALIVE_AFTER_NUL,
107
  KEEPALIVE_ALWAYS
108
} use_keepalive;
109
static int keepalive_in_sec = 5;
110
111
static struct tmp_objdir *tmp_objdir;
112
113
static struct proc_receive_ref {
114
  unsigned int want_add:1,
115
         want_delete:1,
116
         want_modify:1,
117
         negative_ref:1;
118
  char *ref_prefix;
119
  struct proc_receive_ref *next;
120
} *proc_receive_ref;
121
122
static void proc_receive_ref_append(const char *prefix);
123
124
static enum deny_action parse_deny_action(const char *var, const char *value)
125
0
{
126
0
  if (value) {
127
0
    if (!strcasecmp(value, "ignore"))
128
0
      return DENY_IGNORE;
129
0
    if (!strcasecmp(value, "warn"))
130
0
      return DENY_WARN;
131
0
    if (!strcasecmp(value, "refuse"))
132
0
      return DENY_REFUSE;
133
0
    if (!strcasecmp(value, "updateinstead"))
134
0
      return DENY_UPDATE_INSTEAD;
135
0
  }
136
0
  if (git_config_bool(var, value))
137
0
    return DENY_REFUSE;
138
0
  return DENY_IGNORE;
139
0
}
140
141
static int receive_pack_config(const char *var, const char *value,
142
             const struct config_context *ctx, void *cb)
143
0
{
144
0
  const char *msg_id;
145
0
  int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
146
147
0
  if (status)
148
0
    return status;
149
150
0
  if (strcmp(var, "receive.denydeletes") == 0) {
151
0
    deny_deletes = git_config_bool(var, value);
152
0
    return 0;
153
0
  }
154
155
0
  if (strcmp(var, "receive.denynonfastforwards") == 0) {
156
0
    deny_non_fast_forwards = git_config_bool(var, value);
157
0
    return 0;
158
0
  }
159
160
0
  if (strcmp(var, "receive.unpacklimit") == 0) {
161
0
    receive_unpack_limit = git_config_int(var, value, ctx->kvi);
162
0
    return 0;
163
0
  }
164
165
0
  if (strcmp(var, "transfer.unpacklimit") == 0) {
166
0
    transfer_unpack_limit = git_config_int(var, value, ctx->kvi);
167
0
    return 0;
168
0
  }
169
170
0
  if (strcmp(var, "receive.fsck.skiplist") == 0) {
171
0
    char *path;
172
173
0
    if (git_config_pathname(&path, var, value))
174
0
      return 1;
175
0
    strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
176
0
      fsck_msg_types.len ? ',' : '=', path);
177
0
    free(path);
178
0
    return 0;
179
0
  }
180
181
0
  if (skip_prefix(var, "receive.fsck.", &msg_id)) {
182
0
    if (!value)
183
0
      return config_error_nonbool(var);
184
0
    if (is_valid_msg_type(msg_id, value))
185
0
      strbuf_addf(&fsck_msg_types, "%c%s=%s",
186
0
        fsck_msg_types.len ? ',' : '=', msg_id, value);
187
0
    else
188
0
      warning("skipping unknown msg id '%s'", msg_id);
189
0
    return 0;
190
0
  }
191
192
0
  if (strcmp(var, "receive.fsckobjects") == 0) {
193
0
    receive_fsck_objects = git_config_bool(var, value);
194
0
    return 0;
195
0
  }
196
197
0
  if (strcmp(var, "transfer.fsckobjects") == 0) {
198
0
    transfer_fsck_objects = git_config_bool(var, value);
199
0
    return 0;
200
0
  }
201
202
0
  if (!strcmp(var, "receive.denycurrentbranch")) {
203
0
    deny_current_branch = parse_deny_action(var, value);
204
0
    return 0;
205
0
  }
206
207
0
  if (strcmp(var, "receive.denydeletecurrent") == 0) {
208
0
    deny_delete_current = parse_deny_action(var, value);
209
0
    return 0;
210
0
  }
211
212
0
  if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
213
0
    prefer_ofs_delta = git_config_bool(var, value);
214
0
    return 0;
215
0
  }
216
217
0
  if (strcmp(var, "receive.updateserverinfo") == 0) {
218
0
    auto_update_server_info = git_config_bool(var, value);
219
0
    return 0;
220
0
  }
221
222
0
  if (strcmp(var, "receive.autogc") == 0) {
223
0
    auto_gc = git_config_bool(var, value);
224
0
    return 0;
225
0
  }
226
227
0
  if (strcmp(var, "receive.shallowupdate") == 0) {
228
0
    shallow_update = git_config_bool(var, value);
229
0
    return 0;
230
0
  }
231
232
0
  if (strcmp(var, "receive.certnonceseed") == 0)
233
0
    return git_config_string(&cert_nonce_seed, var, value);
234
235
0
  if (strcmp(var, "receive.certnonceslop") == 0) {
236
0
    nonce_stamp_slop_limit = git_config_ulong(var, value, ctx->kvi);
237
0
    return 0;
238
0
  }
239
240
0
  if (strcmp(var, "receive.advertiseatomic") == 0) {
241
0
    advertise_atomic_push = git_config_bool(var, value);
242
0
    return 0;
243
0
  }
244
245
0
  if (strcmp(var, "receive.advertisepushoptions") == 0) {
246
0
    advertise_push_options = git_config_bool(var, value);
247
0
    return 0;
248
0
  }
249
250
0
  if (strcmp(var, "receive.keepalive") == 0) {
251
0
    keepalive_in_sec = git_config_int(var, value, ctx->kvi);
252
0
    return 0;
253
0
  }
254
255
0
  if (strcmp(var, "receive.maxinputsize") == 0) {
256
0
    max_input_size = git_config_int64(var, value, ctx->kvi);
257
0
    return 0;
258
0
  }
259
260
0
  if (strcmp(var, "receive.procreceiverefs") == 0) {
261
0
    if (!value)
262
0
      return config_error_nonbool(var);
263
0
    proc_receive_ref_append(value);
264
0
    return 0;
265
0
  }
266
267
0
  if (strcmp(var, "transfer.advertisesid") == 0) {
268
0
    advertise_sid = git_config_bool(var, value);
269
0
    return 0;
270
0
  }
271
272
0
  return git_default_config(var, value, ctx, cb);
273
0
}
274
275
static void show_ref(const char *path, const struct object_id *oid)
276
0
{
277
0
  if (sent_capabilities) {
278
0
    packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), path);
279
0
  } else {
280
0
    struct strbuf cap = STRBUF_INIT;
281
282
0
    strbuf_addstr(&cap,
283
0
            "report-status report-status-v2 delete-refs side-band-64k quiet");
284
0
    if (advertise_atomic_push)
285
0
      strbuf_addstr(&cap, " atomic");
286
0
    if (prefer_ofs_delta)
287
0
      strbuf_addstr(&cap, " ofs-delta");
288
0
    if (push_cert_nonce)
289
0
      strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
290
0
    if (advertise_push_options)
291
0
      strbuf_addstr(&cap, " push-options");
292
0
    if (advertise_sid)
293
0
      strbuf_addf(&cap, " session-id=%s", trace2_session_id());
294
0
    strbuf_addf(&cap, " object-format=%s", the_hash_algo->name);
295
0
    strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
296
0
    packet_write_fmt(1, "%s %s%c%s\n",
297
0
           oid_to_hex(oid), path, 0, cap.buf);
298
0
    strbuf_release(&cap);
299
0
    sent_capabilities = 1;
300
0
  }
301
0
}
302
303
static int show_ref_cb(const char *path_full, const char *referent UNUSED, const struct object_id *oid,
304
           int flag UNUSED, void *data)
305
0
{
306
0
  struct oidset *seen = data;
307
0
  const char *path = strip_namespace(path_full);
308
309
0
  if (ref_is_hidden(path, path_full, &hidden_refs))
310
0
    return 0;
311
312
  /*
313
   * Advertise refs outside our current namespace as ".have"
314
   * refs, so that the client can use them to minimize data
315
   * transfer but will otherwise ignore them.
316
   */
317
0
  if (!path) {
318
0
    if (oidset_insert(seen, oid))
319
0
      return 0;
320
0
    path = ".have";
321
0
  } else {
322
0
    oidset_insert(seen, oid);
323
0
  }
324
0
  show_ref(path, oid);
325
0
  return 0;
326
0
}
327
328
static void show_one_alternate_ref(const struct object_id *oid,
329
           void *data)
330
0
{
331
0
  struct oidset *seen = data;
332
333
0
  if (oidset_insert(seen, oid))
334
0
    return;
335
336
0
  show_ref(".have", oid);
337
0
}
338
339
static void write_head_info(void)
340
0
{
341
0
  static struct oidset seen = OIDSET_INIT;
342
343
0
  refs_for_each_fullref_in(get_main_ref_store(the_repository), "",
344
0
         hidden_refs_to_excludes(&hidden_refs),
345
0
         show_ref_cb, &seen);
346
0
  for_each_alternate_ref(show_one_alternate_ref, &seen);
347
0
  oidset_clear(&seen);
348
0
  if (!sent_capabilities)
349
0
    show_ref("capabilities^{}", null_oid());
350
351
0
  advertise_shallow_grafts(1);
352
353
  /* EOF */
354
0
  packet_flush(1);
355
0
}
356
357
0
#define RUN_PROC_RECEIVE_SCHEDULED  1
358
0
#define RUN_PROC_RECEIVE_RETURNED 2
359
struct command {
360
  struct command *next;
361
  const char *error_string;
362
  struct ref_push_report *report;
363
  unsigned int skip_update:1,
364
         did_not_exist:1,
365
         run_proc_receive:2;
366
  int index;
367
  struct object_id old_oid;
368
  struct object_id new_oid;
369
  char ref_name[FLEX_ARRAY]; /* more */
370
};
371
372
static void proc_receive_ref_append(const char *prefix)
373
0
{
374
0
  struct proc_receive_ref *ref_pattern;
375
0
  char *p;
376
0
  int len;
377
378
0
  CALLOC_ARRAY(ref_pattern, 1);
379
0
  p = strchr(prefix, ':');
380
0
  if (p) {
381
0
    while (prefix < p) {
382
0
      if (*prefix == 'a')
383
0
        ref_pattern->want_add = 1;
384
0
      else if (*prefix == 'd')
385
0
        ref_pattern->want_delete = 1;
386
0
      else if (*prefix == 'm')
387
0
        ref_pattern->want_modify = 1;
388
0
      else if (*prefix == '!')
389
0
        ref_pattern->negative_ref = 1;
390
0
      prefix++;
391
0
    }
392
0
    prefix++;
393
0
  } else {
394
0
    ref_pattern->want_add = 1;
395
0
    ref_pattern->want_delete = 1;
396
0
    ref_pattern->want_modify = 1;
397
0
  }
398
0
  len = strlen(prefix);
399
0
  while (len && prefix[len - 1] == '/')
400
0
    len--;
401
0
  ref_pattern->ref_prefix = xmemdupz(prefix, len);
402
0
  if (!proc_receive_ref) {
403
0
    proc_receive_ref = ref_pattern;
404
0
  } else {
405
0
    struct proc_receive_ref *end;
406
407
0
    end = proc_receive_ref;
408
0
    while (end->next)
409
0
      end = end->next;
410
0
    end->next = ref_pattern;
411
0
  }
412
0
}
413
414
static int proc_receive_ref_matches(struct command *cmd)
415
0
{
416
0
  struct proc_receive_ref *p;
417
418
0
  if (!proc_receive_ref)
419
0
    return 0;
420
421
0
  for (p = proc_receive_ref; p; p = p->next) {
422
0
    const char *match = p->ref_prefix;
423
0
    const char *remains;
424
425
0
    if (!p->want_add && is_null_oid(&cmd->old_oid))
426
0
      continue;
427
0
    else if (!p->want_delete && is_null_oid(&cmd->new_oid))
428
0
      continue;
429
0
    else if (!p->want_modify &&
430
0
       !is_null_oid(&cmd->old_oid) &&
431
0
       !is_null_oid(&cmd->new_oid))
432
0
      continue;
433
434
0
    if (skip_prefix(cmd->ref_name, match, &remains) &&
435
0
        (!*remains || *remains == '/')) {
436
0
      if (!p->negative_ref)
437
0
        return 1;
438
0
    } else if (p->negative_ref) {
439
0
      return 1;
440
0
    }
441
0
  }
442
0
  return 0;
443
0
}
444
445
static void report_message(const char *prefix, const char *err, va_list params)
446
0
{
447
0
  int sz;
448
0
  char msg[4096];
449
450
0
  sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
451
0
  sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
452
0
  if (sz > (sizeof(msg) - 1))
453
0
    sz = sizeof(msg) - 1;
454
0
  msg[sz++] = '\n';
455
456
0
  if (use_sideband)
457
0
    send_sideband(1, 2, msg, sz, use_sideband);
458
0
  else
459
0
    xwrite(2, msg, sz);
460
0
}
461
462
__attribute__((format (printf, 1, 2)))
463
static void rp_warning(const char *err, ...)
464
0
{
465
0
  va_list params;
466
0
  va_start(params, err);
467
0
  report_message("warning: ", err, params);
468
0
  va_end(params);
469
0
}
470
471
__attribute__((format (printf, 1, 2)))
472
static void rp_error(const char *err, ...)
473
0
{
474
0
  va_list params;
475
0
  va_start(params, err);
476
0
  report_message("error: ", err, params);
477
0
  va_end(params);
478
0
}
479
480
static int copy_to_sideband(int in, int out UNUSED, void *arg UNUSED)
481
0
{
482
0
  char data[128];
483
0
  int keepalive_active = 0;
484
485
0
  if (keepalive_in_sec <= 0)
486
0
    use_keepalive = KEEPALIVE_NEVER;
487
0
  if (use_keepalive == KEEPALIVE_ALWAYS)
488
0
    keepalive_active = 1;
489
490
0
  while (1) {
491
0
    ssize_t sz;
492
493
0
    if (keepalive_active) {
494
0
      struct pollfd pfd;
495
0
      int ret;
496
497
0
      pfd.fd = in;
498
0
      pfd.events = POLLIN;
499
0
      ret = poll(&pfd, 1, 1000 * keepalive_in_sec);
500
501
0
      if (ret < 0) {
502
0
        if (errno == EINTR)
503
0
          continue;
504
0
        else
505
0
          break;
506
0
      } else if (ret == 0) {
507
        /* no data; send a keepalive packet */
508
0
        static const char buf[] = "0005\1";
509
0
        write_or_die(1, buf, sizeof(buf) - 1);
510
0
        continue;
511
0
      } /* else there is actual data to read */
512
0
    }
513
514
0
    sz = xread(in, data, sizeof(data));
515
0
    if (sz <= 0)
516
0
      break;
517
518
0
    if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) {
519
0
      const char *p = memchr(data, '\0', sz);
520
0
      if (p) {
521
        /*
522
         * The NUL tells us to start sending keepalives. Make
523
         * sure we send any other data we read along
524
         * with it.
525
         */
526
0
        keepalive_active = 1;
527
0
        send_sideband(1, 2, data, p - data, use_sideband);
528
0
        send_sideband(1, 2, p + 1, sz - (p - data + 1), use_sideband);
529
0
        continue;
530
0
      }
531
0
    }
532
533
    /*
534
     * Either we're not looking for a NUL signal, or we didn't see
535
     * it yet; just pass along the data.
536
     */
537
0
    send_sideband(1, 2, data, sz, use_sideband);
538
0
  }
539
0
  close(in);
540
0
  return 0;
541
0
}
542
543
static void hmac_hash(unsigned char *out,
544
          const char *key_in, size_t key_len,
545
          const char *text, size_t text_len)
546
0
{
547
0
  unsigned char key[GIT_MAX_BLKSZ];
548
0
  unsigned char k_ipad[GIT_MAX_BLKSZ];
549
0
  unsigned char k_opad[GIT_MAX_BLKSZ];
550
0
  int i;
551
0
  git_hash_ctx ctx;
552
553
  /* RFC 2104 2. (1) */
554
0
  memset(key, '\0', GIT_MAX_BLKSZ);
555
0
  if (the_hash_algo->blksz < key_len) {
556
0
    the_hash_algo->init_fn(&ctx);
557
0
    the_hash_algo->update_fn(&ctx, key_in, key_len);
558
0
    the_hash_algo->final_fn(key, &ctx);
559
0
  } else {
560
0
    memcpy(key, key_in, key_len);
561
0
  }
562
563
  /* RFC 2104 2. (2) & (5) */
564
0
  for (i = 0; i < sizeof(key); i++) {
565
0
    k_ipad[i] = key[i] ^ 0x36;
566
0
    k_opad[i] = key[i] ^ 0x5c;
567
0
  }
568
569
  /* RFC 2104 2. (3) & (4) */
570
0
  the_hash_algo->init_fn(&ctx);
571
0
  the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
572
0
  the_hash_algo->update_fn(&ctx, text, text_len);
573
0
  the_hash_algo->final_fn(out, &ctx);
574
575
  /* RFC 2104 2. (6) & (7) */
576
0
  the_hash_algo->init_fn(&ctx);
577
0
  the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
578
0
  the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
579
0
  the_hash_algo->final_fn(out, &ctx);
580
0
}
581
582
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
583
0
{
584
0
  struct strbuf buf = STRBUF_INIT;
585
0
  unsigned char hash[GIT_MAX_RAWSZ];
586
587
0
  strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
588
0
  hmac_hash(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
589
0
  strbuf_release(&buf);
590
591
  /* RFC 2104 5. HMAC-SHA1 or HMAC-SHA256 */
592
0
  strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash));
593
0
  return strbuf_detach(&buf, NULL);
594
0
}
595
596
/*
597
 * Return zero if a and b are equal up to n bytes and nonzero if they are not.
598
 * This operation is guaranteed to run in constant time to avoid leaking data.
599
 */
600
static int constant_memequal(const char *a, const char *b, size_t n)
601
0
{
602
0
  int res = 0;
603
0
  size_t i;
604
605
0
  for (i = 0; i < n; i++)
606
0
    res |= a[i] ^ b[i];
607
0
  return res;
608
0
}
609
610
static const char *check_nonce(const char *buf)
611
0
{
612
0
  size_t noncelen;
613
0
  const char *found = find_commit_header(buf, "nonce", &noncelen);
614
0
  char *nonce = found ? xmemdupz(found, noncelen) : NULL;
615
0
  timestamp_t stamp, ostamp;
616
0
  char *bohmac, *expect = NULL;
617
0
  const char *retval = NONCE_BAD;
618
619
0
  if (!nonce) {
620
0
    retval = NONCE_MISSING;
621
0
    goto leave;
622
0
  } else if (!push_cert_nonce) {
623
0
    retval = NONCE_UNSOLICITED;
624
0
    goto leave;
625
0
  } else if (!strcmp(push_cert_nonce, nonce)) {
626
0
    retval = NONCE_OK;
627
0
    goto leave;
628
0
  }
629
630
0
  if (!stateless_rpc) {
631
    /* returned nonce MUST match what we gave out earlier */
632
0
    retval = NONCE_BAD;
633
0
    goto leave;
634
0
  }
635
636
  /*
637
   * In stateless mode, we may be receiving a nonce issued by
638
   * another instance of the server that serving the same
639
   * repository, and the timestamps may not match, but the
640
   * nonce-seed and dir should match, so we can recompute and
641
   * report the time slop.
642
   *
643
   * In addition, when a nonce issued by another instance has
644
   * timestamp within receive.certnonceslop seconds, we pretend
645
   * as if we issued that nonce when reporting to the hook.
646
   */
647
648
  /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
649
0
  if (*nonce <= '0' || '9' < *nonce) {
650
0
    retval = NONCE_BAD;
651
0
    goto leave;
652
0
  }
653
0
  stamp = parse_timestamp(nonce, &bohmac, 10);
654
0
  if (bohmac == nonce || bohmac[0] != '-') {
655
0
    retval = NONCE_BAD;
656
0
    goto leave;
657
0
  }
658
659
0
  expect = prepare_push_cert_nonce(service_dir, stamp);
660
0
  if (noncelen != strlen(expect)) {
661
    /* This is not even the right size. */
662
0
    retval = NONCE_BAD;
663
0
    goto leave;
664
0
  }
665
0
  if (constant_memequal(expect, nonce, noncelen)) {
666
    /* Not what we would have signed earlier */
667
0
    retval = NONCE_BAD;
668
0
    goto leave;
669
0
  }
670
671
  /*
672
   * By how many seconds is this nonce stale?  Negative value
673
   * would mean it was issued by another server with its clock
674
   * skewed in the future.
675
   */
676
0
  ostamp = parse_timestamp(push_cert_nonce, NULL, 10);
677
0
  nonce_stamp_slop = (long)ostamp - (long)stamp;
678
679
0
  if (nonce_stamp_slop_limit &&
680
0
      labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
681
    /*
682
     * Pretend as if the received nonce (which passes the
683
     * HMAC check, so it is not a forged by third-party)
684
     * is what we issued.
685
     */
686
0
    free((void *)push_cert_nonce);
687
0
    push_cert_nonce = xstrdup(nonce);
688
0
    retval = NONCE_OK;
689
0
  } else {
690
0
    retval = NONCE_SLOP;
691
0
  }
692
693
0
leave:
694
0
  free(nonce);
695
0
  free(expect);
696
0
  return retval;
697
0
}
698
699
/*
700
 * Return 1 if there is no push_cert or if the push options in push_cert are
701
 * the same as those in the argument; 0 otherwise.
702
 */
703
static int check_cert_push_options(const struct string_list *push_options)
704
0
{
705
0
  const char *buf = push_cert.buf;
706
707
0
  const char *option;
708
0
  size_t optionlen;
709
0
  int options_seen = 0;
710
711
0
  int retval = 1;
712
713
0
  if (!*buf)
714
0
    return 1;
715
716
0
  while ((option = find_commit_header(buf, "push-option", &optionlen))) {
717
0
    buf = option + optionlen + 1;
718
0
    options_seen++;
719
0
    if (options_seen > push_options->nr
720
0
        || xstrncmpz(push_options->items[options_seen - 1].string,
721
0
         option, optionlen))
722
0
      return 0;
723
0
  }
724
725
0
  if (options_seen != push_options->nr)
726
0
    retval = 0;
727
728
0
  return retval;
729
0
}
730
731
static void prepare_push_cert_sha1(struct child_process *proc)
732
0
{
733
0
  static int already_done;
734
735
0
  if (!push_cert.len)
736
0
    return;
737
738
0
  if (!already_done) {
739
0
    int bogs /* beginning_of_gpg_sig */;
740
741
0
    already_done = 1;
742
0
    if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
743
0
              &push_cert_oid))
744
0
      oidclr(&push_cert_oid, the_repository->hash_algo);
745
746
0
    memset(&sigcheck, '\0', sizeof(sigcheck));
747
748
0
    bogs = parse_signed_buffer(push_cert.buf, push_cert.len);
749
0
    sigcheck.payload = xmemdupz(push_cert.buf, bogs);
750
0
    sigcheck.payload_len = bogs;
751
0
    check_signature(&sigcheck, push_cert.buf + bogs,
752
0
        push_cert.len - bogs);
753
754
0
    nonce_status = check_nonce(sigcheck.payload);
755
0
  }
756
0
  if (!is_null_oid(&push_cert_oid)) {
757
0
    strvec_pushf(&proc->env, "GIT_PUSH_CERT=%s",
758
0
           oid_to_hex(&push_cert_oid));
759
0
    strvec_pushf(&proc->env, "GIT_PUSH_CERT_SIGNER=%s",
760
0
           sigcheck.signer ? sigcheck.signer : "");
761
0
    strvec_pushf(&proc->env, "GIT_PUSH_CERT_KEY=%s",
762
0
           sigcheck.key ? sigcheck.key : "");
763
0
    strvec_pushf(&proc->env, "GIT_PUSH_CERT_STATUS=%c",
764
0
           sigcheck.result);
765
0
    if (push_cert_nonce) {
766
0
      strvec_pushf(&proc->env,
767
0
             "GIT_PUSH_CERT_NONCE=%s",
768
0
             push_cert_nonce);
769
0
      strvec_pushf(&proc->env,
770
0
             "GIT_PUSH_CERT_NONCE_STATUS=%s",
771
0
             nonce_status);
772
0
      if (nonce_status == NONCE_SLOP)
773
0
        strvec_pushf(&proc->env,
774
0
               "GIT_PUSH_CERT_NONCE_SLOP=%ld",
775
0
               nonce_stamp_slop);
776
0
    }
777
0
  }
778
0
}
779
780
struct receive_hook_feed_state {
781
  struct command *cmd;
782
  struct ref_push_report *report;
783
  int skip_broken;
784
  struct strbuf buf;
785
  const struct string_list *push_options;
786
};
787
788
typedef int (*feed_fn)(void *, const char **, size_t *);
789
static int run_and_feed_hook(const char *hook_name, feed_fn feed,
790
           struct receive_hook_feed_state *feed_state)
791
0
{
792
0
  struct child_process proc = CHILD_PROCESS_INIT;
793
0
  struct async muxer;
794
0
  int code;
795
0
  const char *hook_path = find_hook(the_repository, hook_name);
796
797
0
  if (!hook_path)
798
0
    return 0;
799
800
0
  strvec_push(&proc.args, hook_path);
801
0
  proc.in = -1;
802
0
  proc.stdout_to_stderr = 1;
803
0
  proc.trace2_hook_name = hook_name;
804
805
0
  if (feed_state->push_options) {
806
0
    size_t i;
807
0
    for (i = 0; i < feed_state->push_options->nr; i++)
808
0
      strvec_pushf(&proc.env,
809
0
             "GIT_PUSH_OPTION_%"PRIuMAX"=%s",
810
0
             (uintmax_t)i,
811
0
             feed_state->push_options->items[i].string);
812
0
    strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT=%"PRIuMAX"",
813
0
           (uintmax_t)feed_state->push_options->nr);
814
0
  } else
815
0
    strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT");
816
817
0
  if (tmp_objdir)
818
0
    strvec_pushv(&proc.env, tmp_objdir_env(tmp_objdir));
819
820
0
  if (use_sideband) {
821
0
    memset(&muxer, 0, sizeof(muxer));
822
0
    muxer.proc = copy_to_sideband;
823
0
    muxer.in = -1;
824
0
    code = start_async(&muxer);
825
0
    if (code)
826
0
      return code;
827
0
    proc.err = muxer.in;
828
0
  }
829
830
0
  prepare_push_cert_sha1(&proc);
831
832
0
  code = start_command(&proc);
833
0
  if (code) {
834
0
    if (use_sideband)
835
0
      finish_async(&muxer);
836
0
    return code;
837
0
  }
838
839
0
  sigchain_push(SIGPIPE, SIG_IGN);
840
841
0
  while (1) {
842
0
    const char *buf;
843
0
    size_t n;
844
0
    if (feed(feed_state, &buf, &n))
845
0
      break;
846
0
    if (write_in_full(proc.in, buf, n) < 0)
847
0
      break;
848
0
  }
849
0
  close(proc.in);
850
0
  if (use_sideband)
851
0
    finish_async(&muxer);
852
853
0
  sigchain_pop(SIGPIPE);
854
855
0
  return finish_command(&proc);
856
0
}
857
858
static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
859
0
{
860
0
  struct receive_hook_feed_state *state = state_;
861
0
  struct command *cmd = state->cmd;
862
863
0
  while (cmd &&
864
0
         state->skip_broken && (cmd->error_string || cmd->did_not_exist))
865
0
    cmd = cmd->next;
866
0
  if (!cmd)
867
0
    return -1; /* EOF */
868
0
  if (!bufp)
869
0
    return 0; /* OK, can feed something. */
870
0
  strbuf_reset(&state->buf);
871
0
  if (!state->report)
872
0
    state->report = cmd->report;
873
0
  if (state->report) {
874
0
    struct object_id *old_oid;
875
0
    struct object_id *new_oid;
876
0
    const char *ref_name;
877
878
0
    old_oid = state->report->old_oid ? state->report->old_oid : &cmd->old_oid;
879
0
    new_oid = state->report->new_oid ? state->report->new_oid : &cmd->new_oid;
880
0
    ref_name = state->report->ref_name ? state->report->ref_name : cmd->ref_name;
881
0
    strbuf_addf(&state->buf, "%s %s %s\n",
882
0
          oid_to_hex(old_oid), oid_to_hex(new_oid),
883
0
          ref_name);
884
0
    state->report = state->report->next;
885
0
    if (!state->report)
886
0
      state->cmd = cmd->next;
887
0
  } else {
888
0
    strbuf_addf(&state->buf, "%s %s %s\n",
889
0
          oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid),
890
0
          cmd->ref_name);
891
0
    state->cmd = cmd->next;
892
0
  }
893
0
  if (bufp) {
894
0
    *bufp = state->buf.buf;
895
0
    *sizep = state->buf.len;
896
0
  }
897
0
  return 0;
898
0
}
899
900
static int run_receive_hook(struct command *commands,
901
          const char *hook_name,
902
          int skip_broken,
903
          const struct string_list *push_options)
904
0
{
905
0
  struct receive_hook_feed_state state;
906
0
  int status;
907
908
0
  strbuf_init(&state.buf, 0);
909
0
  state.cmd = commands;
910
0
  state.skip_broken = skip_broken;
911
0
  state.report = NULL;
912
0
  if (feed_receive_hook(&state, NULL, NULL))
913
0
    return 0;
914
0
  state.cmd = commands;
915
0
  state.push_options = push_options;
916
0
  status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
917
0
  strbuf_release(&state.buf);
918
0
  return status;
919
0
}
920
921
static int run_update_hook(struct command *cmd)
922
0
{
923
0
  struct child_process proc = CHILD_PROCESS_INIT;
924
0
  int code;
925
0
  const char *hook_path = find_hook(the_repository, "update");
926
927
0
  if (!hook_path)
928
0
    return 0;
929
930
0
  strvec_push(&proc.args, hook_path);
931
0
  strvec_push(&proc.args, cmd->ref_name);
932
0
  strvec_push(&proc.args, oid_to_hex(&cmd->old_oid));
933
0
  strvec_push(&proc.args, oid_to_hex(&cmd->new_oid));
934
935
0
  proc.no_stdin = 1;
936
0
  proc.stdout_to_stderr = 1;
937
0
  proc.err = use_sideband ? -1 : 0;
938
0
  proc.trace2_hook_name = "update";
939
940
0
  code = start_command(&proc);
941
0
  if (code)
942
0
    return code;
943
0
  if (use_sideband)
944
0
    copy_to_sideband(proc.err, -1, NULL);
945
0
  return finish_command(&proc);
946
0
}
947
948
static struct command *find_command_by_refname(struct command *list,
949
                 const char *refname)
950
0
{
951
0
  for (; list; list = list->next)
952
0
    if (!strcmp(list->ref_name, refname))
953
0
      return list;
954
0
  return NULL;
955
0
}
956
957
static int read_proc_receive_report(struct packet_reader *reader,
958
            struct command *commands,
959
            struct strbuf *errmsg)
960
0
{
961
0
  struct command *cmd;
962
0
  struct command *hint = NULL;
963
0
  struct ref_push_report *report = NULL;
964
0
  int new_report = 0;
965
0
  int code = 0;
966
0
  int once = 0;
967
0
  int response = 0;
968
969
0
  for (;;) {
970
0
    struct object_id old_oid, new_oid;
971
0
    const char *head;
972
0
    const char *refname;
973
0
    char *p;
974
0
    enum packet_read_status status;
975
976
0
    status = packet_reader_read(reader);
977
0
    if (status != PACKET_READ_NORMAL) {
978
      /* Check whether proc-receive exited abnormally */
979
0
      if (status == PACKET_READ_EOF && !response) {
980
0
        strbuf_addstr(errmsg, "proc-receive exited abnormally");
981
0
        return -1;
982
0
      }
983
0
      break;
984
0
    }
985
0
    response++;
986
987
0
    head = reader->line;
988
0
    p = strchr(head, ' ');
989
0
    if (!p) {
990
0
      strbuf_addf(errmsg, "proc-receive reported incomplete status line: '%s'\n", head);
991
0
      code = -1;
992
0
      continue;
993
0
    }
994
0
    *p++ = '\0';
995
0
    if (!strcmp(head, "option")) {
996
0
      const char *key, *val;
997
998
0
      if (!hint || !(report || new_report)) {
999
0
        if (!once++)
1000
0
          strbuf_addstr(errmsg, "proc-receive reported 'option' without a matching 'ok/ng' directive\n");
1001
0
        code = -1;
1002
0
        continue;
1003
0
      }
1004
0
      if (new_report) {
1005
0
        if (!hint->report) {
1006
0
          CALLOC_ARRAY(hint->report, 1);
1007
0
          report = hint->report;
1008
0
        } else {
1009
0
          report = hint->report;
1010
0
          while (report->next)
1011
0
            report = report->next;
1012
0
          report->next = xcalloc(1, sizeof(struct ref_push_report));
1013
0
          report = report->next;
1014
0
        }
1015
0
        new_report = 0;
1016
0
      }
1017
0
      key = p;
1018
0
      p = strchr(key, ' ');
1019
0
      if (p)
1020
0
        *p++ = '\0';
1021
0
      val = p;
1022
0
      if (!strcmp(key, "refname"))
1023
0
        report->ref_name = xstrdup_or_null(val);
1024
0
      else if (!strcmp(key, "old-oid") && val &&
1025
0
         !parse_oid_hex(val, &old_oid, &val))
1026
0
        report->old_oid = oiddup(&old_oid);
1027
0
      else if (!strcmp(key, "new-oid") && val &&
1028
0
         !parse_oid_hex(val, &new_oid, &val))
1029
0
        report->new_oid = oiddup(&new_oid);
1030
0
      else if (!strcmp(key, "forced-update"))
1031
0
        report->forced_update = 1;
1032
0
      else if (!strcmp(key, "fall-through"))
1033
        /* Fall through, let 'receive-pack' to execute it. */
1034
0
        hint->run_proc_receive = 0;
1035
0
      continue;
1036
0
    }
1037
1038
0
    report = NULL;
1039
0
    new_report = 0;
1040
0
    refname = p;
1041
0
    p = strchr(refname, ' ');
1042
0
    if (p)
1043
0
      *p++ = '\0';
1044
0
    if (strcmp(head, "ok") && strcmp(head, "ng")) {
1045
0
      strbuf_addf(errmsg, "proc-receive reported bad status '%s' on ref '%s'\n",
1046
0
            head, refname);
1047
0
      code = -1;
1048
0
      continue;
1049
0
    }
1050
1051
    /* first try searching at our hint, falling back to all refs */
1052
0
    if (hint)
1053
0
      hint = find_command_by_refname(hint, refname);
1054
0
    if (!hint)
1055
0
      hint = find_command_by_refname(commands, refname);
1056
0
    if (!hint) {
1057
0
      strbuf_addf(errmsg, "proc-receive reported status on unknown ref: %s\n",
1058
0
            refname);
1059
0
      code = -1;
1060
0
      continue;
1061
0
    }
1062
0
    if (!hint->run_proc_receive) {
1063
0
      strbuf_addf(errmsg, "proc-receive reported status on unexpected ref: %s\n",
1064
0
            refname);
1065
0
      code = -1;
1066
0
      continue;
1067
0
    }
1068
0
    hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
1069
0
    if (!strcmp(head, "ng")) {
1070
0
      if (p)
1071
0
        hint->error_string = xstrdup(p);
1072
0
      else
1073
0
        hint->error_string = "failed";
1074
0
      code = -1;
1075
0
      continue;
1076
0
    }
1077
0
    new_report = 1;
1078
0
  }
1079
1080
0
  for (cmd = commands; cmd; cmd = cmd->next)
1081
0
    if (cmd->run_proc_receive && !cmd->error_string &&
1082
0
        !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
1083
0
        cmd->error_string = "proc-receive failed to report status";
1084
0
        code = -1;
1085
0
    }
1086
0
  return code;
1087
0
}
1088
1089
static int run_proc_receive_hook(struct command *commands,
1090
         const struct string_list *push_options)
1091
0
{
1092
0
  struct child_process proc = CHILD_PROCESS_INIT;
1093
0
  struct async muxer;
1094
0
  struct command *cmd;
1095
0
  struct packet_reader reader;
1096
0
  struct strbuf cap = STRBUF_INIT;
1097
0
  struct strbuf errmsg = STRBUF_INIT;
1098
0
  int hook_use_push_options = 0;
1099
0
  int version = 0;
1100
0
  int code;
1101
0
  const char *hook_path = find_hook(the_repository, "proc-receive");
1102
1103
0
  if (!hook_path) {
1104
0
    rp_error("cannot find hook 'proc-receive'");
1105
0
    return -1;
1106
0
  }
1107
1108
0
  strvec_push(&proc.args, hook_path);
1109
0
  proc.in = -1;
1110
0
  proc.out = -1;
1111
0
  proc.trace2_hook_name = "proc-receive";
1112
1113
0
  if (use_sideband) {
1114
0
    memset(&muxer, 0, sizeof(muxer));
1115
0
    muxer.proc = copy_to_sideband;
1116
0
    muxer.in = -1;
1117
0
    code = start_async(&muxer);
1118
0
    if (code)
1119
0
      return code;
1120
0
    proc.err = muxer.in;
1121
0
  } else {
1122
0
    proc.err = 0;
1123
0
  }
1124
1125
0
  code = start_command(&proc);
1126
0
  if (code) {
1127
0
    if (use_sideband)
1128
0
      finish_async(&muxer);
1129
0
    return code;
1130
0
  }
1131
1132
0
  sigchain_push(SIGPIPE, SIG_IGN);
1133
1134
  /* Version negotiaton */
1135
0
  packet_reader_init(&reader, proc.out, NULL, 0,
1136
0
         PACKET_READ_CHOMP_NEWLINE |
1137
0
         PACKET_READ_GENTLE_ON_EOF);
1138
0
  if (use_atomic)
1139
0
    strbuf_addstr(&cap, " atomic");
1140
0
  if (use_push_options)
1141
0
    strbuf_addstr(&cap, " push-options");
1142
0
  if (cap.len) {
1143
0
    code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
1144
0
    strbuf_release(&cap);
1145
0
  } else {
1146
0
    code = packet_write_fmt_gently(proc.in, "version=1\n");
1147
0
  }
1148
0
  if (!code)
1149
0
    code = packet_flush_gently(proc.in);
1150
1151
0
  if (!code)
1152
0
    for (;;) {
1153
0
      int linelen;
1154
0
      enum packet_read_status status;
1155
1156
0
      status = packet_reader_read(&reader);
1157
0
      if (status != PACKET_READ_NORMAL) {
1158
        /* Check whether proc-receive exited abnormally */
1159
0
        if (status == PACKET_READ_EOF)
1160
0
          code = -1;
1161
0
        break;
1162
0
      }
1163
1164
0
      if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1165
0
        version = atoi(reader.line + 8);
1166
0
        linelen = strlen(reader.line);
1167
0
        if (linelen < reader.pktlen) {
1168
0
          const char *feature_list = reader.line + linelen + 1;
1169
0
          if (parse_feature_request(feature_list, "push-options"))
1170
0
            hook_use_push_options = 1;
1171
0
        }
1172
0
      }
1173
0
    }
1174
1175
0
  if (code) {
1176
0
    strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook");
1177
0
    goto cleanup;
1178
0
  }
1179
1180
0
  switch (version) {
1181
0
  case 0:
1182
    /* fallthrough */
1183
0
  case 1:
1184
0
    break;
1185
0
  default:
1186
0
    strbuf_addf(&errmsg, "proc-receive version '%d' is not supported",
1187
0
          version);
1188
0
    code = -1;
1189
0
    goto cleanup;
1190
0
  }
1191
1192
  /* Send commands */
1193
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1194
0
    if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
1195
0
      continue;
1196
0
    code = packet_write_fmt_gently(proc.in, "%s %s %s",
1197
0
                 oid_to_hex(&cmd->old_oid),
1198
0
                 oid_to_hex(&cmd->new_oid),
1199
0
                 cmd->ref_name);
1200
0
    if (code)
1201
0
      break;
1202
0
  }
1203
0
  if (!code)
1204
0
    code = packet_flush_gently(proc.in);
1205
0
  if (code) {
1206
0
    strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook");
1207
0
    goto cleanup;
1208
0
  }
1209
1210
  /* Send push options */
1211
0
  if (hook_use_push_options) {
1212
0
    struct string_list_item *item;
1213
1214
0
    for_each_string_list_item(item, push_options) {
1215
0
      code = packet_write_fmt_gently(proc.in, "%s", item->string);
1216
0
      if (code)
1217
0
        break;
1218
0
    }
1219
0
    if (!code)
1220
0
      code = packet_flush_gently(proc.in);
1221
0
    if (code) {
1222
0
      strbuf_addstr(&errmsg,
1223
0
              "fail to write push-options to proc-receive hook");
1224
0
      goto cleanup;
1225
0
    }
1226
0
  }
1227
1228
  /* Read result from proc-receive */
1229
0
  code = read_proc_receive_report(&reader, commands, &errmsg);
1230
1231
0
cleanup:
1232
0
  close(proc.in);
1233
0
  close(proc.out);
1234
0
  if (use_sideband)
1235
0
    finish_async(&muxer);
1236
0
  if (finish_command(&proc))
1237
0
    code = -1;
1238
0
  if (errmsg.len >0) {
1239
0
    char *p = errmsg.buf;
1240
1241
0
    p += errmsg.len - 1;
1242
0
    if (*p == '\n')
1243
0
      *p = '\0';
1244
0
    rp_error("%s", errmsg.buf);
1245
0
    strbuf_release(&errmsg);
1246
0
  }
1247
0
  sigchain_pop(SIGPIPE);
1248
1249
0
  return code;
1250
0
}
1251
1252
static const char *refuse_unconfigured_deny_msg =
1253
  N_("By default, updating the current branch in a non-bare repository\n"
1254
     "is denied, because it will make the index and work tree inconsistent\n"
1255
     "with what you pushed, and will require 'git reset --hard' to match\n"
1256
     "the work tree to HEAD.\n"
1257
     "\n"
1258
     "You can set the 'receive.denyCurrentBranch' configuration variable\n"
1259
     "to 'ignore' or 'warn' in the remote repository to allow pushing into\n"
1260
     "its current branch; however, this is not recommended unless you\n"
1261
     "arranged to update its work tree to match what you pushed in some\n"
1262
     "other way.\n"
1263
     "\n"
1264
     "To squelch this message and still keep the default behaviour, set\n"
1265
     "'receive.denyCurrentBranch' configuration variable to 'refuse'.");
1266
1267
static void refuse_unconfigured_deny(void)
1268
0
{
1269
0
  rp_error("%s", _(refuse_unconfigured_deny_msg));
1270
0
}
1271
1272
static const char *refuse_unconfigured_deny_delete_current_msg =
1273
  N_("By default, deleting the current branch is denied, because the next\n"
1274
     "'git clone' won't result in any file checked out, causing confusion.\n"
1275
     "\n"
1276
     "You can set 'receive.denyDeleteCurrent' configuration variable to\n"
1277
     "'warn' or 'ignore' in the remote repository to allow deleting the\n"
1278
     "current branch, with or without a warning message.\n"
1279
     "\n"
1280
     "To squelch this message, you can set it to 'refuse'.");
1281
1282
static void refuse_unconfigured_deny_delete_current(void)
1283
0
{
1284
0
  rp_error("%s", _(refuse_unconfigured_deny_delete_current_msg));
1285
0
}
1286
1287
static const struct object_id *command_singleton_iterator(void *cb_data);
1288
static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
1289
0
{
1290
0
  struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
1291
0
  struct oid_array extra = OID_ARRAY_INIT;
1292
0
  struct check_connected_options opt = CHECK_CONNECTED_INIT;
1293
0
  uint32_t mask = 1 << (cmd->index % 32);
1294
0
  int i;
1295
1296
0
  trace_printf_key(&trace_shallow,
1297
0
       "shallow: update_shallow_ref %s\n", cmd->ref_name);
1298
0
  for (i = 0; i < si->shallow->nr; i++)
1299
0
    if (si->used_shallow[i] &&
1300
0
        (si->used_shallow[i][cmd->index / 32] & mask) &&
1301
0
        !delayed_reachability_test(si, i))
1302
0
      oid_array_append(&extra, &si->shallow->oid[i]);
1303
1304
0
  opt.env = tmp_objdir_env(tmp_objdir);
1305
0
  setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra);
1306
0
  if (check_connected(command_singleton_iterator, cmd, &opt)) {
1307
0
    rollback_shallow_file(the_repository, &shallow_lock);
1308
0
    oid_array_clear(&extra);
1309
0
    return -1;
1310
0
  }
1311
1312
0
  commit_shallow_file(the_repository, &shallow_lock);
1313
1314
  /*
1315
   * Make sure setup_alternate_shallow() for the next ref does
1316
   * not lose these new roots..
1317
   */
1318
0
  for (i = 0; i < extra.nr; i++)
1319
0
    register_shallow(the_repository, &extra.oid[i]);
1320
1321
0
  si->shallow_ref[cmd->index] = 0;
1322
0
  oid_array_clear(&extra);
1323
0
  return 0;
1324
0
}
1325
1326
/*
1327
 * NEEDSWORK: we should consolidate various implementions of "are we
1328
 * on an unborn branch?" test into one, and make the unified one more
1329
 * robust. !get_sha1() based check used here and elsewhere would not
1330
 * allow us to tell an unborn branch from corrupt ref, for example.
1331
 * For the purpose of fixing "deploy-to-update does not work when
1332
 * pushing into an empty repository" issue, this should suffice for
1333
 * now.
1334
 */
1335
static int head_has_history(void)
1336
0
{
1337
0
  struct object_id oid;
1338
1339
0
  return !repo_get_oid(the_repository, "HEAD", &oid);
1340
0
}
1341
1342
static const char *push_to_deploy(unsigned char *sha1,
1343
          struct strvec *env,
1344
          const char *work_tree)
1345
0
{
1346
0
  struct child_process child = CHILD_PROCESS_INIT;
1347
1348
0
  strvec_pushl(&child.args, "update-index", "-q", "--ignore-submodules",
1349
0
         "--refresh", NULL);
1350
0
  strvec_pushv(&child.env, env->v);
1351
0
  child.dir = work_tree;
1352
0
  child.no_stdin = 1;
1353
0
  child.stdout_to_stderr = 1;
1354
0
  child.git_cmd = 1;
1355
0
  if (run_command(&child))
1356
0
    return "Up-to-date check failed";
1357
1358
  /* run_command() does not clean up completely; reinitialize */
1359
0
  child_process_init(&child);
1360
0
  strvec_pushl(&child.args, "diff-files", "--quiet",
1361
0
         "--ignore-submodules", "--", NULL);
1362
0
  strvec_pushv(&child.env, env->v);
1363
0
  child.dir = work_tree;
1364
0
  child.no_stdin = 1;
1365
0
  child.stdout_to_stderr = 1;
1366
0
  child.git_cmd = 1;
1367
0
  if (run_command(&child))
1368
0
    return "Working directory has unstaged changes";
1369
1370
0
  child_process_init(&child);
1371
0
  strvec_pushl(&child.args, "diff-index", "--quiet", "--cached",
1372
0
         "--ignore-submodules",
1373
         /* diff-index with either HEAD or an empty tree */
1374
0
         head_has_history() ? "HEAD" : empty_tree_oid_hex(the_repository->hash_algo),
1375
0
         "--", NULL);
1376
0
  strvec_pushv(&child.env, env->v);
1377
0
  child.no_stdin = 1;
1378
0
  child.no_stdout = 1;
1379
0
  child.stdout_to_stderr = 0;
1380
0
  child.git_cmd = 1;
1381
0
  if (run_command(&child))
1382
0
    return "Working directory has staged changes";
1383
1384
0
  child_process_init(&child);
1385
0
  strvec_pushl(&child.args, "read-tree", "-u", "-m", hash_to_hex(sha1),
1386
0
         NULL);
1387
0
  strvec_pushv(&child.env, env->v);
1388
0
  child.dir = work_tree;
1389
0
  child.no_stdin = 1;
1390
0
  child.no_stdout = 1;
1391
0
  child.stdout_to_stderr = 0;
1392
0
  child.git_cmd = 1;
1393
0
  if (run_command(&child))
1394
0
    return "Could not update working tree to new HEAD";
1395
1396
0
  return NULL;
1397
0
}
1398
1399
static const char *push_to_checkout_hook = "push-to-checkout";
1400
1401
static const char *push_to_checkout(unsigned char *hash,
1402
            int *invoked_hook,
1403
            struct strvec *env,
1404
            const char *work_tree)
1405
0
{
1406
0
  struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1407
0
  opt.invoked_hook = invoked_hook;
1408
1409
0
  strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
1410
0
  strvec_pushv(&opt.env, env->v);
1411
0
  strvec_push(&opt.args, hash_to_hex(hash));
1412
0
  if (run_hooks_opt(the_repository, push_to_checkout_hook, &opt))
1413
0
    return "push-to-checkout hook declined";
1414
0
  else
1415
0
    return NULL;
1416
0
}
1417
1418
static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
1419
0
{
1420
0
  const char *retval, *git_dir;
1421
0
  struct strvec env = STRVEC_INIT;
1422
0
  int invoked_hook;
1423
1424
0
  if (!worktree || !worktree->path)
1425
0
    BUG("worktree->path must be non-NULL");
1426
1427
0
  if (worktree->is_bare)
1428
0
    return "denyCurrentBranch = updateInstead needs a worktree";
1429
0
  git_dir = get_worktree_git_dir(worktree);
1430
1431
0
  strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
1432
1433
0
  retval = push_to_checkout(sha1, &invoked_hook, &env, worktree->path);
1434
0
  if (!invoked_hook)
1435
0
    retval = push_to_deploy(sha1, &env, worktree->path);
1436
1437
0
  strvec_clear(&env);
1438
0
  return retval;
1439
0
}
1440
1441
static const char *update(struct command *cmd, struct shallow_info *si)
1442
0
{
1443
0
  const char *name = cmd->ref_name;
1444
0
  struct strbuf namespaced_name_buf = STRBUF_INIT;
1445
0
  static char *namespaced_name;
1446
0
  const char *ret;
1447
0
  struct object_id *old_oid = &cmd->old_oid;
1448
0
  struct object_id *new_oid = &cmd->new_oid;
1449
0
  int do_update_worktree = 0;
1450
0
  struct worktree **worktrees = get_worktrees();
1451
0
  const struct worktree *worktree =
1452
0
    find_shared_symref(worktrees, "HEAD", name);
1453
1454
  /* only refs/... are allowed */
1455
0
  if (!starts_with(name, "refs/") ||
1456
0
      check_refname_format(name + 5, is_null_oid(new_oid) ?
1457
0
         REFNAME_ALLOW_ONELEVEL : 0)) {
1458
0
    rp_error("refusing to update funny ref '%s' remotely", name);
1459
0
    ret = "funny refname";
1460
0
    goto out;
1461
0
  }
1462
1463
0
  strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
1464
0
  free(namespaced_name);
1465
0
  namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
1466
1467
0
  if (worktree && !worktree->is_bare) {
1468
0
    switch (deny_current_branch) {
1469
0
    case DENY_IGNORE:
1470
0
      break;
1471
0
    case DENY_WARN:
1472
0
      rp_warning("updating the current branch");
1473
0
      break;
1474
0
    case DENY_REFUSE:
1475
0
    case DENY_UNCONFIGURED:
1476
0
      rp_error("refusing to update checked out branch: %s", name);
1477
0
      if (deny_current_branch == DENY_UNCONFIGURED)
1478
0
        refuse_unconfigured_deny();
1479
0
      ret = "branch is currently checked out";
1480
0
      goto out;
1481
0
    case DENY_UPDATE_INSTEAD:
1482
      /* pass -- let other checks intervene first */
1483
0
      do_update_worktree = 1;
1484
0
      break;
1485
0
    }
1486
0
  }
1487
1488
0
  if (!is_null_oid(new_oid) && !repo_has_object_file(the_repository, new_oid)) {
1489
0
    error("unpack should have generated %s, "
1490
0
          "but I can't find it!", oid_to_hex(new_oid));
1491
0
    ret = "bad pack";
1492
0
    goto out;
1493
0
  }
1494
1495
0
  if (!is_null_oid(old_oid) && is_null_oid(new_oid)) {
1496
0
    if (deny_deletes && starts_with(name, "refs/heads/")) {
1497
0
      rp_error("denying ref deletion for %s", name);
1498
0
      ret = "deletion prohibited";
1499
0
      goto out;
1500
0
    }
1501
1502
0
    if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
1503
0
      switch (deny_delete_current) {
1504
0
      case DENY_IGNORE:
1505
0
        break;
1506
0
      case DENY_WARN:
1507
0
        rp_warning("deleting the current branch");
1508
0
        break;
1509
0
      case DENY_REFUSE:
1510
0
      case DENY_UNCONFIGURED:
1511
0
      case DENY_UPDATE_INSTEAD:
1512
0
        if (deny_delete_current == DENY_UNCONFIGURED)
1513
0
          refuse_unconfigured_deny_delete_current();
1514
0
        rp_error("refusing to delete the current branch: %s", name);
1515
0
        ret = "deletion of the current branch prohibited";
1516
0
        goto out;
1517
0
      default:
1518
0
        ret = "Invalid denyDeleteCurrent setting";
1519
0
        goto out;
1520
0
      }
1521
0
    }
1522
0
  }
1523
1524
0
  if (deny_non_fast_forwards && !is_null_oid(new_oid) &&
1525
0
      !is_null_oid(old_oid) &&
1526
0
      starts_with(name, "refs/heads/")) {
1527
0
    struct object *old_object, *new_object;
1528
0
    struct commit *old_commit, *new_commit;
1529
0
    int ret2;
1530
1531
0
    old_object = parse_object(the_repository, old_oid);
1532
0
    new_object = parse_object(the_repository, new_oid);
1533
1534
0
    if (!old_object || !new_object ||
1535
0
        old_object->type != OBJ_COMMIT ||
1536
0
        new_object->type != OBJ_COMMIT) {
1537
0
      error("bad sha1 objects for %s", name);
1538
0
      ret = "bad ref";
1539
0
      goto out;
1540
0
    }
1541
0
    old_commit = (struct commit *)old_object;
1542
0
    new_commit = (struct commit *)new_object;
1543
0
    ret2 = repo_in_merge_bases(the_repository, old_commit, new_commit);
1544
0
    if (ret2 < 0)
1545
0
      exit(128);
1546
0
    if (!ret2) {
1547
0
      rp_error("denying non-fast-forward %s"
1548
0
         " (you should pull first)", name);
1549
0
      ret = "non-fast-forward";
1550
0
      goto out;
1551
0
    }
1552
0
  }
1553
0
  if (run_update_hook(cmd)) {
1554
0
    rp_error("hook declined to update %s", name);
1555
0
    ret = "hook declined";
1556
0
    goto out;
1557
0
  }
1558
1559
0
  if (do_update_worktree) {
1560
0
    ret = update_worktree(new_oid->hash, worktree);
1561
0
    if (ret)
1562
0
      goto out;
1563
0
  }
1564
1565
0
  if (is_null_oid(new_oid)) {
1566
0
    struct strbuf err = STRBUF_INIT;
1567
0
    if (!parse_object(the_repository, old_oid)) {
1568
0
      old_oid = NULL;
1569
0
      if (refs_ref_exists(get_main_ref_store(the_repository), name)) {
1570
0
        rp_warning("allowing deletion of corrupt ref");
1571
0
      } else {
1572
0
        rp_warning("deleting a non-existent ref");
1573
0
        cmd->did_not_exist = 1;
1574
0
      }
1575
0
    }
1576
0
    if (ref_transaction_delete(transaction,
1577
0
             namespaced_name,
1578
0
             old_oid,
1579
0
             NULL, 0,
1580
0
             "push", &err)) {
1581
0
      rp_error("%s", err.buf);
1582
0
      ret = "failed to delete";
1583
0
    } else {
1584
0
      ret = NULL; /* good */
1585
0
    }
1586
0
    strbuf_release(&err);
1587
0
  }
1588
0
  else {
1589
0
    struct strbuf err = STRBUF_INIT;
1590
0
    if (shallow_update && si->shallow_ref[cmd->index] &&
1591
0
        update_shallow_ref(cmd, si)) {
1592
0
      ret = "shallow error";
1593
0
      goto out;
1594
0
    }
1595
1596
0
    if (ref_transaction_update(transaction,
1597
0
             namespaced_name,
1598
0
             new_oid, old_oid,
1599
0
             NULL, NULL,
1600
0
             0, "push",
1601
0
             &err)) {
1602
0
      rp_error("%s", err.buf);
1603
0
      ret = "failed to update ref";
1604
0
    } else {
1605
0
      ret = NULL; /* good */
1606
0
    }
1607
0
    strbuf_release(&err);
1608
0
  }
1609
1610
0
out:
1611
0
  free_worktrees(worktrees);
1612
0
  return ret;
1613
0
}
1614
1615
static void run_update_post_hook(struct command *commands)
1616
0
{
1617
0
  struct command *cmd;
1618
0
  struct child_process proc = CHILD_PROCESS_INIT;
1619
0
  const char *hook;
1620
1621
0
  hook = find_hook(the_repository, "post-update");
1622
0
  if (!hook)
1623
0
    return;
1624
1625
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1626
0
    if (cmd->error_string || cmd->did_not_exist)
1627
0
      continue;
1628
0
    if (!proc.args.nr)
1629
0
      strvec_push(&proc.args, hook);
1630
0
    strvec_push(&proc.args, cmd->ref_name);
1631
0
  }
1632
0
  if (!proc.args.nr)
1633
0
    return;
1634
1635
0
  proc.no_stdin = 1;
1636
0
  proc.stdout_to_stderr = 1;
1637
0
  proc.err = use_sideband ? -1 : 0;
1638
0
  proc.trace2_hook_name = "post-update";
1639
1640
0
  if (!start_command(&proc)) {
1641
0
    if (use_sideband)
1642
0
      copy_to_sideband(proc.err, -1, NULL);
1643
0
    finish_command(&proc);
1644
0
  }
1645
0
}
1646
1647
static void check_aliased_update_internal(struct command *cmd,
1648
            struct string_list *list,
1649
            const char *dst_name, int flag)
1650
0
{
1651
0
  struct string_list_item *item;
1652
0
  struct command *dst_cmd;
1653
1654
0
  if (!(flag & REF_ISSYMREF))
1655
0
    return;
1656
1657
0
  if (!dst_name) {
1658
0
    rp_error("refusing update to broken symref '%s'", cmd->ref_name);
1659
0
    cmd->skip_update = 1;
1660
0
    cmd->error_string = "broken symref";
1661
0
    return;
1662
0
  }
1663
0
  dst_name = strip_namespace(dst_name);
1664
1665
0
  if (!(item = string_list_lookup(list, dst_name)))
1666
0
    return;
1667
1668
0
  cmd->skip_update = 1;
1669
1670
0
  dst_cmd = (struct command *) item->util;
1671
1672
0
  if (oideq(&cmd->old_oid, &dst_cmd->old_oid) &&
1673
0
      oideq(&cmd->new_oid, &dst_cmd->new_oid))
1674
0
    return;
1675
1676
0
  dst_cmd->skip_update = 1;
1677
1678
0
  rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1679
0
     " its target '%s' (%s..%s)",
1680
0
     cmd->ref_name,
1681
0
     repo_find_unique_abbrev(the_repository, &cmd->old_oid, DEFAULT_ABBREV),
1682
0
     repo_find_unique_abbrev(the_repository, &cmd->new_oid, DEFAULT_ABBREV),
1683
0
     dst_cmd->ref_name,
1684
0
     repo_find_unique_abbrev(the_repository, &dst_cmd->old_oid, DEFAULT_ABBREV),
1685
0
     repo_find_unique_abbrev(the_repository, &dst_cmd->new_oid, DEFAULT_ABBREV));
1686
1687
0
  cmd->error_string = dst_cmd->error_string =
1688
0
    "inconsistent aliased update";
1689
0
}
1690
1691
static void check_aliased_update(struct command *cmd, struct string_list *list)
1692
0
{
1693
0
  struct strbuf buf = STRBUF_INIT;
1694
0
  const char *dst_name;
1695
0
  int flag;
1696
1697
0
  strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
1698
0
  dst_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1699
0
             buf.buf, 0, NULL, &flag);
1700
0
  check_aliased_update_internal(cmd, list, dst_name, flag);
1701
0
  strbuf_release(&buf);
1702
0
}
1703
1704
static void check_aliased_updates(struct command *commands)
1705
0
{
1706
0
  struct command *cmd;
1707
0
  struct string_list ref_list = STRING_LIST_INIT_NODUP;
1708
1709
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1710
0
    struct string_list_item *item =
1711
0
      string_list_append(&ref_list, cmd->ref_name);
1712
0
    item->util = (void *)cmd;
1713
0
  }
1714
0
  string_list_sort(&ref_list);
1715
1716
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1717
0
    if (!cmd->error_string)
1718
0
      check_aliased_update(cmd, &ref_list);
1719
0
  }
1720
1721
0
  string_list_clear(&ref_list, 0);
1722
0
}
1723
1724
static const struct object_id *command_singleton_iterator(void *cb_data)
1725
0
{
1726
0
  struct command **cmd_list = cb_data;
1727
0
  struct command *cmd = *cmd_list;
1728
1729
0
  if (!cmd || is_null_oid(&cmd->new_oid))
1730
0
    return NULL;
1731
0
  *cmd_list = NULL; /* this returns only one */
1732
0
  return &cmd->new_oid;
1733
0
}
1734
1735
static void set_connectivity_errors(struct command *commands,
1736
            struct shallow_info *si)
1737
0
{
1738
0
  struct command *cmd;
1739
1740
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1741
0
    struct command *singleton = cmd;
1742
0
    struct check_connected_options opt = CHECK_CONNECTED_INIT;
1743
1744
0
    if (shallow_update && si->shallow_ref[cmd->index])
1745
      /* to be checked in update_shallow_ref() */
1746
0
      continue;
1747
1748
0
    opt.env = tmp_objdir_env(tmp_objdir);
1749
0
    if (!check_connected(command_singleton_iterator, &singleton,
1750
0
             &opt))
1751
0
      continue;
1752
1753
0
    cmd->error_string = "missing necessary objects";
1754
0
  }
1755
0
}
1756
1757
struct iterate_data {
1758
  struct command *cmds;
1759
  struct shallow_info *si;
1760
};
1761
1762
static const struct object_id *iterate_receive_command_list(void *cb_data)
1763
0
{
1764
0
  struct iterate_data *data = cb_data;
1765
0
  struct command **cmd_list = &data->cmds;
1766
0
  struct command *cmd = *cmd_list;
1767
1768
0
  for (; cmd; cmd = cmd->next) {
1769
0
    if (shallow_update && data->si->shallow_ref[cmd->index])
1770
      /* to be checked in update_shallow_ref() */
1771
0
      continue;
1772
0
    if (!is_null_oid(&cmd->new_oid) && !cmd->skip_update) {
1773
0
      *cmd_list = cmd->next;
1774
0
      return &cmd->new_oid;
1775
0
    }
1776
0
  }
1777
0
  return NULL;
1778
0
}
1779
1780
static void reject_updates_to_hidden(struct command *commands)
1781
0
{
1782
0
  struct strbuf refname_full = STRBUF_INIT;
1783
0
  size_t prefix_len;
1784
0
  struct command *cmd;
1785
1786
0
  strbuf_addstr(&refname_full, get_git_namespace());
1787
0
  prefix_len = refname_full.len;
1788
1789
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1790
0
    if (cmd->error_string)
1791
0
      continue;
1792
1793
0
    strbuf_setlen(&refname_full, prefix_len);
1794
0
    strbuf_addstr(&refname_full, cmd->ref_name);
1795
1796
0
    if (!ref_is_hidden(cmd->ref_name, refname_full.buf, &hidden_refs))
1797
0
      continue;
1798
0
    if (is_null_oid(&cmd->new_oid))
1799
0
      cmd->error_string = "deny deleting a hidden ref";
1800
0
    else
1801
0
      cmd->error_string = "deny updating a hidden ref";
1802
0
  }
1803
1804
0
  strbuf_release(&refname_full);
1805
0
}
1806
1807
static int should_process_cmd(struct command *cmd)
1808
0
{
1809
0
  return !cmd->error_string && !cmd->skip_update;
1810
0
}
1811
1812
static void BUG_if_skipped_connectivity_check(struct command *commands,
1813
                 struct shallow_info *si)
1814
0
{
1815
0
  struct command *cmd;
1816
1817
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1818
0
    if (should_process_cmd(cmd) && si->shallow_ref[cmd->index])
1819
0
      bug("connectivity check has not been run on ref %s",
1820
0
          cmd->ref_name);
1821
0
  }
1822
0
  BUG_if_bug("connectivity check skipped???");
1823
0
}
1824
1825
static void execute_commands_non_atomic(struct command *commands,
1826
          struct shallow_info *si)
1827
0
{
1828
0
  struct command *cmd;
1829
0
  struct strbuf err = STRBUF_INIT;
1830
1831
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1832
0
    if (!should_process_cmd(cmd) || cmd->run_proc_receive)
1833
0
      continue;
1834
1835
0
    transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1836
0
                &err);
1837
0
    if (!transaction) {
1838
0
      rp_error("%s", err.buf);
1839
0
      strbuf_reset(&err);
1840
0
      cmd->error_string = "transaction failed to start";
1841
0
      continue;
1842
0
    }
1843
1844
0
    cmd->error_string = update(cmd, si);
1845
1846
0
    if (!cmd->error_string
1847
0
        && ref_transaction_commit(transaction, &err)) {
1848
0
      rp_error("%s", err.buf);
1849
0
      strbuf_reset(&err);
1850
0
      cmd->error_string = "failed to update ref";
1851
0
    }
1852
0
    ref_transaction_free(transaction);
1853
0
  }
1854
0
  strbuf_release(&err);
1855
0
}
1856
1857
static void execute_commands_atomic(struct command *commands,
1858
          struct shallow_info *si)
1859
0
{
1860
0
  struct command *cmd;
1861
0
  struct strbuf err = STRBUF_INIT;
1862
0
  const char *reported_error = "atomic push failure";
1863
1864
0
  transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1865
0
              &err);
1866
0
  if (!transaction) {
1867
0
    rp_error("%s", err.buf);
1868
0
    strbuf_reset(&err);
1869
0
    reported_error = "transaction failed to start";
1870
0
    goto failure;
1871
0
  }
1872
1873
0
  for (cmd = commands; cmd; cmd = cmd->next) {
1874
0
    if (!should_process_cmd(cmd) || cmd->run_proc_receive)
1875
0
      continue;
1876
1877
0
    cmd->error_string = update(cmd, si);
1878
1879
0
    if (cmd->error_string)
1880
0
      goto failure;
1881
0
  }
1882
1883
0
  if (ref_transaction_commit(transaction, &err)) {
1884
0
    rp_error("%s", err.buf);
1885
0
    reported_error = "atomic transaction failed";
1886
0
    goto failure;
1887
0
  }
1888
0
  goto cleanup;
1889
1890
0
failure:
1891
0
  for (cmd = commands; cmd; cmd = cmd->next)
1892
0
    if (!cmd->error_string)
1893
0
      cmd->error_string = reported_error;
1894
1895
0
cleanup:
1896
0
  ref_transaction_free(transaction);
1897
0
  strbuf_release(&err);
1898
0
}
1899
1900
static void execute_commands(struct command *commands,
1901
           const char *unpacker_error,
1902
           struct shallow_info *si,
1903
           const struct string_list *push_options)
1904
0
{
1905
0
  struct check_connected_options opt = CHECK_CONNECTED_INIT;
1906
0
  struct command *cmd;
1907
0
  struct iterate_data data;
1908
0
  struct async muxer;
1909
0
  int err_fd = 0;
1910
0
  int run_proc_receive = 0;
1911
1912
0
  if (unpacker_error) {
1913
0
    for (cmd = commands; cmd; cmd = cmd->next)
1914
0
      cmd->error_string = "unpacker error";
1915
0
    return;
1916
0
  }
1917
1918
0
  if (use_sideband) {
1919
0
    memset(&muxer, 0, sizeof(muxer));
1920
0
    muxer.proc = copy_to_sideband;
1921
0
    muxer.in = -1;
1922
0
    if (!start_async(&muxer))
1923
0
      err_fd = muxer.in;
1924
    /* ...else, continue without relaying sideband */
1925
0
  }
1926
1927
0
  data.cmds = commands;
1928
0
  data.si = si;
1929
0
  opt.err_fd = err_fd;
1930
0
  opt.progress = err_fd && !quiet;
1931
0
  opt.env = tmp_objdir_env(tmp_objdir);
1932
0
  opt.exclude_hidden_refs_section = "receive";
1933
1934
0
  if (check_connected(iterate_receive_command_list, &data, &opt))
1935
0
    set_connectivity_errors(commands, si);
1936
1937
0
  if (use_sideband)
1938
0
    finish_async(&muxer);
1939
1940
0
  reject_updates_to_hidden(commands);
1941
1942
  /*
1943
   * Try to find commands that have special prefix in their reference names,
1944
   * and mark them to run an external "proc-receive" hook later.
1945
   */
1946
0
  if (proc_receive_ref) {
1947
0
    for (cmd = commands; cmd; cmd = cmd->next) {
1948
0
      if (!should_process_cmd(cmd))
1949
0
        continue;
1950
1951
0
      if (proc_receive_ref_matches(cmd)) {
1952
0
        cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
1953
0
        run_proc_receive = 1;
1954
0
      }
1955
0
    }
1956
0
  }
1957
1958
0
  if (run_receive_hook(commands, "pre-receive", 0, push_options)) {
1959
0
    for (cmd = commands; cmd; cmd = cmd->next) {
1960
0
      if (!cmd->error_string)
1961
0
        cmd->error_string = "pre-receive hook declined";
1962
0
    }
1963
0
    return;
1964
0
  }
1965
1966
  /*
1967
   * If there is no command ready to run, should return directly to destroy
1968
   * temporary data in the quarantine area.
1969
   */
1970
0
  for (cmd = commands; cmd && cmd->error_string; cmd = cmd->next)
1971
0
    ; /* nothing */
1972
0
  if (!cmd)
1973
0
    return;
1974
1975
  /*
1976
   * Now we'll start writing out refs, which means the objects need
1977
   * to be in their final positions so that other processes can see them.
1978
   */
1979
0
  if (tmp_objdir_migrate(tmp_objdir) < 0) {
1980
0
    for (cmd = commands; cmd; cmd = cmd->next) {
1981
0
      if (!cmd->error_string)
1982
0
        cmd->error_string = "unable to migrate objects to permanent storage";
1983
0
    }
1984
0
    return;
1985
0
  }
1986
0
  tmp_objdir = NULL;
1987
1988
0
  check_aliased_updates(commands);
1989
1990
0
  free(head_name_to_free);
1991
0
  head_name = head_name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),
1992
0
                  "HEAD", 0, NULL,
1993
0
                  NULL);
1994
1995
0
  if (run_proc_receive &&
1996
0
      run_proc_receive_hook(commands, push_options))
1997
0
    for (cmd = commands; cmd; cmd = cmd->next)
1998
0
      if (!cmd->error_string &&
1999
0
          !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED) &&
2000
0
          (cmd->run_proc_receive || use_atomic))
2001
0
        cmd->error_string = "fail to run proc-receive hook";
2002
2003
0
  if (use_atomic)
2004
0
    execute_commands_atomic(commands, si);
2005
0
  else
2006
0
    execute_commands_non_atomic(commands, si);
2007
2008
0
  if (shallow_update)
2009
0
    BUG_if_skipped_connectivity_check(commands, si);
2010
0
}
2011
2012
static struct command **queue_command(struct command **tail,
2013
              const char *line,
2014
              int linelen)
2015
0
{
2016
0
  struct object_id old_oid, new_oid;
2017
0
  struct command *cmd;
2018
0
  const char *refname;
2019
0
  int reflen;
2020
0
  const char *p;
2021
2022
0
  if (parse_oid_hex(line, &old_oid, &p) ||
2023
0
      *p++ != ' ' ||
2024
0
      parse_oid_hex(p, &new_oid, &p) ||
2025
0
      *p++ != ' ')
2026
0
    die("protocol error: expected old/new/ref, got '%s'", line);
2027
2028
0
  refname = p;
2029
0
  reflen = linelen - (p - line);
2030
0
  FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen);
2031
0
  oidcpy(&cmd->old_oid, &old_oid);
2032
0
  oidcpy(&cmd->new_oid, &new_oid);
2033
0
  *tail = cmd;
2034
0
  return &cmd->next;
2035
0
}
2036
2037
static void free_commands(struct command *commands)
2038
0
{
2039
0
  while (commands) {
2040
0
    struct command *next = commands->next;
2041
2042
0
    free(commands);
2043
0
    commands = next;
2044
0
  }
2045
0
}
2046
2047
static void queue_commands_from_cert(struct command **tail,
2048
             struct strbuf *push_cert)
2049
0
{
2050
0
  const char *boc, *eoc;
2051
2052
0
  if (*tail)
2053
0
    die("protocol error: got both push certificate and unsigned commands");
2054
2055
0
  boc = strstr(push_cert->buf, "\n\n");
2056
0
  if (!boc)
2057
0
    die("malformed push certificate %.*s", 100, push_cert->buf);
2058
0
  else
2059
0
    boc += 2;
2060
0
  eoc = push_cert->buf + parse_signed_buffer(push_cert->buf, push_cert->len);
2061
2062
0
  while (boc < eoc) {
2063
0
    const char *eol = memchr(boc, '\n', eoc - boc);
2064
0
    tail = queue_command(tail, boc, eol ? eol - boc : eoc - boc);
2065
0
    boc = eol ? eol + 1 : eoc;
2066
0
  }
2067
0
}
2068
2069
static struct command *read_head_info(struct packet_reader *reader,
2070
              struct oid_array *shallow)
2071
0
{
2072
0
  struct command *commands = NULL;
2073
0
  struct command **p = &commands;
2074
0
  for (;;) {
2075
0
    int linelen;
2076
2077
0
    if (packet_reader_read(reader) != PACKET_READ_NORMAL)
2078
0
      break;
2079
2080
0
    if (reader->pktlen > 8 && starts_with(reader->line, "shallow ")) {
2081
0
      struct object_id oid;
2082
0
      if (get_oid_hex(reader->line + 8, &oid))
2083
0
        die("protocol error: expected shallow sha, got '%s'",
2084
0
            reader->line + 8);
2085
0
      oid_array_append(shallow, &oid);
2086
0
      continue;
2087
0
    }
2088
2089
0
    linelen = strlen(reader->line);
2090
0
    if (linelen < reader->pktlen) {
2091
0
      const char *feature_list = reader->line + linelen + 1;
2092
0
      const char *hash = NULL;
2093
0
      const char *client_sid;
2094
0
      size_t len = 0;
2095
0
      if (parse_feature_request(feature_list, "report-status"))
2096
0
        report_status = 1;
2097
0
      if (parse_feature_request(feature_list, "report-status-v2"))
2098
0
        report_status_v2 = 1;
2099
0
      if (parse_feature_request(feature_list, "side-band-64k"))
2100
0
        use_sideband = LARGE_PACKET_MAX;
2101
0
      if (parse_feature_request(feature_list, "quiet"))
2102
0
        quiet = 1;
2103
0
      if (advertise_atomic_push
2104
0
          && parse_feature_request(feature_list, "atomic"))
2105
0
        use_atomic = 1;
2106
0
      if (advertise_push_options
2107
0
          && parse_feature_request(feature_list, "push-options"))
2108
0
        use_push_options = 1;
2109
0
      hash = parse_feature_value(feature_list, "object-format", &len, NULL);
2110
0
      if (!hash) {
2111
0
        hash = hash_algos[GIT_HASH_SHA1].name;
2112
0
        len = strlen(hash);
2113
0
      }
2114
0
      if (xstrncmpz(the_hash_algo->name, hash, len))
2115
0
        die("error: unsupported object format '%s'", hash);
2116
0
      client_sid = parse_feature_value(feature_list, "session-id", &len, NULL);
2117
0
      if (client_sid) {
2118
0
        char *sid = xstrndup(client_sid, len);
2119
0
        trace2_data_string("transfer", NULL, "client-sid", client_sid);
2120
0
        free(sid);
2121
0
      }
2122
0
    }
2123
2124
0
    if (!strcmp(reader->line, "push-cert")) {
2125
0
      int true_flush = 0;
2126
0
      int saved_options = reader->options;
2127
0
      reader->options &= ~PACKET_READ_CHOMP_NEWLINE;
2128
2129
0
      for (;;) {
2130
0
        packet_reader_read(reader);
2131
0
        if (reader->status == PACKET_READ_FLUSH) {
2132
0
          true_flush = 1;
2133
0
          break;
2134
0
        }
2135
0
        if (reader->status != PACKET_READ_NORMAL) {
2136
0
          die("protocol error: got an unexpected packet");
2137
0
        }
2138
0
        if (!strcmp(reader->line, "push-cert-end\n"))
2139
0
          break; /* end of cert */
2140
0
        strbuf_addstr(&push_cert, reader->line);
2141
0
      }
2142
0
      reader->options = saved_options;
2143
2144
0
      if (true_flush)
2145
0
        break;
2146
0
      continue;
2147
0
    }
2148
2149
0
    p = queue_command(p, reader->line, linelen);
2150
0
  }
2151
2152
0
  if (push_cert.len)
2153
0
    queue_commands_from_cert(p, &push_cert);
2154
2155
0
  return commands;
2156
0
}
2157
2158
static void read_push_options(struct packet_reader *reader,
2159
            struct string_list *options)
2160
0
{
2161
0
  while (1) {
2162
0
    if (packet_reader_read(reader) != PACKET_READ_NORMAL)
2163
0
      break;
2164
2165
0
    string_list_append(options, reader->line);
2166
0
  }
2167
0
}
2168
2169
static const char *parse_pack_header(struct pack_header *hdr)
2170
0
{
2171
0
  switch (read_pack_header(0, hdr)) {
2172
0
  case PH_ERROR_EOF:
2173
0
    return "eof before pack header was fully read";
2174
2175
0
  case PH_ERROR_PACK_SIGNATURE:
2176
0
    return "protocol error (pack signature mismatch detected)";
2177
2178
0
  case PH_ERROR_PROTOCOL:
2179
0
    return "protocol error (pack version unsupported)";
2180
2181
0
  default:
2182
0
    return "unknown error in parse_pack_header";
2183
2184
0
  case 0:
2185
0
    return NULL;
2186
0
  }
2187
0
}
2188
2189
static struct tempfile *pack_lockfile;
2190
2191
static void push_header_arg(struct strvec *args, struct pack_header *hdr)
2192
0
{
2193
0
  strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
2194
0
         ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
2195
0
}
2196
2197
static const char *unpack(int err_fd, struct shallow_info *si)
2198
0
{
2199
0
  struct pack_header hdr;
2200
0
  const char *hdr_err;
2201
0
  int status;
2202
0
  struct child_process child = CHILD_PROCESS_INIT;
2203
0
  int fsck_objects = (receive_fsck_objects >= 0
2204
0
          ? receive_fsck_objects
2205
0
          : transfer_fsck_objects >= 0
2206
0
          ? transfer_fsck_objects
2207
0
          : 0);
2208
2209
0
  hdr_err = parse_pack_header(&hdr);
2210
0
  if (hdr_err) {
2211
0
    if (err_fd > 0)
2212
0
      close(err_fd);
2213
0
    return hdr_err;
2214
0
  }
2215
2216
0
  if (si->nr_ours || si->nr_theirs) {
2217
0
    alt_shallow_file = setup_temporary_shallow(si->shallow);
2218
0
    strvec_push(&child.args, "--shallow-file");
2219
0
    strvec_push(&child.args, alt_shallow_file);
2220
0
  }
2221
2222
0
  tmp_objdir = tmp_objdir_create("incoming");
2223
0
  if (!tmp_objdir) {
2224
0
    if (err_fd > 0)
2225
0
      close(err_fd);
2226
0
    return "unable to create temporary object directory";
2227
0
  }
2228
0
  strvec_pushv(&child.env, tmp_objdir_env(tmp_objdir));
2229
2230
  /*
2231
   * Normally we just pass the tmp_objdir environment to the child
2232
   * processes that do the heavy lifting, but we may need to see these
2233
   * objects ourselves to set up shallow information.
2234
   */
2235
0
  tmp_objdir_add_as_alternate(tmp_objdir);
2236
2237
0
  if (ntohl(hdr.hdr_entries) < unpack_limit) {
2238
0
    strvec_push(&child.args, "unpack-objects");
2239
0
    push_header_arg(&child.args, &hdr);
2240
0
    if (quiet)
2241
0
      strvec_push(&child.args, "-q");
2242
0
    if (fsck_objects)
2243
0
      strvec_pushf(&child.args, "--strict%s",
2244
0
             fsck_msg_types.buf);
2245
0
    if (max_input_size)
2246
0
      strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2247
0
             (uintmax_t)max_input_size);
2248
0
    child.no_stdout = 1;
2249
0
    child.err = err_fd;
2250
0
    child.git_cmd = 1;
2251
0
    status = run_command(&child);
2252
0
    if (status)
2253
0
      return "unpack-objects abnormal exit";
2254
0
  } else {
2255
0
    char hostname[HOST_NAME_MAX + 1];
2256
0
    char *lockfile;
2257
2258
0
    strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
2259
0
    push_header_arg(&child.args, &hdr);
2260
2261
0
    if (xgethostname(hostname, sizeof(hostname)))
2262
0
      xsnprintf(hostname, sizeof(hostname), "localhost");
2263
0
    strvec_pushf(&child.args,
2264
0
           "--keep=receive-pack %"PRIuMAX" on %s",
2265
0
           (uintmax_t)getpid(),
2266
0
           hostname);
2267
2268
0
    if (!quiet && err_fd)
2269
0
      strvec_push(&child.args, "--show-resolving-progress");
2270
0
    if (use_sideband)
2271
0
      strvec_push(&child.args, "--report-end-of-input");
2272
0
    if (fsck_objects)
2273
0
      strvec_pushf(&child.args, "--strict%s",
2274
0
             fsck_msg_types.buf);
2275
0
    if (!reject_thin)
2276
0
      strvec_push(&child.args, "--fix-thin");
2277
0
    if (max_input_size)
2278
0
      strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2279
0
             (uintmax_t)max_input_size);
2280
0
    child.out = -1;
2281
0
    child.err = err_fd;
2282
0
    child.git_cmd = 1;
2283
0
    status = start_command(&child);
2284
0
    if (status)
2285
0
      return "index-pack fork failed";
2286
2287
0
    lockfile = index_pack_lockfile(child.out, NULL);
2288
0
    if (lockfile) {
2289
0
      pack_lockfile = register_tempfile(lockfile);
2290
0
      free(lockfile);
2291
0
    }
2292
0
    close(child.out);
2293
2294
0
    status = finish_command(&child);
2295
0
    if (status)
2296
0
      return "index-pack abnormal exit";
2297
0
    reprepare_packed_git(the_repository);
2298
0
  }
2299
0
  return NULL;
2300
0
}
2301
2302
static const char *unpack_with_sideband(struct shallow_info *si)
2303
0
{
2304
0
  struct async muxer;
2305
0
  const char *ret;
2306
2307
0
  if (!use_sideband)
2308
0
    return unpack(0, si);
2309
2310
0
  use_keepalive = KEEPALIVE_AFTER_NUL;
2311
0
  memset(&muxer, 0, sizeof(muxer));
2312
0
  muxer.proc = copy_to_sideband;
2313
0
  muxer.in = -1;
2314
0
  if (start_async(&muxer))
2315
0
    return NULL;
2316
2317
0
  ret = unpack(muxer.in, si);
2318
2319
0
  finish_async(&muxer);
2320
0
  return ret;
2321
0
}
2322
2323
static void prepare_shallow_update(struct shallow_info *si)
2324
0
{
2325
0
  int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
2326
2327
0
  ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
2328
0
  assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
2329
2330
0
  CALLOC_ARRAY(si->need_reachability_test, si->shallow->nr);
2331
0
  CALLOC_ARRAY(si->reachable, si->shallow->nr);
2332
0
  CALLOC_ARRAY(si->shallow_ref, si->ref->nr);
2333
2334
0
  for (i = 0; i < si->nr_ours; i++)
2335
0
    si->need_reachability_test[si->ours[i]] = 1;
2336
2337
0
  for (i = 0; i < si->shallow->nr; i++) {
2338
0
    if (!si->used_shallow[i])
2339
0
      continue;
2340
0
    for (j = 0; j < bitmap_size; j++) {
2341
0
      if (!si->used_shallow[i][j])
2342
0
        continue;
2343
0
      si->need_reachability_test[i]++;
2344
0
      for (k = 0; k < 32; k++)
2345
0
        if (si->used_shallow[i][j] & (1U << k))
2346
0
          si->shallow_ref[j * 32 + k]++;
2347
0
    }
2348
2349
    /*
2350
     * true for those associated with some refs and belong
2351
     * in "ours" list aka "step 7 not done yet"
2352
     */
2353
0
    si->need_reachability_test[i] =
2354
0
      si->need_reachability_test[i] > 1;
2355
0
  }
2356
2357
  /*
2358
   * keep hooks happy by forcing a temporary shallow file via
2359
   * env variable because we can't add --shallow-file to every
2360
   * command. check_connected() will be done with
2361
   * true .git/shallow though.
2362
   */
2363
0
  setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
2364
0
}
2365
2366
static void update_shallow_info(struct command *commands,
2367
        struct shallow_info *si,
2368
        struct oid_array *ref)
2369
0
{
2370
0
  struct command *cmd;
2371
0
  int *ref_status;
2372
0
  remove_nonexistent_theirs_shallow(si);
2373
0
  if (!si->nr_ours && !si->nr_theirs) {
2374
0
    shallow_update = 0;
2375
0
    return;
2376
0
  }
2377
2378
0
  for (cmd = commands; cmd; cmd = cmd->next) {
2379
0
    if (is_null_oid(&cmd->new_oid))
2380
0
      continue;
2381
0
    oid_array_append(ref, &cmd->new_oid);
2382
0
    cmd->index = ref->nr - 1;
2383
0
  }
2384
0
  si->ref = ref;
2385
2386
0
  if (shallow_update) {
2387
0
    prepare_shallow_update(si);
2388
0
    return;
2389
0
  }
2390
2391
0
  ALLOC_ARRAY(ref_status, ref->nr);
2392
0
  assign_shallow_commits_to_refs(si, NULL, ref_status);
2393
0
  for (cmd = commands; cmd; cmd = cmd->next) {
2394
0
    if (is_null_oid(&cmd->new_oid))
2395
0
      continue;
2396
0
    if (ref_status[cmd->index]) {
2397
0
      cmd->error_string = "shallow update not allowed";
2398
0
      cmd->skip_update = 1;
2399
0
    }
2400
0
  }
2401
0
  free(ref_status);
2402
0
}
2403
2404
static void report(struct command *commands, const char *unpack_status)
2405
0
{
2406
0
  struct command *cmd;
2407
0
  struct strbuf buf = STRBUF_INIT;
2408
2409
0
  packet_buf_write(&buf, "unpack %s\n",
2410
0
       unpack_status ? unpack_status : "ok");
2411
0
  for (cmd = commands; cmd; cmd = cmd->next) {
2412
0
    if (!cmd->error_string)
2413
0
      packet_buf_write(&buf, "ok %s\n",
2414
0
           cmd->ref_name);
2415
0
    else
2416
0
      packet_buf_write(&buf, "ng %s %s\n",
2417
0
           cmd->ref_name, cmd->error_string);
2418
0
  }
2419
0
  packet_buf_flush(&buf);
2420
2421
0
  if (use_sideband)
2422
0
    send_sideband(1, 1, buf.buf, buf.len, use_sideband);
2423
0
  else
2424
0
    write_or_die(1, buf.buf, buf.len);
2425
0
  strbuf_release(&buf);
2426
0
}
2427
2428
static void report_v2(struct command *commands, const char *unpack_status)
2429
0
{
2430
0
  struct command *cmd;
2431
0
  struct strbuf buf = STRBUF_INIT;
2432
0
  struct ref_push_report *report;
2433
2434
0
  packet_buf_write(&buf, "unpack %s\n",
2435
0
       unpack_status ? unpack_status : "ok");
2436
0
  for (cmd = commands; cmd; cmd = cmd->next) {
2437
0
    int count = 0;
2438
2439
0
    if (cmd->error_string) {
2440
0
      packet_buf_write(&buf, "ng %s %s\n",
2441
0
           cmd->ref_name,
2442
0
           cmd->error_string);
2443
0
      continue;
2444
0
    }
2445
0
    packet_buf_write(&buf, "ok %s\n",
2446
0
         cmd->ref_name);
2447
0
    for (report = cmd->report; report; report = report->next) {
2448
0
      if (count++ > 0)
2449
0
        packet_buf_write(&buf, "ok %s\n",
2450
0
             cmd->ref_name);
2451
0
      if (report->ref_name)
2452
0
        packet_buf_write(&buf, "option refname %s\n",
2453
0
             report->ref_name);
2454
0
      if (report->old_oid)
2455
0
        packet_buf_write(&buf, "option old-oid %s\n",
2456
0
             oid_to_hex(report->old_oid));
2457
0
      if (report->new_oid)
2458
0
        packet_buf_write(&buf, "option new-oid %s\n",
2459
0
             oid_to_hex(report->new_oid));
2460
0
      if (report->forced_update)
2461
0
        packet_buf_write(&buf, "option forced-update\n");
2462
0
    }
2463
0
  }
2464
0
  packet_buf_flush(&buf);
2465
2466
0
  if (use_sideband)
2467
0
    send_sideband(1, 1, buf.buf, buf.len, use_sideband);
2468
0
  else
2469
0
    write_or_die(1, buf.buf, buf.len);
2470
0
  strbuf_release(&buf);
2471
0
}
2472
2473
static int delete_only(struct command *commands)
2474
0
{
2475
0
  struct command *cmd;
2476
0
  for (cmd = commands; cmd; cmd = cmd->next) {
2477
0
    if (!is_null_oid(&cmd->new_oid))
2478
0
      return 0;
2479
0
  }
2480
0
  return 1;
2481
0
}
2482
2483
int cmd_receive_pack(int argc, const char **argv, const char *prefix)
2484
0
{
2485
0
  int advertise_refs = 0;
2486
0
  struct command *commands;
2487
0
  struct oid_array shallow = OID_ARRAY_INIT;
2488
0
  struct oid_array ref = OID_ARRAY_INIT;
2489
0
  struct shallow_info si;
2490
0
  struct packet_reader reader;
2491
2492
0
  struct option options[] = {
2493
0
    OPT__QUIET(&quiet, N_("quiet")),
2494
0
    OPT_HIDDEN_BOOL(0, "stateless-rpc", &stateless_rpc, NULL),
2495
0
    OPT_HIDDEN_BOOL(0, "http-backend-info-refs", &advertise_refs, NULL),
2496
0
    OPT_ALIAS(0, "advertise-refs", "http-backend-info-refs"),
2497
0
    OPT_HIDDEN_BOOL(0, "reject-thin-pack-for-testing", &reject_thin, NULL),
2498
0
    OPT_END()
2499
0
  };
2500
2501
0
  packet_trace_identity("receive-pack");
2502
2503
0
  argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0);
2504
2505
0
  if (argc > 1)
2506
0
    usage_msg_opt(_("too many arguments"), receive_pack_usage, options);
2507
0
  if (argc == 0)
2508
0
    usage_msg_opt(_("you must specify a directory"), receive_pack_usage, options);
2509
2510
0
  service_dir = argv[0];
2511
2512
0
  setup_path();
2513
2514
0
  if (!enter_repo(service_dir, 0))
2515
0
    die("'%s' does not appear to be a git repository", service_dir);
2516
2517
0
  git_config(receive_pack_config, NULL);
2518
0
  if (cert_nonce_seed)
2519
0
    push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL));
2520
2521
0
  if (0 <= receive_unpack_limit)
2522
0
    unpack_limit = receive_unpack_limit;
2523
0
  else if (0 <= transfer_unpack_limit)
2524
0
    unpack_limit = transfer_unpack_limit;
2525
2526
0
  switch (determine_protocol_version_server()) {
2527
0
  case protocol_v2:
2528
    /*
2529
     * push support for protocol v2 has not been implemented yet,
2530
     * so ignore the request to use v2 and fallback to using v0.
2531
     */
2532
0
    break;
2533
0
  case protocol_v1:
2534
    /*
2535
     * v1 is just the original protocol with a version string,
2536
     * so just fall through after writing the version string.
2537
     */
2538
0
    if (advertise_refs || !stateless_rpc)
2539
0
      packet_write_fmt(1, "version 1\n");
2540
2541
    /* fallthrough */
2542
0
  case protocol_v0:
2543
0
    break;
2544
0
  case protocol_unknown_version:
2545
0
    BUG("unknown protocol version");
2546
0
  }
2547
2548
0
  if (advertise_refs || !stateless_rpc) {
2549
0
    write_head_info();
2550
0
  }
2551
0
  if (advertise_refs)
2552
0
    return 0;
2553
2554
0
  packet_reader_init(&reader, 0, NULL, 0,
2555
0
         PACKET_READ_CHOMP_NEWLINE |
2556
0
         PACKET_READ_DIE_ON_ERR_PACKET);
2557
2558
0
  if ((commands = read_head_info(&reader, &shallow))) {
2559
0
    const char *unpack_status = NULL;
2560
0
    struct string_list push_options = STRING_LIST_INIT_DUP;
2561
2562
0
    if (use_push_options)
2563
0
      read_push_options(&reader, &push_options);
2564
0
    if (!check_cert_push_options(&push_options)) {
2565
0
      struct command *cmd;
2566
0
      for (cmd = commands; cmd; cmd = cmd->next)
2567
0
        cmd->error_string = "inconsistent push options";
2568
0
    }
2569
2570
0
    prepare_shallow_info(&si, &shallow);
2571
0
    if (!si.nr_ours && !si.nr_theirs)
2572
0
      shallow_update = 0;
2573
0
    if (!delete_only(commands)) {
2574
0
      unpack_status = unpack_with_sideband(&si);
2575
0
      update_shallow_info(commands, &si, &ref);
2576
0
    }
2577
0
    use_keepalive = KEEPALIVE_ALWAYS;
2578
0
    execute_commands(commands, unpack_status, &si,
2579
0
         &push_options);
2580
0
    delete_tempfile(&pack_lockfile);
2581
0
    sigchain_push(SIGPIPE, SIG_IGN);
2582
0
    if (report_status_v2)
2583
0
      report_v2(commands, unpack_status);
2584
0
    else if (report_status)
2585
0
      report(commands, unpack_status);
2586
0
    sigchain_pop(SIGPIPE);
2587
0
    run_receive_hook(commands, "post-receive", 1,
2588
0
         &push_options);
2589
0
    run_update_post_hook(commands);
2590
0
    free_commands(commands);
2591
0
    string_list_clear(&push_options, 0);
2592
0
    if (auto_gc) {
2593
0
      struct child_process proc = CHILD_PROCESS_INIT;
2594
2595
0
      if (prepare_auto_maintenance(1, &proc)) {
2596
0
        proc.no_stdin = 1;
2597
0
        proc.stdout_to_stderr = 1;
2598
0
        proc.err = use_sideband ? -1 : 0;
2599
2600
0
        if (!start_command(&proc)) {
2601
0
          if (use_sideband)
2602
0
            copy_to_sideband(proc.err, -1, NULL);
2603
0
          finish_command(&proc);
2604
0
        }
2605
0
      }
2606
0
    }
2607
0
    if (auto_update_server_info)
2608
0
      update_server_info(0);
2609
0
    clear_shallow_info(&si);
2610
0
  }
2611
0
  if (use_sideband)
2612
0
    packet_flush(1);
2613
0
  oid_array_clear(&shallow);
2614
0
  oid_array_clear(&ref);
2615
0
  strvec_clear(&hidden_refs);
2616
0
  free((void *)push_cert_nonce);
2617
0
  return 0;
2618
0
}