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, '.');
225 branch = make_branch(name, subkey - name);
226 if (!strcmp(subkey, ".remote")) {
228 return config_error_nonbool(key);
229 branch->remote_name = xstrdup(value);
230 if (branch == current_branch)
231 default_remote_name = branch->remote_name;
232 } else if (!strcmp(subkey, ".merge")) {
234 return config_error_nonbool(key);
235 add_merge(branch, xstrdup(value));
239 if (prefixcmp(key, "remote."))
242 subkey = strrchr(name, '.');
244 return error("Config with no key for remote %s", name);
245 if (*subkey == '/') {
246 warning("Config remote shorthand cannot begin with '/': %s", name);
249 remote = make_remote(name, subkey - name);
251 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
254 * is a valid way to set it to true; we get NULL in value so
255 * we need to handle it here.
257 * if (!strcmp(subkey, ".disabled")) {
258 * val = git_config_bool(key, value);
263 return 0; /* ignore unknown booleans */
265 if (!strcmp(subkey, ".url")) {
266 add_url(remote, xstrdup(value));
267 } else if (!strcmp(subkey, ".push")) {
268 add_push_refspec(remote, xstrdup(value));
269 } else if (!strcmp(subkey, ".fetch")) {
270 add_fetch_refspec(remote, xstrdup(value));
271 } else if (!strcmp(subkey, ".receivepack")) {
272 if (!remote->receivepack)
273 remote->receivepack = xstrdup(value);
275 error("more than one receivepack given, using the first");
276 } else if (!strcmp(subkey, ".uploadpack")) {
277 if (!remote->uploadpack)
278 remote->uploadpack = xstrdup(value);
280 error("more than one uploadpack given, using the first");
281 } else if (!strcmp(subkey, ".tagopt")) {
282 if (!strcmp(value, "--no-tags"))
283 remote->fetch_tags = -1;
284 } else if (!strcmp(subkey, ".proxy")) {
285 remote->http_proxy = xstrdup(value);
290 static void read_config(void)
292 unsigned char sha1[20];
293 const char *head_ref;
295 if (default_remote_name) // did this already
297 default_remote_name = xstrdup("origin");
298 current_branch = NULL;
299 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
300 if (head_ref && (flag & REF_ISSYMREF) &&
301 !prefixcmp(head_ref, "refs/heads/")) {
303 make_branch(head_ref + strlen("refs/heads/"), 0);
305 git_config(handle_config);
308 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
312 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
314 for (i = 0; i < nr_refspec; i++) {
317 const char *lhs, *rhs;
319 llen = rlen = is_glob = 0;
327 rhs = strrchr(lhs, ':');
331 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
334 rs[i].dst = xstrndup(rhs, rlen);
337 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
338 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
339 if ((rhs && !is_glob) || (!rhs && fetch))
343 } else if (rhs && is_glob) {
347 rs[i].pattern = is_glob;
348 rs[i].src = xstrndup(lhs, llen);
353 * - empty is allowed; it means HEAD.
354 * - otherwise it must be a valid looking ref.
359 st = check_ref_format(rs[i].src);
360 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
365 * - missing is ok, and is same as empty.
366 * - empty is ok; it means not to store.
367 * - otherwise it must be a valid looking ref.
371 } else if (!*rs[i].dst) {
374 st = check_ref_format(rs[i].dst);
375 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
381 * - empty is allowed; it means delete.
382 * - when wildcarded, it must be a valid looking ref.
383 * - otherwise, it must be an extended SHA-1, but
384 * there is no existing way to validate this.
389 st = check_ref_format(rs[i].src);
390 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
394 ; /* anything goes, for now */
397 * - missing is allowed, but LHS then must be a
399 * - empty is not allowed.
400 * - otherwise it must be a valid looking ref.
403 st = check_ref_format(rs[i].src);
404 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
406 } else if (!*rs[i].dst) {
409 st = check_ref_format(rs[i].dst);
410 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
418 die("Invalid refspec '%s'", refspec[i]);
421 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
423 return parse_refspec_internal(nr_refspec, refspec, 1);
426 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
428 return parse_refspec_internal(nr_refspec, refspec, 0);
431 static int valid_remote_nick(const char *name)
433 if (!name[0] || /* not empty */
434 (name[0] == '.' && /* not "." */
435 (!name[1] || /* not ".." */
436 (name[1] == '.' && !name[2]))))
438 return !strchr(name, '/'); /* no slash */
441 struct remote *remote_get(const char *name)
447 name = default_remote_name;
448 ret = make_remote(name, 0);
449 if (valid_remote_nick(name)) {
451 read_remotes_file(ret);
453 read_branches_file(ret);
459 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
460 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
464 int for_each_remote(each_remote_fn fn, void *priv)
468 for (i = 0; i < allocated_remotes && !result; i++) {
469 struct remote *r = remotes[i];
473 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
476 r->push = parse_push_refspec(r->push_refspec_nr,
478 result = fn(r, priv);
483 void ref_remove_duplicates(struct ref *ref_map)
487 for (; ref_map; ref_map = ref_map->next) {
488 if (!ref_map->peer_ref)
490 posn = &ref_map->next;
492 if ((*posn)->peer_ref &&
493 !strcmp((*posn)->peer_ref->name,
494 ref_map->peer_ref->name)) {
495 if (strcmp((*posn)->name, ref_map->name))
496 die("%s tracks both %s and %s",
497 ref_map->peer_ref->name,
498 (*posn)->name, ref_map->name);
499 next = (*posn)->next;
500 free((*posn)->peer_ref);
504 posn = &(*posn)->next;
510 int remote_has_url(struct remote *remote, const char *url)
513 for (i = 0; i < remote->url_nr; i++) {
514 if (!strcmp(remote->url[i], url))
520 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
522 int find_src = refspec->src == NULL;
523 char *needle, **result;
528 return error("find_tracking: need either src or dst");
529 needle = refspec->dst;
530 result = &refspec->src;
532 needle = refspec->src;
533 result = &refspec->dst;
536 for (i = 0; i < remote->fetch_refspec_nr; i++) {
537 struct refspec *fetch = &remote->fetch[i];
538 const char *key = find_src ? fetch->dst : fetch->src;
539 const char *value = find_src ? fetch->src : fetch->dst;
542 if (fetch->pattern) {
543 if (!prefixcmp(needle, key) &&
544 needle[strlen(key)] == '/') {
545 *result = xmalloc(strlen(value) +
548 strcpy(*result, value);
549 strcpy(*result + strlen(value),
550 needle + strlen(key));
551 refspec->force = fetch->force;
554 } else if (!strcmp(needle, key)) {
555 *result = xstrdup(value);
556 refspec->force = fetch->force;
563 struct ref *alloc_ref(unsigned namelen)
565 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
566 memset(ret, 0, sizeof(struct ref) + namelen);
570 static struct ref *copy_ref(const struct ref *ref)
572 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
573 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
578 struct ref *copy_ref_list(const struct ref *ref)
580 struct ref *ret = NULL;
581 struct ref **tail = &ret;
583 *tail = copy_ref(ref);
585 tail = &((*tail)->next);
590 void free_refs(struct ref *ref)
602 static int count_refspec_match(const char *pattern,
604 struct ref **matched_ref)
606 int patlen = strlen(pattern);
607 struct ref *matched_weak = NULL;
608 struct ref *matched = NULL;
612 for (weak_match = match = 0; refs; refs = refs->next) {
613 char *name = refs->name;
614 int namelen = strlen(name);
616 if (!refname_match(pattern, name, ref_rev_parse_rules))
619 /* A match is "weak" if it is with refs outside
620 * heads or tags, and did not specify the pattern
621 * in full (e.g. "refs/remotes/origin/master") or at
622 * least from the toplevel (e.g. "remotes/origin/master");
623 * otherwise "git push $URL master" would result in
624 * ambiguity between remotes/origin/master and heads/master
625 * at the remote site.
627 if (namelen != patlen &&
628 patlen != namelen - 5 &&
629 prefixcmp(name, "refs/heads/") &&
630 prefixcmp(name, "refs/tags/")) {
631 /* We want to catch the case where only weak
632 * matches are found and there are multiple
633 * matches, and where more than one strong
634 * matches are found, as ambiguous. One
635 * strong match with zero or more weak matches
636 * are acceptable as a unique match.
647 *matched_ref = matched_weak;
651 *matched_ref = matched;
656 static void tail_link_ref(struct ref *ref, struct ref ***tail)
664 static struct ref *try_explicit_object_name(const char *name)
666 unsigned char sha1[20];
672 strcpy(ref->name, "(delete)");
673 hashclr(ref->new_sha1);
676 if (get_sha1(name, sha1))
678 len = strlen(name) + 1;
679 ref = alloc_ref(len);
680 memcpy(ref->name, name, len);
681 hashcpy(ref->new_sha1, sha1);
685 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
690 len = strlen(name) + 1;
691 ret = alloc_ref(len);
692 memcpy(ret->name, name, len);
693 tail_link_ref(ret, tail);
697 static int match_explicit(struct ref *src, struct ref *dst,
698 struct ref ***dst_tail,
702 struct ref *matched_src, *matched_dst;
704 const char *dst_value = rs->dst;
709 matched_src = matched_dst = NULL;
710 switch (count_refspec_match(rs->src, src, &matched_src)) {
714 /* The source could be in the get_sha1() format
715 * not a reference name. :refs/other is a
716 * way to delete 'other' ref at the remote end.
718 matched_src = try_explicit_object_name(rs->src);
720 error("src refspec %s does not match any.", rs->src);
724 error("src refspec %s matches more than one.", rs->src);
734 dst_value = matched_src->name;
737 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
741 if (!memcmp(dst_value, "refs/", 5))
742 matched_dst = make_linked_ref(dst_value, dst_tail);
744 error("dst refspec %s does not match any "
745 "existing ref on the remote and does "
746 "not start with refs/.", dst_value);
750 error("dst refspec %s matches more than one.",
754 if (errs || !matched_dst)
756 if (matched_dst->peer_ref) {
758 error("dst ref %s receives from more than one src.",
762 matched_dst->peer_ref = matched_src;
763 matched_dst->force = rs->force;
768 static int match_explicit_refs(struct ref *src, struct ref *dst,
769 struct ref ***dst_tail, struct refspec *rs,
773 for (i = errs = 0; i < rs_nr; i++)
774 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
778 static const struct refspec *check_pattern_match(const struct refspec *rs,
780 const struct ref *src)
783 for (i = 0; i < rs_nr; i++) {
785 !prefixcmp(src->name, rs[i].src) &&
786 src->name[strlen(rs[i].src)] == '/')
793 * Note. This is used only by "push"; refspec matching rules for
794 * push and fetch are subtly different, so do not try to reuse it
797 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
798 int nr_refspec, const char **refspec, int flags)
801 parse_push_refspec(nr_refspec, (const char **) refspec);
802 int send_all = flags & MATCH_REFS_ALL;
803 int send_mirror = flags & MATCH_REFS_MIRROR;
805 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
808 /* pick the remainder */
809 for ( ; src; src = src->next) {
810 struct ref *dst_peer;
811 const struct refspec *pat = NULL;
816 pat = check_pattern_match(rs, nr_refspec, src);
820 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
822 * "matching refs"; traditionally we pushed everything
823 * including refs outside refs/heads/ hierarchy, but
824 * that does not make much sense these days.
829 const char *dst_side = pat->dst ? pat->dst : pat->src;
830 dst_name = xmalloc(strlen(dst_side) +
832 strlen(pat->src) + 2);
833 strcpy(dst_name, dst_side);
834 strcat(dst_name, src->name + strlen(pat->src));
836 dst_name = xstrdup(src->name);
837 dst_peer = find_ref_by_name(dst, dst_name);
838 if (dst_peer && dst_peer->peer_ref)
839 /* We're already sending something to this ref. */
842 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
844 * Remote doesn't have it, and we have no
845 * explicit pattern, and we don't have
846 * --all nor --mirror.
850 /* Create a new one and link it */
851 dst_peer = make_linked_ref(dst_name, dst_tail);
852 hashcpy(dst_peer->new_sha1, src->new_sha1);
854 dst_peer->peer_ref = src;
856 dst_peer->force = pat->force;
863 struct branch *branch_get(const char *name)
868 if (!name || !*name || !strcmp(name, "HEAD"))
869 ret = current_branch;
871 ret = make_branch(name, 0);
872 if (ret && ret->remote_name) {
873 ret->remote = remote_get(ret->remote_name);
876 ret->merge = xcalloc(sizeof(*ret->merge),
878 for (i = 0; i < ret->merge_nr; i++) {
879 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
880 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
881 remote_find_tracking(ret->remote,
889 int branch_has_merge_config(struct branch *branch)
891 return branch && !!branch->merge;
894 int branch_merge_matches(struct branch *branch,
898 if (!branch || i < 0 || i >= branch->merge_nr)
900 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
903 static struct ref *get_expanded_map(const struct ref *remote_refs,
904 const struct refspec *refspec)
906 const struct ref *ref;
907 struct ref *ret = NULL;
908 struct ref **tail = &ret;
910 int remote_prefix_len = strlen(refspec->src);
911 int local_prefix_len = strlen(refspec->dst);
913 for (ref = remote_refs; ref; ref = ref->next) {
914 if (strchr(ref->name, '^'))
915 continue; /* a dereference item */
916 if (!prefixcmp(ref->name, refspec->src)) {
918 struct ref *cpy = copy_ref(ref);
919 match = ref->name + remote_prefix_len;
921 cpy->peer_ref = alloc_ref(local_prefix_len +
923 sprintf(cpy->peer_ref->name, "%s%s",
924 refspec->dst, match);
926 cpy->peer_ref->force = 1;
935 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
937 const struct ref *ref;
938 for (ref = refs; ref; ref = ref->next) {
939 if (refname_match(name, ref->name, ref_fetch_rules))
945 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
947 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
952 return copy_ref(ref);
955 static struct ref *get_local_ref(const char *name)
961 if (!prefixcmp(name, "refs/")) {
962 ret = alloc_ref(strlen(name) + 1);
963 strcpy(ret->name, name);
967 if (!prefixcmp(name, "heads/") ||
968 !prefixcmp(name, "tags/") ||
969 !prefixcmp(name, "remotes/")) {
970 ret = alloc_ref(strlen(name) + 6);
971 sprintf(ret->name, "refs/%s", name);
975 ret = alloc_ref(strlen(name) + 12);
976 sprintf(ret->name, "refs/heads/%s", name);
980 int get_fetch_map(const struct ref *remote_refs,
981 const struct refspec *refspec,
985 struct ref *ref_map, **rmp;
987 if (refspec->pattern) {
988 ref_map = get_expanded_map(remote_refs, refspec);
990 const char *name = refspec->src[0] ? refspec->src : "HEAD";
992 ref_map = get_remote_ref(remote_refs, name);
993 if (!missing_ok && !ref_map)
994 die("Couldn't find remote ref %s", name);
996 ref_map->peer_ref = get_local_ref(refspec->dst);
997 if (ref_map->peer_ref && refspec->force)
998 ref_map->peer_ref->force = 1;
1002 for (rmp = &ref_map; *rmp; ) {
1003 if ((*rmp)->peer_ref) {
1004 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1005 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1006 struct ref *ignore = *rmp;
1007 error("* Ignoring funny ref '%s' locally",
1008 (*rmp)->peer_ref->name);
1009 *rmp = (*rmp)->next;
1010 free(ignore->peer_ref);
1015 rmp = &((*rmp)->next);
1019 tail_link_ref(ref_map, tail);