7 const char **instead_of;
12 static struct remote **remotes;
13 static int remotes_alloc;
14 static int remotes_nr;
16 static struct branch **branches;
17 static int branches_alloc;
18 static int branches_nr;
20 static struct branch *current_branch;
21 static const char *default_remote_name;
23 static struct rewrite **rewrite;
24 static int rewrite_alloc;
25 static int rewrite_nr;
27 #define BUF_SIZE (2048)
28 static char buffer[BUF_SIZE];
30 static const char *alias_url(const char *url)
33 for (i = 0; i < rewrite_nr; i++) {
36 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
37 if (!prefixcmp(url, rewrite[i]->instead_of[j])) {
38 char *ret = malloc(strlen(rewrite[i]->base) -
39 strlen(rewrite[i]->instead_of[j]) +
41 strcpy(ret, rewrite[i]->base);
42 strcat(ret, url + strlen(rewrite[i]->instead_of[j]));
50 static void add_push_refspec(struct remote *remote, const char *ref)
52 ALLOC_GROW(remote->push_refspec,
53 remote->push_refspec_nr + 1,
54 remote->push_refspec_alloc);
55 remote->push_refspec[remote->push_refspec_nr++] = ref;
58 static void add_fetch_refspec(struct remote *remote, const char *ref)
60 ALLOC_GROW(remote->fetch_refspec,
61 remote->fetch_refspec_nr + 1,
62 remote->fetch_refspec_alloc);
63 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
66 static void add_url(struct remote *remote, const char *url)
68 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
69 remote->url[remote->url_nr++] = url;
72 static void add_url_alias(struct remote *remote, const char *url)
74 add_url(remote, alias_url(url));
77 static struct remote *make_remote(const char *name, int len)
82 for (i = 0; i < remotes_nr; i++) {
83 if (len ? (!strncmp(name, remotes[i]->name, len) &&
84 !remotes[i]->name[len]) :
85 !strcmp(name, remotes[i]->name))
89 ret = xcalloc(1, sizeof(struct remote));
90 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
91 remotes[remotes_nr++] = ret;
93 ret->name = xstrndup(name, len);
95 ret->name = xstrdup(name);
99 static void add_merge(struct branch *branch, const char *name)
101 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
102 branch->merge_alloc);
103 branch->merge_name[branch->merge_nr++] = name;
106 static struct branch *make_branch(const char *name, int len)
112 for (i = 0; i < branches_nr; i++) {
113 if (len ? (!strncmp(name, branches[i]->name, len) &&
114 !branches[i]->name[len]) :
115 !strcmp(name, branches[i]->name))
119 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
120 ret = xcalloc(1, sizeof(struct branch));
121 branches[branches_nr++] = ret;
123 ret->name = xstrndup(name, len);
125 ret->name = xstrdup(name);
126 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
127 strcpy(refname, "refs/heads/");
128 strcpy(refname + strlen("refs/heads/"), ret->name);
129 ret->refname = refname;
134 static struct rewrite *make_rewrite(const char *base, int len)
139 for (i = 0; i < rewrite_nr; i++) {
140 if (len ? (!strncmp(base, rewrite[i]->base, len) &&
141 !rewrite[i]->base[len]) :
142 !strcmp(base, rewrite[i]->base))
146 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
147 ret = xcalloc(1, sizeof(struct rewrite));
148 rewrite[rewrite_nr++] = ret;
150 ret->base = xstrndup(base, len);
152 ret->base = xstrdup(base);
157 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
159 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
160 rewrite->instead_of[rewrite->instead_of_nr++] = instead_of;
163 static void read_remotes_file(struct remote *remote)
165 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
169 while (fgets(buffer, BUF_SIZE, f)) {
173 if (!prefixcmp(buffer, "URL:")) {
176 } else if (!prefixcmp(buffer, "Push:")) {
179 } else if (!prefixcmp(buffer, "Pull:")) {
191 while (isspace(p[-1]))
194 switch (value_list) {
196 add_url_alias(remote, xstrdup(s));
199 add_push_refspec(remote, xstrdup(s));
202 add_fetch_refspec(remote, xstrdup(s));
209 static void read_branches_file(struct remote *remote)
211 const char *slash = strchr(remote->name, '/');
214 int n = slash ? slash - remote->name : 1000;
215 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
221 s = fgets(buffer, BUF_SIZE, f);
230 while (isspace(p[-1]))
234 len += strlen(slash);
235 p = xmalloc(len + 1);
239 frag = strchr(p, '#');
242 branch = xmalloc(strlen(frag) + 12);
243 strcpy(branch, "refs/heads/");
244 strcat(branch, frag);
246 branch = "refs/heads/master";
248 add_url_alias(remote, p);
249 add_fetch_refspec(remote, branch);
250 remote->fetch_tags = 1; /* always auto-follow */
253 static int handle_config(const char *key, const char *value)
257 struct remote *remote;
258 struct branch *branch;
259 if (!prefixcmp(key, "branch.")) {
261 subkey = strrchr(name, '.');
264 branch = make_branch(name, subkey - name);
265 if (!strcmp(subkey, ".remote")) {
267 return config_error_nonbool(key);
268 branch->remote_name = xstrdup(value);
269 if (branch == current_branch)
270 default_remote_name = branch->remote_name;
271 } else if (!strcmp(subkey, ".merge")) {
273 return config_error_nonbool(key);
274 add_merge(branch, xstrdup(value));
278 if (!prefixcmp(key, "url.")) {
279 struct rewrite *rewrite;
281 subkey = strrchr(name, '.');
284 rewrite = make_rewrite(name, subkey - name);
285 if (!strcmp(subkey, ".insteadof")) {
287 return config_error_nonbool(key);
288 add_instead_of(rewrite, xstrdup(value));
291 if (prefixcmp(key, "remote."))
294 subkey = strrchr(name, '.');
296 return error("Config with no key for remote %s", name);
297 if (*subkey == '/') {
298 warning("Config remote shorthand cannot begin with '/': %s", name);
301 remote = make_remote(name, subkey - name);
303 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
306 * is a valid way to set it to true; we get NULL in value so
307 * we need to handle it here.
309 * if (!strcmp(subkey, ".disabled")) {
310 * val = git_config_bool(key, value);
315 return 0; /* ignore unknown booleans */
317 if (!strcmp(subkey, ".url")) {
318 add_url(remote, xstrdup(value));
319 } else if (!strcmp(subkey, ".push")) {
320 add_push_refspec(remote, xstrdup(value));
321 } else if (!strcmp(subkey, ".fetch")) {
322 add_fetch_refspec(remote, xstrdup(value));
323 } else if (!strcmp(subkey, ".receivepack")) {
324 if (!remote->receivepack)
325 remote->receivepack = xstrdup(value);
327 error("more than one receivepack given, using the first");
328 } else if (!strcmp(subkey, ".uploadpack")) {
329 if (!remote->uploadpack)
330 remote->uploadpack = xstrdup(value);
332 error("more than one uploadpack given, using the first");
333 } else if (!strcmp(subkey, ".tagopt")) {
334 if (!strcmp(value, "--no-tags"))
335 remote->fetch_tags = -1;
336 } else if (!strcmp(subkey, ".proxy")) {
337 remote->http_proxy = xstrdup(value);
342 static void alias_all_urls(void)
345 for (i = 0; i < remotes_nr; i++) {
348 for (j = 0; j < remotes[i]->url_nr; j++) {
349 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
354 static void read_config(void)
356 unsigned char sha1[20];
357 const char *head_ref;
359 if (default_remote_name) // did this already
361 default_remote_name = xstrdup("origin");
362 current_branch = NULL;
363 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
364 if (head_ref && (flag & REF_ISSYMREF) &&
365 !prefixcmp(head_ref, "refs/heads/")) {
367 make_branch(head_ref + strlen("refs/heads/"), 0);
369 git_config(handle_config);
373 struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
376 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
377 for (i = 0; i < nr_refspec; i++) {
378 const char *sp, *ep, *gp;
384 gp = strchr(sp, '*');
385 ep = strchr(sp, ':');
386 if (gp && ep && gp > ep)
390 const char *glob = strchr(ep + 1, '*');
394 rs[i].dst = xstrndup(ep + 1,
397 rs[i].dst = xstrdup(ep + 1);
400 ep = sp + strlen(sp);
406 rs[i].src = xstrndup(sp, ep - sp);
411 static int valid_remote_nick(const char *name)
413 if (!name[0] || /* not empty */
414 (name[0] == '.' && /* not "." */
415 (!name[1] || /* not ".." */
416 (name[1] == '.' && !name[2]))))
418 return !strchr(name, '/'); /* no slash */
421 struct remote *remote_get(const char *name)
427 name = default_remote_name;
428 ret = make_remote(name, 0);
429 if (valid_remote_nick(name)) {
431 read_remotes_file(ret);
433 read_branches_file(ret);
436 add_url_alias(ret, name);
439 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
440 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
444 int for_each_remote(each_remote_fn fn, void *priv)
448 for (i = 0; i < remotes_nr && !result; i++) {
449 struct remote *r = remotes[i];
453 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
456 r->push = parse_ref_spec(r->push_refspec_nr,
458 result = fn(r, priv);
463 void ref_remove_duplicates(struct ref *ref_map)
467 for (; ref_map; ref_map = ref_map->next) {
468 if (!ref_map->peer_ref)
470 posn = &ref_map->next;
472 if ((*posn)->peer_ref &&
473 !strcmp((*posn)->peer_ref->name,
474 ref_map->peer_ref->name)) {
475 if (strcmp((*posn)->name, ref_map->name))
476 die("%s tracks both %s and %s",
477 ref_map->peer_ref->name,
478 (*posn)->name, ref_map->name);
479 next = (*posn)->next;
480 free((*posn)->peer_ref);
484 posn = &(*posn)->next;
490 int remote_has_url(struct remote *remote, const char *url)
493 for (i = 0; i < remote->url_nr; i++) {
494 if (!strcmp(remote->url[i], url))
500 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
502 int find_src = refspec->src == NULL;
503 char *needle, **result;
508 return error("find_tracking: need either src or dst");
509 needle = refspec->dst;
510 result = &refspec->src;
512 needle = refspec->src;
513 result = &refspec->dst;
516 for (i = 0; i < remote->fetch_refspec_nr; i++) {
517 struct refspec *fetch = &remote->fetch[i];
518 const char *key = find_src ? fetch->dst : fetch->src;
519 const char *value = find_src ? fetch->src : fetch->dst;
522 if (fetch->pattern) {
523 if (!prefixcmp(needle, key)) {
524 *result = xmalloc(strlen(value) +
527 strcpy(*result, value);
528 strcpy(*result + strlen(value),
529 needle + strlen(key));
530 refspec->force = fetch->force;
533 } else if (!strcmp(needle, key)) {
534 *result = xstrdup(value);
535 refspec->force = fetch->force;
542 struct ref *alloc_ref(unsigned namelen)
544 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
545 memset(ret, 0, sizeof(struct ref) + namelen);
549 static struct ref *copy_ref(const struct ref *ref)
551 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
552 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
557 struct ref *copy_ref_list(const struct ref *ref)
559 struct ref *ret = NULL;
560 struct ref **tail = &ret;
562 *tail = copy_ref(ref);
564 tail = &((*tail)->next);
569 void free_refs(struct ref *ref)
581 static int count_refspec_match(const char *pattern,
583 struct ref **matched_ref)
585 int patlen = strlen(pattern);
586 struct ref *matched_weak = NULL;
587 struct ref *matched = NULL;
591 for (weak_match = match = 0; refs; refs = refs->next) {
592 char *name = refs->name;
593 int namelen = strlen(name);
595 if (!refname_match(pattern, name, ref_rev_parse_rules))
598 /* A match is "weak" if it is with refs outside
599 * heads or tags, and did not specify the pattern
600 * in full (e.g. "refs/remotes/origin/master") or at
601 * least from the toplevel (e.g. "remotes/origin/master");
602 * otherwise "git push $URL master" would result in
603 * ambiguity between remotes/origin/master and heads/master
604 * at the remote site.
606 if (namelen != patlen &&
607 patlen != namelen - 5 &&
608 prefixcmp(name, "refs/heads/") &&
609 prefixcmp(name, "refs/tags/")) {
610 /* We want to catch the case where only weak
611 * matches are found and there are multiple
612 * matches, and where more than one strong
613 * matches are found, as ambiguous. One
614 * strong match with zero or more weak matches
615 * are acceptable as a unique match.
626 *matched_ref = matched_weak;
630 *matched_ref = matched;
635 static void tail_link_ref(struct ref *ref, struct ref ***tail)
643 static struct ref *try_explicit_object_name(const char *name)
645 unsigned char sha1[20];
651 strcpy(ref->name, "(delete)");
652 hashclr(ref->new_sha1);
655 if (get_sha1(name, sha1))
657 len = strlen(name) + 1;
658 ref = alloc_ref(len);
659 memcpy(ref->name, name, len);
660 hashcpy(ref->new_sha1, sha1);
664 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
669 len = strlen(name) + 1;
670 ret = alloc_ref(len);
671 memcpy(ret->name, name, len);
672 tail_link_ref(ret, tail);
676 static int match_explicit(struct ref *src, struct ref *dst,
677 struct ref ***dst_tail,
681 struct ref *matched_src, *matched_dst;
683 const char *dst_value = rs->dst;
688 matched_src = matched_dst = NULL;
689 switch (count_refspec_match(rs->src, src, &matched_src)) {
693 /* The source could be in the get_sha1() format
694 * not a reference name. :refs/other is a
695 * way to delete 'other' ref at the remote end.
697 matched_src = try_explicit_object_name(rs->src);
699 error("src refspec %s does not match any.", rs->src);
703 error("src refspec %s matches more than one.", rs->src);
713 dst_value = matched_src->name;
716 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
720 if (!memcmp(dst_value, "refs/", 5))
721 matched_dst = make_linked_ref(dst_value, dst_tail);
723 error("dst refspec %s does not match any "
724 "existing ref on the remote and does "
725 "not start with refs/.", dst_value);
729 error("dst refspec %s matches more than one.",
733 if (errs || !matched_dst)
735 if (matched_dst->peer_ref) {
737 error("dst ref %s receives from more than one src.",
741 matched_dst->peer_ref = matched_src;
742 matched_dst->force = rs->force;
747 static int match_explicit_refs(struct ref *src, struct ref *dst,
748 struct ref ***dst_tail, struct refspec *rs,
752 for (i = errs = 0; i < rs_nr; i++)
753 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
757 static const struct refspec *check_pattern_match(const struct refspec *rs,
759 const struct ref *src)
762 for (i = 0; i < rs_nr; i++) {
763 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
770 * Note. This is used only by "push"; refspec matching rules for
771 * push and fetch are subtly different, so do not try to reuse it
774 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
775 int nr_refspec, const char **refspec, int flags)
778 parse_ref_spec(nr_refspec, (const char **) refspec);
779 int send_all = flags & MATCH_REFS_ALL;
780 int send_mirror = flags & MATCH_REFS_MIRROR;
782 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
785 /* pick the remainder */
786 for ( ; src; src = src->next) {
787 struct ref *dst_peer;
788 const struct refspec *pat = NULL;
793 pat = check_pattern_match(rs, nr_refspec, src);
797 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
799 * "matching refs"; traditionally we pushed everything
800 * including refs outside refs/heads/ hierarchy, but
801 * that does not make much sense these days.
806 const char *dst_side = pat->dst ? pat->dst : pat->src;
807 dst_name = xmalloc(strlen(dst_side) +
809 strlen(pat->src) + 2);
810 strcpy(dst_name, dst_side);
811 strcat(dst_name, src->name + strlen(pat->src));
813 dst_name = xstrdup(src->name);
814 dst_peer = find_ref_by_name(dst, dst_name);
815 if (dst_peer && dst_peer->peer_ref)
816 /* We're already sending something to this ref. */
819 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
821 * Remote doesn't have it, and we have no
822 * explicit pattern, and we don't have
823 * --all nor --mirror.
827 /* Create a new one and link it */
828 dst_peer = make_linked_ref(dst_name, dst_tail);
829 hashcpy(dst_peer->new_sha1, src->new_sha1);
831 dst_peer->peer_ref = src;
833 dst_peer->force = pat->force;
840 struct branch *branch_get(const char *name)
845 if (!name || !*name || !strcmp(name, "HEAD"))
846 ret = current_branch;
848 ret = make_branch(name, 0);
849 if (ret && ret->remote_name) {
850 ret->remote = remote_get(ret->remote_name);
853 ret->merge = xcalloc(sizeof(*ret->merge),
855 for (i = 0; i < ret->merge_nr; i++) {
856 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
857 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
858 remote_find_tracking(ret->remote,
866 int branch_has_merge_config(struct branch *branch)
868 return branch && !!branch->merge;
871 int branch_merge_matches(struct branch *branch,
875 if (!branch || i < 0 || i >= branch->merge_nr)
877 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
880 static struct ref *get_expanded_map(const struct ref *remote_refs,
881 const struct refspec *refspec)
883 const struct ref *ref;
884 struct ref *ret = NULL;
885 struct ref **tail = &ret;
887 int remote_prefix_len = strlen(refspec->src);
888 int local_prefix_len = strlen(refspec->dst);
890 for (ref = remote_refs; ref; ref = ref->next) {
891 if (strchr(ref->name, '^'))
892 continue; /* a dereference item */
893 if (!prefixcmp(ref->name, refspec->src)) {
895 struct ref *cpy = copy_ref(ref);
896 match = ref->name + remote_prefix_len;
898 cpy->peer_ref = alloc_ref(local_prefix_len +
900 sprintf(cpy->peer_ref->name, "%s%s",
901 refspec->dst, match);
903 cpy->peer_ref->force = 1;
912 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
914 const struct ref *ref;
915 for (ref = refs; ref; ref = ref->next) {
916 if (refname_match(name, ref->name, ref_fetch_rules))
922 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
924 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
929 return copy_ref(ref);
932 static struct ref *get_local_ref(const char *name)
938 if (!prefixcmp(name, "refs/")) {
939 ret = alloc_ref(strlen(name) + 1);
940 strcpy(ret->name, name);
944 if (!prefixcmp(name, "heads/") ||
945 !prefixcmp(name, "tags/") ||
946 !prefixcmp(name, "remotes/")) {
947 ret = alloc_ref(strlen(name) + 6);
948 sprintf(ret->name, "refs/%s", name);
952 ret = alloc_ref(strlen(name) + 12);
953 sprintf(ret->name, "refs/heads/%s", name);
957 int get_fetch_map(const struct ref *remote_refs,
958 const struct refspec *refspec,
962 struct ref *ref_map, *rm;
964 if (refspec->pattern) {
965 ref_map = get_expanded_map(remote_refs, refspec);
967 const char *name = refspec->src[0] ? refspec->src : "HEAD";
969 ref_map = get_remote_ref(remote_refs, name);
970 if (!missing_ok && !ref_map)
971 die("Couldn't find remote ref %s", name);
973 ref_map->peer_ref = get_local_ref(refspec->dst);
974 if (ref_map->peer_ref && refspec->force)
975 ref_map->peer_ref->force = 1;
979 for (rm = ref_map; rm; rm = rm->next) {
980 if (rm->peer_ref && check_ref_format(rm->peer_ref->name + 5))
981 die("* refusing to create funny ref '%s' locally",
986 tail_link_ref(ref_map, tail);