Coverage Report

Created: 2024-09-08 06:23

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