Coverage Report

Created: 2023-11-19 07:08

/src/git/transport.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "advice.h"
3
#include "config.h"
4
#include "environment.h"
5
#include "hex.h"
6
#include "transport.h"
7
#include "hook.h"
8
#include "pkt-line.h"
9
#include "fetch-pack.h"
10
#include "remote.h"
11
#include "connect.h"
12
#include "send-pack.h"
13
#include "walker.h"
14
#include "bundle.h"
15
#include "dir.h"
16
#include "gettext.h"
17
#include "refs.h"
18
#include "refspec.h"
19
#include "branch.h"
20
#include "url.h"
21
#include "submodule.h"
22
#include "string-list.h"
23
#include "oid-array.h"
24
#include "sigchain.h"
25
#include "trace2.h"
26
#include "transport-internal.h"
27
#include "protocol.h"
28
#include "object-name.h"
29
#include "object-store-ll.h"
30
#include "color.h"
31
#include "bundle-uri.h"
32
33
static int transport_use_color = -1;
34
static char transport_colors[][COLOR_MAXLEN] = {
35
  GIT_COLOR_RESET,
36
  GIT_COLOR_RED   /* REJECTED */
37
};
38
39
enum color_transport {
40
  TRANSPORT_COLOR_RESET = 0,
41
  TRANSPORT_COLOR_REJECTED = 1
42
};
43
44
static int transport_color_config(void)
45
0
{
46
0
  const char *keys[] = {
47
0
    "color.transport.reset",
48
0
    "color.transport.rejected"
49
0
  }, *key = "color.transport";
50
0
  char *value;
51
0
  int i;
52
0
  static int initialized;
53
54
0
  if (initialized)
55
0
    return 0;
56
0
  initialized = 1;
57
58
0
  if (!git_config_get_string(key, &value))
59
0
    transport_use_color = git_config_colorbool(key, value);
60
61
0
  if (!want_color_stderr(transport_use_color))
62
0
    return 0;
63
64
0
  for (i = 0; i < ARRAY_SIZE(keys); i++)
65
0
    if (!git_config_get_string(keys[i], &value)) {
66
0
      if (!value)
67
0
        return config_error_nonbool(keys[i]);
68
0
      if (color_parse(value, transport_colors[i]) < 0)
69
0
        return -1;
70
0
    }
71
72
0
  return 0;
73
0
}
74
75
static const char *transport_get_color(enum color_transport ix)
76
0
{
77
0
  if (want_color_stderr(transport_use_color))
78
0
    return transport_colors[ix];
79
0
  return "";
80
0
}
81
82
static void set_upstreams(struct transport *transport, struct ref *refs,
83
  int pretend)
84
0
{
85
0
  struct ref *ref;
86
0
  for (ref = refs; ref; ref = ref->next) {
87
0
    const char *localname;
88
0
    const char *tmp;
89
0
    const char *remotename;
90
0
    int flag = 0;
91
    /*
92
     * Check suitability for tracking. Must be successful /
93
     * already up-to-date ref create/modify (not delete).
94
     */
95
0
    if (ref->status != REF_STATUS_OK &&
96
0
      ref->status != REF_STATUS_UPTODATE)
97
0
      continue;
98
0
    if (!ref->peer_ref)
99
0
      continue;
100
0
    if (is_null_oid(&ref->new_oid))
101
0
      continue;
102
103
    /* Follow symbolic refs (mainly for HEAD). */
104
0
    localname = ref->peer_ref->name;
105
0
    remotename = ref->name;
106
0
    tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
107
0
           NULL, &flag);
108
0
    if (tmp && flag & REF_ISSYMREF &&
109
0
      starts_with(tmp, "refs/heads/"))
110
0
      localname = tmp;
111
112
    /* Both source and destination must be local branches. */
113
0
    if (!localname || !starts_with(localname, "refs/heads/"))
114
0
      continue;
115
0
    if (!remotename || !starts_with(remotename, "refs/heads/"))
116
0
      continue;
117
118
0
    if (!pretend) {
119
0
      int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE;
120
0
      install_branch_config(flag, localname + 11,
121
0
        transport->remote->name, remotename);
122
0
    } else if (transport->verbose >= 0)
123
0
      printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124
0
        localname + 11, remotename + 11,
125
0
        transport->remote->name);
126
0
  }
127
0
}
128
129
struct bundle_transport_data {
130
  int fd;
131
  struct bundle_header header;
132
  unsigned get_refs_from_bundle_called : 1;
133
};
134
135
static void get_refs_from_bundle_inner(struct transport *transport)
136
0
{
137
0
  struct bundle_transport_data *data = transport->data;
138
139
0
  data->get_refs_from_bundle_called = 1;
140
141
0
  if (data->fd > 0)
142
0
    close(data->fd);
143
0
  data->fd = read_bundle_header(transport->url, &data->header);
144
0
  if (data->fd < 0)
145
0
    die(_("could not read bundle '%s'"), transport->url);
146
147
0
  transport->hash_algo = data->header.hash_algo;
148
0
}
149
150
static struct ref *get_refs_from_bundle(struct transport *transport,
151
          int for_push,
152
          struct transport_ls_refs_options *transport_options UNUSED)
153
0
{
154
0
  struct bundle_transport_data *data = transport->data;
155
0
  struct ref *result = NULL;
156
0
  int i;
157
158
0
  if (for_push)
159
0
    return NULL;
160
161
0
  get_refs_from_bundle_inner(transport);
162
163
0
  for (i = 0; i < data->header.references.nr; i++) {
164
0
    struct string_list_item *e = data->header.references.items + i;
165
0
    const char *name = e->string;
166
0
    struct ref *ref = alloc_ref(name);
167
0
    struct object_id *oid = e->util;
168
0
    oidcpy(&ref->old_oid, oid);
169
0
    ref->next = result;
170
0
    result = ref;
171
0
  }
172
0
  return result;
173
0
}
174
175
static int fetch_refs_from_bundle(struct transport *transport,
176
          int nr_heads UNUSED,
177
          struct ref **to_fetch UNUSED)
178
0
{
179
0
  struct bundle_transport_data *data = transport->data;
180
0
  struct strvec extra_index_pack_args = STRVEC_INIT;
181
0
  int ret;
182
183
0
  if (transport->progress)
184
0
    strvec_push(&extra_index_pack_args, "-v");
185
186
0
  if (!data->get_refs_from_bundle_called)
187
0
    get_refs_from_bundle_inner(transport);
188
0
  ret = unbundle(the_repository, &data->header, data->fd,
189
0
           &extra_index_pack_args, 0);
190
0
  transport->hash_algo = data->header.hash_algo;
191
0
  return ret;
192
0
}
193
194
static int close_bundle(struct transport *transport)
195
0
{
196
0
  struct bundle_transport_data *data = transport->data;
197
0
  if (data->fd > 0)
198
0
    close(data->fd);
199
0
  bundle_header_release(&data->header);
200
0
  free(data);
201
0
  return 0;
202
0
}
203
204
struct git_transport_data {
205
  struct git_transport_options options;
206
  struct child_process *conn;
207
  int fd[2];
208
  unsigned finished_handshake : 1;
209
  enum protocol_version version;
210
  struct oid_array extra_have;
211
  struct oid_array shallow;
212
};
213
214
static int set_git_option(struct git_transport_options *opts,
215
        const char *name, const char *value)
216
0
{
217
0
  if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
218
0
    opts->uploadpack = value;
219
0
    return 0;
220
0
  } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
221
0
    opts->receivepack = value;
222
0
    return 0;
223
0
  } else if (!strcmp(name, TRANS_OPT_THIN)) {
224
0
    opts->thin = !!value;
225
0
    return 0;
226
0
  } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
