-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.c
2890 lines (2512 loc) · 74.7 KB
/
remote.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "git-compat-util.h"
#include "abspath.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
#include "remote.h"
#include "urlmatch.h"
#include "refs.h"
#include "refspec.h"
#include "object-name.h"
#include "object-store-ll.h"
#include "path.h"
#include "commit.h"
#include "diff.h"
#include "revision.h"
#include "dir.h"
#include "setup.h"
#include "string-list.h"
#include "strvec.h"
#include "commit-reach.h"
#include "advice.h"
#include "connect.h"
#include "parse-options.h"
enum map_direction { FROM_SRC, FROM_DST };
struct counted_string {
size_t len;
const char *s;
};
static int valid_remote(const struct remote *remote)
{
return (!!remote->url) || (!!remote->foreign_vcs);
}
static const char *alias_url(const char *url, struct rewrites *r)
{
int i, j;
struct counted_string *longest;
int longest_i;
longest = NULL;
longest_i = -1;
for (i = 0; i < r->rewrite_nr; i++) {
if (!r->rewrite[i])
continue;
for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
(!longest ||
longest->len < r->rewrite[i]->instead_of[j].len)) {
longest = &(r->rewrite[i]->instead_of[j]);
longest_i = i;
}
}
}
if (!longest)
return url;
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
}
static void add_url(struct remote *remote, const char *url)
{
ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
remote->url[remote->url_nr++] = url;
}
static void add_pushurl(struct remote *remote, const char *pushurl)
{
ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
remote->pushurl[remote->pushurl_nr++] = pushurl;
}
static void add_pushurl_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{
const char *pushurl = alias_url(url, &remote_state->rewrites_push);
if (pushurl != url)
add_pushurl(remote, pushurl);
}
static void add_url_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{
add_url(remote, alias_url(url, &remote_state->rewrites));
add_pushurl_alias(remote_state, remote, url);
}
struct remotes_hash_key {
const char *str;
int len;
};
static int remotes_hash_cmp(const void *cmp_data UNUSED,
const struct hashmap_entry *eptr,
const struct hashmap_entry *entry_or_key,
const void *keydata)
{
const struct remote *a, *b;
const struct remotes_hash_key *key = keydata;
a = container_of(eptr, const struct remote, ent);
b = container_of(entry_or_key, const struct remote, ent);
if (key)
return !!xstrncmpz(a->name, key->str, key->len);
else
return strcmp(a->name, b->name);
}
static struct remote *make_remote(struct remote_state *remote_state,
const char *name, int len)
{
struct remote *ret;
struct remotes_hash_key lookup;
struct hashmap_entry lookup_entry, *e;
if (!len)
len = strlen(name);
lookup.str = name;
lookup.len = len;
hashmap_entry_init(&lookup_entry, memhash(name, len));
e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup);
if (e)
return container_of(e, struct remote, ent);
CALLOC_ARRAY(ret, 1);
ret->prune = -1; /* unspecified */
ret->prune_tags = -1; /* unspecified */
ret->name = xstrndup(name, len);
refspec_init(&ret->push, REFSPEC_PUSH);
refspec_init(&ret->fetch, REFSPEC_FETCH);
ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
remote_state->remotes_alloc);
remote_state->remotes[remote_state->remotes_nr++] = ret;
hashmap_entry_init(&ret->ent, lookup_entry.hash);
if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent))
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
return ret;
}
static void remote_clear(struct remote *remote)
{
int i;
free((char *)remote->name);
free((char *)remote->foreign_vcs);
for (i = 0; i < remote->url_nr; i++)
free((char *)remote->url[i]);
FREE_AND_NULL(remote->url);
for (i = 0; i < remote->pushurl_nr; i++)
free((char *)remote->pushurl[i]);
FREE_AND_NULL(remote->pushurl);
free((char *)remote->receivepack);
free((char *)remote->uploadpack);
FREE_AND_NULL(remote->http_proxy);
FREE_AND_NULL(remote->http_proxy_authmethod);
}
static void add_merge(struct branch *branch, const char *name)
{
ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
branch->merge_alloc);
branch->merge_name[branch->merge_nr++] = name;
}
struct branches_hash_key {
const char *str;
int len;
};
static int branches_hash_cmp(const void *cmp_data UNUSED,
const struct hashmap_entry *eptr,
const struct hashmap_entry *entry_or_key,
const void *keydata)
{
const struct branch *a, *b;
const struct branches_hash_key *key = keydata;
a = container_of(eptr, const struct branch, ent);
b = container_of(entry_or_key, const struct branch, ent);
if (key)
return !!xstrncmpz(a->name, key->str, key->len);
else
return strcmp(a->name, b->name);
}
static struct branch *find_branch(struct remote_state *remote_state,
const char *name, size_t len)
{
struct branches_hash_key lookup;
struct hashmap_entry lookup_entry, *e;
lookup.str = name;
lookup.len = len;
hashmap_entry_init(&lookup_entry, memhash(name, len));
e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup);
if (e)
return container_of(e, struct branch, ent);
return NULL;
}
static void die_on_missing_branch(struct repository *repo,
struct branch *branch)
{
/* branch == NULL is always valid because it represents detached HEAD. */
if (branch &&
branch != find_branch(repo->remote_state, branch->name,
strlen(branch->name)))
die("branch %s was not found in the repository", branch->name);
}
static struct branch *make_branch(struct remote_state *remote_state,
const char *name, size_t len)
{
struct branch *ret;
ret = find_branch(remote_state, name, len);
if (ret)
return ret;
CALLOC_ARRAY(ret, 1);
ret->name = xstrndup(name, len);
ret->refname = xstrfmt("refs/heads/%s", ret->name);
hashmap_entry_init(&ret->ent, memhash(name, len));
if (hashmap_put_entry(&remote_state->branches_hash, ret, ent))
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
return ret;
}
static struct rewrite *make_rewrite(struct rewrites *r,
const char *base, size_t len)
{
struct rewrite *ret;
int i;
for (i = 0; i < r->rewrite_nr; i++) {
if (len == r->rewrite[i]->baselen &&
!strncmp(base, r->rewrite[i]->base, len))
return r->rewrite[i];
}
ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
CALLOC_ARRAY(ret, 1);
r->rewrite[r->rewrite_nr++] = ret;
ret->base = xstrndup(base, len);
ret->baselen = len;
return ret;
}
static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
{
ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
rewrite->instead_of_nr++;
}
static const char *skip_spaces(const char *s)
{
while (isspace(*s))
s++;
return s;
}
static void read_remotes_file(struct remote_state *remote_state,
struct remote *remote)
{
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
if (!f)
return;
remote->configured_in_repo = 1;
remote->origin = REMOTE_REMOTES;
while (strbuf_getline(&buf, f) != EOF) {
const char *v;
strbuf_rtrim(&buf);
if (skip_prefix(buf.buf, "URL:", &v))
add_url_alias(remote_state, remote,
xstrdup(skip_spaces(v)));
else if (skip_prefix(buf.buf, "Push:", &v))
refspec_append(&remote->push, skip_spaces(v));
else if (skip_prefix(buf.buf, "Pull:", &v))
refspec_append(&remote->fetch, skip_spaces(v));
}
strbuf_release(&buf);
fclose(f);
}
static void read_branches_file(struct remote_state *remote_state,
struct remote *remote)
{
char *frag;
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
if (!f)
return;
strbuf_getline_lf(&buf, f);
fclose(f);
strbuf_trim(&buf);
if (!buf.len) {
strbuf_release(&buf);
return;
}
remote->configured_in_repo = 1;
remote->origin = REMOTE_BRANCHES;
/*
* The branches file would have URL and optionally
* #branch specified. The default (or specified) branch is
* fetched and stored in the local branch matching the
* remote name.
*/
frag = strchr(buf.buf, '#');
if (frag)
*(frag++) = '\0';
else
frag = (char *)git_default_branch_name(0);
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
frag, remote->name);
/*
* Cogito compatible push: push current HEAD to remote #branch
* (master if missing)
*/
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
remote->fetch_tags = 1; /* always auto-follow */
}
static int handle_config(const char *key, const char *value,
const struct config_context *ctx, void *cb)
{
const char *name;
size_t namelen;
const char *subkey;
struct remote *remote;
struct branch *branch;
struct remote_state *remote_state = cb;
const struct key_value_info *kvi = ctx->kvi;
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
/* There is no subsection. */
if (!name)
return 0;
/* There is a subsection, but it is empty. */
if (!namelen)
return -1;
branch = make_branch(remote_state, name, namelen);
if (!strcmp(subkey, "remote")) {
return git_config_string(&branch->remote_name, key, value);
} else if (!strcmp(subkey, "pushremote")) {
return git_config_string(&branch->pushremote_name, key, value);
} else if (!strcmp(subkey, "merge")) {
if (!value)
return config_error_nonbool(key);
add_merge(branch, xstrdup(value));
}
return 0;
}
if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
struct rewrite *rewrite;
if (!name)
return 0;
if (!strcmp(subkey, "insteadof")) {
if (!value)
return config_error_nonbool(key);
rewrite = make_rewrite(&remote_state->rewrites, name,
namelen);
add_instead_of(rewrite, xstrdup(value));
} else if (!strcmp(subkey, "pushinsteadof")) {
if (!value)
return config_error_nonbool(key);
rewrite = make_rewrite(&remote_state->rewrites_push,
name, namelen);
add_instead_of(rewrite, xstrdup(value));
}
}
if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
return 0;
/* Handle remote.* variables */
if (!name && !strcmp(subkey, "pushdefault"))
return git_config_string(&remote_state->pushremote_name, key,
value);
if (!name)
return 0;
/* Handle remote.<name>.* variables */
if (*name == '/') {
warning(_("config remote shorthand cannot begin with '/': %s"),
name);
return 0;
}
remote = make_remote(remote_state, name, namelen);
remote->origin = REMOTE_CONFIG;
if (kvi->scope == CONFIG_SCOPE_LOCAL ||
kvi->scope == CONFIG_SCOPE_WORKTREE)
remote->configured_in_repo = 1;
if (!strcmp(subkey, "mirror"))
remote->mirror = git_config_bool(key, value);
else if (!strcmp(subkey, "skipdefaultupdate"))
remote->skip_default_update = git_config_bool(key, value);
else if (!strcmp(subkey, "skipfetchall"))
remote->skip_default_update = git_config_bool(key, value);
else if (!strcmp(subkey, "prune"))
remote->prune = git_config_bool(key, value);
else if (!strcmp(subkey, "prunetags"))
remote->prune_tags = git_config_bool(key, value);
else if (!strcmp(subkey, "url")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
add_url(remote, v);
} else if (!strcmp(subkey, "pushurl")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
add_pushurl(remote, v);
} else if (!strcmp(subkey, "push")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
refspec_append(&remote->push, v);
free((char *)v);
} else if (!strcmp(subkey, "fetch")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
refspec_append(&remote->fetch, v);
free((char *)v);
} else if (!strcmp(subkey, "receivepack")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
if (!remote->receivepack)
remote->receivepack = v;
else
error(_("more than one receivepack given, using the first"));
} else if (!strcmp(subkey, "uploadpack")) {
const char *v;
if (git_config_string(&v, key, value))
return -1;
if (!remote->uploadpack)
remote->uploadpack = v;
else
error(_("more than one uploadpack given, using the first"));
} else if (!strcmp(subkey, "tagopt")) {
if (!strcmp(value, "--no-tags"))
remote->fetch_tags = -1;
else if (!strcmp(value, "--tags"))
remote->fetch_tags = 2;
} else if (!strcmp(subkey, "proxy")) {
return git_config_string((const char **)&remote->http_proxy,
key, value);
} else if (!strcmp(subkey, "proxyauthmethod")) {
return git_config_string((const char **)&remote->http_proxy_authmethod,
key, value);
} else if (!strcmp(subkey, "vcs")) {
return git_config_string(&remote->foreign_vcs, key, value);
}
return 0;
}
static void alias_all_urls(struct remote_state *remote_state)
{
int i, j;
for (i = 0; i < remote_state->remotes_nr; i++) {
int add_pushurl_aliases;
if (!remote_state->remotes[i])
continue;
for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {
remote_state->remotes[i]->pushurl[j] =
alias_url(remote_state->remotes[i]->pushurl[j],
&remote_state->rewrites);
}
add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;
for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {
if (add_pushurl_aliases)
add_pushurl_alias(
remote_state, remote_state->remotes[i],
remote_state->remotes[i]->url[j]);
remote_state->remotes[i]->url[j] =
alias_url(remote_state->remotes[i]->url[j],
&remote_state->rewrites);
}
}
}
static void read_config(struct repository *repo, int early)
{
int flag;
if (repo->remote_state->initialized)
return;
repo->remote_state->initialized = 1;
repo->remote_state->current_branch = NULL;
if (startup_info->have_repository && !early) {
const char *head_ref = refs_resolve_ref_unsafe(
get_main_ref_store(repo), "HEAD", 0, NULL, &flag);
if (head_ref && (flag & REF_ISSYMREF) &&
skip_prefix(head_ref, "refs/heads/", &head_ref)) {
repo->remote_state->current_branch = make_branch(
repo->remote_state, head_ref, strlen(head_ref));
}
}
repo_config(repo, handle_config, repo->remote_state);
alias_all_urls(repo->remote_state);
}
static int valid_remote_nick(const char *name)
{
if (!name[0] || is_dot_or_dotdot(name))
return 0;
/* remote nicknames cannot contain slashes */
while (*name)
if (is_dir_sep(*name++))
return 0;
return 1;
}
static const char *remotes_remote_for_branch(struct remote_state *remote_state,
struct branch *branch,
int *explicit)
{
if (branch && branch->remote_name) {
if (explicit)
*explicit = 1;
return branch->remote_name;
}
if (explicit)
*explicit = 0;
if (remote_state->remotes_nr == 1)
return remote_state->remotes[0]->name;
return "origin";
}
const char *remote_for_branch(struct branch *branch, int *explicit)
{
read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch);
return remotes_remote_for_branch(the_repository->remote_state, branch,
explicit);
}
static const char *
remotes_pushremote_for_branch(struct remote_state *remote_state,
struct branch *branch, int *explicit)
{
if (branch && branch->pushremote_name) {
if (explicit)
*explicit = 1;
return branch->pushremote_name;
}
if (remote_state->pushremote_name) {
if (explicit)
*explicit = 1;
return remote_state->pushremote_name;
}
return remotes_remote_for_branch(remote_state, branch, explicit);
}
const char *pushremote_for_branch(struct branch *branch, int *explicit)
{
read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch);
return remotes_pushremote_for_branch(the_repository->remote_state,
branch, explicit);
}
static struct remote *remotes_remote_get(struct remote_state *remote_state,
const char *name);
const char *remote_ref_for_branch(struct branch *branch, int for_push)
{
read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch);
if (branch) {
if (!for_push) {
if (branch->merge_nr) {
return branch->merge_name[0];
}
} else {
const char *dst,
*remote_name = remotes_pushremote_for_branch(
the_repository->remote_state, branch,
NULL);
struct remote *remote = remotes_remote_get(
the_repository->remote_state, remote_name);
if (remote && remote->push.nr &&
(dst = apply_refspecs(&remote->push,
branch->refname))) {
return dst;
}
}
}
return NULL;
}
static void validate_remote_url(struct remote *remote)
{
int i;
const char *value;
struct strbuf redacted = STRBUF_INIT;
int warn_not_die;
if (git_config_get_string_tmp("transfer.credentialsinurl", &value))
return;
if (!strcmp("warn", value))
warn_not_die = 1;
else if (!strcmp("die", value))
warn_not_die = 0;
else if (!strcmp("allow", value))
return;
else
die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value);
for (i = 0; i < remote->url_nr; i++) {
struct url_info url_info = { 0 };
if (!url_normalize(remote->url[i], &url_info) ||
!url_info.passwd_off)
goto loop_cleanup;
strbuf_reset(&redacted);
strbuf_add(&redacted, url_info.url, url_info.passwd_off);
strbuf_addstr(&redacted, "<redacted>");
strbuf_addstr(&redacted,
url_info.url + url_info.passwd_off + url_info.passwd_len);
if (warn_not_die)
warning(_("URL '%s' uses plaintext credentials"), redacted.buf);
else
die(_("URL '%s' uses plaintext credentials"), redacted.buf);
loop_cleanup:
free(url_info.url);
}
strbuf_release(&redacted);
}
static struct remote *
remotes_remote_get_1(struct remote_state *remote_state, const char *name,
const char *(*get_default)(struct remote_state *,
struct branch *, int *))
{
struct remote *ret;
int name_given = 0;
if (name)
name_given = 1;
else
name = get_default(remote_state, remote_state->current_branch,
&name_given);
ret = make_remote(remote_state, name, 0);
if (valid_remote_nick(name) && have_git_dir()) {
if (!valid_remote(ret))
read_remotes_file(remote_state, ret);
if (!valid_remote(ret))
read_branches_file(remote_state, ret);
}
if (name_given && !valid_remote(ret))
add_url_alias(remote_state, ret, name);
if (!valid_remote(ret))
return NULL;
validate_remote_url(ret);
return ret;
}
static inline struct remote *
remotes_remote_get(struct remote_state *remote_state, const char *name)
{
return remotes_remote_get_1(remote_state, name,
remotes_remote_for_branch);
}
struct remote *remote_get(const char *name)
{
read_config(the_repository, 0);
return remotes_remote_get(the_repository->remote_state, name);
}
struct remote *remote_get_early(const char *name)
{
read_config(the_repository, 1);
return remotes_remote_get(the_repository->remote_state, name);
}
static inline struct remote *
remotes_pushremote_get(struct remote_state *remote_state, const char *name)
{
return remotes_remote_get_1(remote_state, name,
remotes_pushremote_for_branch);
}
struct remote *pushremote_get(const char *name)
{
read_config(the_repository, 0);
return remotes_pushremote_get(the_repository->remote_state, name);
}
int remote_is_configured(struct remote *remote, int in_repo)
{
if (!remote)
return 0;
if (in_repo)
return remote->configured_in_repo;
return !!remote->origin;
}
int for_each_remote(each_remote_fn fn, void *priv)
{
int i, result = 0;
read_config(the_repository, 0);
for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
i++) {
struct remote *remote =
the_repository->remote_state->remotes[i];
if (!remote)
continue;
result = fn(remote, priv);
}
return result;
}
static void handle_duplicate(struct ref *ref1, struct ref *ref2)
{
if (strcmp(ref1->name, ref2->name)) {
if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
die(_("Cannot fetch both %s and %s to %s"),
ref1->name, ref2->name, ref2->peer_ref->name);
} else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
warning(_("%s usually tracks %s, not %s"),
ref2->peer_ref->name, ref2->name, ref1->name);
} else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
die(_("%s tracks both %s and %s"),
ref2->peer_ref->name, ref1->name, ref2->name);
} else {
/*
* This last possibility doesn't occur because
* FETCH_HEAD_IGNORE entries always appear at
* the end of the list.
*/
BUG("Internal error");
}
}
free(ref2->peer_ref);
free(ref2);
}
struct ref *ref_remove_duplicates(struct ref *ref_map)
{
struct string_list refs = STRING_LIST_INIT_NODUP;
struct ref *retval = NULL;
struct ref **p = &retval;
while (ref_map) {
struct ref *ref = ref_map;
ref_map = ref_map->next;
ref->next = NULL;
if (!ref->peer_ref) {
*p = ref;
p = &ref->next;
} else {
struct string_list_item *item =
string_list_insert(&refs, ref->peer_ref->name);
if (item->util) {
/* Entry already existed */
handle_duplicate((struct ref *)item->util, ref);
} else {
*p = ref;
p = &ref->next;
item->util = ref;
}
}
}
string_list_clear(&refs, 0);
return retval;
}
int remote_has_url(struct remote *remote, const char *url)
{
int i;
for (i = 0; i < remote->url_nr; i++) {
if (!strcmp(remote->url[i], url))
return 1;
}
return 0;
}
static int match_name_with_pattern(const char *key, const char *name,
const char *value, char **result)
{
const char *kstar = strchr(key, '*');
size_t klen;
size_t ksuffixlen;
size_t namelen;
int ret;
if (!kstar)
die(_("key '%s' of pattern had no '*'"), key);
klen = kstar - key;
ksuffixlen = strlen(kstar + 1);
namelen = strlen(name);
ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
!memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
if (ret && value) {
struct strbuf sb = STRBUF_INIT;
const char *vstar = strchr(value, '*');
if (!vstar)
die(_("value '%s' of pattern has no '*'"), value);
strbuf_add(&sb, value, vstar - value);
strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
strbuf_addstr(&sb, vstar + 1);
*result = strbuf_detach(&sb, NULL);
}
return ret;
}
static int refspec_match(const struct refspec_item *refspec,
const char *name)
{
if (refspec->pattern)
return match_name_with_pattern(refspec->src, name, NULL, NULL);
return !strcmp(refspec->src, name);
}
int omit_name_by_refspec(const char *name, struct refspec *rs)
{
int i;
for (i = 0; i < rs->nr; i++) {
if (rs->items[i].negative && refspec_match(&rs->items[i], name))
return 1;
}
return 0;
}
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
{
struct ref **tail;
for (tail = &ref_map; *tail; ) {
struct ref *ref = *tail;
if (omit_name_by_refspec(ref->name, rs)) {
*tail = ref->next;
free(ref->peer_ref);
free(ref);
} else
tail = &ref->next;
}
return ref_map;
}
static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
{
int i, matched_negative = 0;
int find_src = !query->src;
struct string_list reversed = STRING_LIST_INIT_DUP;
const char *needle = find_src ? query->dst : query->src;
/*
* Check whether the queried ref matches any negative refpsec. If so,
* then we should ultimately treat this as not matching the query at
* all.
*
* Note that negative refspecs always match the source, but the query
* item uses the destination. To handle this, we apply pattern
* refspecs in reverse to figure out if the query source matches any
* of the negative refspecs.
*
* The first loop finds and expands all positive refspecs
* matched by the queried ref.
*
* The second loop checks if any of the results of the first loop
* match any negative refspec.
*/
for (i = 0; i < rs->nr; i++) {
struct refspec_item *refspec = &rs->items[i];
char *expn_name;
if (refspec->negative)
continue;
/* Note the reversal of src and dst */
if (refspec->pattern) {
const char *key = refspec->dst ? refspec->dst : refspec->src;
const char *value = refspec->src;
if (match_name_with_pattern(key, needle, value, &expn_name))
string_list_append_nodup(&reversed, expn_name);
} else if (refspec->matching) {
/* For the special matching refspec, any query should match */
string_list_append(&reversed, needle);
} else if (!refspec->src) {
BUG("refspec->src should not be null here");
} else if (!strcmp(needle, refspec->src)) {
string_list_append(&reversed, refspec->src);
}
}
for (i = 0; !matched_negative && i < reversed.nr; i++) {
if (omit_name_by_refspec(reversed.items[i].string, rs))
matched_negative = 1;
}
string_list_clear(&reversed, 0);
return matched_negative;
}
static void query_refspecs_multiple(struct refspec *rs,
struct refspec_item *query,
struct string_list *results)
{
int i;
int find_src = !query->src;
if (find_src && !query->dst)
BUG("query_refspecs_multiple: need either src or dst");
if (query_matches_negative_refspec(rs, query))
return;
for (i = 0; i < rs->nr; i++) {
struct refspec_item *refspec = &rs->items[i];
const char *key = find_src ? refspec->dst : refspec->src;
const char *value = find_src ? refspec->src : refspec->dst;
const char *needle = find_src ? query->dst : query->src;
char **result = find_src ? &query->src : &query->dst;
if (!refspec->dst || refspec->negative)
continue;
if (refspec->pattern) {
if (match_name_with_pattern(key, needle, value, result))
string_list_append_nodup(results, *result);
} else if (!strcmp(needle, key)) {
string_list_append(results, value);
}
}
}
int query_refspecs(struct refspec *rs, struct refspec_item *query)
{
int i;
int find_src = !query->src;
const char *needle = find_src ? query->dst : query->src;
char **result = find_src ? &query->src : &query->dst;
if (find_src && !query->dst)
BUG("query_refspecs: need either src or dst");
if (query_matches_negative_refspec(rs, query))
return -1;
for (i = 0; i < rs->nr; i++) {
struct refspec_item *refspec = &rs->items[i];
const char *key = find_src ? refspec->dst : refspec->src;
const char *value = find_src ? refspec->src : refspec->dst;