5 struct counted_string {
12 struct counted_string *instead_of;
17 static struct remote **remotes;
18 static int remotes_alloc;
19 static int remotes_nr;
21 static struct branch **branches;
22 static int branches_alloc;
23 static int branches_nr;
25 static struct branch *current_branch;
26 static const char *default_remote_name;
28 static struct rewrite **rewrite;
29 static int rewrite_alloc;
30 static int rewrite_nr;
32 #define BUF_SIZE (2048)
33 static char buffer[BUF_SIZE];
35 static const char *alias_url(const char *url)
39 struct counted_string *longest;
44 for (i = 0; i < rewrite_nr; i++) {
47 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
48 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
50 longest->len < rewrite[i]->instead_of[j].len)) {
51 longest = &(rewrite[i]->instead_of[j]);
59 ret = malloc(rewrite[longest_i]->baselen +
60 (strlen(url) - longest->len) + 1);
61 strcpy(ret, rewrite[longest_i]->base);
62 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
66 static void add_push_refspec(struct remote *remote, const char *ref)
68 ALLOC_GROW(remote->push_refspec,
69 remote->push_refspec_nr + 1,
70 remote->push_refspec_alloc);
71 remote->push_refspec[remote->push_refspec_nr++] = ref;
74 static void add_fetch_refspec(struct remote *remote, const char *ref)
76 ALLOC_GROW(remote->fetch_refspec,
77 remote->fetch_refspec_nr + 1,
78 remote->fetch_refspec_alloc);
79 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
82 static void add_url(struct remote *remote, const char *url)
84 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
85 remote->url[remote->url_nr++] = url;
88 static void add_url_alias(struct remote *remote, const char *url)
90 add_url(remote, alias_url(url));
93 static struct remote *make_remote(const char *name, int len)
98 for (i = 0; i < remotes_nr; i++) {
99 if (len ? (!strncmp(name, remotes[i]->name, len) &&
100 !remotes[i]->name[len]) :
101 !strcmp(name, remotes[i]->name))
105 ret = xcalloc(1, sizeof(struct remote));
106 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
107 remotes[remotes_nr++] = ret;
109 ret->name = xstrndup(name, len);
111 ret->name = xstrdup(name);
115 static void add_merge(struct branch *branch, const char *name)
117 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
118 branch->merge_alloc);
119 branch->merge_name[branch->merge_nr++] = name;
122 static struct branch *make_branch(const char *name, int len)
128 for (i = 0; i < branches_nr; i++) {
129 if (len ? (!strncmp(name, branches[i]->name, len) &&
130 !branches[i]->name[len]) :
131 !strcmp(name, branches[i]->name))
135 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
136 ret = xcalloc(1, sizeof(struct branch));
137 branches[branches_nr++] = ret;
139 ret->name = xstrndup(name, len);
141 ret->name = xstrdup(name);
142 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
143 strcpy(refname, "refs/heads/");
144 strcpy(refname + strlen("refs/heads/"), ret->name);
145 ret->refname = refname;
150 static struct rewrite *make_rewrite(const char *base, int len)
155 for (i = 0; i < rewrite_nr; i++) {
157 ? (len == rewrite[i]->baselen &&
158 !strncmp(base, rewrite[i]->base, len))
159 : !strcmp(base, rewrite[i]->base))
163 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
164 ret = xcalloc(1, sizeof(struct rewrite));
165 rewrite[rewrite_nr++] = ret;
167 ret->base = xstrndup(base, len);
171 ret->base = xstrdup(base);
172 ret->baselen = strlen(base);
177 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
179 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
180 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
181 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
182 rewrite->instead_of_nr++;
185 static void read_remotes_file(struct remote *remote)
187 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
191 while (fgets(buffer, BUF_SIZE, f)) {
195 if (!prefixcmp(buffer, "URL:")) {
198 } else if (!prefixcmp(buffer, "Push:")) {
201 } else if (!prefixcmp(buffer, "Pull:")) {
213 while (isspace(p[-1]))
216 switch (value_list) {
218 add_url_alias(remote, xstrdup(s));
221 add_push_refspec(remote, xstrdup(s));
224 add_fetch_refspec(remote, xstrdup(s));
231 static void read_branches_file(struct remote *remote)
233 const char *slash = strchr(remote->name, '/');
236 int n = slash ? slash - remote->name : 1000;
237 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
243 s = fgets(buffer, BUF_SIZE, f);
252 while (isspace(p[-1]))
256 len += strlen(slash);
257 p = xmalloc(len + 1);
261 frag = strchr(p, '#');
264 branch = xmalloc(strlen(frag) + 12);
265 strcpy(branch, "refs/heads/");
266 strcat(branch, frag);
268 branch = "refs/heads/master";
270 add_url_alias(remote, p);
271 add_fetch_refspec(remote, branch);
272 remote->fetch_tags = 1; /* always auto-follow */
275 static int handle_config(const char *key, const char *value)
279 struct remote *remote;
280 struct branch *branch;
281 if (!prefixcmp(key, "branch.")) {
283 subkey = strrchr(name, '.');
286 branch = make_branch(name, subkey - name);
287 if (!strcmp(subkey, ".remote")) {
289 return config_error_nonbool(key);
290 branch->remote_name = xstrdup(value);
291 if (branch == current_branch)
292 default_remote_name = branch->remote_name;
293 } else if (!strcmp(subkey, ".merge")) {
295 return config_error_nonbool(key);
296 add_merge(branch, xstrdup(value));
300 if (!prefixcmp(key, "url.")) {
301 struct rewrite *rewrite;
303 subkey = strrchr(name, '.');
306 rewrite = make_rewrite(name, subkey - name);
307 if (!strcmp(subkey, ".insteadof")) {
309 return config_error_nonbool(key);
310 add_instead_of(rewrite, xstrdup(value));
313 if (prefixcmp(key, "remote."))
316 subkey = strrchr(name, '.');
318 return error("Config with no key for remote %s", name);
319 if (*subkey == '/') {
320 warning("Config remote shorthand cannot begin with '/': %s", name);
323 remote = make_remote(name, subkey - name);
325 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
328 * is a valid way to set it to true; we get NULL in value so
329 * we need to handle it here.
331 * if (!strcmp(subkey, ".disabled")) {
332 * val = git_config_bool(key, value);
337 return 0; /* ignore unknown booleans */
339 if (!strcmp(subkey, ".url")) {
340 add_url(remote, xstrdup(value));
341 } else if (!strcmp(subkey, ".push")) {
342 add_push_refspec(remote, xstrdup(value));
343 } else if (!strcmp(subkey, ".fetch")) {
344 add_fetch_refspec(remote, xstrdup(value));
345 } else if (!strcmp(subkey, ".receivepack")) {
346 if (!remote->receivepack)
347 remote->receivepack = xstrdup(value);
349 error("more than one receivepack given, using the first");
350 } else if (!strcmp(subkey, ".uploadpack")) {
351 if (!remote->uploadpack)
352 remote->uploadpack = xstrdup(value);
354 error("more than one uploadpack given, using the first");
355 } else if (!strcmp(subkey, ".tagopt")) {
356 if (!strcmp(value, "--no-tags"))
357 remote->fetch_tags = -1;
358 } else if (!strcmp(subkey, ".proxy")) {
359 remote->http_proxy = xstrdup(value);
360 } else if (!strcmp(subkey, ".skipdefaultupdate"))
361 remote->skip_default_update = 1;
365 static void alias_all_urls(void)
368 for (i = 0; i < remotes_nr; i++) {
371 for (j = 0; j < remotes[i]->url_nr; j++) {
372 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
377 static void read_config(void)
379 unsigned char sha1[20];
380 const char *head_ref;
382 if (default_remote_name) // did this already
384 default_remote_name = xstrdup("origin");
385 current_branch = NULL;
386 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
387 if (head_ref && (flag & REF_ISSYMREF) &&
388 !prefixcmp(head_ref, "refs/heads/")) {
390 make_branch(head_ref + strlen("refs/heads/"), 0);
392 git_config(handle_config);
396 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
399 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
400 for (i = 0; i < nr_refspec; i++) {
401 const char *sp, *ep, *gp;
407 gp = strchr(sp, '*');
408 ep = strchr(sp, ':');
409 if (gp && ep && gp > ep)
413 const char *glob = strchr(ep + 1, '*');
417 rs[i].dst = xstrndup(ep + 1,
420 rs[i].dst = xstrdup(ep + 1);
423 ep = sp + strlen(sp);
429 rs[i].src = xstrndup(sp, ep - sp);
434 static int valid_remote_nick(const char *name)
436 if (!name[0] || /* not empty */
437 (name[0] == '.' && /* not "." */
438 (!name[1] || /* not ".." */
439 (name[1] == '.' && !name[2]))))
441 return !strchr(name, '/'); /* no slash */
444 struct remote *remote_get(const char *name)
450 name = default_remote_name;
451 ret = make_remote(name, 0);
452 if (valid_remote_nick(name)) {
454 read_remotes_file(ret);
456 read_branches_file(ret);
459 add_url_alias(ret, name);
462 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
463 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
467 int for_each_remote(each_remote_fn fn, void *priv)
471 for (i = 0; i < remotes_nr && !result; i++) {
472 struct remote *r = remotes[i];
476 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
479 r->push = parse_ref_spec(r->push_refspec_nr,
481 result = fn(r, priv);
486 void ref_remove_duplicates(struct ref *ref_map)
490 for (; ref_map; ref_map = ref_map->next) {
491 if (!ref_map->peer_ref)
493 posn = &ref_map->next;
495 if ((*posn)->peer_ref &&
496 !strcmp((*posn)->peer_ref->name,
497 ref_map->peer_ref->name)) {
498 if (strcmp((*posn)->name, ref_map->name))
499 die("%s tracks both %s and %s",
500 ref_map->peer_ref->name,
501 (*posn)->name, ref_map->name);
502 next = (*posn)->next;
503 free((*posn)->peer_ref);
507 posn = &(*posn)->next;
513 int remote_has_url(struct remote *remote, const char *url)
516 for (i = 0; i < remote->url_nr; i++) {
517 if (!strcmp(remote->url[i], url))
523 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
525 int find_src = refspec->src == NULL;
526 char *needle, **result;
531 return error("find_tracking: need either src or dst");
532 needle = refspec->dst;
533 result = &refspec->src;
535 needle = refspec->src;
536 result = &refspec->dst;
539 for (i = 0; i < remote->fetch_refspec_nr; i++) {
540 struct refspec *fetch = &remote->fetch[i];
541 const char *key = find_src ? fetch->dst : fetch->src;
542 const char *value = find_src ? fetch->src : fetch->dst;
545 if (fetch->pattern) {
546 if (!prefixcmp(needle, key)) {
547 *result = xmalloc(strlen(value) +
550 strcpy(*result, value);
551 strcpy(*result + strlen(value),
552 needle + strlen(key));
553 refspec->force = fetch->force;
556 } else if (!strcmp(needle, key)) {
557 *result = xstrdup(value);
558 refspec->force = fetch->force;
565 struct ref *alloc_ref(unsigned namelen)
567 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
568 memset(ret, 0, sizeof(struct ref) + namelen);
572 static struct ref *copy_ref(const struct ref *ref)
574 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
575 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
580 struct ref *copy_ref_list(const struct ref *ref)
582 struct ref *ret = NULL;
583 struct ref **tail = &ret;
585 *tail = copy_ref(ref);
587 tail = &((*tail)->next);
592 void free_refs(struct ref *ref)
603 static int count_refspec_match(const char *pattern,
605 struct ref **matched_ref)
607 int patlen = strlen(pattern);
608 struct ref *matched_weak = NULL;
609 struct ref *matched = NULL;
613 for (weak_match = match = 0; refs; refs = refs->next) {
614 char *name = refs->name;
615 int namelen = strlen(name);
617 if (!refname_match(pattern, name, ref_rev_parse_rules))
620 /* A match is "weak" if it is with refs outside
621 * heads or tags, and did not specify the pattern
622 * in full (e.g. "refs/remotes/origin/master") or at
623 * least from the toplevel (e.g. "remotes/origin/master");
624 * otherwise "git push $URL master" would result in
625 * ambiguity between remotes/origin/master and heads/master
626 * at the remote site.
628 if (namelen != patlen &&
629 patlen != namelen - 5 &&
630 prefixcmp(name, "refs/heads/") &&
631 prefixcmp(name, "refs/tags/")) {
632 /* We want to catch the case where only weak
633 * matches are found and there are multiple
634 * matches, and where more than one strong
635 * matches are found, as ambiguous. One
636 * strong match with zero or more weak matches
637 * are acceptable as a unique match.
648 *matched_ref = matched_weak;
652 *matched_ref = matched;
657 static void tail_link_ref(struct ref *ref, struct ref ***tail)
665 static struct ref *try_explicit_object_name(const char *name)
667 unsigned char sha1[20];
673 strcpy(ref->name, "(delete)");
674 hashclr(ref->new_sha1);
677 if (get_sha1(name, sha1))
679 len = strlen(name) + 1;
680 ref = alloc_ref(len);
681 memcpy(ref->name, name, len);
682 hashcpy(ref->new_sha1, sha1);
686 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
691 len = strlen(name) + 1;
692 ret = alloc_ref(len);
693 memcpy(ret->name, name, len);
694 tail_link_ref(ret, tail);
698 static int match_explicit(struct ref *src, struct ref *dst,
699 struct ref ***dst_tail,
703 struct ref *matched_src, *matched_dst;
705 const char *dst_value = rs->dst;
710 matched_src = matched_dst = NULL;
711 switch (count_refspec_match(rs->src, src, &matched_src)) {
715 /* The source could be in the get_sha1() format
716 * not a reference name. :refs/other is a
717 * way to delete 'other' ref at the remote end.
719 matched_src = try_explicit_object_name(rs->src);
721 error("src refspec %s does not match any.", rs->src);
725 error("src refspec %s matches more than one.", rs->src);
733 unsigned char sha1[20];
738 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
740 ((flag & REF_ISSYMREF) &&
741 prefixcmp(dst_value, "refs/heads/")))
742 die("%s cannot be resolved to branch.",
746 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
750 if (!memcmp(dst_value, "refs/", 5))
751 matched_dst = make_linked_ref(dst_value, dst_tail);
753 error("dst refspec %s does not match any "
754 "existing ref on the remote and does "
755 "not start with refs/.", dst_value);
759 error("dst refspec %s matches more than one.",
763 if (errs || !matched_dst)
765 if (matched_dst->peer_ref) {
767 error("dst ref %s receives from more than one src.",
771 matched_dst->peer_ref = matched_src;
772 matched_dst->force = rs->force;
777 static int match_explicit_refs(struct ref *src, struct ref *dst,
778 struct ref ***dst_tail, struct refspec *rs,
782 for (i = errs = 0; i < rs_nr; i++)
783 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
787 static const struct refspec *check_pattern_match(const struct refspec *rs,
789 const struct ref *src)
792 for (i = 0; i < rs_nr; i++) {
793 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
800 * Note. This is used only by "push"; refspec matching rules for
801 * push and fetch are subtly different, so do not try to reuse it
804 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
805 int nr_refspec, const char **refspec, int flags)
808 parse_ref_spec(nr_refspec, (const char **) refspec);
809 int send_all = flags & MATCH_REFS_ALL;
810 int send_mirror = flags & MATCH_REFS_MIRROR;
812 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
815 /* pick the remainder */
816 for ( ; src; src = src->next) {
817 struct ref *dst_peer;
818 const struct refspec *pat = NULL;
823 pat = check_pattern_match(rs, nr_refspec, src);
827 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
829 * "matching refs"; traditionally we pushed everything
830 * including refs outside refs/heads/ hierarchy, but
831 * that does not make much sense these days.
836 const char *dst_side = pat->dst ? pat->dst : pat->src;
837 dst_name = xmalloc(strlen(dst_side) +
839 strlen(pat->src) + 2);
840 strcpy(dst_name, dst_side);
841 strcat(dst_name, src->name + strlen(pat->src));
843 dst_name = xstrdup(src->name);
844 dst_peer = find_ref_by_name(dst, dst_name);
845 if (dst_peer && dst_peer->peer_ref)
846 /* We're already sending something to this ref. */
849 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
851 * Remote doesn't have it, and we have no
852 * explicit pattern, and we don't have
853 * --all nor --mirror.
857 /* Create a new one and link it */
858 dst_peer = make_linked_ref(dst_name, dst_tail);
859 hashcpy(dst_peer->new_sha1, src->new_sha1);
861 dst_peer->peer_ref = src;
863 dst_peer->force = pat->force;
870 struct branch *branch_get(const char *name)
875 if (!name || !*name || !strcmp(name, "HEAD"))
876 ret = current_branch;
878 ret = make_branch(name, 0);
879 if (ret && ret->remote_name) {
880 ret->remote = remote_get(ret->remote_name);
883 ret->merge = xcalloc(sizeof(*ret->merge),
885 for (i = 0; i < ret->merge_nr; i++) {
886 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
887 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
888 remote_find_tracking(ret->remote,
896 int branch_has_merge_config(struct branch *branch)
898 return branch && !!branch->merge;
901 int branch_merge_matches(struct branch *branch,
905 if (!branch || i < 0 || i >= branch->merge_nr)
907 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
910 static struct ref *get_expanded_map(const struct ref *remote_refs,
911 const struct refspec *refspec)
913 const struct ref *ref;
914 struct ref *ret = NULL;
915 struct ref **tail = &ret;
917 int remote_prefix_len = strlen(refspec->src);
918 int local_prefix_len = strlen(refspec->dst);
920 for (ref = remote_refs; ref; ref = ref->next) {
921 if (strchr(ref->name, '^'))
922 continue; /* a dereference item */
923 if (!prefixcmp(ref->name, refspec->src)) {
925 struct ref *cpy = copy_ref(ref);
926 match = ref->name + remote_prefix_len;
928 cpy->peer_ref = alloc_ref(local_prefix_len +
930 sprintf(cpy->peer_ref->name, "%s%s",
931 refspec->dst, match);
933 cpy->peer_ref->force = 1;
942 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
944 const struct ref *ref;
945 for (ref = refs; ref; ref = ref->next) {
946 if (refname_match(name, ref->name, ref_fetch_rules))
952 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
954 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
959 return copy_ref(ref);
962 static struct ref *get_local_ref(const char *name)
968 if (!prefixcmp(name, "refs/")) {
969 ret = alloc_ref(strlen(name) + 1);
970 strcpy(ret->name, name);
974 if (!prefixcmp(name, "heads/") ||
975 !prefixcmp(name, "tags/") ||
976 !prefixcmp(name, "remotes/")) {
977 ret = alloc_ref(strlen(name) + 6);
978 sprintf(ret->name, "refs/%s", name);
982 ret = alloc_ref(strlen(name) + 12);
983 sprintf(ret->name, "refs/heads/%s", name);
987 int get_fetch_map(const struct ref *remote_refs,
988 const struct refspec *refspec,
992 struct ref *ref_map, *rm;
994 if (refspec->pattern) {
995 ref_map = get_expanded_map(remote_refs, refspec);
997 const char *name = refspec->src[0] ? refspec->src : "HEAD";
999 ref_map = get_remote_ref(remote_refs, name);
1000 if (!missing_ok && !ref_map)
1001 die("Couldn't find remote ref %s", name);
1003 ref_map->peer_ref = get_local_ref(refspec->dst);
1004 if (ref_map->peer_ref && refspec->force)
1005 ref_map->peer_ref->force = 1;
1009 for (rm = ref_map; rm; rm = rm->next) {
1010 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
1011 die("* refusing to create funny ref '%s' locally",
1012 rm->peer_ref->name);
1016 tail_link_ref(ref_map, tail);