227
0
    opts->followtags = !!value;
228
0
    return 0;
229
0
  } else if (!strcmp(name, TRANS_OPT_KEEP)) {
230
0
    opts->keep = !!value;
231
0
    return 0;
232
0
  } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
233
0
    opts->update_shallow = !!value;
234
0
    return 0;
235
0
  } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
236
0
    if (!value)
237
0
      opts->depth = 0;
238
0
    else {
239
0
      char *end;
240
0
      opts->depth = strtol(value, &end, 0);
241
0
      if (*end)
242
0
        die(_("transport: invalid depth option '%s'"), value);
243
0
    }
244
0
    return 0;
245
0
  } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
246
0
    opts->deepen_since = value;
247
0
    return 0;
248
0
  } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
249
0
    opts->deepen_not = (const struct string_list *)value;
250
0
    return 0;
251
0
  } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
252
0
    opts->deepen_relative = !!value;
253
0
    return 0;
254
0
  } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
255
0
    opts->from_promisor = !!value;
256
0
    return 0;
257
0
  } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
258
0
    list_objects_filter_die_if_populated(&opts->filter_options);
259
0
    parse_list_objects_filter(&opts->filter_options, value);
260
0
    return 0;
261
0
  } else if (!strcmp(name, TRANS_OPT_REFETCH)) {
262
0
    opts->refetch = !!value;
263
0
    return 0;
264
0
  } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) {
265
0
    opts->reject_shallow = !!value;
266
0
    return 0;
267
0
  }
268
0
  return 1;
269
0
}
270
271
static int connect_setup(struct transport *transport, int for_push)
272
0
{
273
0
  struct git_transport_data *data = transport->data;
274
0
  int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
275
276
0
  if (data->conn)
277
0
    return 0;
278
279
0
  switch (transport->family) {
280
0
  case TRANSPORT_FAMILY_ALL: break;
281
0
  case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
282
0
  case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
283
0
  }
284
285
0
  data->conn = git_connect(data->fd, transport->url,
286
0
         for_push ?
287
0
          "git-receive-pack" :
288
0
          "git-upload-pack",
289
0
         for_push ?
290
0
          data->options.receivepack :
291
0
          data->options.uploadpack,
292
0
         flags);
293
294
0
  return 0;
295
0
}
296
297
static void die_if_server_options(struct transport *transport)
298
0
{
299
0
  if (!transport->server_options || !transport->server_options->nr)
300
0
    return;
301
0
  advise(_("see protocol.version in 'git help config' for more details"));
302
0
  die(_("server options require protocol version 2 or later"));
303
0
}
304
305
/*
306
 * Obtains the protocol version from the transport and writes it to
307
 * transport->data->version, first connecting if not already connected.
308
 *
309
 * If the protocol version is one that allows skipping the listing of remote
310
 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
311
 * this function returns NULL. Otherwise, this function returns the list of
312
 * remote refs.
313
 */
314
static struct ref *handshake(struct transport *transport, int for_push,
315
           struct transport_ls_refs_options *options,
316
           int must_list_refs)
317
0
{
318
0
  struct git_transport_data *data = transport->data;
319
0
  struct ref *refs = NULL;
320
0
  struct packet_reader reader;
321
0
  size_t sid_len;
322
0
  const char *server_sid;
323
324
0
  connect_setup(transport, for_push);
325
326
0
  packet_reader_init(&reader, data->fd[0], NULL, 0,
327
0
         PACKET_READ_CHOMP_NEWLINE |
328
0
         PACKET_READ_GENTLE_ON_EOF |
329
0
         PACKET_READ_DIE_ON_ERR_PACKET);
330
331
0
  data->version = discover_version(&reader);
332
0
  switch (data->version) {
333
0
  case protocol_v2:
334
0
    if (server_feature_v2("session-id", &server_sid))
335
0
      trace2_data_string("transfer", NULL, "server-sid", server_sid);
336
0
    if (must_list_refs)
337
0
      get_remote_refs(data->fd[1], &reader, &refs, for_push,
338
0
          options,
339
0
          transport->server_options,
340
0
          transport->stateless_rpc);
341
0
    break;
342
0
  case protocol_v1:
343
0
  case protocol_v0:
344
0
    die_if_server_options(transport);
345
0
    get_remote_heads(&reader, &refs,
346
0
         for_push ? REF_NORMAL : 0,
347
0
         &data->extra_have,
348
0
         &data->shallow);
349
0
    server_sid = server_feature_value("session-id", &sid_len);
350
0
    if (server_sid) {
351
0
      char *sid = xstrndup(server_sid, sid_len);
352
0
      trace2_data_string("transfer", NULL, "server-sid", sid);
353
0
      free(sid);
354
0
    }
355
0
    break;
356
0
  case protocol_unknown_version:
357
0
    BUG("unknown protocol version");
358
0
  }
359
0
  data->finished_handshake = 1;
360
0
  transport->hash_algo = reader.hash_algo;
361
362
0
  if (reader.line_peeked)
363
0
    BUG("buffer must be empty at the end of handshake()");
364
365
0
  return refs;
366
0
}
367
368
static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
369
          struct transport_ls_refs_options *options)
370
0
{
371
0
  return handshake(transport, for_push, options, 1);
372
0
}
373
374
static int get_bundle_uri(struct transport *transport)
375
0
{
376
0
  struct git_transport_data *data = transport->data;
377
0
  struct packet_reader reader;
378
0
  int stateless_rpc = transport->stateless_rpc;
379
380
0
  if (!transport->bundles) {
381
0
    CALLOC_ARRAY(transport->bundles, 1);
382
0
    init_bundle_list(transport->bundles);
383
0
  }
384
385
0
  if (!data->finished_handshake) {
386
0
    struct ref *refs = handshake(transport, 0, NULL, 0);
387
388
0
    if (refs)
389
0
      free_refs(refs);
390
0
  }
391
392
  /*
393
   * "Support" protocol v0 and v2 without bundle-uri support by
394
   * silently degrading to a NOOP.
395
   */
396
0
  if (!server_supports_v2("bundle-uri"))
397
0
    return 0;
398
399
0
  packet_reader_init(&reader, data->fd[0], NULL, 0,
400
0
         PACKET_READ_CHOMP_NEWLINE |
401
0
         PACKET_READ_GENTLE_ON_EOF);
402
403
0
  return get_remote_bundle_uri(data->fd[1], &reader,
404
0
             transport->bundles, stateless_rpc);
405
0
}
406
407
static int fetch_refs_via_pack(struct transport *transport,
408
             int nr_heads, struct ref **to_fetch)
