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)
457 result = check_ref_format(name);
458 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
459 result = CHECK_REF_FORMAT_OK;
464 * This function frees a refspec array.
465 * Warning: code paths should be checked to ensure that the src
466 * and dst pointers are always freeable pointers as well
467 * as the refspec pointer itself.
469 static void free_refspecs(struct refspec *refspec, int nr_refspec)
476 for (i = 0; i < nr_refspec; i++) {
477 free(refspec[i].src);
478 free(refspec[i].dst);
483 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
487 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
489 for (i = 0; i < nr_refspec; i++) {
492 const char *lhs, *rhs;
502 rhs = strrchr(lhs, ':');
505 * Before going on, special case ":" (or "+:") as a refspec
508 if (!fetch && rhs == lhs && rhs[1] == '\0') {
514 size_t rlen = strlen(++rhs);
515 is_glob = (1 <= rlen && strchr(rhs, '*'));
516 rs[i].dst = xstrndup(rhs, rlen);
519 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
520 if (1 <= llen && memchr(lhs, '*', llen)) {
521 if ((rhs && !is_glob) || (!rhs && fetch))
524 } else if (rhs && is_glob) {
528 rs[i].pattern = is_glob;
529 rs[i].src = xstrndup(lhs, llen);
534 * - empty is allowed; it means HEAD.
535 * - otherwise it must be a valid looking ref.
540 st = verify_refname(rs[i].src, is_glob);
541 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
546 * - missing is ok, and is same as empty.
547 * - empty is ok; it means not to store.
548 * - otherwise it must be a valid looking ref.
552 } else if (!*rs[i].dst) {
555 st = verify_refname(rs[i].dst, is_glob);
556 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
562 * - empty is allowed; it means delete.
563 * - when wildcarded, it must be a valid looking ref.
564 * - otherwise, it must be an extended SHA-1, but
565 * there is no existing way to validate this.
570 st = verify_refname(rs[i].src, is_glob);
571 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
575 ; /* anything goes, for now */
578 * - missing is allowed, but LHS then must be a
580 * - empty is not allowed.
581 * - otherwise it must be a valid looking ref.
584 st = verify_refname(rs[i].src, is_glob);
585 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
587 } else if (!*rs[i].dst) {
590 st = verify_refname(rs[i].dst, is_glob);
591 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
601 * nr_refspec must be greater than zero and i must be valid
602 * since it is only possible to reach this point from within
603 * the for loop above.
605 free_refspecs(rs, i+1);
608 die("Invalid refspec '%s'", refspec[i]);
611 int valid_fetch_refspec(const char *fetch_refspec_str)
613 const char *fetch_refspec[] = { fetch_refspec_str };
614 struct refspec *refspec;
616 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
617 free_refspecs(refspec, 1);
621 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
623 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
626 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
628 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
631 static int valid_remote_nick(const char *name)
633 if (!name[0] || is_dot_or_dotdot(name))
635 return !strchr(name, '/'); /* no slash */
638 struct remote *remote_get(const char *name)
644 name = default_remote_name;
645 ret = make_remote(name, 0);
646 if (valid_remote_nick(name)) {
648 read_remotes_file(ret);
650 read_branches_file(ret);
653 add_url_alias(ret, name);
656 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
657 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
661 int for_each_remote(each_remote_fn fn, void *priv)
665 for (i = 0; i < remotes_nr && !result; i++) {
666 struct remote *r = remotes[i];
670 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
673 r->push = parse_push_refspec(r->push_refspec_nr,
675 result = fn(r, priv);
680 void ref_remove_duplicates(struct ref *ref_map)
684 for (; ref_map; ref_map = ref_map->next) {
685 if (!ref_map->peer_ref)
687 posn = &ref_map->next;
689 if ((*posn)->peer_ref &&
690 !strcmp((*posn)->peer_ref->name,
691 ref_map->peer_ref->name)) {
692 if (strcmp((*posn)->name, ref_map->name))
693 die("%s tracks both %s and %s",
694 ref_map->peer_ref->name,
695 (*posn)->name, ref_map->name);
696 next = (*posn)->next;
697 free((*posn)->peer_ref);
701 posn = &(*posn)->next;
707 int remote_has_url(struct remote *remote, const char *url)
710 for (i = 0; i < remote->url_nr; i++) {
711 if (!strcmp(remote->url[i], url))
717 static int match_name_with_pattern(const char *key, const char *name,
718 const char *value, char **result)
720 const char *kstar = strchr(key, '*');
726 die("Key '%s' of pattern had no '*'", key);
728 ksuffixlen = strlen(kstar + 1);
729 namelen = strlen(name);
730 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
731 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
733 const char *vstar = strchr(value, '*');
737 die("Value '%s' of pattern has no '*'", value);
738 vlen = vstar - value;
739 vsuffixlen = strlen(vstar + 1);
740 *result = xmalloc(vlen + vsuffixlen +
742 klen - ksuffixlen + 1);
743 strncpy(*result, value, vlen);
744 strncpy(*result + vlen,
745 name + klen, namelen - klen - ksuffixlen);
746 strcpy(*result + vlen + namelen - klen - ksuffixlen,
752 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
754 int find_src = refspec->src == NULL;
755 char *needle, **result;
760 return error("find_tracking: need either src or dst");
761 needle = refspec->dst;
762 result = &refspec->src;
764 needle = refspec->src;
765 result = &refspec->dst;
768 for (i = 0; i < remote->fetch_refspec_nr; i++) {
769 struct refspec *fetch = &remote->fetch[i];
770 const char *key = find_src ? fetch->dst : fetch->src;
771 const char *value = find_src ? fetch->src : fetch->dst;
774 if (fetch->pattern) {
775 if (match_name_with_pattern(key, needle, value, result)) {
776 refspec->force = fetch->force;
779 } else if (!strcmp(needle, key)) {
780 *result = xstrdup(value);
781 refspec->force = fetch->force;
788 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
791 size_t len = strlen(name);
792 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
793 memcpy(ref->name, prefix, prefixlen);
794 memcpy(ref->name + prefixlen, name, len);
798 struct ref *alloc_ref(const char *name)
800 return alloc_ref_with_prefix("", 0, name);
803 static struct ref *copy_ref(const struct ref *ref)
809 len = strlen(ref->name);
810 cpy = xmalloc(sizeof(struct ref) + len + 1);
811 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
813 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
814 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
815 cpy->peer_ref = copy_ref(ref->peer_ref);
819 struct ref *copy_ref_list(const struct ref *ref)
821 struct ref *ret = NULL;
822 struct ref **tail = &ret;
824 *tail = copy_ref(ref);
826 tail = &((*tail)->next);
831 static void free_ref(struct ref *ref)
835 free_ref(ref->peer_ref);
836 free(ref->remote_status);
841 void free_refs(struct ref *ref)
851 static int count_refspec_match(const char *pattern,
853 struct ref **matched_ref)
855 int patlen = strlen(pattern);
856 struct ref *matched_weak = NULL;
857 struct ref *matched = NULL;
861 for (weak_match = match = 0; refs; refs = refs->next) {
862 char *name = refs->name;
863 int namelen = strlen(name);
865 if (!refname_match(pattern, name, ref_rev_parse_rules))
868 /* A match is "weak" if it is with refs outside
869 * heads or tags, and did not specify the pattern
870 * in full (e.g. "refs/remotes/origin/master") or at
871 * least from the toplevel (e.g. "remotes/origin/master");
872 * otherwise "git push $URL master" would result in
873 * ambiguity between remotes/origin/master and heads/master
874 * at the remote site.
876 if (namelen != patlen &&
877 patlen != namelen - 5 &&
878 prefixcmp(name, "refs/heads/") &&
879 prefixcmp(name, "refs/tags/")) {
880 /* We want to catch the case where only weak
881 * matches are found and there are multiple
882 * matches, and where more than one strong
883 * matches are found, as ambiguous. One
884 * strong match with zero or more weak matches
885 * are acceptable as a unique match.
896 *matched_ref = matched_weak;
900 *matched_ref = matched;
905 static void tail_link_ref(struct ref *ref, struct ref ***tail)
913 static struct ref *try_explicit_object_name(const char *name)
915 unsigned char sha1[20];
919 ref = alloc_ref("(delete)");
920 hashclr(ref->new_sha1);
923 if (get_sha1(name, sha1))
925 ref = alloc_ref(name);
926 hashcpy(ref->new_sha1, sha1);
930 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
932 struct ref *ret = alloc_ref(name);
933 tail_link_ref(ret, tail);
937 static char *guess_ref(const char *name, struct ref *peer)
939 struct strbuf buf = STRBUF_INIT;
940 unsigned char sha1[20];
942 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
946 if (!prefixcmp(r, "refs/heads/"))
947 strbuf_addstr(&buf, "refs/heads/");
948 else if (!prefixcmp(r, "refs/tags/"))
949 strbuf_addstr(&buf, "refs/tags/");
953 strbuf_addstr(&buf, name);
954 return strbuf_detach(&buf, NULL);
957 static int match_explicit(struct ref *src, struct ref *dst,
958 struct ref ***dst_tail,
961 struct ref *matched_src, *matched_dst;
964 const char *dst_value = rs->dst;
967 if (rs->pattern || rs->matching)
970 matched_src = matched_dst = NULL;
971 switch (count_refspec_match(rs->src, src, &matched_src)) {
976 /* The source could be in the get_sha1() format
977 * not a reference name. :refs/other is a
978 * way to delete 'other' ref at the remote end.
980 matched_src = try_explicit_object_name(rs->src);
982 return error("src refspec %s does not match any.", rs->src);
986 return error("src refspec %s matches more than one.", rs->src);
990 unsigned char sha1[20];
993 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
995 ((flag & REF_ISSYMREF) &&
996 prefixcmp(dst_value, "refs/heads/")))
997 die("%s cannot be resolved to branch.",
1001 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1005 if (!memcmp(dst_value, "refs/", 5))
1006 matched_dst = make_linked_ref(dst_value, dst_tail);
1007 else if((dst_guess = guess_ref(dst_value, matched_src)))
1008 matched_dst = make_linked_ref(dst_guess, dst_tail);
1010 error("unable to push to unqualified destination: %s\n"
1011 "The destination refspec neither matches an "
1012 "existing ref on the remote nor\n"
1013 "begins with refs/, and we are unable to "
1014 "guess a prefix based on the source ref.",
1019 error("dst refspec %s matches more than one.",
1025 if (matched_dst->peer_ref)
1026 return error("dst ref %s receives from more than one src.",
1029 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1030 matched_dst->force = rs->force;
1035 static int match_explicit_refs(struct ref *src, struct ref *dst,
1036 struct ref ***dst_tail, struct refspec *rs,
1040 for (i = errs = 0; i < rs_nr; i++)
1041 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1045 static const struct refspec *check_pattern_match(const struct refspec *rs,
1047 const struct ref *src)
1050 int matching_refs = -1;
1051 for (i = 0; i < rs_nr; i++) {
1052 if (rs[i].matching &&
1053 (matching_refs == -1 || rs[i].force)) {
1058 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1062 if (matching_refs != -1)
1063 return rs + matching_refs;
1069 * Note. This is used only by "push"; refspec matching rules for
1070 * push and fetch are subtly different, so do not try to reuse it
1073 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1074 int nr_refspec, const char **refspec, int flags)
1077 int send_all = flags & MATCH_REFS_ALL;
1078 int send_mirror = flags & MATCH_REFS_MIRROR;
1080 static const char *default_refspec[] = { ":", 0 };
1084 refspec = default_refspec;
1086 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1087 errs = match_explicit_refs(src, dst, dst_tail, rs, nr_refspec);
1089 /* pick the remainder */
1090 for ( ; src; src = src->next) {
1091 struct ref *dst_peer;
1092 const struct refspec *pat = NULL;
1097 pat = check_pattern_match(rs, nr_refspec, src);
1101 if (pat->matching) {
1103 * "matching refs"; traditionally we pushed everything
1104 * including refs outside refs/heads/ hierarchy, but
1105 * that does not make much sense these days.
1107 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1109 dst_name = xstrdup(src->name);
1112 const char *dst_side = pat->dst ? pat->dst : pat->src;
1113 if (!match_name_with_pattern(pat->src, src->name,
1114 dst_side, &dst_name))
1115 die("Didn't think it matches any more");
1117 dst_peer = find_ref_by_name(dst, dst_name);
1119 if (dst_peer->peer_ref)
1120 /* We're already sending something to this ref. */
1124 if (pat->matching && !(send_all || send_mirror))
1126 * Remote doesn't have it, and we have no
1127 * explicit pattern, and we don't have
1128 * --all nor --mirror.
1132 /* Create a new one and link it */
1133 dst_peer = make_linked_ref(dst_name, dst_tail);
1134 hashcpy(dst_peer->new_sha1, src->new_sha1);
1136 dst_peer->peer_ref = copy_ref(src);
1137 dst_peer->force = pat->force;
1146 struct branch *branch_get(const char *name)
1151 if (!name || !*name || !strcmp(name, "HEAD"))
1152 ret = current_branch;
1154 ret = make_branch(name, 0);
1155 if (ret && ret->remote_name) {
1156 ret->remote = remote_get(ret->remote_name);
1157 if (ret->merge_nr) {
1159 ret->merge = xcalloc(sizeof(*ret->merge),
1161 for (i = 0; i < ret->merge_nr; i++) {
1162 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1163 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1164 remote_find_tracking(ret->remote,
1172 int branch_has_merge_config(struct branch *branch)
1174 return branch && !!branch->merge;
1177 int branch_merge_matches(struct branch *branch,
1179 const char *refname)
1181 if (!branch || i < 0 || i >= branch->merge_nr)
1183 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1186 static struct ref *get_expanded_map(const struct ref *remote_refs,
1187 const struct refspec *refspec)
1189 const struct ref *ref;
1190 struct ref *ret = NULL;
1191 struct ref **tail = &ret;
1195 for (ref = remote_refs; ref; ref = ref->next) {
1196 if (strchr(ref->name, '^'))
1197 continue; /* a dereference item */
1198 if (match_name_with_pattern(refspec->src, ref->name,
1199 refspec->dst, &expn_name)) {
1200 struct ref *cpy = copy_ref(ref);
1202 cpy->peer_ref = alloc_ref(expn_name);
1205 cpy->peer_ref->force = 1;
1214 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1216 const struct ref *ref;
1217 for (ref = refs; ref; ref = ref->next) {
1218 if (refname_match(name, ref->name, ref_fetch_rules))
1224 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1226 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1231 return copy_ref(ref);
1234 static struct ref *get_local_ref(const char *name)
1239 if (!prefixcmp(name, "refs/"))
1240 return alloc_ref(name);
1242 if (!prefixcmp(name, "heads/") ||
1243 !prefixcmp(name, "tags/") ||
1244 !prefixcmp(name, "remotes/"))
1245 return alloc_ref_with_prefix("refs/", 5, name);
1247 return alloc_ref_with_prefix("refs/heads/", 11, name);
1250 int get_fetch_map(const struct ref *remote_refs,
1251 const struct refspec *refspec,
1255 struct ref *ref_map, **rmp;
1257 if (refspec->pattern) {
1258 ref_map = get_expanded_map(remote_refs, refspec);
1260 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1262 ref_map = get_remote_ref(remote_refs, name);
1263 if (!missing_ok && !ref_map)
1264 die("Couldn't find remote ref %s", name);
1266 ref_map->peer_ref = get_local_ref(refspec->dst);
1267 if (ref_map->peer_ref && refspec->force)
1268 ref_map->peer_ref->force = 1;
1272 for (rmp = &ref_map; *rmp; ) {
1273 if ((*rmp)->peer_ref) {
1274 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1275 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1276 struct ref *ignore = *rmp;
1277 error("* Ignoring funny ref '%s' locally",
1278 (*rmp)->peer_ref->name);
1279 *rmp = (*rmp)->next;
1280 free(ignore->peer_ref);
1285 rmp = &((*rmp)->next);
1289 tail_link_ref(ref_map, tail);
1294 int resolve_remote_symref(struct ref *ref, struct ref *list)
1298 for (; list; list = list->next)
1299 if (!strcmp(ref->symref, list->name)) {
1300 hashcpy(ref->old_sha1, list->old_sha1);
1306 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1309 struct commit_list *temp = list;
1310 temp->item->object.flags &= ~mark;
1316 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1319 struct commit *old, *new;
1320 struct commit_list *list, *used;
1323 /* Both new and old must be commit-ish and new is descendant of
1324 * old. Otherwise we require --force.
1326 o = deref_tag(parse_object(old_sha1), NULL, 0);
1327 if (!o || o->type != OBJ_COMMIT)
1329 old = (struct commit *) o;
1331 o = deref_tag(parse_object(new_sha1), NULL, 0);
1332 if (!o || o->type != OBJ_COMMIT)
1334 new = (struct commit *) o;
1336 if (parse_commit(new) < 0)
1340 commit_list_insert(new, &list);
1342 new = pop_most_recent_commit(&list, TMP_MARK);
1343 commit_list_insert(new, &used);
1349 unmark_and_free(list, TMP_MARK);
1350 unmark_and_free(used, TMP_MARK);
1355 * Return true if there is anything to report, otherwise false.
1357 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1359 unsigned char sha1[20];
1360 struct commit *ours, *theirs;
1362 struct rev_info revs;
1363 const char *rev_argv[10], *base;
1367 * Nothing to report unless we are marked to build on top of
1371 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1375 * If what we used to build on no longer exists, there is
1376 * nothing to report.
1378 base = branch->merge[0]->dst;
1379 if (!resolve_ref(base, sha1, 1, NULL))
1381 theirs = lookup_commit(sha1);
1385 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1387 ours = lookup_commit(sha1);
1391 /* are we the same? */
1395 /* Run "rev-list --left-right ours...theirs" internally... */
1397 rev_argv[rev_argc++] = NULL;
1398 rev_argv[rev_argc++] = "--left-right";
1399 rev_argv[rev_argc++] = symmetric;
1400 rev_argv[rev_argc++] = "--";
1401 rev_argv[rev_argc] = NULL;
1403 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1404 strcpy(symmetric + 40, "...");
1405 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1407 init_revisions(&revs, NULL);
1408 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1409 prepare_revision_walk(&revs);
1411 /* ... and count the commits on each side. */
1415 struct commit *c = get_revision(&revs);
1418 if (c->object.flags & SYMMETRIC_LEFT)
1424 /* clear object flags smudged by the above traversal */
1425 clear_commit_marks(ours, ALL_REV_FLAGS);
1426 clear_commit_marks(theirs, ALL_REV_FLAGS);
1431 * Return true when there is anything to report, otherwise false.
1433 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1435 int num_ours, num_theirs;
1438 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1441 base = branch->merge[0]->dst;
1442 if (!prefixcmp(base, "refs/remotes/")) {
1443 base += strlen("refs/remotes/");
1446 strbuf_addf(sb, "Your branch is ahead of '%s' "
1447 "by %d commit%s.\n",
1448 base, num_ours, (num_ours == 1) ? "" : "s");
1450 strbuf_addf(sb, "Your branch is behind '%s' "
1452 "and can be fast-forwarded.\n",
1453 base, num_theirs, (num_theirs == 1) ? "" : "s");
1455 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1456 "and have %d and %d different commit(s) each, "
1458 base, num_ours, num_theirs);
1462 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1464 struct ref ***local_tail = cb_data;
1468 /* we already know it starts with refs/ to get here */
1469 if (check_ref_format(refname + 5))
1472 len = strlen(refname) + 1;
1473 ref = xcalloc(1, sizeof(*ref) + len);
1474 hashcpy(ref->new_sha1, sha1);
1475 memcpy(ref->name, refname, len);
1477 *local_tail = &ref->next;
1481 struct ref *get_local_heads(void)
1483 struct ref *local_refs, **local_tail = &local_refs;
1484 for_each_ref(one_local_ref, &local_tail);
1488 struct ref *guess_remote_head(const struct ref *head,
1489 const struct ref *refs,
1492 const struct ref *r;
1493 struct ref *list = NULL;
1494 struct ref **tail = &list;
1500 * Some transports support directly peeking at
1501 * where HEAD points; if that is the case, then
1502 * we don't have to guess.
1505 return copy_ref(find_ref_by_name(refs, head->symref));
1507 /* If refs/heads/master could be right, it is. */
1509 r = find_ref_by_name(refs, "refs/heads/master");
1510 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1514 /* Look for another ref that points there */
1515 for (r = refs; r; r = r->next) {
1516 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1517 *tail = copy_ref(r);
1518 tail = &((*tail)->next);