Coverage Report

Created: 2026-11-12 06:03

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