409
0
{
410
0
  int ret = 0;
411
0
  struct git_transport_data *data = transport->data;
412
0
  struct ref *refs = NULL;
413
0
  struct fetch_pack_args args;
414
0
  struct ref *refs_tmp = NULL;
415
416
0
  memset(&args, 0, sizeof(args));
417
0
  args.uploadpack = data->options.uploadpack;
418
0
  args.keep_pack = data->options.keep;
419
0
  args.lock_pack = 1;
420
0
  args.use_thin_pack = data->options.thin;
421
0
  args.include_tag = data->options.followtags;
422
0
  args.verbose = (transport->verbose > 1);
423
0
  args.quiet = (transport->verbose < 0);
424
0
  args.no_progress = !transport->progress;
425
0
  args.depth = data->options.depth;
426
0
  args.deepen_since = data->options.deepen_since;
427
0
  args.deepen_not = data->options.deepen_not;
428
0
  args.deepen_relative = data->options.deepen_relative;
429
0
  args.check_self_contained_and_connected =
430
0
    data->options.check_self_contained_and_connected;
431
0
  args.cloning = transport->cloning;
432
0
  args.update_shallow = data->options.update_shallow;
433
0
  args.from_promisor = data->options.from_promisor;
434
0
  list_objects_filter_copy(&args.filter_options,
435
0
         &data->options.filter_options);
436
0
  args.refetch = data->options.refetch;
437
0
  args.stateless_rpc = transport->stateless_rpc;
438
0
  args.server_options = transport->server_options;
439
0
  args.negotiation_tips = data->options.negotiation_tips;
440
0
  args.reject_shallow_remote = transport->smart_options->reject_shallow;
441
442
0
  if (!data->finished_handshake) {
443
0
    int i;
444
0
    int must_list_refs = 0;
445
0
    for (i = 0; i < nr_heads; i++) {
446
0
      if (!to_fetch[i]->exact_oid) {
447
0
        must_list_refs = 1;
448
0
        break;
449
0
      }
450
0
    }
451
0
    refs_tmp = handshake(transport, 0, NULL, must_list_refs);
452
0
  }
453
454
0
  if (data->version == protocol_unknown_version)
455
0
    BUG("unknown protocol version");
456
0
  else if (data->version <= protocol_v1)
457
0
    die_if_server_options(transport);
458
459
0
  if (data->options.acked_commits) {
460
0
    if (data->version < protocol_v2) {
461
0
      warning(_("--negotiate-only requires protocol v2"));
462
0
      ret = -1;
463
0
    } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
464
0
      warning(_("server does not support wait-for-done"));
465
0
      ret = -1;
466
0
    } else {
467
0
      negotiate_using_fetch(data->options.negotiation_tips,
468
0
                transport->server_options,
469
0
                transport->stateless_rpc,
470
0
                data->fd,
471
0
                data->options.acked_commits);
472
0
      ret = 0;
473
0
    }
474
0
    goto cleanup;
475
0
  }
476
477
0
  refs = fetch_pack(&args, data->fd,
478
0
        refs_tmp ? refs_tmp : transport->remote_refs,
479
0
        to_fetch, nr_heads, &data->shallow,
480
0
        &transport->pack_lockfiles, data->version);
481
482
0
  data->finished_handshake = 0;
483
0
  data->options.self_contained_and_connected =
484
0
    args.self_contained_and_connected;
485
0
  data->options.connectivity_checked = args.connectivity_checked;
486
487
0
  if (!refs)
488
0
    ret = -1;
489
0
  if (report_unmatched_refs(to_fetch, nr_heads))
490
0
    ret = -1;
491
492
0
cleanup:
493
0
  close(data->fd[0]);
494
0
  if (data->fd[1] >= 0)
495
0
    close(data->fd[1]);
496
0
  if (finish_connect(data->conn))
497
0
    ret = -1;
498
0
  data->conn = NULL;
499
500
0
  free_refs(refs_tmp);
501
0
  free_refs(refs);
502
0
  list_objects_filter_release(&args.filter_options);
503
0
  return ret;
504
0
}
505
506
static int push_had_errors(struct ref *ref)
507
0
{
508
0
  for (; ref; ref = ref->next) {
509
0
    switch (ref->status) {
510
0
    case REF_STATUS_NONE:
511
0
    case REF_STATUS_UPTODATE:
512
0
    case REF_STATUS_OK:
513
0
      break;
514
0
    default:
515
0
      return 1;
516
0
    }
517
0
  }
518
0
  return 0;
519
0
}
520
521
int transport_refs_pushed(struct ref *ref)
522
0
{
523
0
  for (; ref; ref = ref->next) {
524
0
    switch(ref->status) {
525
0
    case REF_STATUS_NONE:
526
0
    case REF_STATUS_UPTODATE:
527
0
      break;
528
0
    default:
529
0
      return 1;
530
0
    }
531
0
  }
532
0
  return 0;
533
0
}
534
535
static void update_one_tracking_ref(struct remote *remote, char *refname,
536
            struct object_id *new_oid, int deletion,
537
            int verbose)
538
0
{
539
0
  struct refspec_item rs;
540
541
0
  memset(&rs, 0, sizeof(rs));
542
0
  rs.src = refname;
543
0
  rs.dst = NULL;
544
545
0
  if (!remote_find_tracking(remote, &rs)) {
546
0
    if (verbose)
547
0
      fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
548
0
    if (deletion)
549
0
      delete_ref(NULL, rs.dst, NULL, 0);
550
0
    else
551
0
      update_ref("update by push", rs.dst, new_oid,
552
0
           NULL, 0, 0);
553
0
    free(rs.dst);
554
0
  }
555
0
}
556
557
void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
558
0
{
559
0
  char *refname;
560
0
  struct object_id *new_oid;
561
0
  struct ref_push_report *report;
562
563
0
  if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
564
0
    return;
565
566
0
  report = ref->report;
567
0
  if (!report)
568
0
    update_one_tracking_ref(remote, ref->name, &ref->new_oid,
569
0
          ref->deletion, verbose);
570
0
  else
571
0
    for (; report; report = report->next) {
572
0
      refname = report->ref_name ? (char *)report->ref_name : ref->name;
573
0
      new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
574
0
      update_one_tracking_ref(remote, refname, new_oid,
575
0
            is_null_oid(new_oid), verbose);
576
0
    }
577
0
}
578
579
static void print_ref_status(char flag, const char *summary,
580
           struct ref *to, struct ref *from, const char *msg,
581
           struct ref_push_report *report,
582
           int porcelain, int summary_width)
583
0
{
584
0
  const char *to_name;
585
586
0
  if (report && report->ref_name)
587
0
    to_name = report->ref_name;
588
0
  else
589
0
    to_name = to->name;
590
591
0
  if (porcelain) {
592
0
    if (from)
593
0
      fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
594
0
    else
595
0
      fprintf(stdout, "%c\t:%s\t", flag, to_name);
596
0
    if (msg)
597
0
      fprintf(stdout, "%s (%s)\n", summary, msg);
598
0
    else
599
0
      fprintf(stdout, "%s\n", summary);
600
0
  } else {
601
0
    const char *red = "", *reset = "";
602
0
    if (push_had_errors(to)) {
603
0
      red = transport_get_color(TRANSPORT_COLOR_REJECTED);
604
0
      reset = transport_get_color(TRANSPORT_COLOR_RESET);
605
0
    }
606
0
    fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
607
0
      summary, reset);
608
0
    if (from)
609
0
      fprintf(stderr, "%s -> %s",
610
0
        prettify_refname(from->name),
611
0
        prettify_refname(to_name));
612
0
    else
613
0
      fputs(prettify_refname(to_name), stderr);
614
0
    if (msg) {
615
0
      fputs(" (", stderr);
616
0
      fputs(msg, stderr);
617
0
      fputc(')', stderr);
618
0
    }
619
0
    fputc('\n', stderr);
620
0
  }
621
0
}
622
623
static void print_ok_ref_status(struct ref *ref,
624
        struct ref_push_report *report,
625
        int porcelain, int summary_width)
