5 static struct remote
**remotes
;
6 static int allocated_remotes
;
8 static struct branch
**branches
;
9 static int allocated_branches
;
11 static struct branch
*current_branch
;
12 static const char *default_remote_name
;
14 #define BUF_SIZE (2048)
15 static char buffer
[BUF_SIZE
];
17 static void add_push_refspec(struct remote
*remote
, const char *ref
)
19 int nr
= remote
->push_refspec_nr
+ 1;
20 remote
->push_refspec
=
21 xrealloc(remote
->push_refspec
, nr
* sizeof(char *));
22 remote
->push_refspec
[nr
-1] = ref
;
23 remote
->push_refspec_nr
= nr
;
26 static void add_fetch_refspec(struct remote
*remote
, const char *ref
)
28 int nr
= remote
->fetch_refspec_nr
+ 1;
29 remote
->fetch_refspec
=
30 xrealloc(remote
->fetch_refspec
, nr
* sizeof(char *));
31 remote
->fetch_refspec
[nr
-1] = ref
;
32 remote
->fetch_refspec_nr
= nr
;
35 static void add_url(struct remote
*remote
, const char *url
)
37 int nr
= remote
->url_nr
+ 1;
39 xrealloc(remote
->url
, nr
* sizeof(char *));
40 remote
->url
[nr
-1] = url
;
44 static struct remote
*make_remote(const char *name
, int len
)
48 for (i
= 0; i
< allocated_remotes
; i
++) {
53 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
54 !remotes
[i
]->name
[len
]) :
55 !strcmp(name
, remotes
[i
]->name
))
61 empty
= allocated_remotes
;
62 allocated_remotes
+= allocated_remotes
? allocated_remotes
: 1;
63 remotes
= xrealloc(remotes
,
64 sizeof(*remotes
) * allocated_remotes
);
65 memset(remotes
+ empty
, 0,
66 (allocated_remotes
- empty
) * sizeof(*remotes
));
68 remotes
[empty
] = xcalloc(1, sizeof(struct remote
));
70 remotes
[empty
]->name
= xstrndup(name
, len
);
72 remotes
[empty
]->name
= xstrdup(name
);
73 return remotes
[empty
];
76 static void add_merge(struct branch
*branch
, const char *name
)
78 int nr
= branch
->merge_nr
+ 1;
80 xrealloc(branch
->merge_name
, nr
* sizeof(char *));
81 branch
->merge_name
[nr
-1] = name
;
82 branch
->merge_nr
= nr
;
85 static struct branch
*make_branch(const char *name
, int len
)
90 for (i
= 0; i
< allocated_branches
; i
++) {
95 if (len
? (!strncmp(name
, branches
[i
]->name
, len
) &&
96 !branches
[i
]->name
[len
]) :
97 !strcmp(name
, branches
[i
]->name
))
103 empty
= allocated_branches
;
104 allocated_branches
+= allocated_branches
? allocated_branches
: 1;
105 branches
= xrealloc(branches
,
106 sizeof(*branches
) * allocated_branches
);
107 memset(branches
+ empty
, 0,
108 (allocated_branches
- empty
) * sizeof(*branches
));
110 branches
[empty
] = xcalloc(1, sizeof(struct branch
));
112 branches
[empty
]->name
= xstrndup(name
, len
);
114 branches
[empty
]->name
= xstrdup(name
);
115 refname
= malloc(strlen(name
) + strlen("refs/heads/") + 1);
116 strcpy(refname
, "refs/heads/");
117 strcpy(refname
+ strlen("refs/heads/"),
118 branches
[empty
]->name
);
119 branches
[empty
]->refname
= refname
;
121 return branches
[empty
];
124 static void read_remotes_file(struct remote
*remote
)
126 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
130 while (fgets(buffer
, BUF_SIZE
, f
)) {
134 if (!prefixcmp(buffer
, "URL:")) {
137 } else if (!prefixcmp(buffer
, "Push:")) {
140 } else if (!prefixcmp(buffer
, "Pull:")) {
152 while (isspace(p
[-1]))
155 switch (value_list
) {
157 add_url(remote
, xstrdup(s
));
160 add_push_refspec(remote
, xstrdup(s
));
163 add_fetch_refspec(remote
, xstrdup(s
));
170 static void read_branches_file(struct remote
*remote
)
172 const char *slash
= strchr(remote
->name
, '/');
175 int n
= slash
? slash
- remote
->name
: 1000;
176 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
182 s
= fgets(buffer
, BUF_SIZE
, f
);
191 while (isspace(p
[-1]))
195 len
+= strlen(slash
);
196 p
= xmalloc(len
+ 1);
200 frag
= strchr(p
, '#');
203 branch
= xmalloc(strlen(frag
) + 12);
204 strcpy(branch
, "refs/heads/");
205 strcat(branch
, frag
);
207 branch
= "refs/heads/master";
210 add_fetch_refspec(remote
, branch
);
211 remote
->fetch_tags
= 1; /* always auto-follow */
214 static int handle_config(const char *key
, const char *value
)
218 struct remote
*remote
;
219 struct branch
*branch
;
220 if (!prefixcmp(key
, "branch.")) {
222 subkey
= strrchr(name
, '.');
223 branch
= make_branch(name
, subkey
- name
);
228 if (!strcmp(subkey
, ".remote")) {
229 branch
->remote_name
= xstrdup(value
);
230 if (branch
== current_branch
)
231 default_remote_name
= branch
->remote_name
;
232 } else if (!strcmp(subkey
, ".merge"))
233 add_merge(branch
, xstrdup(value
));
236 if (prefixcmp(key
, "remote."))
239 subkey
= strrchr(name
, '.');
241 return error("Config with no key for remote %s", name
);
242 if (*subkey
== '/') {
243 warning("Config remote shorthand cannot begin with '/': %s", name
);
246 remote
= make_remote(name
, subkey
- name
);
248 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
251 * is a valid way to set it to true; we get NULL in value so
252 * we need to handle it here.
254 * if (!strcmp(subkey, ".disabled")) {
255 * val = git_config_bool(key, value);
260 return 0; /* ignore unknown booleans */
262 if (!strcmp(subkey
, ".url")) {
263 add_url(remote
, xstrdup(value
));
264 } else if (!strcmp(subkey
, ".push")) {
265 add_push_refspec(remote
, xstrdup(value
));
266 } else if (!strcmp(subkey
, ".fetch")) {
267 add_fetch_refspec(remote
, xstrdup(value
));
268 } else if (!strcmp(subkey
, ".receivepack")) {
269 if (!remote
->receivepack
)
270 remote
->receivepack
= xstrdup(value
);
272 error("more than one receivepack given, using the first");
273 } else if (!strcmp(subkey
, ".uploadpack")) {
274 if (!remote
->uploadpack
)
275 remote
->uploadpack
= xstrdup(value
);
277 error("more than one uploadpack given, using the first");
278 } else if (!strcmp(subkey
, ".tagopt")) {
279 if (!strcmp(value
, "--no-tags"))
280 remote
->fetch_tags
= -1;
281 } else if (!strcmp(subkey
, ".proxy")) {
282 remote
->http_proxy
= xstrdup(value
);
287 static void read_config(void)
289 unsigned char sha1
[20];
290 const char *head_ref
;
292 if (default_remote_name
) // did this already
294 default_remote_name
= xstrdup("origin");
295 current_branch
= NULL
;
296 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
297 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
298 !prefixcmp(head_ref
, "refs/heads/")) {
300 make_branch(head_ref
+ strlen("refs/heads/"), 0);
302 git_config(handle_config
);
305 struct refspec
*parse_ref_spec(int nr_refspec
, const char **refspec
)
308 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
309 for (i
= 0; i
< nr_refspec
; i
++) {
310 const char *sp
, *ep
, *gp
;
316 gp
= strchr(sp
, '*');
317 ep
= strchr(sp
, ':');
318 if (gp
&& ep
&& gp
> ep
)
322 const char *glob
= strchr(ep
+ 1, '*');
326 rs
[i
].dst
= xstrndup(ep
+ 1,
329 rs
[i
].dst
= xstrdup(ep
+ 1);
332 ep
= sp
+ strlen(sp
);
338 rs
[i
].src
= xstrndup(sp
, ep
- sp
);
343 struct remote
*remote_get(const char *name
)
349 name
= default_remote_name
;
350 ret
= make_remote(name
, 0);
351 if (name
[0] != '/') {
353 read_remotes_file(ret
);
355 read_branches_file(ret
);
361 ret
->fetch
= parse_ref_spec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
362 ret
->push
= parse_ref_spec(ret
->push_refspec_nr
, ret
->push_refspec
);
366 int for_each_remote(each_remote_fn fn
, void *priv
)
370 for (i
= 0; i
< allocated_remotes
&& !result
; i
++) {
371 struct remote
*r
= remotes
[i
];
375 r
->fetch
= parse_ref_spec(r
->fetch_refspec_nr
,
378 r
->push
= parse_ref_spec(r
->push_refspec_nr
,
380 result
= fn(r
, priv
);
385 void ref_remove_duplicates(struct ref
*ref_map
)
389 for (; ref_map
; ref_map
= ref_map
->next
) {
390 if (!ref_map
->peer_ref
)
392 posn
= &ref_map
->next
;
394 if ((*posn
)->peer_ref
&&
395 !strcmp((*posn
)->peer_ref
->name
,
396 ref_map
->peer_ref
->name
)) {
397 if (strcmp((*posn
)->name
, ref_map
->name
))
398 die("%s tracks both %s and %s",
399 ref_map
->peer_ref
->name
,
400 (*posn
)->name
, ref_map
->name
);
401 next
= (*posn
)->next
;
402 free((*posn
)->peer_ref
);
406 posn
= &(*posn
)->next
;
412 int remote_has_url(struct remote
*remote
, const char *url
)
415 for (i
= 0; i
< remote
->url_nr
; i
++) {
416 if (!strcmp(remote
->url
[i
], url
))
422 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
424 int find_src
= refspec
->src
== NULL
;
425 char *needle
, **result
;
430 return error("find_tracking: need either src or dst");
431 needle
= refspec
->dst
;
432 result
= &refspec
->src
;
434 needle
= refspec
->src
;
435 result
= &refspec
->dst
;
438 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
439 struct refspec
*fetch
= &remote
->fetch
[i
];
440 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
441 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
444 if (fetch
->pattern
) {
445 if (!prefixcmp(needle
, key
)) {
446 *result
= xmalloc(strlen(value
) +
449 strcpy(*result
, value
);
450 strcpy(*result
+ strlen(value
),
451 needle
+ strlen(key
));
452 refspec
->force
= fetch
->force
;
455 } else if (!strcmp(needle
, key
)) {
456 *result
= xstrdup(value
);
457 refspec
->force
= fetch
->force
;
464 struct ref
*alloc_ref(unsigned namelen
)
466 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
467 memset(ret
, 0, sizeof(struct ref
) + namelen
);
471 static struct ref
*copy_ref(const struct ref
*ref
)
473 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
474 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
479 struct ref
*copy_ref_list(const struct ref
*ref
)
481 struct ref
*ret
= NULL
;
482 struct ref
**tail
= &ret
;
484 *tail
= copy_ref(ref
);
486 tail
= &((*tail
)->next
);
491 void free_refs(struct ref
*ref
)
503 static int count_refspec_match(const char *pattern
,
505 struct ref
**matched_ref
)
507 int patlen
= strlen(pattern
);
508 struct ref
*matched_weak
= NULL
;
509 struct ref
*matched
= NULL
;
513 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
514 char *name
= refs
->name
;
515 int namelen
= strlen(name
);
517 if (!refname_match(pattern
, name
, ref_rev_parse_rules
))
520 /* A match is "weak" if it is with refs outside
521 * heads or tags, and did not specify the pattern
522 * in full (e.g. "refs/remotes/origin/master") or at
523 * least from the toplevel (e.g. "remotes/origin/master");
524 * otherwise "git push $URL master" would result in
525 * ambiguity between remotes/origin/master and heads/master
526 * at the remote site.
528 if (namelen
!= patlen
&&
529 patlen
!= namelen
- 5 &&
530 prefixcmp(name
, "refs/heads/") &&
531 prefixcmp(name
, "refs/tags/")) {
532 /* We want to catch the case where only weak
533 * matches are found and there are multiple
534 * matches, and where more than one strong
535 * matches are found, as ambiguous. One
536 * strong match with zero or more weak matches
537 * are acceptable as a unique match.
548 *matched_ref
= matched_weak
;
552 *matched_ref
= matched
;
557 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
565 static struct ref
*try_explicit_object_name(const char *name
)
567 unsigned char sha1
[20];
573 strcpy(ref
->name
, "(delete)");
574 hashclr(ref
->new_sha1
);
577 if (get_sha1(name
, sha1
))
579 len
= strlen(name
) + 1;
580 ref
= alloc_ref(len
);
581 memcpy(ref
->name
, name
, len
);
582 hashcpy(ref
->new_sha1
, sha1
);
586 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
591 len
= strlen(name
) + 1;
592 ret
= alloc_ref(len
);
593 memcpy(ret
->name
, name
, len
);
594 tail_link_ref(ret
, tail
);
598 static int match_explicit(struct ref
*src
, struct ref
*dst
,
599 struct ref
***dst_tail
,
603 struct ref
*matched_src
, *matched_dst
;
605 const char *dst_value
= rs
->dst
;
610 matched_src
= matched_dst
= NULL
;
611 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
615 /* The source could be in the get_sha1() format
616 * not a reference name. :refs/other is a
617 * way to delete 'other' ref at the remote end.
619 matched_src
= try_explicit_object_name(rs
->src
);
621 error("src refspec %s does not match any.", rs
->src
);
625 error("src refspec %s matches more than one.", rs
->src
);
635 dst_value
= matched_src
->name
;
638 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
642 if (!memcmp(dst_value
, "refs/", 5))
643 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
645 error("dst refspec %s does not match any "
646 "existing ref on the remote and does "
647 "not start with refs/.", dst_value
);
651 error("dst refspec %s matches more than one.",
655 if (errs
|| !matched_dst
)
657 if (matched_dst
->peer_ref
) {
659 error("dst ref %s receives from more than one src.",
663 matched_dst
->peer_ref
= matched_src
;
664 matched_dst
->force
= rs
->force
;
669 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
670 struct ref
***dst_tail
, struct refspec
*rs
,
674 for (i
= errs
= 0; i
< rs_nr
; i
++)
675 errs
|= match_explicit(src
, dst
, dst_tail
, &rs
[i
], errs
);
679 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
681 const struct ref
*src
)
684 for (i
= 0; i
< rs_nr
; i
++) {
685 if (rs
[i
].pattern
&& !prefixcmp(src
->name
, rs
[i
].src
))
692 * Note. This is used only by "push"; refspec matching rules for
693 * push and fetch are subtly different, so do not try to reuse it
696 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
697 int nr_refspec
, const char **refspec
, int flags
)
700 parse_ref_spec(nr_refspec
, (const char **) refspec
);
701 int send_all
= flags
& MATCH_REFS_ALL
;
702 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
704 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
707 /* pick the remainder */
708 for ( ; src
; src
= src
->next
) {
709 struct ref
*dst_peer
;
710 const struct refspec
*pat
= NULL
;
715 pat
= check_pattern_match(rs
, nr_refspec
, src
);
719 else if (!send_mirror
&& prefixcmp(src
->name
, "refs/heads/"))
721 * "matching refs"; traditionally we pushed everything
722 * including refs outside refs/heads/ hierarchy, but
723 * that does not make much sense these days.
728 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
729 dst_name
= xmalloc(strlen(dst_side
) +
731 strlen(pat
->src
) + 2);
732 strcpy(dst_name
, dst_side
);
733 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
735 dst_name
= xstrdup(src
->name
);
736 dst_peer
= find_ref_by_name(dst
, dst_name
);
737 if (dst_peer
&& dst_peer
->peer_ref
)
738 /* We're already sending something to this ref. */
741 if (!dst_peer
&& !nr_refspec
&& !(send_all
|| send_mirror
))
743 * Remote doesn't have it, and we have no
744 * explicit pattern, and we don't have
745 * --all nor --mirror.
749 /* Create a new one and link it */
750 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
751 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
753 dst_peer
->peer_ref
= src
;
755 dst_peer
->force
= pat
->force
;
762 struct branch
*branch_get(const char *name
)
767 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
768 ret
= current_branch
;
770 ret
= make_branch(name
, 0);
771 if (ret
&& ret
->remote_name
) {
772 ret
->remote
= remote_get(ret
->remote_name
);
775 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
777 for (i
= 0; i
< ret
->merge_nr
; i
++) {
778 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
779 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
780 remote_find_tracking(ret
->remote
,
788 int branch_has_merge_config(struct branch
*branch
)
790 return branch
&& !!branch
->merge
;
793 int branch_merge_matches(struct branch
*branch
,
797 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
799 return refname_match(branch
->merge
[i
]->src
, refname
, ref_fetch_rules
);
802 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
803 const struct refspec
*refspec
)
805 const struct ref
*ref
;
806 struct ref
*ret
= NULL
;
807 struct ref
**tail
= &ret
;
809 int remote_prefix_len
= strlen(refspec
->src
);
810 int local_prefix_len
= strlen(refspec
->dst
);
812 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
813 if (strchr(ref
->name
, '^'))
814 continue; /* a dereference item */
815 if (!prefixcmp(ref
->name
, refspec
->src
)) {
817 struct ref
*cpy
= copy_ref(ref
);
818 match
= ref
->name
+ remote_prefix_len
;
820 cpy
->peer_ref
= alloc_ref(local_prefix_len
+
822 sprintf(cpy
->peer_ref
->name
, "%s%s",
823 refspec
->dst
, match
);
825 cpy
->peer_ref
->force
= 1;
834 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
836 const struct ref
*ref
;
837 for (ref
= refs
; ref
; ref
= ref
->next
) {
838 if (refname_match(name
, ref
->name
, ref_fetch_rules
))
844 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
846 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
851 return copy_ref(ref
);
854 static struct ref
*get_local_ref(const char *name
)
860 if (!prefixcmp(name
, "refs/")) {
861 ret
= alloc_ref(strlen(name
) + 1);
862 strcpy(ret
->name
, name
);
866 if (!prefixcmp(name
, "heads/") ||
867 !prefixcmp(name
, "tags/") ||
868 !prefixcmp(name
, "remotes/")) {
869 ret
= alloc_ref(strlen(name
) + 6);
870 sprintf(ret
->name
, "refs/%s", name
);
874 ret
= alloc_ref(strlen(name
) + 12);
875 sprintf(ret
->name
, "refs/heads/%s", name
);
879 int get_fetch_map(const struct ref
*remote_refs
,
880 const struct refspec
*refspec
,
884 struct ref
*ref_map
, *rm
;
886 if (refspec
->pattern
) {
887 ref_map
= get_expanded_map(remote_refs
, refspec
);
889 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
891 ref_map
= get_remote_ref(remote_refs
, name
);
892 if (!missing_ok
&& !ref_map
)
893 die("Couldn't find remote ref %s", name
);
895 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
896 if (ref_map
->peer_ref
&& refspec
->force
)
897 ref_map
->peer_ref
->force
= 1;
901 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
902 if (rm
->peer_ref
&& check_ref_format(rm
->peer_ref
->name
+ 5))
903 die("* refusing to create funny ref '%s' locally",
908 tail_link_ref(ref_map
, tail
);