Coverage Report

Created: 2025-12-31 07:01

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