626
0
{
627
0
  struct object_id *old_oid;
628
0
  struct object_id *new_oid;
629
0
  const char *ref_name;
630
0
  int forced_update;
631
632
0
  if (report && report->old_oid)
633
0
    old_oid = report->old_oid;
634
0
  else
635
0
    old_oid = &ref->old_oid;
636
0
  if (report && report->new_oid)
637
0
    new_oid = report->new_oid;
638
0
  else
639
0
    new_oid = &ref->new_oid;
640
0
  if (report && report->forced_update)
641
0
    forced_update = report->forced_update;
642
0
  else
643
0
    forced_update = ref->forced_update;
644
0
  if (report && report->ref_name)
645
0
    ref_name = report->ref_name;
646
0
  else
647
0
    ref_name = ref->name;
648
649
0
  if (ref->deletion)
650
0
    print_ref_status('-', "[deleted]", ref, NULL, NULL,
651
0
         report, porcelain, summary_width);
652
0
  else if (is_null_oid(old_oid))
653
0
    print_ref_status('*',
654
0
         (starts_with(ref_name, "refs/tags/")
655
0
          ? "[new tag]"
656
0
          : (starts_with(ref_name, "refs/heads/")
657
0
             ? "[new branch]"
658
0
             : "[new reference]")),
659
0
         ref, ref->peer_ref, NULL,
660
0
         report, porcelain, summary_width);
661
0
  else {
662
0
    struct strbuf quickref = STRBUF_INIT;
663
0
    char type;
664
0
    const char *msg;
665
666
0
    strbuf_add_unique_abbrev(&quickref, old_oid,
667
0
           DEFAULT_ABBREV);
668
0
    if (forced_update) {
669
0
      strbuf_addstr(&quickref, "...");
670
0
      type = '+';
671
0
      msg = "forced update";
672
0
    } else {
673
0
      strbuf_addstr(&quickref, "..");
674
0
      type = ' ';
675
0
      msg = NULL;
676
0
    }
677
0
    strbuf_add_unique_abbrev(&quickref, new_oid,
678
0
           DEFAULT_ABBREV);
679
680
0
    print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
681
0
         report, porcelain, summary_width);
682
0
    strbuf_release(&quickref);
683
0
  }
684
0
}
685
686
static int print_one_push_report(struct ref *ref, const char *dest, int count,
687
         struct ref_push_report *report,
688
         int porcelain, int summary_width)
689
0
{
690
0
  if (!count) {
691
0
    char *url = transport_anonymize_url(dest);
692
0
    fprintf(porcelain ? stdout : stderr, "To %s\n", url);
693
0
    free(url);
694
0
  }
695
696
0
  switch(ref->status) {
697
0
  case REF_STATUS_NONE:
698
0
    print_ref_status('X', "[no match]", ref, NULL, NULL,
699
0
         report, porcelain, summary_width);
700
0
    break;
701
0
  case REF_STATUS_REJECT_NODELETE:
702
0
    print_ref_status('!', "[rejected]", ref, NULL,
703
0
         "remote does not support deleting refs",
704
0
         report, porcelain, summary_width);
705
0
    break;
706
0
  case REF_STATUS_UPTODATE:
707
0
    print_ref_status('=', "[up to date]", ref,
708
0
         ref->peer_ref, NULL,
709
0
         report, porcelain, summary_width);
710
0
    break;
711
0
  case REF_STATUS_REJECT_NONFASTFORWARD:
712
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
713
0
         "non-fast-forward",
714
0
         report, porcelain, summary_width);
715
0
    break;
716
0
  case REF_STATUS_REJECT_ALREADY_EXISTS:
717
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
718
0
         "already exists",
719
0
         report, porcelain, summary_width);
720
0
    break;
721
0
  case REF_STATUS_REJECT_FETCH_FIRST:
722
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
723
0
         "fetch first",
724
0
         report, porcelain, summary_width);
725
0
    break;
726
0
  case REF_STATUS_REJECT_NEEDS_FORCE:
727
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
728
0
         "needs force",
729
0
         report, porcelain, summary_width);
730
0
    break;
731
0
  case REF_STATUS_REJECT_STALE:
732
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
733
0
         "stale info",
734
0
         report, porcelain, summary_width);
735
0
    break;
736
0
  case REF_STATUS_REJECT_REMOTE_UPDATED:
737
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
738
0
         "remote ref updated since checkout",
739
0
         report, porcelain, summary_width);
740
0
    break;
741
0
  case REF_STATUS_REJECT_SHALLOW:
742
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
743
0
         "new shallow roots not allowed",
744
0
         report, porcelain, summary_width);
745
0
    break;
746
0
  case REF_STATUS_REMOTE_REJECT:
747
0
    print_ref_status('!', "[remote rejected]", ref,
748
0
         ref->deletion ? NULL : ref->peer_ref,
749
0
         ref->remote_status,
750
0
         report, porcelain, summary_width);
751
0
    break;
752
0
  case REF_STATUS_EXPECTING_REPORT:
753
0
    print_ref_status('!', "[remote failure]", ref,
754
0
         ref->deletion ? NULL : ref->peer_ref,
755
0
         "remote failed to report status",
756
0
         report, porcelain, summary_width);
757
0
    break;
758
0
  case REF_STATUS_ATOMIC_PUSH_FAILED:
759
0
    print_ref_status('!', "[rejected]", ref, ref->peer_ref,
760
0
         "atomic push failed",
761
0
         report, porcelain, summary_width);
762
0
    break;
763
0
  case REF_STATUS_OK:
764
0
    print_ok_ref_status(ref, report, porcelain, summary_width);
765
0
    break;
766
0
  }
767
768
0
  return 1;
769
0
}
770
771
static int print_one_push_status(struct ref *ref, const char *dest, int count,
772
         int porcelain, int summary_width)
773
0
{
774
0
  struct ref_push_report *report;
775
0
  int n = 0;
776
777
0
  if (!ref->report)
778
0
    return print_one_push_report(ref, dest, count,
779
0
               NULL, porcelain, summary_width);
780
781
0
  for (report = ref->report; report; report = report->next)
782
0
    print_one_push_report(ref, dest, count + n++,
783
0
              report, porcelain, summary_width);
784
0
  return n;
785
0
}
786
787
static int measure_abbrev(const struct object_id *oid, int sofar)
788
0
{
789
0
  char hex[GIT_MAX_HEXSZ + 1];
790
0
  int w = repo_find_unique_abbrev_r(the_repository, hex, oid,
791
0
            DEFAULT_ABBREV);
792
793
0
  return (w < sofar) ? sofar : w;
794
0
}
795
796
int transport_summary_width(const struct ref *refs)
797
0
{
798
0
  int maxw = -1;
799
800
0
  for (; refs; refs = refs->next) {
801
0
    maxw = measure_abbrev(&refs->old_oid, maxw);
802
0
    maxw = measure_abbrev(&refs->new_oid, maxw);
803
0
  }
804
0
  if (maxw < 0)
805
0
    maxw = FALLBACK_DEFAULT_ABBREV;
806
0
  return (2 * maxw + 3);
807
0
}
808
809
void transport_print_push_status(const char *dest, struct ref *refs,
810
          int verbose, int porcelain, unsigned int *reject_reasons)
811
0
{
812
0
  struct ref *ref;
813
0
  int n = 0;
814
0
  char *head;
815
0
  int summary_width = transport_summary_width(refs);
816
817
0
  if (transport_color_config() < 0)
818
0
    warning(_("could not parse transport.color.* config"));
819
820
0
  head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
821
822
0
  if (verbose) {
823
0
    for (ref = refs; ref; ref = ref->next)
824
0
      if (ref->status == REF_STATUS_UPTODATE)
825
0
        n += print_one_push_status(ref, dest, n,
826
0
                 porcelain, summary_width);
827
0
  }
828
829
0
  for (ref = refs; ref; ref = ref->next)
830
0
    if (ref->status == REF_STATUS_OK)
831
0
      n += print_one_push_status(ref, dest, n,
832
0
               porcelain, summary_width);
833
834
0
  *reject_reasons = 0;
835
0
  for (ref = refs; ref; ref = ref->next) {
836
0
    if (ref->status != REF_STATUS_NONE &&
837
0
        ref->status != REF_STATUS_UPTODATE &&
838
0
        ref->status != REF_STATUS_OK)
839
0
      n += print_one_push_status(ref, dest, n,
840
0
               porcelain, summary_width);
841
0
    if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
842
0
      if (head != NULL && !strcmp(head, ref->name))
843
0
        *reject_reasons |= REJECT_NON_FF_HEAD;
844
0
      else
845
0
        *reject_reasons |= REJECT_NON_FF_OTHER;
846
0
    } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
847
0
      *reject_reasons |= REJECT_ALREADY_EXISTS;
848
0
    } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
