5 static struct remote **remotes;
6 static int remotes_alloc;
9 static struct branch **branches;
10 static int branches_alloc;
11 static int branches_nr;
13 static struct branch *current_branch;
14 static const char *default_remote_name;
16 #define BUF_SIZE (2048)
17 static char buffer[BUF_SIZE];
19 static void add_push_refspec(struct remote *remote, const char *ref)
21 ALLOC_GROW(remote->push_refspec,
22 remote->push_refspec_nr + 1,
23 remote->push_refspec_alloc);
24 remote->push_refspec[remote->push_refspec_nr++] = ref;
27 static void add_fetch_refspec(struct remote *remote, const char *ref)
29 ALLOC_GROW(remote->fetch_refspec,
30 remote->fetch_refspec_nr + 1,
31 remote->fetch_refspec_alloc);
32 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
35 static void add_url(struct remote *remote, const char *url)
37 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
38 remote->url[remote->url_nr++] = url;
41 static struct remote *make_remote(const char *name, int len)
46 for (i = 0; i < remotes_nr; i++) {
47 if (len ? (!strncmp(name, remotes[i]->name, len) &&
48 !remotes[i]->name[len]) :
49 !strcmp(name, remotes[i]->name))
53 ret = xcalloc(1, sizeof(struct remote));
54 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
55 remotes[remotes_nr++] = ret;
57 ret->name = xstrndup(name, len);
59 ret->name = xstrdup(name);
63 static void add_merge(struct branch *branch, const char *name)
65 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
67 branch->merge_name[branch->merge_nr++] = name;
70 static struct branch *make_branch(const char *name, int len)
76 for (i = 0; i < branches_nr; i++) {
77 if (len ? (!strncmp(name, branches[i]->name, len) &&
78 !branches[i]->name[len]) :
79 !strcmp(name, branches[i]->name))
83 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
84 ret = xcalloc(1, sizeof(struct branch));
85 branches[branches_nr++] = ret;
87 ret->name = xstrndup(name, len);
89 ret->name = xstrdup(name);
90 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
91 strcpy(refname, "refs/heads/");
92 strcpy(refname + strlen("refs/heads/"), ret->name);
93 ret->refname = refname;
98 static void read_remotes_file(struct remote *remote)
100 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
104 while (fgets(buffer, BUF_SIZE, f)) {
108 if (!prefixcmp(buffer, "URL:")) {
111 } else if (!prefixcmp(buffer, "Push:")) {
114 } else if (!prefixcmp(buffer, "Pull:")) {
126 while (isspace(p[-1]))
129 switch (value_list) {
131 add_url(remote, xstrdup(s));
134 add_push_refspec(remote, xstrdup(s));
137 add_fetch_refspec(remote, xstrdup(s));
144 static void read_branches_file(struct remote *remote)
146 const char *slash = strchr(remote->name, '/');
149 int n = slash ? slash - remote->name : 1000;
150 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
156 s = fgets(buffer, BUF_SIZE, f);
165 while (isspace(p[-1]))
169 len += strlen(slash);
170 p = xmalloc(len + 1);
174 frag = strchr(p, '#');
177 branch = xmalloc(strlen(frag) + 12);
178 strcpy(branch, "refs/heads/");
179 strcat(branch, frag);
181 branch = "refs/heads/master";
184 add_fetch_refspec(remote, branch);
185 remote->fetch_tags = 1; /* always auto-follow */
188 static int handle_config(const char *key, const char *value)
192 struct remote *remote;
193 struct branch *branch;
194 if (!prefixcmp(key, "branch.")) {
196 subkey = strrchr(name, '.');
199 branch = make_branch(name, subkey - name);
200 if (!strcmp(subkey, ".remote")) {
202 return config_error_nonbool(key);
203 branch->remote_name = xstrdup(value);
204 if (branch == current_branch)
205 default_remote_name = branch->remote_name;
206 } else if (!strcmp(subkey, ".merge")) {
208 return config_error_nonbool(key);
209 add_merge(branch, xstrdup(value));
213 if (prefixcmp(key, "remote."))
216 subkey = strrchr(name, '.');
218 return error("Config with no key for remote %s", name);
219 if (*subkey == '/') {
220 warning("Config remote shorthand cannot begin with '/': %s", name);
223 remote = make_remote(name, subkey - name);
225 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
228 * is a valid way to set it to true; we get NULL in value so
229 * we need to handle it here.
231 * if (!strcmp(subkey, ".disabled")) {
232 * val = git_config_bool(key, value);
237 return 0; /* ignore unknown booleans */
239 if (!strcmp(subkey, ".url")) {
240 add_url(remote, xstrdup(value));
241 } else if (!strcmp(subkey, ".push")) {
242 add_push_refspec(remote, xstrdup(value));
243 } else if (!strcmp(subkey, ".fetch")) {
244 add_fetch_refspec(remote, xstrdup(value));
245 } else if (!strcmp(subkey, ".receivepack")) {
246 if (!remote->receivepack)
247 remote->receivepack = xstrdup(value);
249 error("more than one receivepack given, using the first");
250 } else if (!strcmp(subkey, ".uploadpack")) {
251 if (!remote->uploadpack)
252 remote->uploadpack = xstrdup(value);
254 error("more than one uploadpack given, using the first");
255 } else if (!strcmp(subkey, ".tagopt")) {
256 if (!strcmp(value, "--no-tags"))
257 remote->fetch_tags = -1;
258 } else if (!strcmp(subkey, ".proxy")) {
259 remote->http_proxy = xstrdup(value);
264 static void read_config(void)
266 unsigned char sha1[20];
267 const char *head_ref;
269 if (default_remote_name) // did this already
271 default_remote_name = xstrdup("origin");
272 current_branch = NULL;
273 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
274 if (head_ref && (flag & REF_ISSYMREF) &&
275 !prefixcmp(head_ref, "refs/heads/")) {
277 make_branch(head_ref + strlen("refs/heads/"), 0);
279 git_config(handle_config);
282 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
285 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
286 for (i = 0; i < nr_refspec; i++) {
287 const char *sp, *ep, *gp;
293 gp = strchr(sp, '*');
294 ep = strchr(sp, ':');
295 if (gp && ep && gp > ep)
299 const char *glob = strchr(ep + 1, '*');
303 rs[i].dst = xstrndup(ep + 1,
306 rs[i].dst = xstrdup(ep + 1);
309 ep = sp + strlen(sp);
315 rs[i].src = xstrndup(sp, ep - sp);
320 static int valid_remote_nick(const char *name)
322 if (!name[0] || /* not empty */
323 (name[0] == '.' && /* not "." */
324 (!name[1] || /* not ".." */
325 (name[1] == '.' && !name[2]))))
327 return !strchr(name, '/'); /* no slash */
330 struct remote *remote_get(const char *name)
336 name = default_remote_name;
337 ret = make_remote(name, 0);
338 if (valid_remote_nick(name)) {
340 read_remotes_file(ret);
342 read_branches_file(ret);
348 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
349 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
353 int for_each_remote(each_remote_fn fn, void *priv)
357 for (i = 0; i < remotes_nr && !result; i++) {
358 struct remote *r = remotes[i];
362 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
365 r->push = parse_ref_spec(r->push_refspec_nr,
367 result = fn(r, priv);
372 void ref_remove_duplicates(struct ref *ref_map)
376 for (; ref_map; ref_map = ref_map->next) {
377 if (!ref_map->peer_ref)
379 posn = &ref_map->next;
381 if ((*posn)->peer_ref &&
382 !strcmp((*posn)->peer_ref->name,
383 ref_map->peer_ref->name)) {
384 if (strcmp((*posn)->name, ref_map->name))
385 die("%s tracks both %s and %s",
386 ref_map->peer_ref->name,
387 (*posn)->name, ref_map->name);
388 next = (*posn)->next;
389 free((*posn)->peer_ref);
393 posn = &(*posn)->next;
399 int remote_has_url(struct remote *remote, const char *url)
402 for (i = 0; i < remote->url_nr; i++) {
403 if (!strcmp(remote->url[i], url))
409 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
411 int find_src = refspec->src == NULL;
412 char *needle, **result;
417 return error("find_tracking: need either src or dst");
418 needle = refspec->dst;
419 result = &refspec->src;
421 needle = refspec->src;
422 result = &refspec->dst;
425 for (i = 0; i < remote->fetch_refspec_nr; i++) {
426 struct refspec *fetch = &remote->fetch[i];
427 const char *key = find_src ? fetch->dst : fetch->src;
428 const char *value = find_src ? fetch->src : fetch->dst;
431 if (fetch->pattern) {
432 if (!prefixcmp(needle, key)) {
433 *result = xmalloc(strlen(value) +
436 strcpy(*result, value);
437 strcpy(*result + strlen(value),
438 needle + strlen(key));
439 refspec->force = fetch->force;
442 } else if (!strcmp(needle, key)) {
443 *result = xstrdup(value);
444 refspec->force = fetch->force;
451 struct ref *alloc_ref(unsigned namelen)
453 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
454 memset(ret, 0, sizeof(struct ref) + namelen);
458 static struct ref *copy_ref(const struct ref *ref)
460 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
461 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
466 struct ref *copy_ref_list(const struct ref *ref)
468 struct ref *ret = NULL;
469 struct ref **tail = &ret;
471 *tail = copy_ref(ref);
473 tail = &((*tail)->next);
478 void free_refs(struct ref *ref)
490 static int count_refspec_match(const char *pattern,
492 struct ref **matched_ref)
494 int patlen = strlen(pattern);
495 struct ref *matched_weak = NULL;
496 struct ref *matched = NULL;
500 for (weak_match = match = 0; refs; refs = refs->next) {
501 char *name = refs->name;
502 int namelen = strlen(name);
504 if (!refname_match(pattern, name, ref_rev_parse_rules))
507 /* A match is "weak" if it is with refs outside
508 * heads or tags, and did not specify the pattern
509 * in full (e.g. "refs/remotes/origin/master") or at
510 * least from the toplevel (e.g. "remotes/origin/master");
511 * otherwise "git push $URL master" would result in
512 * ambiguity between remotes/origin/master and heads/master
513 * at the remote site.
515 if (namelen != patlen &&
516 patlen != namelen - 5 &&
517 prefixcmp(name, "refs/heads/") &&
518 prefixcmp(name, "refs/tags/")) {
519 /* We want to catch the case where only weak
520 * matches are found and there are multiple
521 * matches, and where more than one strong
522 * matches are found, as ambiguous. One
523 * strong match with zero or more weak matches
524 * are acceptable as a unique match.
535 *matched_ref = matched_weak;
539 *matched_ref = matched;
544 static void tail_link_ref(struct ref *ref, struct ref ***tail)
552 static struct ref *try_explicit_object_name(const char *name)
554 unsigned char sha1[20];
560 strcpy(ref->name, "(delete)");
561 hashclr(ref->new_sha1);
564 if (get_sha1(name, sha1))
566 len = strlen(name) + 1;
567 ref = alloc_ref(len);
568 memcpy(ref->name, name, len);
569 hashcpy(ref->new_sha1, sha1);
573 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
578 len = strlen(name) + 1;
579 ret = alloc_ref(len);
580 memcpy(ret->name, name, len);
581 tail_link_ref(ret, tail);
585 static int match_explicit(struct ref *src, struct ref *dst,
586 struct ref ***dst_tail,
590 struct ref *matched_src, *matched_dst;
592 const char *dst_value = rs->dst;
597 matched_src = matched_dst = NULL;
598 switch (count_refspec_match(rs->src, src, &matched_src)) {
602 /* The source could be in the get_sha1() format
603 * not a reference name. :refs/other is a
604 * way to delete 'other' ref at the remote end.
606 matched_src = try_explicit_object_name(rs->src);
608 error("src refspec %s does not match any.", rs->src);
612 error("src refspec %s matches more than one.", rs->src);
622 dst_value = matched_src->name;
625 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
629 if (!memcmp(dst_value, "refs/", 5))
630 matched_dst = make_linked_ref(dst_value, dst_tail);
632 error("dst refspec %s does not match any "
633 "existing ref on the remote and does "
634 "not start with refs/.", dst_value);
638 error("dst refspec %s matches more than one.",
642 if (errs || !matched_dst)
644 if (matched_dst->peer_ref) {
646 error("dst ref %s receives from more than one src.",
650 matched_dst->peer_ref = matched_src;
651 matched_dst->force = rs->force;
656 static int match_explicit_refs(struct ref *src, struct ref *dst,
657 struct ref ***dst_tail, struct refspec *rs,
661 for (i = errs = 0; i < rs_nr; i++)
662 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
666 static const struct refspec *check_pattern_match(const struct refspec *rs,
668 const struct ref *src)
671 for (i = 0; i < rs_nr; i++) {
672 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
679 * Note. This is used only by "push"; refspec matching rules for
680 * push and fetch are subtly different, so do not try to reuse it
683 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
684 int nr_refspec, const char **refspec, int flags)
687 parse_ref_spec(nr_refspec, (const char **) refspec);
688 int send_all = flags & MATCH_REFS_ALL;
689 int send_mirror = flags & MATCH_REFS_MIRROR;
691 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
694 /* pick the remainder */
695 for ( ; src; src = src->next) {
696 struct ref *dst_peer;
697 const struct refspec *pat = NULL;
702 pat = check_pattern_match(rs, nr_refspec, src);
706 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
708 * "matching refs"; traditionally we pushed everything
709 * including refs outside refs/heads/ hierarchy, but
710 * that does not make much sense these days.
715 const char *dst_side = pat->dst ? pat->dst : pat->src;
716 dst_name = xmalloc(strlen(dst_side) +
718 strlen(pat->src) + 2);
719 strcpy(dst_name, dst_side);
720 strcat(dst_name, src->name + strlen(pat->src));
722 dst_name = xstrdup(src->name);
723 dst_peer = find_ref_by_name(dst, dst_name);
724 if (dst_peer && dst_peer->peer_ref)
725 /* We're already sending something to this ref. */
728 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
730 * Remote doesn't have it, and we have no
731 * explicit pattern, and we don't have
732 * --all nor --mirror.
736 /* Create a new one and link it */
737 dst_peer = make_linked_ref(dst_name, dst_tail);
738 hashcpy(dst_peer->new_sha1, src->new_sha1);
740 dst_peer->peer_ref = src;
742 dst_peer->force = pat->force;
749 struct branch *branch_get(const char *name)
754 if (!name || !*name || !strcmp(name, "HEAD"))
755 ret = current_branch;
757 ret = make_branch(name, 0);
758 if (ret && ret->remote_name) {
759 ret->remote = remote_get(ret->remote_name);
762 ret->merge = xcalloc(sizeof(*ret->merge),
764 for (i = 0; i < ret->merge_nr; i++) {
765 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
766 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
767 remote_find_tracking(ret->remote,
775 int branch_has_merge_config(struct branch *branch)
777 return branch && !!branch->merge;
780 int branch_merge_matches(struct branch *branch,
784 if (!branch || i < 0 || i >= branch->merge_nr)
786 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
789 static struct ref *get_expanded_map(const struct ref *remote_refs,
790 const struct refspec *refspec)
792 const struct ref *ref;
793 struct ref *ret = NULL;
794 struct ref **tail = &ret;
796 int remote_prefix_len = strlen(refspec->src);
797 int local_prefix_len = strlen(refspec->dst);
799 for (ref = remote_refs; ref; ref = ref->next) {
800 if (strchr(ref->name, '^'))
801 continue; /* a dereference item */
802 if (!prefixcmp(ref->name, refspec->src)) {
804 struct ref *cpy = copy_ref(ref);
805 match = ref->name + remote_prefix_len;
807 cpy->peer_ref = alloc_ref(local_prefix_len +
809 sprintf(cpy->peer_ref->name, "%s%s",
810 refspec->dst, match);
812 cpy->peer_ref->force = 1;
821 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
823 const struct ref *ref;
824 for (ref = refs; ref; ref = ref->next) {
825 if (refname_match(name, ref->name, ref_fetch_rules))
831 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
833 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
838 return copy_ref(ref);
841 static struct ref *get_local_ref(const char *name)
847 if (!prefixcmp(name, "refs/")) {
848 ret = alloc_ref(strlen(name) + 1);
849 strcpy(ret->name, name);
853 if (!prefixcmp(name, "heads/") ||
854 !prefixcmp(name, "tags/") ||
855 !prefixcmp(name, "remotes/")) {
856 ret = alloc_ref(strlen(name) + 6);
857 sprintf(ret->name, "refs/%s", name);
861 ret = alloc_ref(strlen(name) + 12);
862 sprintf(ret->name, "refs/heads/%s", name);
866 int get_fetch_map(const struct ref *remote_refs,
867 const struct refspec *refspec,
871 struct ref *ref_map, *rm;
873 if (refspec->pattern) {
874 ref_map = get_expanded_map(remote_refs, refspec);
876 const char *name = refspec->src[0] ? refspec->src : "HEAD";
878 ref_map = get_remote_ref(remote_refs, name);
879 if (!missing_ok && !ref_map)
880 die("Couldn't find remote ref %s", name);
882 ref_map->peer_ref = get_local_ref(refspec->dst);
883 if (ref_map->peer_ref && refspec->force)
884 ref_map->peer_ref->force = 1;
888 for (rm = ref_map; rm; rm = rm->next) {
889 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
890 die("* refusing to create funny ref '%s' locally",
895 tail_link_ref(ref_map, tail);