10 static struct refspec s_tag_refspec = {
18 const struct refspec *tag_refspec = &s_tag_refspec;
20 struct counted_string {
27 struct counted_string *instead_of;
32 static struct remote **remotes;
33 static int remotes_alloc;
34 static int remotes_nr;
36 static struct branch **branches;
37 static int branches_alloc;
38 static int branches_nr;
40 static struct branch *current_branch;
41 static const char *default_remote_name;
43 static struct rewrite **rewrite;
44 static int rewrite_alloc;
45 static int rewrite_nr;
47 #define BUF_SIZE (2048)
48 static char buffer[BUF_SIZE];
50 static const char *alias_url(const char *url)
54 struct counted_string *longest;
59 for (i = 0; i < rewrite_nr; i++) {
62 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
63 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
65 longest->len < rewrite[i]->instead_of[j].len)) {
66 longest = &(rewrite[i]->instead_of[j]);
74 ret = xmalloc(rewrite[longest_i]->baselen +
75 (strlen(url) - longest->len) + 1);
76 strcpy(ret, rewrite[longest_i]->base);
77 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
81 static void add_push_refspec(struct remote *remote, const char *ref)
83 ALLOC_GROW(remote->push_refspec,
84 remote->push_refspec_nr + 1,
85 remote->push_refspec_alloc);
86 remote->push_refspec[remote->push_refspec_nr++] = ref;
89 static void add_fetch_refspec(struct remote *remote, const char *ref)
91 ALLOC_GROW(remote->fetch_refspec,
92 remote->fetch_refspec_nr + 1,
93 remote->fetch_refspec_alloc);
94 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
97 static void add_url(struct remote *remote, const char *url)
99 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
100 remote->url[remote->url_nr++] = url;
103 static void add_url_alias(struct remote *remote, const char *url)
105 add_url(remote, alias_url(url));
108 static struct remote *make_remote(const char *name, int len)
113 for (i = 0; i < remotes_nr; i++) {
114 if (len ? (!strncmp(name, remotes[i]->name, len) &&
115 !remotes[i]->name[len]) :
116 !strcmp(name, remotes[i]->name))
120 ret = xcalloc(1, sizeof(struct remote));
121 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
122 remotes[remotes_nr++] = ret;
124 ret->name = xstrndup(name, len);
126 ret->name = xstrdup(name);
130 static void add_merge(struct branch *branch, const char *name)
132 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
133 branch->merge_alloc);
134 branch->merge_name[branch->merge_nr++] = name;
137 static struct branch *make_branch(const char *name, int len)
143 for (i = 0; i < branches_nr; i++) {
144 if (len ? (!strncmp(name, branches[i]->name, len) &&
145 !branches[i]->name[len]) :
146 !strcmp(name, branches[i]->name))
150 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
151 ret = xcalloc(1, sizeof(struct branch));
152 branches[branches_nr++] = ret;
154 ret->name = xstrndup(name, len);
156 ret->name = xstrdup(name);
157 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
158 strcpy(refname, "refs/heads/");
159 strcpy(refname + strlen("refs/heads/"), ret->name);
160 ret->refname = refname;
165 static struct rewrite *make_rewrite(const char *base, int len)
170 for (i = 0; i < rewrite_nr; i++) {
172 ? (len == rewrite[i]->baselen &&
173 !strncmp(base, rewrite[i]->base, len))
174 : !strcmp(base, rewrite[i]->base))
178 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
179 ret = xcalloc(1, sizeof(struct rewrite));
180 rewrite[rewrite_nr++] = ret;
182 ret->base = xstrndup(base, len);
186 ret->base = xstrdup(base);
187 ret->baselen = strlen(base);
192 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
194 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
195 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
196 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
197 rewrite->instead_of_nr++;
200 static void read_remotes_file(struct remote *remote)
202 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
206 remote->origin = REMOTE_REMOTES;
207 while (fgets(buffer, BUF_SIZE, f)) {
211 if (!prefixcmp(buffer, "URL:")) {
214 } else if (!prefixcmp(buffer, "Push:")) {
217 } else if (!prefixcmp(buffer, "Pull:")) {
229 while (isspace(p[-1]))
232 switch (value_list) {
234 add_url_alias(remote, xstrdup(s));
237 add_push_refspec(remote, xstrdup(s));
240 add_fetch_refspec(remote, xstrdup(s));
247 static void read_branches_file(struct remote *remote)
249 const char *slash = strchr(remote->name, '/');
251 struct strbuf branch = STRBUF_INIT;
252 int n = slash ? slash - remote->name : 1000;
253 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
259 s = fgets(buffer, BUF_SIZE, f);
267 remote->origin = REMOTE_BRANCHES;
269 while (isspace(p[-1]))
273 len += strlen(slash);
274 p = xmalloc(len + 1);
280 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
281 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
282 * the partial URL obtained from the branches file plus
283 * "/netdev-2.6" and does not store it in any tracking ref.
284 * #branch specifier in the file is ignored.
286 * Otherwise, the branches file would have URL and optionally
287 * #branch specified. The "master" (or specified) branch is
288 * fetched and stored in the local branch of the same name.
290 frag = strchr(p, '#');
293 strbuf_addf(&branch, "refs/heads/%s", frag);
295 strbuf_addstr(&branch, "refs/heads/master");
297 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
299 strbuf_reset(&branch);
300 strbuf_addstr(&branch, "HEAD:");
302 add_url_alias(remote, p);
303 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
305 * Cogito compatible push: push current HEAD to remote #branch
306 * (master if missing)
308 strbuf_init(&branch, 0);
309 strbuf_addstr(&branch, "HEAD");
311 strbuf_addf(&branch, ":refs/heads/%s", frag);
313 strbuf_addstr(&branch, ":refs/heads/master");
314 add_push_refspec(remote, strbuf_detach(&branch, 0));
315 remote->fetch_tags = 1; /* always auto-follow */
318 static int handle_config(const char *key, const char *value, void *cb)
322 struct remote *remote;
323 struct branch *branch;
324 if (!prefixcmp(key, "branch.")) {
326 subkey = strrchr(name, '.');
329 branch = make_branch(name, subkey - name);
330 if (!strcmp(subkey, ".remote")) {
332 return config_error_nonbool(key);
333 branch->remote_name = xstrdup(value);
334 if (branch == current_branch)
335 default_remote_name = branch->remote_name;
336 } else if (!strcmp(subkey, ".merge")) {
338 return config_error_nonbool(key);
339 add_merge(branch, xstrdup(value));
343 if (!prefixcmp(key, "url.")) {
344 struct rewrite *rewrite;
346 subkey = strrchr(name, '.');
349 rewrite = make_rewrite(name, subkey - name);
350 if (!strcmp(subkey, ".insteadof")) {
352 return config_error_nonbool(key);
353 add_instead_of(rewrite, xstrdup(value));
356 if (prefixcmp(key, "remote."))
360 warning("Config remote shorthand cannot begin with '/': %s",
364 subkey = strrchr(name, '.');
366 return error("Config with no key for remote %s", name);
367 remote = make_remote(name, subkey - name);
368 remote->origin = REMOTE_CONFIG;
369 if (!strcmp(subkey, ".mirror"))
370 remote->mirror = git_config_bool(key, value);
371 else if (!strcmp(subkey, ".skipdefaultupdate"))
372 remote->skip_default_update = git_config_bool(key, value);
374 else if (!strcmp(subkey, ".url")) {
376 if (git_config_string(&v, key, value))
379 } else if (!strcmp(subkey, ".push")) {
381 if (git_config_string(&v, key, value))
383 add_push_refspec(remote, v);
384 } else if (!strcmp(subkey, ".fetch")) {
386 if (git_config_string(&v, key, value))
388 add_fetch_refspec(remote, v);
389 } else if (!strcmp(subkey, ".receivepack")) {
391 if (git_config_string(&v, key, value))
393 if (!remote->receivepack)
394 remote->receivepack = v;
396 error("more than one receivepack given, using the first");
397 } else if (!strcmp(subkey, ".uploadpack")) {
399 if (git_config_string(&v, key, value))
401 if (!remote->uploadpack)
402 remote->uploadpack = v;
404 error("more than one uploadpack given, using the first");
405 } else if (!strcmp(subkey, ".tagopt")) {
406 if (!strcmp(value, "--no-tags"))
407 remote->fetch_tags = -1;
408 } else if (!strcmp(subkey, ".proxy")) {
409 return git_config_string((const char **)&remote->http_proxy,
415 static void alias_all_urls(void)
418 for (i = 0; i < remotes_nr; i++) {
421 for (j = 0; j < remotes[i]->url_nr; j++) {
422 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
427 static void read_config(void)
429 unsigned char sha1[20];
430 const char *head_ref;
432 if (default_remote_name) // did this already
434 default_remote_name = xstrdup("origin");
435 current_branch = NULL;
436 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
437 if (head_ref && (flag & REF_ISSYMREF) &&
438 !prefixcmp(head_ref, "refs/heads/")) {
440 make_branch(head_ref + strlen("refs/heads/"), 0);
442 git_config(handle_config, NULL);
447 * We need to make sure the tracking branches are well formed, but a
448 * wildcard refspec in "struct refspec" must have a trailing slash. We
449 * temporarily drop the trailing '/' while calling check_ref_format(),
450 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
451 * error return is Ok for a wildcard refspec.
453 static int verify_refname(char *name, int is_glob)
455 int result, len = -1;
459 assert(name[len - 1] == '/');
460 name[len - 1] = '\0';
462 result = check_ref_format(name);
469 * This function frees a refspec array.
470 * Warning: code paths should be checked to ensure that the src
471 * and dst pointers are always freeable pointers as well
472 * as the refspec pointer itself.
474 static void free_refspecs(struct refspec *refspec, int nr_refspec)
481 for (i = 0; i < nr_refspec; i++) {
482 free(refspec[i].src);
483 free(refspec[i].dst);
488 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
492 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
494 for (i = 0; i < nr_refspec; i++) {
497 const char *lhs, *rhs;
507 rhs = strrchr(lhs, ':');
510 * Before going on, special case ":" (or "+:") as a refspec
513 if (!fetch && rhs == lhs && rhs[1] == '\0') {
519 size_t rlen = strlen(++rhs);
520 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
521 rs[i].dst = xstrndup(rhs, rlen - is_glob);
524 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
525 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
526 if ((rhs && !is_glob) || (!rhs && fetch))
530 } else if (rhs && is_glob) {
534 rs[i].pattern = is_glob;
535 rs[i].src = xstrndup(lhs, llen);
540 * - empty is allowed; it means HEAD.
541 * - otherwise it must be a valid looking ref.
546 st = verify_refname(rs[i].src, is_glob);
547 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
552 * - missing is ok, and is same as empty.
553 * - empty is ok; it means not to store.
554 * - otherwise it must be a valid looking ref.
558 } else if (!*rs[i].dst) {
561 st = verify_refname(rs[i].dst, is_glob);
562 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
568 * - empty is allowed; it means delete.
569 * - when wildcarded, it must be a valid looking ref.
570 * - otherwise, it must be an extended SHA-1, but
571 * there is no existing way to validate this.
576 st = verify_refname(rs[i].src, is_glob);
577 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
581 ; /* anything goes, for now */
584 * - missing is allowed, but LHS then must be a
586 * - empty is not allowed.
587 * - otherwise it must be a valid looking ref.
590 st = verify_refname(rs[i].src, is_glob);
591 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
593 } else if (!*rs[i].dst) {
596 st = verify_refname(rs[i].dst, is_glob);
597 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
607 * nr_refspec must be greater than zero and i must be valid
608 * since it is only possible to reach this point from within
609 * the for loop above.
611 free_refspecs(rs, i+1);
614 die("Invalid refspec '%s'", refspec[i]);
617 int valid_fetch_refspec(const char *fetch_refspec_str)
619 const char *fetch_refspec[] = { fetch_refspec_str };
620 struct refspec *refspec;
622 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
623 free_refspecs(refspec, 1);
627 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
629 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
632 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
634 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
637 static int valid_remote_nick(const char *name)
639 if (!name[0] || is_dot_or_dotdot(name))
641 return !strchr(name, '/'); /* no slash */
644 struct remote *remote_get(const char *name)
650 name = default_remote_name;
651 ret = make_remote(name, 0);
652 if (valid_remote_nick(name)) {
654 read_remotes_file(ret);
656 read_branches_file(ret);
659 add_url_alias(ret, name);
662 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
663 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
667 int for_each_remote(each_remote_fn fn, void *priv)
671 for (i = 0; i < remotes_nr && !result; i++) {
672 struct remote *r = remotes[i];
676 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
679 r->push = parse_push_refspec(r->push_refspec_nr,
681 result = fn(r, priv);
686 void ref_remove_duplicates(struct ref *ref_map)
690 for (; ref_map; ref_map = ref_map->next) {
691 if (!ref_map->peer_ref)
693 posn = &ref_map->next;
695 if ((*posn)->peer_ref &&
696 !strcmp((*posn)->peer_ref->name,
697 ref_map->peer_ref->name)) {
698 if (strcmp((*posn)->name, ref_map->name))
699 die("%s tracks both %s and %s",
700 ref_map->peer_ref->name,
701 (*posn)->name, ref_map->name);
702 next = (*posn)->next;
703 free((*posn)->peer_ref);
707 posn = &(*posn)->next;
713 int remote_has_url(struct remote *remote, const char *url)
716 for (i = 0; i < remote->url_nr; i++) {
717 if (!strcmp(remote->url[i], url))
723 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
725 int find_src = refspec->src == NULL;
726 char *needle, **result;
731 return error("find_tracking: need either src or dst");
732 needle = refspec->dst;
733 result = &refspec->src;
735 needle = refspec->src;
736 result = &refspec->dst;
739 for (i = 0; i < remote->fetch_refspec_nr; i++) {
740 struct refspec *fetch = &remote->fetch[i];
741 const char *key = find_src ? fetch->dst : fetch->src;
742 const char *value = find_src ? fetch->src : fetch->dst;
745 if (fetch->pattern) {
746 if (!prefixcmp(needle, key)) {
747 *result = xmalloc(strlen(value) +
750 strcpy(*result, value);
751 strcpy(*result + strlen(value),
752 needle + strlen(key));
753 refspec->force = fetch->force;
756 } else if (!strcmp(needle, key)) {
757 *result = xstrdup(value);
758 refspec->force = fetch->force;
765 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
768 size_t len = strlen(name);
769 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
770 memcpy(ref->name, prefix, prefixlen);
771 memcpy(ref->name + prefixlen, name, len);
775 struct ref *alloc_ref(const char *name)
777 return alloc_ref_with_prefix("", 0, name);
780 static struct ref *copy_ref(const struct ref *ref)
782 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
783 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
788 struct ref *copy_ref_list(const struct ref *ref)
790 struct ref *ret = NULL;
791 struct ref **tail = &ret;
793 *tail = copy_ref(ref);
795 tail = &((*tail)->next);
800 static void free_ref(struct ref *ref)
804 free(ref->remote_status);
809 void free_refs(struct ref *ref)
820 static int count_refspec_match(const char *pattern,
822 struct ref **matched_ref)
824 int patlen = strlen(pattern);
825 struct ref *matched_weak = NULL;
826 struct ref *matched = NULL;
830 for (weak_match = match = 0; refs; refs = refs->next) {
831 char *name = refs->name;
832 int namelen = strlen(name);
834 if (!refname_match(pattern, name, ref_rev_parse_rules))
837 /* A match is "weak" if it is with refs outside
838 * heads or tags, and did not specify the pattern
839 * in full (e.g. "refs/remotes/origin/master") or at
840 * least from the toplevel (e.g. "remotes/origin/master");
841 * otherwise "git push $URL master" would result in
842 * ambiguity between remotes/origin/master and heads/master
843 * at the remote site.
845 if (namelen != patlen &&
846 patlen != namelen - 5 &&
847 prefixcmp(name, "refs/heads/") &&
848 prefixcmp(name, "refs/tags/")) {
849 /* We want to catch the case where only weak
850 * matches are found and there are multiple
851 * matches, and where more than one strong
852 * matches are found, as ambiguous. One
853 * strong match with zero or more weak matches
854 * are acceptable as a unique match.
865 *matched_ref = matched_weak;
869 *matched_ref = matched;
874 static void tail_link_ref(struct ref *ref, struct ref ***tail)
882 static struct ref *try_explicit_object_name(const char *name)
884 unsigned char sha1[20];
888 ref = alloc_ref("(delete)");
889 hashclr(ref->new_sha1);
892 if (get_sha1(name, sha1))
894 ref = alloc_ref(name);
895 hashcpy(ref->new_sha1, sha1);
899 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
901 struct ref *ret = alloc_ref(name);
902 tail_link_ref(ret, tail);
906 static char *guess_ref(const char *name, struct ref *peer)
908 struct strbuf buf = STRBUF_INIT;
909 unsigned char sha1[20];
911 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
915 if (!prefixcmp(r, "refs/heads/"))
916 strbuf_addstr(&buf, "refs/heads/");
917 else if (!prefixcmp(r, "refs/tags/"))
918 strbuf_addstr(&buf, "refs/tags/");
922 strbuf_addstr(&buf, name);
923 return strbuf_detach(&buf, NULL);
926 static int match_explicit(struct ref *src, struct ref *dst,
927 struct ref ***dst_tail,
930 struct ref *matched_src, *matched_dst;
932 const char *dst_value = rs->dst;
935 if (rs->pattern || rs->matching)
938 matched_src = matched_dst = NULL;
939 switch (count_refspec_match(rs->src, src, &matched_src)) {
943 /* The source could be in the get_sha1() format
944 * not a reference name. :refs/other is a
945 * way to delete 'other' ref at the remote end.
947 matched_src = try_explicit_object_name(rs->src);
949 return error("src refspec %s does not match any.", rs->src);
952 return error("src refspec %s matches more than one.", rs->src);
956 unsigned char sha1[20];
959 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
961 ((flag & REF_ISSYMREF) &&
962 prefixcmp(dst_value, "refs/heads/")))
963 die("%s cannot be resolved to branch.",
967 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
971 if (!memcmp(dst_value, "refs/", 5))
972 matched_dst = make_linked_ref(dst_value, dst_tail);
973 else if((dst_guess = guess_ref(dst_value, matched_src)))
974 matched_dst = make_linked_ref(dst_guess, dst_tail);
976 error("unable to push to unqualified destination: %s\n"
977 "The destination refspec neither matches an "
978 "existing ref on the remote nor\n"
979 "begins with refs/, and we are unable to "
980 "guess a prefix based on the source ref.",
985 error("dst refspec %s matches more than one.",
991 if (matched_dst->peer_ref)
992 return error("dst ref %s receives from more than one src.",
995 matched_dst->peer_ref = matched_src;
996 matched_dst->force = rs->force;
1001 static int match_explicit_refs(struct ref *src, struct ref *dst,
1002 struct ref ***dst_tail, struct refspec *rs,
1006 for (i = errs = 0; i < rs_nr; i++)
1007 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1011 static const struct refspec *check_pattern_match(const struct refspec *rs,
1013 const struct ref *src)
1016 int matching_refs = -1;
1017 for (i = 0; i < rs_nr; i++) {
1018 if (rs[i].matching &&
1019 (matching_refs == -1 || rs[i].force)) {
1024 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
1027 if (matching_refs != -1)
1028 return rs + matching_refs;
1034 * Note. This is used only by "push"; refspec matching rules for
1035 * push and fetch are subtly different, so do not try to reuse it
1038 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1039 int nr_refspec, const char **refspec, int flags)
1042 int send_all = flags & MATCH_REFS_ALL;
1043 int send_mirror = flags & MATCH_REFS_MIRROR;
1044 static const char *default_refspec[] = { ":", 0 };
1048 refspec = default_refspec;
1050 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1051 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
1054 /* pick the remainder */
1055 for ( ; src; src = src->next) {
1056 struct ref *dst_peer;
1057 const struct refspec *pat = NULL;
1062 pat = check_pattern_match(rs, nr_refspec, src);
1066 if (pat->matching) {
1068 * "matching refs"; traditionally we pushed everything
1069 * including refs outside refs/heads/ hierarchy, but
1070 * that does not make much sense these days.
1072 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1074 dst_name = xstrdup(src->name);
1077 const char *dst_side = pat->dst ? pat->dst : pat->src;
1078 dst_name = xmalloc(strlen(dst_side) +
1080 strlen(pat->src) + 2);
1081 strcpy(dst_name, dst_side);
1082 strcat(dst_name, src->name + strlen(pat->src));
1084 dst_peer = find_ref_by_name(dst, dst_name);
1086 if (dst_peer->peer_ref)
1087 /* We're already sending something to this ref. */
1091 if (pat->matching && !(send_all || send_mirror))
1093 * Remote doesn't have it, and we have no
1094 * explicit pattern, and we don't have
1095 * --all nor --mirror.
1099 /* Create a new one and link it */
1100 dst_peer = make_linked_ref(dst_name, dst_tail);
1101 hashcpy(dst_peer->new_sha1, src->new_sha1);
1103 dst_peer->peer_ref = src;
1104 dst_peer->force = pat->force;
1111 struct branch *branch_get(const char *name)
1116 if (!name || !*name || !strcmp(name, "HEAD"))
1117 ret = current_branch;
1119 ret = make_branch(name, 0);
1120 if (ret && ret->remote_name) {
1121 ret->remote = remote_get(ret->remote_name);
1122 if (ret->merge_nr) {
1124 ret->merge = xcalloc(sizeof(*ret->merge),
1126 for (i = 0; i < ret->merge_nr; i++) {
1127 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1128 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1129 remote_find_tracking(ret->remote,
1137 int branch_has_merge_config(struct branch *branch)
1139 return branch && !!branch->merge;
1142 int branch_merge_matches(struct branch *branch,
1144 const char *refname)
1146 if (!branch || i < 0 || i >= branch->merge_nr)
1148 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1151 static struct ref *get_expanded_map(const struct ref *remote_refs,
1152 const struct refspec *refspec)
1154 const struct ref *ref;
1155 struct ref *ret = NULL;
1156 struct ref **tail = &ret;
1158 int remote_prefix_len = strlen(refspec->src);
1159 int local_prefix_len = strlen(refspec->dst);
1161 for (ref = remote_refs; ref; ref = ref->next) {
1162 if (strchr(ref->name, '^'))
1163 continue; /* a dereference item */
1164 if (!prefixcmp(ref->name, refspec->src)) {
1166 struct ref *cpy = copy_ref(ref);
1167 match = ref->name + remote_prefix_len;
1169 cpy->peer_ref = alloc_ref_with_prefix(refspec->dst,
1170 local_prefix_len, match);
1172 cpy->peer_ref->force = 1;
1181 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1183 const struct ref *ref;
1184 for (ref = refs; ref; ref = ref->next) {
1185 if (refname_match(name, ref->name, ref_fetch_rules))
1191 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1193 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1198 return copy_ref(ref);
1201 static struct ref *get_local_ref(const char *name)
1206 if (!prefixcmp(name, "refs/"))
1207 return alloc_ref(name);
1209 if (!prefixcmp(name, "heads/") ||
1210 !prefixcmp(name, "tags/") ||
1211 !prefixcmp(name, "remotes/"))
1212 return alloc_ref_with_prefix("refs/", 5, name);
1214 return alloc_ref_with_prefix("refs/heads/", 11, name);
1217 int get_fetch_map(const struct ref *remote_refs,
1218 const struct refspec *refspec,
1222 struct ref *ref_map, **rmp;
1224 if (refspec->pattern) {
1225 ref_map = get_expanded_map(remote_refs, refspec);
1227 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1229 ref_map = get_remote_ref(remote_refs, name);
1230 if (!missing_ok && !ref_map)
1231 die("Couldn't find remote ref %s", name);
1233 ref_map->peer_ref = get_local_ref(refspec->dst);
1234 if (ref_map->peer_ref && refspec->force)
1235 ref_map->peer_ref->force = 1;
1239 for (rmp = &ref_map; *rmp; ) {
1240 if ((*rmp)->peer_ref) {
1241 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1242 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1243 struct ref *ignore = *rmp;
1244 error("* Ignoring funny ref '%s' locally",
1245 (*rmp)->peer_ref->name);
1246 *rmp = (*rmp)->next;
1247 free(ignore->peer_ref);
1252 rmp = &((*rmp)->next);
1256 tail_link_ref(ref_map, tail);
1261 int resolve_remote_symref(struct ref *ref, struct ref *list)
1265 for (; list; list = list->next)
1266 if (!strcmp(ref->symref, list->name)) {
1267 hashcpy(ref->old_sha1, list->old_sha1);
1273 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1276 struct commit_list *temp = list;
1277 temp->item->object.flags &= ~mark;
1283 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1286 struct commit *old, *new;
1287 struct commit_list *list, *used;
1290 /* Both new and old must be commit-ish and new is descendant of
1291 * old. Otherwise we require --force.
1293 o = deref_tag(parse_object(old_sha1), NULL, 0);
1294 if (!o || o->type != OBJ_COMMIT)
1296 old = (struct commit *) o;
1298 o = deref_tag(parse_object(new_sha1), NULL, 0);
1299 if (!o || o->type != OBJ_COMMIT)
1301 new = (struct commit *) o;
1303 if (parse_commit(new) < 0)
1307 commit_list_insert(new, &list);
1309 new = pop_most_recent_commit(&list, TMP_MARK);
1310 commit_list_insert(new, &used);
1316 unmark_and_free(list, TMP_MARK);
1317 unmark_and_free(used, TMP_MARK);
1322 * Return true if there is anything to report, otherwise false.
1324 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1326 unsigned char sha1[20];
1327 struct commit *ours, *theirs;
1329 struct rev_info revs;
1330 const char *rev_argv[10], *base;
1334 * Nothing to report unless we are marked to build on top of
1338 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1342 * If what we used to build on no longer exists, there is
1343 * nothing to report.
1345 base = branch->merge[0]->dst;
1346 if (!resolve_ref(base, sha1, 1, NULL))
1348 theirs = lookup_commit(sha1);
1352 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1354 ours = lookup_commit(sha1);
1358 /* are we the same? */
1362 /* Run "rev-list --left-right ours...theirs" internally... */
1364 rev_argv[rev_argc++] = NULL;
1365 rev_argv[rev_argc++] = "--left-right";
1366 rev_argv[rev_argc++] = symmetric;
1367 rev_argv[rev_argc++] = "--";
1368 rev_argv[rev_argc] = NULL;
1370 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1371 strcpy(symmetric + 40, "...");
1372 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1374 init_revisions(&revs, NULL);
1375 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1376 prepare_revision_walk(&revs);
1378 /* ... and count the commits on each side. */
1382 struct commit *c = get_revision(&revs);
1385 if (c->object.flags & SYMMETRIC_LEFT)
1391 /* clear object flags smudged by the above traversal */
1392 clear_commit_marks(ours, ALL_REV_FLAGS);
1393 clear_commit_marks(theirs, ALL_REV_FLAGS);
1398 * Return true when there is anything to report, otherwise false.
1400 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1402 int num_ours, num_theirs;
1405 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1408 base = branch->merge[0]->dst;
1409 if (!prefixcmp(base, "refs/remotes/")) {
1410 base += strlen("refs/remotes/");
1413 strbuf_addf(sb, "Your branch is ahead of '%s' "
1414 "by %d commit%s.\n",
1415 base, num_ours, (num_ours == 1) ? "" : "s");
1417 strbuf_addf(sb, "Your branch is behind '%s' "
1419 "and can be fast-forwarded.\n",
1420 base, num_theirs, (num_theirs == 1) ? "" : "s");
1422 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1423 "and have %d and %d different commit(s) each, "
1425 base, num_ours, num_theirs);
1429 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1431 struct ref ***local_tail = cb_data;
1435 /* we already know it starts with refs/ to get here */
1436 if (check_ref_format(refname + 5))
1439 len = strlen(refname) + 1;
1440 ref = xcalloc(1, sizeof(*ref) + len);
1441 hashcpy(ref->new_sha1, sha1);
1442 memcpy(ref->name, refname, len);
1444 *local_tail = &ref->next;
1448 struct ref *get_local_heads(void)
1450 struct ref *local_refs, **local_tail = &local_refs;
1451 for_each_ref(one_local_ref, &local_tail);
1455 const struct ref *guess_remote_head(const struct ref *refs,
1456 const struct ref *mapped_refs,
1457 const struct ref **remote_head_p)
1459 const struct ref *remote_head = NULL;
1460 const struct ref *remote_master = NULL;
1461 const struct ref *r;
1462 for (r = refs; r; r = r->next)
1463 if (!strcmp(r->name, "HEAD"))
1466 for (r = mapped_refs; r; r = r->next)
1467 if (!strcmp(r->name, "refs/heads/master"))
1471 *remote_head_p = remote_head;
1473 /* If there's no HEAD value at all, never mind. */
1477 /* If refs/heads/master could be right, it is. */
1478 if (remote_master && !hashcmp(remote_master->old_sha1,
1479 remote_head->old_sha1))
1480 return remote_master;
1482 /* Look for another ref that points there */
1483 for (r = mapped_refs; r; r = r->next)
1484 if (r != remote_head &&
1485 !hashcmp(r->old_sha1, remote_head->old_sha1))
1488 /* Nothing is the same */