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;
285 static void read_config(void)
287 unsigned char sha1
[20];
288 const char *head_ref
;
290 if (default_remote_name
) // did this already
292 default_remote_name
= xstrdup("origin");
293 current_branch
= NULL
;
294 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
295 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
296 !prefixcmp(head_ref
, "refs/heads/")) {
298 make_branch(head_ref
+ strlen("refs/heads/"), 0);
300 git_config(handle_config
);
303 struct refspec
*parse_ref_spec(int nr_refspec
, const char **refspec
)
306 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
307 for (i
= 0; i
< nr_refspec
; i
++) {
308 const char *sp
, *ep
, *gp
;
314 gp
= strchr(sp
, '*');
315 ep
= strchr(sp
, ':');
316 if (gp
&& ep
&& gp
> ep
)
320 const char *glob
= strchr(ep
+ 1, '*');
324 rs
[i
].dst
= xstrndup(ep
+ 1,
327 rs
[i
].dst
= xstrdup(ep
+ 1);
330 ep
= sp
+ strlen(sp
);
336 rs
[i
].src
= xstrndup(sp
, ep
- sp
);
341 struct remote
*remote_get(const char *name
)
347 name
= default_remote_name
;
348 ret
= make_remote(name
, 0);
349 if (name
[0] != '/') {
351 read_remotes_file(ret
);
353 read_branches_file(ret
);
359 ret
->fetch
= parse_ref_spec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
360 ret
->push
= parse_ref_spec(ret
->push_refspec_nr
, ret
->push_refspec
);
364 int for_each_remote(each_remote_fn fn
, void *priv
)
368 for (i
= 0; i
< allocated_remotes
&& !result
; i
++) {
369 struct remote
*r
= remotes
[i
];
373 r
->fetch
= parse_ref_spec(r
->fetch_refspec_nr
,
376 r
->push
= parse_ref_spec(r
->push_refspec_nr
,
378 result
= fn(r
, priv
);
383 void ref_remove_duplicates(struct ref
*ref_map
)
387 for (; ref_map
; ref_map
= ref_map
->next
) {
388 if (!ref_map
->peer_ref
)
390 posn
= &ref_map
->next
;
392 if ((*posn
)->peer_ref
&&
393 !strcmp((*posn
)->peer_ref
->name
,
394 ref_map
->peer_ref
->name
)) {
395 if (strcmp((*posn
)->name
, ref_map
->name
))
396 die("%s tracks both %s and %s",
397 ref_map
->peer_ref
->name
,
398 (*posn
)->name
, ref_map
->name
);
399 next
= (*posn
)->next
;
400 free((*posn
)->peer_ref
);
404 posn
= &(*posn
)->next
;
410 int remote_has_url(struct remote
*remote
, const char *url
)
413 for (i
= 0; i
< remote
->url_nr
; i
++) {
414 if (!strcmp(remote
->url
[i
], url
))
421 * Returns true if, under the matching rules for fetching, name is the
422 * same as the given full name.
424 static int ref_matches_abbrev(const char *name
, const char *full
)
426 if (!prefixcmp(name
, "refs/") || !strcmp(name
, "HEAD"))
427 return !strcmp(name
, full
);
428 if (prefixcmp(full
, "refs/"))
430 if (!prefixcmp(name
, "heads/") ||
431 !prefixcmp(name
, "tags/") ||
432 !prefixcmp(name
, "remotes/"))
433 return !strcmp(name
, full
+ 5);
434 if (prefixcmp(full
+ 5, "heads/"))
436 return !strcmp(full
+ 11, name
);
439 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
441 int find_src
= refspec
->src
== NULL
;
442 char *needle
, **result
;
447 return error("find_tracking: need either src or dst");
448 needle
= refspec
->dst
;
449 result
= &refspec
->src
;
451 needle
= refspec
->src
;
452 result
= &refspec
->dst
;
455 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
456 struct refspec
*fetch
= &remote
->fetch
[i
];
457 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
458 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
461 if (fetch
->pattern
) {
462 if (!prefixcmp(needle
, key
)) {
463 *result
= xmalloc(strlen(value
) +
466 strcpy(*result
, value
);
467 strcpy(*result
+ strlen(value
),
468 needle
+ strlen(key
));
469 refspec
->force
= fetch
->force
;
472 } else if (!strcmp(needle
, key
)) {
473 *result
= xstrdup(value
);
474 refspec
->force
= fetch
->force
;
481 struct ref
*alloc_ref(unsigned namelen
)
483 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
484 memset(ret
, 0, sizeof(struct ref
) + namelen
);
488 static struct ref
*copy_ref(struct ref
*ref
)
490 struct ref
*ret
= xmalloc(sizeof(struct ref
) + strlen(ref
->name
) + 1);
491 memcpy(ret
, ref
, sizeof(struct ref
) + strlen(ref
->name
) + 1);
496 void free_refs(struct ref
*ref
)
508 static int count_refspec_match(const char *pattern
,
510 struct ref
**matched_ref
)
512 int patlen
= strlen(pattern
);
513 struct ref
*matched_weak
= NULL
;
514 struct ref
*matched
= NULL
;
518 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
519 char *name
= refs
->name
;
520 int namelen
= strlen(name
);
522 if (namelen
< patlen
||
523 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
525 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
528 /* A match is "weak" if it is with refs outside
529 * heads or tags, and did not specify the pattern
530 * in full (e.g. "refs/remotes/origin/master") or at
531 * least from the toplevel (e.g. "remotes/origin/master");
532 * otherwise "git push $URL master" would result in
533 * ambiguity between remotes/origin/master and heads/master
534 * at the remote site.
536 if (namelen
!= patlen
&&
537 patlen
!= namelen
- 5 &&
538 prefixcmp(name
, "refs/heads/") &&
539 prefixcmp(name
, "refs/tags/")) {
540 /* We want to catch the case where only weak
541 * matches are found and there are multiple
542 * matches, and where more than one strong
543 * matches are found, as ambiguous. One
544 * strong match with zero or more weak matches
545 * are acceptable as a unique match.
556 *matched_ref
= matched_weak
;
560 *matched_ref
= matched
;
565 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
573 static struct ref
*try_explicit_object_name(const char *name
)
575 unsigned char sha1
[20];
581 strcpy(ref
->name
, "(delete)");
582 hashclr(ref
->new_sha1
);
585 if (get_sha1(name
, sha1
))
587 len
= strlen(name
) + 1;
588 ref
= alloc_ref(len
);
589 memcpy(ref
->name
, name
, len
);
590 hashcpy(ref
->new_sha1
, sha1
);
594 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
599 len
= strlen(name
) + 1;
600 ret
= alloc_ref(len
);
601 memcpy(ret
->name
, name
, len
);
602 tail_link_ref(ret
, tail
);
606 static int match_explicit(struct ref
*src
, struct ref
*dst
,
607 struct ref
***dst_tail
,
611 struct ref
*matched_src
, *matched_dst
;
613 const char *dst_value
= rs
->dst
;
618 matched_src
= matched_dst
= NULL
;
619 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
623 /* The source could be in the get_sha1() format
624 * not a reference name. :refs/other is a
625 * way to delete 'other' ref at the remote end.
627 matched_src
= try_explicit_object_name(rs
->src
);
629 error("src refspec %s does not match any.", rs
->src
);
633 error("src refspec %s matches more than one.", rs
->src
);
643 dst_value
= matched_src
->name
;
646 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
650 if (!memcmp(dst_value
, "refs/", 5))
651 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
653 error("dst refspec %s does not match any "
654 "existing ref on the remote and does "
655 "not start with refs/.", dst_value
);
659 error("dst refspec %s matches more than one.",
663 if (errs
|| !matched_dst
)
665 if (matched_dst
->peer_ref
) {
667 error("dst ref %s receives from more than one src.",
671 matched_dst
->peer_ref
= matched_src
;
672 matched_dst
->force
= rs
->force
;
677 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
678 struct ref
***dst_tail
, struct refspec
*rs
,
682 for (i
= errs
= 0; i
< rs_nr
; i
++)
683 errs
|= match_explicit(src
, dst
, dst_tail
, &rs
[i
], errs
);
687 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
689 for ( ; list
; list
= list
->next
)
690 if (!strcmp(list
->name
, name
))
695 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
697 const struct ref
*src
)
700 for (i
= 0; i
< rs_nr
; i
++) {
701 if (rs
[i
].pattern
&& !prefixcmp(src
->name
, rs
[i
].src
))
708 * Note. This is used only by "push"; refspec matching rules for
709 * push and fetch are subtly different, so do not try to reuse it
712 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
713 int nr_refspec
, char **refspec
, int all
)
716 parse_ref_spec(nr_refspec
, (const char **) refspec
);
718 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
721 /* pick the remainder */
722 for ( ; src
; src
= src
->next
) {
723 struct ref
*dst_peer
;
724 const struct refspec
*pat
= NULL
;
729 pat
= check_pattern_match(rs
, nr_refspec
, src
);
733 else if (prefixcmp(src
->name
, "refs/heads/"))
735 * "matching refs"; traditionally we pushed everything
736 * including refs outside refs/heads/ hierarchy, but
737 * that does not make much sense these days.
742 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
743 dst_name
= xmalloc(strlen(dst_side
) +
745 strlen(pat
->src
) + 2);
746 strcpy(dst_name
, dst_side
);
747 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
749 dst_name
= xstrdup(src
->name
);
750 dst_peer
= find_ref_by_name(dst
, dst_name
);
751 if (dst_peer
&& dst_peer
->peer_ref
)
752 /* We're already sending something to this ref. */
754 if (!dst_peer
&& !nr_refspec
&& !all
)
755 /* Remote doesn't have it, and we have no
756 * explicit pattern, and we don't have
760 /* Create a new one and link it */
761 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
762 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
764 dst_peer
->peer_ref
= src
;
766 dst_peer
->force
= pat
->force
;
773 struct branch
*branch_get(const char *name
)
778 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
779 ret
= current_branch
;
781 ret
= make_branch(name
, 0);
782 if (ret
&& ret
->remote_name
) {
783 ret
->remote
= remote_get(ret
->remote_name
);
786 ret
->merge
= xcalloc(sizeof(*ret
->merge
),
788 for (i
= 0; i
< ret
->merge_nr
; i
++) {
789 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
790 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
791 remote_find_tracking(ret
->remote
,
799 int branch_has_merge_config(struct branch
*branch
)
801 return branch
&& !!branch
->merge
;
804 int branch_merge_matches(struct branch
*branch
,
808 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
810 return ref_matches_abbrev(branch
->merge
[i
]->src
, refname
);
813 static struct ref
*get_expanded_map(struct ref
*remote_refs
,
814 const struct refspec
*refspec
)
817 struct ref
*ret
= NULL
;
818 struct ref
**tail
= &ret
;
820 int remote_prefix_len
= strlen(refspec
->src
);
821 int local_prefix_len
= strlen(refspec
->dst
);
823 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
824 if (strchr(ref
->name
, '^'))
825 continue; /* a dereference item */
826 if (!prefixcmp(ref
->name
, refspec
->src
)) {
828 struct ref
*cpy
= copy_ref(ref
);
829 match
= ref
->name
+ remote_prefix_len
;
831 cpy
->peer_ref
= alloc_ref(local_prefix_len
+
833 sprintf(cpy
->peer_ref
->name
, "%s%s",
834 refspec
->dst
, match
);
836 cpy
->peer_ref
->force
= 1;
845 static struct ref
*find_ref_by_name_abbrev(struct ref
*refs
, const char *name
)
848 for (ref
= refs
; ref
; ref
= ref
->next
) {
849 if (ref_matches_abbrev(name
, ref
->name
))
855 struct ref
*get_remote_ref(struct ref
*remote_refs
, const char *name
)
857 struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
862 return copy_ref(ref
);
865 static struct ref
*get_local_ref(const char *name
)
871 if (!prefixcmp(name
, "refs/")) {
872 ret
= alloc_ref(strlen(name
) + 1);
873 strcpy(ret
->name
, name
);
877 if (!prefixcmp(name
, "heads/") ||
878 !prefixcmp(name
, "tags/") ||
879 !prefixcmp(name
, "remotes/")) {
880 ret
= alloc_ref(strlen(name
) + 6);
881 sprintf(ret
->name
, "refs/%s", name
);
885 ret
= alloc_ref(strlen(name
) + 12);
886 sprintf(ret
->name
, "refs/heads/%s", name
);
890 int get_fetch_map(struct ref
*remote_refs
,
891 const struct refspec
*refspec
,
895 struct ref
*ref_map
, *rm
;
897 if (refspec
->pattern
) {
898 ref_map
= get_expanded_map(remote_refs
, refspec
);
900 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
902 ref_map
= get_remote_ref(remote_refs
, name
);
903 if (!missing_ok
&& !ref_map
)
904 die("Couldn't find remote ref %s", name
);
906 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
907 if (ref_map
->peer_ref
&& refspec
->force
)
908 ref_map
->peer_ref
->force
= 1;
912 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
913 if (rm
->peer_ref
&& check_ref_format(rm
->peer_ref
->name
+ 5))
914 die("* refusing to create funny ref '%s' locally",
919 tail_link_ref(ref_map
, tail
);