849
0
      *reject_reasons |= REJECT_FETCH_FIRST;
850
0
    } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
851
0
      *reject_reasons |= REJECT_NEEDS_FORCE;
852
0
    } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
853
0
      *reject_reasons |= REJECT_REF_NEEDS_UPDATE;
854
0
    }
855
0
  }
856
0
  free(head);
857
0
}
858
859
static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
860
0
{
861
0
  struct git_transport_data *data = transport->data;
862
0
  struct send_pack_args args;
863
0
  int ret = 0;
864
865
0
  if (transport_color_config() < 0)
866
0
    return -1;
867
868
0
  if (!data->finished_handshake)
869
0
    get_refs_via_connect(transport, 1, NULL);
870
871
0
  memset(&args, 0, sizeof(args));
872
0
  args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
873
0
  args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
874
0
  args.use_thin_pack = data->options.thin;
875
0
  args.verbose = (transport->verbose > 0);
876
0
  args.quiet = (transport->verbose < 0);
877
0
  args.progress = transport->progress;
878
0
  args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
879
0
  args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
880
0
  args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
881
0
  args.push_options = transport->push_options;
882
0
  args.url = transport->url;
883
884
0
  if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
885
0
    args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
886
0
  else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
887
0
    args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
888
0
  else
889
0
    args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
890
891
0
  switch (data->version) {
892
0
  case protocol_v2:
893
0
    die(_("support for protocol v2 not implemented yet"));
894
0
    break;
895
0
  case protocol_v1:
896
0
  case protocol_v0:
897
0
    ret = send_pack(&args, data->fd, data->conn, remote_refs,
898
0
        &data->extra_have);
899
0
    break;
900
0
  case protocol_unknown_version:
901
0
    BUG("unknown protocol version");
902
0
  }
903
904
0
  close(data->fd[1]);
905
0
  close(data->fd[0]);
906
  /*
907
   * Atomic push may abort the connection early and close the pipe,
908
   * which may cause an error for `finish_connect()`. Ignore this error
909
   * for atomic git-push.
910
   */
911
0
  if (ret || args.atomic)
912
0
    finish_connect(data->conn);
913
0
  else
914
0
    ret = finish_connect(data->conn);
915
0
  data->conn = NULL;
916
0
  data->finished_handshake = 0;
917
918
0
  return ret;
919
0
}
920
921
static int connect_git(struct transport *transport, const char *name,
922
           const char *executable, int fd[2])
923
0
{
924
0
  struct git_transport_data *data = transport->data;
925
0
  data->conn = git_connect(data->fd, transport->url,
926
0
         name, executable, 0);
927
0
  fd[0] = data->fd[0];
928
0
  fd[1] = data->fd[1];
929
0
  return 0;
930
0
}
931
932
static int disconnect_git(struct transport *transport)
933
0
{
934
0
  struct git_transport_data *data = transport->data;
935
0
  if (data->conn) {
936
0
    if (data->finished_handshake && !transport->stateless_rpc)
937
0
      packet_flush(data->fd[1]);
938
0
    close(data->fd[0]);
939
0
    if (data->fd[1] >= 0)
940
0
      close(data->fd[1]);
941
0
    finish_connect(data->conn);
942
0
  }
943
944
0
  list_objects_filter_release(&data->options.filter_options);
945
0
  free(data);
946
0
  return 0;
947
0
}
948
949
static struct transport_vtable taken_over_vtable = {
950
  .get_refs_list  = get_refs_via_connect,
951
  .get_bundle_uri = get_bundle_uri,
952
  .fetch_refs = fetch_refs_via_pack,
953
  .push_refs  = git_transport_push,
954
  .disconnect = disconnect_git
955
};
956
957
void transport_take_over(struct transport *transport,
958
       struct child_process *child)
959
0
{
960
0
  struct git_transport_data *data;
961
962
0
  if (!transport->smart_options)
963
0
    BUG("taking over transport requires non-NULL "
964
0
        "smart_options field.");
965
966
0
  CALLOC_ARRAY(data, 1);
967
0
  data->options = *transport->smart_options;
968
0
  data->conn = child;
969
0
  data->fd[0] = data->conn->out;
970
0
  data->fd[1] = data->conn->in;
971
0
  data->finished_handshake = 0;
972
0
  transport->data = data;
973
974
0
  transport->vtable = &taken_over_vtable;
975
0
  transport->smart_options = &(data->options);
976
977
0
  transport->cannot_reuse = 1;
978
0
}
979
980
static int is_file(const char *url)
981
0
{
982
0
  struct stat buf;
983
0
  if (stat(url, &buf))
984
0
    return 0;
985
0
  return S_ISREG(buf.st_mode);
986
0
}
987
988
static int external_specification_len(const char *url)
989
0
{
990
0
  return strchr(url, ':') - url;
991
0
}
992
993
static const struct string_list *protocol_allow_list(void)
994
0
{
995
0
  static int enabled = -1;
996
0
  static struct string_list allowed = STRING_LIST_INIT_DUP;
997
998
0
  if (enabled < 0) {
999
0
    const char *v = getenv("GIT_ALLOW_PROTOCOL");
1000
0
    if (v) {
1001
0
      string_list_split(&allowed, v, ':', -1);
1002
0
      string_list_sort(&allowed);
1003
0
      enabled = 1;
1004
0
    } else {
1005
0
      enabled = 0;
1006
0
    }
1007
0
  }
1008
1009
0
  return enabled ? &allowed : NULL;
1010
0
}
1011
1012
enum protocol_allow_config {
1013
  PROTOCOL_ALLOW_NEVER = 0,
1014
  PROTOCOL_ALLOW_USER_ONLY,
1015
  PROTOCOL_ALLOW_ALWAYS
1016
};
1017
1018
static enum protocol_allow_config parse_protocol_config(const char *key,
1019
              const char *value)
1020
0
{
1021
0
  if (!strcasecmp(value, "always"))
1022
0
    return PROTOCOL_ALLOW_ALWAYS;
1023
0
  else if (!strcasecmp(value, "never"))
1024
0
    return PROTOCOL_ALLOW_NEVER;
1025
0
  else if (!strcasecmp(value, "user"))
1026
0
    return PROTOCOL_ALLOW_USER_ONLY;
1027
1028
0
  die(_("unknown value for config '%s': %s"), key, value);
1029
0
}
1030
1031
static enum protocol_allow_config get_protocol_config(const char *type)
1032
0
{
1033
0
  char *key = xstrfmt("protocol.%s.allow", type);
1034
0
  char *value;
1035
1036
  /* first check the per-protocol config */
1037
0
  if (!git_config_get_string(key, &value)) {
1038
0
    enum protocol_allow_config ret =
1039
0
      parse_protocol_config(key, value);
1040
0
    free(key);
1041
0
    free(value);
1042
0
    return ret;
1043
0
  }
1044
0
  free(key);
1045
1046
  /* if defined, fallback to user-defined default for unknown protocols */
1047
0
  if (!git_config_get_string("protocol.allow", &value)) {
1048
0
    enum protocol_allow_config ret =
1049
0
      parse_protocol_config("protocol.allow", value);
1050
0
    free(value);
1051
0
    return ret;
1052
0
  }
1053
1054
  /* fallback to built-in defaults */
1055
  /* known safe */
1056
0
  if (!strcmp(type, "http") ||
1057
0
      !strcmp(type, "https") ||
1058
0
      !strcmp(type, "git") ||
1059
0
      !strcmp(type, "ssh"))
1060
0
    return PROTOCOL_ALLOW_ALWAYS;
1061
1062
  /* known scary; err on the side of caution */
1063
0
  if (!strcmp(type, "ext"))
1064
0
    return PROTOCOL_ALLOW_NEVER;
1065
1066
  /* unknown; by default let them be used only directly by the user */
1067
0
  return PROTOCOL_ALLOW_USER_ONLY;
1068
0
}
1069
1070
int is_transport_allowed(const char *type, int from_user)
1071
0
{
1072
0
  const struct string_list *allow_list = protocol_allow_list();
1073
0
  if (allow_list)
1074
0
    return string_list_has_string(allow_list, type);
1075
1076
0
  switch (get_protocol_config(type)) {
1077
0
  case PROTOCOL_ALLOW_ALWAYS:
1078
0
    return 1;
1079
0
  case PROTOCOL_ALLOW_NEVER:
1080
0
    return 0;
1081
0
  case PROTOCOL_ALLOW_USER_ONLY:
1082
0
    if (from_user < 0)
1083
0
      from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1084
0
    return from_user;
1085
0
  }
1086
1087
0
  BUG("invalid protocol_allow_config type");
1088
0
}
1089
1090
void transport_check_allowed(const char *type)
1091
0
{
1092
0
  if (!is_transport_allowed(type, -1))
1093
0
    die(_("transport '%s' not allowed"), type);
1094
0
}
1095
1096
static struct transport_vtable bundle_vtable = {
1097
  .get_refs_list  = get_refs_from_bundle,
1098
  .fetch_refs = fetch_refs_from_bundle,
1099
  .disconnect = close_bundle
1100
};
1101
1102
static struct transport_vtable builtin_smart_vtable = {
1103
  .get_refs_list  = get_refs_via_connect,
1104
  .get_bundle_uri = get_bundle_uri,
1105
  .fetch_refs = fetch_refs_via_pack,
1106
  .push_refs  = git_transport_push,
1107
  .connect  = connect_git,
1108
  .disconnect = disconnect_git
1109
};
1110
1111
struct transport *transport_get(struct remote *remote, const char *url)
1112
0
{
1113
0
  const char *helper;
1114
0
  struct transport *ret = xcalloc(1, sizeof(*ret));
1115
1116
0
  ret->progress = isatty(2);
1117
0
  string_list_init_dup(&ret->pack_lockfiles);
1118
1119
0
  CALLOC_ARRAY(ret->bundles, 1);
1120
0
  init_bundle_list(ret->bundles);
1121
1122
0
  if (!remote)
1123
0
    BUG("No remote provided to transport_get()");
1124
1125
0
  ret->got_remote_refs = 0;
1126
0
  ret->remote = remote;
1127
0
  helper = remote->foreign_vcs;
1128
1129
0
  if (!url && remote->url)
1130
0
    url = remote->url[0];
1131
0
  ret->url = url;
1132
1133
  /* maybe it is a foreign URL? */
1134
0
  if (url) {
1135
0
    const char *p = url;
1136
1137
0
    while (is_urlschemechar(p == url, *p))
1138
0
      p++;
1139
0
    if (starts_with(p, "::"))
1140
0
      helper = xstrndup(url, p - url);
1141
0
  }
1142
1143
0
  if (helper) {
1144
0
    transport_helper_init(ret, helper);
1145
0
  } else if (starts_with(url, "rsync:")) {
1146
0
    die(_("git-over-rsync is no longer supported"));
1147
0
  } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1148
0
    struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1149
0
    bundle_header_init(&data->header);
1150
0
    transport_check_allowed("file");
1151
0
    ret->data = data;
1152
0
    ret->vtable = &bundle_vtable;
1153
0
    ret->smart_options = NULL;
1154
0
  } else if (!is_url(url)
1155
0
    || starts_with(url, "file://")
1156
0
    || starts_with(url, "git://")
1157
0
    || starts_with(url, "ssh://")
1158
0
    || starts_with(url, "git+ssh://") /* deprecated - do not use */
1159
0
    || starts_with(url, "ssh+git://") /* deprecated - do not use */
1160
0
    ) {
1161
    /*
1162
     * These are builtin smart transports; "allowed" transports
1163
     * will be checked individually in git_connect.
1164
     */
1165
0
    struct git_transport_data *data = xcalloc(1, sizeof(*data));
1166
0
    list_objects_filter_init(&data->options.filter_options);
1167
0
    ret->data = data;
1168
0
    ret->vtable = &builtin_smart_vtable;
1169
0
    ret->smart_options = &(data->options);
1170
1171
0
    data->conn = NULL;
1172
0
    data->finished_handshake = 0;
1173
0
  } else {
1174
    /* Unknown protocol in URL. Pass to external handler. */
1175
0
    int len = external_specification_len(url);
1176
0
    char *handler = xmemdupz(url, len);
1177
0
    transport_helper_init(ret, handler);
1178
0
  }
1179
1180
0
  if (ret->smart_options) {
1181
0
    ret->smart_options->thin = 1;
1182
0
    ret->smart_options->uploadpack = "git-upload-pack";
1183
0
    if (remote->uploadpack)
1184
0
      ret->smart_options->uploadpack = remote->uploadpack;
1185
0
    ret->smart_options->receivepack = "git-receive-pack";
1186
0
    if (remote->receivepack)
1187
0
      ret->smart_options->receivepack = remote->receivepack;
1188
0
  }
1189
1190
0
  ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1191
1192
0
  return ret;
1193
0
}
1194
1195
const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1196
0
{
1197
0
  return transport->hash_algo;
1198
0
}
1199
1200
int transport_set_option(struct transport *transport,
1201
       const char *name, const char *value)
1202
0
{
1203
0
  int git_reports = 1, protocol_reports = 1;
1204
1205
0
  if (transport->smart_options)
1206
0
    git_reports = set_git_option(transport->smart_options,
1207
0
               name, value);
1208
1209
0
  if (transport->vtable->set_option)
1210
0
    protocol_reports = transport->vtable->set_option(transport,
1211
0
                 name, value);
1212
1213
  /* If either report is 0, report 0 (success). */
1214
0
  if (!git_reports || !protocol_reports)
1215
0
    return 0;
1216
  /* If either reports -1 (invalid value), report -1. */
1217
0
  if ((git_reports == -1) || (protocol_reports == -1))
1218
0
    return -1;
1219
  /* Otherwise if both report unknown, report unknown. */
1220
0
  return 1;
1221
0
}
1222
1223
void transport_set_verbosity(struct transport *transport, int verbosity,
1224
  int force_progress)
1225
0
{
1226
0
  if (verbosity >= 1)
1227
0
    transport->verbose = verbosity <= 3 ? verbosity : 3;
1228
0
  if (verbosity < 0)
1229
0
    transport->verbose = -1;
1230
1231
  /**
1232
   * Rules used to determine whether to report progress (processing aborts
1233
   * when a rule is satisfied):
1234
   *
1235
   *   . Report progress, if force_progress is 1 (ie. --progress).
1236
   *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
1237
   *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1238
   *   . Report progress if isatty(2) is 1.
1239
   **/
1240
0
  if (force_progress >= 0)
1241
0
    transport->progress = !!force_progress;
1242
0
  else
1243
0
    transport->progress = verbosity >= 0 && isatty(2);
1244
0
}
1245
1246
static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1247
0
{
1248
0
  int i;
1249
1250
0
  fprintf(stderr, _("The following submodule paths contain changes that can\n"
1251
0
      "not be found on any remote:\n"));
1252
0
  for (i = 0; i < needs_pushing->nr; i++)
1253
0
    fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
1254
0
  fprintf(stderr, _("\nPlease try\n\n"
1255
0
        " git push --recurse-submodules=on-demand\n\n"
1256
0
        "or cd to the path and use\n\n"
1257
0
        " git push\n\n"
1258
0
        "to push them to a remote.\n\n"));
1259
1260
0
  string_list_clear(needs_pushing, 0);
1261
1262
0
  die(_("Aborting."));
1263
0
}
1264
1265
static int run_pre_push_hook(struct transport *transport,
1266
           struct ref *remote_refs)
1267
0
{
1268
0
  int ret = 0, x;
1269
0
  struct ref *r;
1270
0
  struct child_process proc = CHILD_PROCESS_INIT;
1271
0
  struct strbuf buf;
1272
0
  const char *hook_path = find_hook("pre-push");
1273
1274
0
  if (!hook_path)
1275
0
    return 0;
1276
1277
0
  strvec_push(&proc.args, hook_path);
1278
0
  strvec_push(&proc.args, transport->remote->name);
1279
0
  strvec_push(&proc.args, transport->url);
1280
1281
0
  proc.in = -1;
1282
0
  proc.trace2_hook_name = "pre-push";
1283
1284
0
  if (start_command(&proc)) {
1285
0
    finish_command(&proc);
1286
0
    return -1;
1287
0
  }
1288
1289
0
  sigchain_push(SIGPIPE, SIG_IGN);
1290
1291
0
  strbuf_init(&buf, 256);
1292
1293
0
  for (r = remote_refs; r; r = r->next) {
1294
0
    if (!r->peer_ref) continue;
1295
0
    if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1296
0
    if (r->status == REF_STATUS_REJECT_STALE) continue;
1297
0
    if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1298
0
    if (r->status == REF_STATUS_UPTODATE) continue;
1299
1300
0
    strbuf_reset(&buf);
1301
0
    strbuf_addf( &buf, "%s %s %s %s\n",
1302
0
       r->peer_ref->name, oid_to_hex(&r->new_oid),
1303
0
       r->name, oid_to_hex(&r->old_oid));
1304
1305
0
    if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1306
      /* We do not mind if a hook does not read all refs. */
1307
0
      if (errno != EPIPE)
1308
0
        ret = -1;
1309
0
      break;
1310
0
    }
1311
0
  }
1312
1313
0
  strbuf_release(&buf);
1314
1315
0
  x = close(proc.in);
1316
0
  if (!ret)
1317
0
    ret = x;
1318
1319
0
  sigchain_pop(SIGPIPE);
1320
1321
0
  x = finish_command(&proc);
1322
0
  if (!ret)
1323
0
    ret = x;
1324
1325
0
  return ret;
1326
0
}
1327
1328
int transport_push(struct repository *r,
1329
       struct transport *transport,
1330
       struct refspec *rs, int flags,
1331
       unsigned int *reject_reasons)
1332
0
{
1333
0
  struct ref *remote_refs = NULL;
1334
0
  struct ref *local_refs = NULL;
1335
0
  int match_flags = MATCH_REFS_NONE;
1336
0
  int verbose = (transport->verbose > 0);
1337
0
  int quiet = (transport->verbose < 0);
1338
0
  int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1339
0
  int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1340
0
  int push_ret, err;
1341
0
  int ret = -1;
1342
0
  struct transport_ls_refs_options transport_options =
1343
0
    TRANSPORT_LS_REFS_OPTIONS_INIT;
1344
1345
0
  *reject_reasons = 0;
1346
1347
0
  if (transport_color_config() < 0)
1348
0
    goto done;
1349
1350
0
  if (!transport->vtable->push_refs)
1351
0
    goto done;
1352
1353
0
  local_refs = get_local_heads();
1354
1355
0
  if (check_push_refs(local_refs, rs) < 0)
1356
0
    goto done;
1357
1358
0
  refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1359
1360
0
  trace2_region_enter("transport_push", "get_refs_list", r);
1361
0
  remote_refs = transport->vtable->get_refs_list(transport, 1,
1362
0
                   &transport_options);
1363
0
  trace2_region_leave("transport_push", "get_refs_list", r);
1364
1365
0
  transport_ls_refs_options_release(&transport_options);
1366
1367
0
  if (flags & TRANSPORT_PUSH_ALL)
1368
0
    match_flags |= MATCH_REFS_ALL;
1369
0
  if (flags & TRANSPORT_PUSH_MIRROR)
1370
0
    match_flags |= MATCH_REFS_MIRROR;
1371
0
  if (flags & TRANSPORT_PUSH_PRUNE)
1372
0
    match_flags |= MATCH_REFS_PRUNE;
1373
0
  if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1374
0
    match_flags |= MATCH_REFS_FOLLOW_TAGS;
1375
1376
0
  if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1377
0
    goto done;
1378
1379
0
  if (transport->smart_options &&
1380
0
      transport->smart_options->cas &&
1381
0
      !is_empty_cas(transport->smart_options->cas))
1382
0
    apply_push_cas(transport->smart_options->cas,
1383
0
             transport->remote, remote_refs);
1384
1385
0
  set_ref_status_for_push(remote_refs,
1386
0
    flags & TRANSPORT_PUSH_MIRROR,
1387
0
    flags & TRANSPORT_PUSH_FORCE);
1388
1389
0
  if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1390
0
    if (run_pre_push_hook(transport, remote_refs))
1391
0
      goto done;
1392
1393
0
  if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1394
0
          TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1395
0
      !is_bare_repository()) {
1396
0
    struct ref *ref = remote_refs;
1397
0
    struct oid_array commits = OID_ARRAY_INIT;
1398
1399
0
    trace2_region_enter("transport_push", "push_submodules", r);
1400
0
    for (; ref; ref = ref->next)
1401
0
      if (!is_null_oid(&ref->new_oid))
1402
0
        oid_array_append(&commits,
1403
0
              &ref->new_oid);
1404
1405
0
    if (!push_unpushed_submodules(r,
1406
0
                &commits,
1407
0
                transport->remote,
1408
0
                rs,
1409
0
                transport->push_options,
1410
0
                pretend)) {
1411
0
      oid_array_clear(&commits);
1412
0
      trace2_region_leave("transport_push", "push_submodules", r);
1413
0
      die(_("failed to push all needed submodules"));
1414
0
    }
1415
0
    oid_array_clear(&commits);
1416
0
    trace2_region_leave("transport_push", "push_submodules", r);
1417
0
  }
1418
1419
0
  if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1420
0
       ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1421
0
      TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1422
0
        !pretend)) && !is_bare_repository()) {
1423
0
    struct ref *ref = remote_refs;
1424
0
    struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1425
0
    struct oid_array commits = OID_ARRAY_INIT;
1426
1427
0
    trace2_region_enter("transport_push", "check_submodules", r);
1428
0
    for (; ref; ref = ref->next)
1429
0
      if (!is_null_oid(&ref->new_oid))
1430
0
        oid_array_append(&commits,
1431
0
              &ref->new_oid);
1432
1433
0
    if (find_unpushed_submodules(r,
1434
0
               &commits,
1435
0
               transport->remote->name,
1436
0
               &needs_pushing)) {
1437
0
      oid_array_clear(&commits);
1438
0
      trace2_region_leave("transport_push", "check_submodules", r);
1439
0
      die_with_unpushed_submodules(&needs_pushing);
1440
0
    }
1441
0
    string_list_clear(&needs_pushing, 0);
1442
0
    oid_array_clear(&commits);
1443
0
    trace2_region_leave("transport_push", "check_submodules", r);
1444
0
  }
1445
1446
0
  if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1447
0
    trace2_region_enter("transport_push", "push_refs", r);
1448
0
    push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1449
0
    trace2_region_leave("transport_push", "push_refs", r);
1450
0
  } else
1451
0
    push_ret = 0;
1452
0
  err = push_had_errors(remote_refs);
1453
0
  ret = push_ret | err;
1454
1455
0
  if (!quiet || err)
1456
0
    transport_print_push_status(transport->url, remote_refs,
1457
0
        verbose | porcelain, porcelain,
1458
0
        reject_reasons);
1459
1460
0
  if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1461
0
    set_upstreams(transport, remote_refs, pretend);
1462
1463
0
  if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1464
0
           TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1465
0
    struct ref *ref;
1466
0
    for (ref = remote_refs; ref; ref = ref->next)
1467
0
      transport_update_tracking_ref(transport->remote, ref, verbose);
1468
0
  }
1469
1470
0
  if (porcelain && !push_ret)
1471
0
    puts("Done");
1472
0
  else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1473
0
    fprintf(stderr, "Everything up-to-date\n");
1474
1475
0
done:
1476
0
  free_refs(local_refs);
1477
0
  free_refs(remote_refs);
1478
0
  return ret;
1479
0
}
1480
1481
const struct ref *transport_get_remote_refs(struct transport *transport,
1482
              struct transport_ls_refs_options *transport_options)
1483
0
{
1484
0
  if (!transport->got_remote_refs) {
1485
0
    transport->remote_refs =
1486
0
      transport->vtable->get_refs_list(transport, 0,
1487
0
               transport_options);
1488
0
    transport->got_remote_refs = 1;
1489
0
  }
1490
1491
0
  return transport->remote_refs;
1492
0
}
1493
1494
void transport_ls_refs_options_release(struct transport_ls_refs_options *opts)
1495
0
{
1496
0
  strvec_clear(&opts->ref_prefixes);
1497
0
  free((char *)opts->unborn_head_target);
1498
0
}
1499
1500
int transport_fetch_refs(struct transport *transport, struct ref *refs)
1501
0
{
1502
0
  int rc;
1503
0
  int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1504
0
  struct ref **heads = NULL;
1505
0
  struct ref *rm;
1506
1507
0
  for (rm = refs; rm; rm = rm->next) {
1508
0
    nr_refs++;
1509
0
    if (rm->peer_ref &&
1510
0
        !is_null_oid(&rm->old_oid) &&
1511
0
        oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1512
0
      continue;
1513
0
    ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1514
0
    heads[nr_heads++] = rm;
1515
0
  }
1516
1517
0
  if (!nr_heads) {
1518
    /*
1519
     * When deepening of a shallow repository is requested,
1520
     * then local and remote refs are likely to still be equal.
1521
     * Just feed them all to the fetch method in that case.
1522
     * This condition shouldn't be met in a non-deepening fetch
1523
     * (see builtin/fetch.c:quickfetch()).
1524
     */
1525
0
    ALLOC_ARRAY(heads, nr_refs);
1526
0
    for (rm = refs; rm; rm = rm->next)
1527
0
      heads[nr_heads++] = rm;
1528
0
  }
1529
1530
0
  rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1531
1532
0
  free(heads);
1533
0
  return rc;
1534
0
}
1535
1536
int transport_get_remote_bundle_uri(struct transport *transport)
1537
0
{
1538
0
  int value = 0;
1539
0
  const struct transport_vtable *vtable = transport->vtable;
1540
1541
  /* Check config only once. */
1542
0
  if (transport->got_remote_bundle_uri)
1543
0
    return 0;
1544
0
  transport->got_remote_bundle_uri = 1;
1545
1546
  /*
1547
   * Don't request bundle-uri from the server unless configured to
1548
   * do so by the transfer.bundleURI=true config option.
1549
   */
1550
0
  if (git_config_get_bool("transfer.bundleuri", &value) || !value)
1551
0
    return 0;
1552
1553
0
  if (!transport->bundles->baseURI)
1554
0
    transport->bundles->baseURI = xstrdup(transport->url);
1555
1556
0
  if (!vtable->get_bundle_uri)
1557
0
    return error(_("bundle-uri operation not supported by protocol"));
1558
1559
0
  if (vtable->get_bundle_uri(transport) < 0)
1560
0
    return error(_("could not retrieve server-advertised bundle-uri list"));
1561
0
  return 0;
1562
0
}
1563
1564
void transport_unlock_pack(struct transport *transport, unsigned int flags)
1565
0
{
1566
0
  int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
1567
0
  int i;
1568
1569
0
  for (i = 0; i < transport->pack_lockfiles.nr; i++)
1570
0
    if (in_signal_handler)
1571
0
      unlink(transport->pack_lockfiles.items[i].string);
1572
0
    else
1573
0
      unlink_or_warn(transport->pack_lockfiles.items[i].string);
1574
0
  if (!in_signal_handler)
1575
0
    string_list_clear(&transport->pack_lockfiles, 0);
1576
0
}
1577
1578
int transport_connect(struct transport *transport, const char *name,
1579
          const char *exec, int fd[2])
1580
0
{
1581
0
  if (transport->vtable->connect)
1582
0
    return transport->vtable->connect(transport, name, exec, fd);
1583
0
  else
1584
0
    die(_("operation not supported by protocol"));
1585
0
}
1586
1587
int transport_disconnect(struct transport *transport)
1588
0
{
1589
0
  int ret = 0;
1590
0
  if (transport->vtable->disconnect)
1591
0
    ret = transport->vtable->disconnect(transport);
1592
0
  if (transport->got_remote_refs)
1593
0
    free_refs((void *)transport->remote_refs);
1594
0
  clear_bundle_list(transport->bundles);
1595
0
  free(transport->bundles);
1596
0
  free(transport);
1597
0
  return ret;
1598
0
}
1599
1600
/*
1601
 * Strip username (and password) from a URL and return
1602
 * it in a newly allocated string.
1603
 */
1604
char *transport_anonymize_url(const char *url)
1605
0
{
1606
0
  char *scheme_prefix, *anon_part;
1607
0
  size_t anon_len, prefix_len = 0;
1608
1609
0
  anon_part = strchr(url, '@');
1610
0
  if (url_is_local_not_ssh(url) || !anon_part)
1611
0
    goto literal_copy;
1612
1613
0
  anon_len = strlen(++anon_part);
1614
0
  scheme_prefix = strstr(url, "://");
1615
0
  if (!scheme_prefix) {
1616
0
    if (!strchr(anon_part, ':'))
1617
      /* cannot be "me@there:/path/name" */
1618
0
      goto literal_copy;
1619
0
  } else {
1620
0
    const char *cp;
1621
    /* make sure scheme is reasonable */
1622
0
    for (cp = url; cp < scheme_prefix; cp++) {
1623
0
      switch (*cp) {
1624
        /* RFC 1738 2.1 */
1625
0
      case '+': case '.': case '-':
1626
0
        break; /* ok */
1627
0
      default:
1628
0
        if (isalnum(*cp))
1629
0
          break;
1630
        /* it isn't */
1631
0
        goto literal_copy;
1632
0
      }
1633
0
    }
1634
    /* @ past the first slash does not count */
1635
0
    cp = strchr(scheme_prefix + 3, '/');
1636
0
    if (cp && cp < anon_part)
1637
0
      goto literal_copy;
1638
0
    prefix_len = scheme_prefix - url + 3;
1639
0
  }
1640
0
  return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1641
0
           (int)anon_len, anon_part);
1642
0
literal_copy:
1643
0
  return xstrdup(url);
1644
0
}