9 #include "string-list.h"
10 #include "mergesort.h"
12 enum map_direction { FROM_SRC, FROM_DST };
14 static struct refspec s_tag_refspec = {
23 const struct refspec *tag_refspec = &s_tag_refspec;
25 struct counted_string {
32 struct counted_string *instead_of;
37 struct rewrite **rewrite;
42 static struct remote **remotes;
43 static int remotes_alloc;
44 static int remotes_nr;
46 static struct branch **branches;
47 static int branches_alloc;
48 static int branches_nr;
50 static struct branch *current_branch;
51 static const char *default_remote_name;
52 static const char *pushremote_name;
53 static int explicit_default_remote_name;
55 static struct rewrites rewrites;
56 static struct rewrites rewrites_push;
58 #define BUF_SIZE (2048)
59 static char buffer[BUF_SIZE];
61 static int valid_remote(const struct remote *remote)
63 return (!!remote->url) || (!!remote->foreign_vcs);
66 static const char *alias_url(const char *url, struct rewrites *r)
70 struct counted_string *longest;
75 for (i = 0; i < r->rewrite_nr; i++) {
78 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
79 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
81 longest->len < r->rewrite[i]->instead_of[j].len)) {
82 longest = &(r->rewrite[i]->instead_of[j]);
90 ret = xmalloc(r->rewrite[longest_i]->baselen +
91 (strlen(url) - longest->len) + 1);
92 strcpy(ret, r->rewrite[longest_i]->base);
93 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
97 static void add_push_refspec(struct remote *remote, const char *ref)
99 ALLOC_GROW(remote->push_refspec,
100 remote->push_refspec_nr + 1,
101 remote->push_refspec_alloc);
102 remote->push_refspec[remote->push_refspec_nr++] = ref;
105 static void add_fetch_refspec(struct remote *remote, const char *ref)
107 ALLOC_GROW(remote->fetch_refspec,
108 remote->fetch_refspec_nr + 1,
109 remote->fetch_refspec_alloc);
110 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
113 static void add_url(struct remote *remote, const char *url)
115 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
116 remote->url[remote->url_nr++] = url;
119 static void add_pushurl(struct remote *remote, const char *pushurl)
121 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
122 remote->pushurl[remote->pushurl_nr++] = pushurl;
125 static void add_pushurl_alias(struct remote *remote, const char *url)
127 const char *pushurl = alias_url(url, &rewrites_push);
129 add_pushurl(remote, pushurl);
132 static void add_url_alias(struct remote *remote, const char *url)
134 add_url(remote, alias_url(url, &rewrites));
135 add_pushurl_alias(remote, url);
138 static struct remote *make_remote(const char *name, int len)
143 for (i = 0; i < remotes_nr; i++) {
144 if (len ? (!strncmp(name, remotes[i]->name, len) &&
145 !remotes[i]->name[len]) :
146 !strcmp(name, remotes[i]->name))
150 ret = xcalloc(1, sizeof(struct remote));
151 ret->prune = -1; /* unspecified */
152 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
153 remotes[remotes_nr++] = ret;
155 ret->name = xstrndup(name, len);
157 ret->name = xstrdup(name);
161 static void add_merge(struct branch *branch, const char *name)
163 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
164 branch->merge_alloc);
165 branch->merge_name[branch->merge_nr++] = name;
168 static struct branch *make_branch(const char *name, int len)
174 for (i = 0; i < branches_nr; i++) {
175 if (len ? (!strncmp(name, branches[i]->name, len) &&
176 !branches[i]->name[len]) :
177 !strcmp(name, branches[i]->name))
181 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
182 ret = xcalloc(1, sizeof(struct branch));
183 branches[branches_nr++] = ret;
185 ret->name = xstrndup(name, len);
187 ret->name = xstrdup(name);
188 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
189 strcpy(refname, "refs/heads/");
190 strcpy(refname + strlen("refs/heads/"), ret->name);
191 ret->refname = refname;
196 static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
201 for (i = 0; i < r->rewrite_nr; i++) {
203 ? (len == r->rewrite[i]->baselen &&
204 !strncmp(base, r->rewrite[i]->base, len))
205 : !strcmp(base, r->rewrite[i]->base))
206 return r->rewrite[i];
209 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
210 ret = xcalloc(1, sizeof(struct rewrite));
211 r->rewrite[r->rewrite_nr++] = ret;
213 ret->base = xstrndup(base, len);
217 ret->base = xstrdup(base);
218 ret->baselen = strlen(base);
223 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
225 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
226 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
227 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
228 rewrite->instead_of_nr++;
231 static void read_remotes_file(struct remote *remote)
233 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
237 remote->origin = REMOTE_REMOTES;
238 while (fgets(buffer, BUF_SIZE, f)) {
242 if (!prefixcmp(buffer, "URL:")) {
245 } else if (!prefixcmp(buffer, "Push:")) {
248 } else if (!prefixcmp(buffer, "Pull:")) {
260 while (isspace(p[-1]))
263 switch (value_list) {
265 add_url_alias(remote, xstrdup(s));
268 add_push_refspec(remote, xstrdup(s));
271 add_fetch_refspec(remote, xstrdup(s));
278 static void read_branches_file(struct remote *remote)
280 const char *slash = strchr(remote->name, '/');
282 struct strbuf branch = STRBUF_INIT;
283 int n = slash ? slash - remote->name : 1000;
284 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
290 s = fgets(buffer, BUF_SIZE, f);
298 remote->origin = REMOTE_BRANCHES;
300 while (isspace(p[-1]))
304 len += strlen(slash);
305 p = xmalloc(len + 1);
311 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
312 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
313 * the partial URL obtained from the branches file plus
314 * "/netdev-2.6" and does not store it in any tracking ref.
315 * #branch specifier in the file is ignored.
317 * Otherwise, the branches file would have URL and optionally
318 * #branch specified. The "master" (or specified) branch is
319 * fetched and stored in the local branch of the same name.
321 frag = strchr(p, '#');
324 strbuf_addf(&branch, "refs/heads/%s", frag);
326 strbuf_addstr(&branch, "refs/heads/master");
328 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
330 strbuf_reset(&branch);
331 strbuf_addstr(&branch, "HEAD:");
333 add_url_alias(remote, p);
334 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
336 * Cogito compatible push: push current HEAD to remote #branch
337 * (master if missing)
339 strbuf_init(&branch, 0);
340 strbuf_addstr(&branch, "HEAD");
342 strbuf_addf(&branch, ":refs/heads/%s", frag);
344 strbuf_addstr(&branch, ":refs/heads/master");
345 add_push_refspec(remote, strbuf_detach(&branch, NULL));
346 remote->fetch_tags = 1; /* always auto-follow */
349 static int handle_config(const char *key, const char *value, void *cb)
353 struct remote *remote;
354 struct branch *branch;
355 if (!prefixcmp(key, "branch.")) {
357 subkey = strrchr(name, '.');
360 branch = make_branch(name, subkey - name);
361 if (!strcmp(subkey, ".remote")) {
362 if (git_config_string(&branch->remote_name, key, value))
364 if (branch == current_branch) {
365 default_remote_name = branch->remote_name;
366 explicit_default_remote_name = 1;
368 } else if (!strcmp(subkey, ".pushremote")) {
369 if (branch == current_branch)
370 if (git_config_string(&pushremote_name, key, value))
372 } else if (!strcmp(subkey, ".merge")) {
374 return config_error_nonbool(key);
375 add_merge(branch, xstrdup(value));
379 if (!prefixcmp(key, "url.")) {
380 struct rewrite *rewrite;
382 subkey = strrchr(name, '.');
385 if (!strcmp(subkey, ".insteadof")) {
386 rewrite = make_rewrite(&rewrites, name, subkey - name);
388 return config_error_nonbool(key);
389 add_instead_of(rewrite, xstrdup(value));
390 } else if (!strcmp(subkey, ".pushinsteadof")) {
391 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
393 return config_error_nonbool(key);
394 add_instead_of(rewrite, xstrdup(value));
398 if (prefixcmp(key, "remote."))
402 /* Handle remote.* variables */
403 if (!strcmp(name, "pushdefault"))
404 return git_config_string(&pushremote_name, key, value);
406 /* Handle remote.<name>.* variables */
408 warning("Config remote shorthand cannot begin with '/': %s",
412 subkey = strrchr(name, '.');
415 remote = make_remote(name, subkey - name);
416 remote->origin = REMOTE_CONFIG;
417 if (!strcmp(subkey, ".mirror"))
418 remote->mirror = git_config_bool(key, value);
419 else if (!strcmp(subkey, ".skipdefaultupdate"))
420 remote->skip_default_update = git_config_bool(key, value);
421 else if (!strcmp(subkey, ".skipfetchall"))
422 remote->skip_default_update = git_config_bool(key, value);
423 else if (!strcmp(subkey, ".prune"))
424 remote->prune = git_config_bool(key, value);
425 else if (!strcmp(subkey, ".url")) {
427 if (git_config_string(&v, key, value))
430 } else if (!strcmp(subkey, ".pushurl")) {
432 if (git_config_string(&v, key, value))
434 add_pushurl(remote, v);
435 } else if (!strcmp(subkey, ".push")) {
437 if (git_config_string(&v, key, value))
439 add_push_refspec(remote, v);
440 } else if (!strcmp(subkey, ".fetch")) {
442 if (git_config_string(&v, key, value))
444 add_fetch_refspec(remote, v);
445 } else if (!strcmp(subkey, ".receivepack")) {
447 if (git_config_string(&v, key, value))
449 if (!remote->receivepack)
450 remote->receivepack = v;
452 error("more than one receivepack given, using the first");
453 } else if (!strcmp(subkey, ".uploadpack")) {
455 if (git_config_string(&v, key, value))
457 if (!remote->uploadpack)
458 remote->uploadpack = v;
460 error("more than one uploadpack given, using the first");
461 } else if (!strcmp(subkey, ".tagopt")) {
462 if (!strcmp(value, "--no-tags"))
463 remote->fetch_tags = -1;
464 else if (!strcmp(value, "--tags"))
465 remote->fetch_tags = 2;
466 } else if (!strcmp(subkey, ".proxy")) {
467 return git_config_string((const char **)&remote->http_proxy,
469 } else if (!strcmp(subkey, ".vcs")) {
470 return git_config_string(&remote->foreign_vcs, key, value);
475 static void alias_all_urls(void)
478 for (i = 0; i < remotes_nr; i++) {
479 int add_pushurl_aliases;
482 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
483 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
485 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
486 for (j = 0; j < remotes[i]->url_nr; j++) {
487 if (add_pushurl_aliases)
488 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
489 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
494 static void read_config(void)
496 unsigned char sha1[20];
497 const char *head_ref;
499 if (default_remote_name) /* did this already */
501 default_remote_name = xstrdup("origin");
502 current_branch = NULL;
503 head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
504 if (head_ref && (flag & REF_ISSYMREF) &&
505 !prefixcmp(head_ref, "refs/heads/")) {
507 make_branch(head_ref + strlen("refs/heads/"), 0);
509 git_config(handle_config, NULL);
514 * This function frees a refspec array.
515 * Warning: code paths should be checked to ensure that the src
516 * and dst pointers are always freeable pointers as well
517 * as the refspec pointer itself.
519 static void free_refspecs(struct refspec *refspec, int nr_refspec)
526 for (i = 0; i < nr_refspec; i++) {
527 free(refspec[i].src);
528 free(refspec[i].dst);
533 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
536 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
538 for (i = 0; i < nr_refspec; i++) {
541 const char *lhs, *rhs;
552 rhs = strrchr(lhs, ':');
555 * Before going on, special case ":" (or "+:") as a refspec
556 * for pushing matching refs.
558 if (!fetch && rhs == lhs && rhs[1] == '\0') {
564 size_t rlen = strlen(++rhs);
565 is_glob = (1 <= rlen && strchr(rhs, '*'));
566 rs[i].dst = xstrndup(rhs, rlen);
569 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
570 if (1 <= llen && memchr(lhs, '*', llen)) {
571 if ((rhs && !is_glob) || (!rhs && fetch))
574 } else if (rhs && is_glob) {
578 rs[i].pattern = is_glob;
579 rs[i].src = xstrndup(lhs, llen);
580 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
583 unsigned char unused[40];
587 ; /* empty is ok; it means "HEAD" */
588 else if (llen == 40 && !get_sha1_hex(rs[i].src, unused))
589 rs[i].exact_sha1 = 1; /* ok */
590 else if (!check_refname_format(rs[i].src, flags))
591 ; /* valid looking ref is ok */
596 ; /* missing is ok; it is the same as empty */
597 else if (!*rs[i].dst)
598 ; /* empty is ok; it means "do not store" */
599 else if (!check_refname_format(rs[i].dst, flags))
600 ; /* valid looking ref is ok */
606 * - empty is allowed; it means delete.
607 * - when wildcarded, it must be a valid looking ref.
608 * - otherwise, it must be an extended SHA-1, but
609 * there is no existing way to validate this.
614 if (check_refname_format(rs[i].src, flags))
618 ; /* anything goes, for now */
621 * - missing is allowed, but LHS then must be a
623 * - empty is not allowed.
624 * - otherwise it must be a valid looking ref.
627 if (check_refname_format(rs[i].src, flags))
629 } else if (!*rs[i].dst) {
632 if (check_refname_format(rs[i].dst, flags))
642 * nr_refspec must be greater than zero and i must be valid
643 * since it is only possible to reach this point from within
644 * the for loop above.
646 free_refspecs(rs, i+1);
649 die("Invalid refspec '%s'", refspec[i]);
652 int valid_fetch_refspec(const char *fetch_refspec_str)
654 struct refspec *refspec;
656 refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
657 free_refspecs(refspec, 1);
661 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
663 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
666 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
668 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
671 void free_refspec(int nr_refspec, struct refspec *refspec)
674 for (i = 0; i < nr_refspec; i++) {
675 free(refspec[i].src);
676 free(refspec[i].dst);
681 static int valid_remote_nick(const char *name)
683 if (!name[0] || is_dot_or_dotdot(name))
685 return !strchr(name, '/'); /* no slash */
688 static struct remote *remote_get_1(const char *name, const char *pushremote_name)
696 if (pushremote_name) {
697 name = pushremote_name;
700 name = default_remote_name;
701 name_given = explicit_default_remote_name;
705 ret = make_remote(name, 0);
706 if (valid_remote_nick(name)) {
707 if (!valid_remote(ret))
708 read_remotes_file(ret);
709 if (!valid_remote(ret))
710 read_branches_file(ret);
712 if (name_given && !valid_remote(ret))
713 add_url_alias(ret, name);
714 if (!valid_remote(ret))
716 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
717 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
721 struct remote *remote_get(const char *name)
724 return remote_get_1(name, NULL);
727 struct remote *pushremote_get(const char *name)
730 return remote_get_1(name, pushremote_name);
733 int remote_is_configured(const char *name)
738 for (i = 0; i < remotes_nr; i++)
739 if (!strcmp(name, remotes[i]->name))
744 int for_each_remote(each_remote_fn fn, void *priv)
748 for (i = 0; i < remotes_nr && !result; i++) {
749 struct remote *r = remotes[i];
753 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
756 r->push = parse_push_refspec(r->push_refspec_nr,
758 result = fn(r, priv);
763 void ref_remove_duplicates(struct ref *ref_map)
765 struct string_list refs = STRING_LIST_INIT_NODUP;
766 struct string_list_item *item = NULL;
767 struct ref *prev = NULL, *next = NULL;
768 for (; ref_map; prev = ref_map, ref_map = next) {
769 next = ref_map->next;
770 if (!ref_map->peer_ref)
773 item = string_list_lookup(&refs, ref_map->peer_ref->name);
775 if (strcmp(((struct ref *)item->util)->name,
777 die("%s tracks both %s and %s",
778 ref_map->peer_ref->name,
779 ((struct ref *)item->util)->name,
781 prev->next = ref_map->next;
782 free(ref_map->peer_ref);
784 ref_map = prev; /* skip this; we freed it */
788 item = string_list_insert(&refs, ref_map->peer_ref->name);
789 item->util = ref_map;
791 string_list_clear(&refs, 0);
794 int remote_has_url(struct remote *remote, const char *url)
797 for (i = 0; i < remote->url_nr; i++) {
798 if (!strcmp(remote->url[i], url))
804 static int match_name_with_pattern(const char *key, const char *name,
805 const char *value, char **result)
807 const char *kstar = strchr(key, '*');
813 die("Key '%s' of pattern had no '*'", key);
815 ksuffixlen = strlen(kstar + 1);
816 namelen = strlen(name);
817 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
818 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
820 const char *vstar = strchr(value, '*');
824 die("Value '%s' of pattern has no '*'", value);
825 vlen = vstar - value;
826 vsuffixlen = strlen(vstar + 1);
827 *result = xmalloc(vlen + vsuffixlen +
829 klen - ksuffixlen + 1);
830 strncpy(*result, value, vlen);
831 strncpy(*result + vlen,
832 name + klen, namelen - klen - ksuffixlen);
833 strcpy(*result + vlen + namelen - klen - ksuffixlen,
839 static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
842 int find_src = !query->src;
844 if (find_src && !query->dst)
845 return error("query_refspecs: need either src or dst");
847 for (i = 0; i < ref_count; i++) {
848 struct refspec *refspec = &refs[i];
849 const char *key = find_src ? refspec->dst : refspec->src;
850 const char *value = find_src ? refspec->src : refspec->dst;
851 const char *needle = find_src ? query->dst : query->src;
852 char **result = find_src ? &query->src : &query->dst;
856 if (refspec->pattern) {
857 if (match_name_with_pattern(key, needle, value, result)) {
858 query->force = refspec->force;
861 } else if (!strcmp(needle, key)) {
862 *result = xstrdup(value);
863 query->force = refspec->force;
870 char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
873 struct refspec query;
875 memset(&query, 0, sizeof(struct refspec));
876 query.src = (char *)name;
878 if (query_refspecs(refspecs, nr_refspec, &query))
884 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
886 return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
889 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
892 size_t len = strlen(name);
893 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
894 memcpy(ref->name, prefix, prefixlen);
895 memcpy(ref->name + prefixlen, name, len);
899 struct ref *alloc_ref(const char *name)
901 return alloc_ref_with_prefix("", 0, name);
904 struct ref *copy_ref(const struct ref *ref)
910 len = strlen(ref->name);
911 cpy = xmalloc(sizeof(struct ref) + len + 1);
912 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
914 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
915 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
916 cpy->peer_ref = copy_ref(ref->peer_ref);
920 struct ref *copy_ref_list(const struct ref *ref)
922 struct ref *ret = NULL;
923 struct ref **tail = &ret;
925 *tail = copy_ref(ref);
927 tail = &((*tail)->next);
932 static void free_ref(struct ref *ref)
936 free_ref(ref->peer_ref);
937 free(ref->remote_status);
942 void free_refs(struct ref *ref)
952 int ref_compare_name(const void *va, const void *vb)
954 const struct ref *a = va, *b = vb;
955 return strcmp(a->name, b->name);
958 static void *ref_list_get_next(const void *a)
960 return ((const struct ref *)a)->next;
963 static void ref_list_set_next(void *a, void *next)
965 ((struct ref *)a)->next = next;
968 void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
970 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
973 static int count_refspec_match(const char *pattern,
975 struct ref **matched_ref)
977 int patlen = strlen(pattern);
978 struct ref *matched_weak = NULL;
979 struct ref *matched = NULL;
983 for (weak_match = match = 0; refs; refs = refs->next) {
984 char *name = refs->name;
985 int namelen = strlen(name);
987 if (!refname_match(pattern, name, ref_rev_parse_rules))
990 /* A match is "weak" if it is with refs outside
991 * heads or tags, and did not specify the pattern
992 * in full (e.g. "refs/remotes/origin/master") or at
993 * least from the toplevel (e.g. "remotes/origin/master");
994 * otherwise "git push $URL master" would result in
995 * ambiguity between remotes/origin/master and heads/master
996 * at the remote site.
998 if (namelen != patlen &&
999 patlen != namelen - 5 &&
1000 prefixcmp(name, "refs/heads/") &&
1001 prefixcmp(name, "refs/tags/")) {
1002 /* We want to catch the case where only weak
1003 * matches are found and there are multiple
1004 * matches, and where more than one strong
1005 * matches are found, as ambiguous. One
1006 * strong match with zero or more weak matches
1007 * are acceptable as a unique match.
1009 matched_weak = refs;
1018 *matched_ref = matched_weak;
1022 *matched_ref = matched;
1027 static void tail_link_ref(struct ref *ref, struct ref ***tail)
1035 static struct ref *alloc_delete_ref(void)
1037 struct ref *ref = alloc_ref("(delete)");
1038 hashclr(ref->new_sha1);
1042 static struct ref *try_explicit_object_name(const char *name)
1044 unsigned char sha1[20];
1048 return alloc_delete_ref();
1049 if (get_sha1(name, sha1))
1051 ref = alloc_ref(name);
1052 hashcpy(ref->new_sha1, sha1);
1056 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
1058 struct ref *ret = alloc_ref(name);
1059 tail_link_ref(ret, tail);
1063 static char *guess_ref(const char *name, struct ref *peer)
1065 struct strbuf buf = STRBUF_INIT;
1066 unsigned char sha1[20];
1068 const char *r = resolve_ref_unsafe(peer->name, sha1, 1, NULL);
1072 if (!prefixcmp(r, "refs/heads/"))
1073 strbuf_addstr(&buf, "refs/heads/");
1074 else if (!prefixcmp(r, "refs/tags/"))
1075 strbuf_addstr(&buf, "refs/tags/");
1079 strbuf_addstr(&buf, name);
1080 return strbuf_detach(&buf, NULL);
1083 static int match_explicit(struct ref *src, struct ref *dst,
1084 struct ref ***dst_tail,
1087 struct ref *matched_src, *matched_dst;
1090 const char *dst_value = rs->dst;
1093 if (rs->pattern || rs->matching)
1096 matched_src = matched_dst = NULL;
1097 switch (count_refspec_match(rs->src, src, &matched_src)) {
1102 /* The source could be in the get_sha1() format
1103 * not a reference name. :refs/other is a
1104 * way to delete 'other' ref at the remote end.
1106 matched_src = try_explicit_object_name(rs->src);
1108 return error("src refspec %s does not match any.", rs->src);
1112 return error("src refspec %s matches more than one.", rs->src);
1116 unsigned char sha1[20];
1119 dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
1121 ((flag & REF_ISSYMREF) &&
1122 prefixcmp(dst_value, "refs/heads/")))
1123 die("%s cannot be resolved to branch.",
1127 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1131 if (!memcmp(dst_value, "refs/", 5))
1132 matched_dst = make_linked_ref(dst_value, dst_tail);
1133 else if (is_null_sha1(matched_src->new_sha1))
1134 error("unable to delete '%s': remote ref does not exist",
1136 else if ((dst_guess = guess_ref(dst_value, matched_src)))
1137 matched_dst = make_linked_ref(dst_guess, dst_tail);
1139 error("unable to push to unqualified destination: %s\n"
1140 "The destination refspec neither matches an "
1141 "existing ref on the remote nor\n"
1142 "begins with refs/, and we are unable to "
1143 "guess a prefix based on the source ref.",
1148 error("dst refspec %s matches more than one.",
1154 if (matched_dst->peer_ref)
1155 return error("dst ref %s receives from more than one src.",
1158 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1159 matched_dst->force = rs->force;
1164 static int match_explicit_refs(struct ref *src, struct ref *dst,
1165 struct ref ***dst_tail, struct refspec *rs,
1169 for (i = errs = 0; i < rs_nr; i++)
1170 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1174 static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref *ref,
1175 int send_mirror, int direction, const struct refspec **ret_pat)
1177 const struct refspec *pat;
1180 int matching_refs = -1;
1181 for (i = 0; i < rs_nr; i++) {
1182 if (rs[i].matching &&
1183 (matching_refs == -1 || rs[i].force)) {
1188 if (rs[i].pattern) {
1189 const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src;
1191 if (direction == FROM_SRC)
1192 match = match_name_with_pattern(rs[i].src, ref->name, dst_side, &name);
1194 match = match_name_with_pattern(dst_side, ref->name, rs[i].src, &name);
1201 if (matching_refs == -1)
1204 pat = rs + matching_refs;
1205 if (pat->matching) {
1207 * "matching refs"; traditionally we pushed everything
1208 * including refs outside refs/heads/ hierarchy, but
1209 * that does not make much sense these days.
1211 if (!send_mirror && prefixcmp(ref->name, "refs/heads/"))
1213 name = xstrdup(ref->name);
1220 static struct ref **tail_ref(struct ref **head)
1222 struct ref **tail = head;
1224 tail = &((*tail)->next);
1229 struct commit **tip;
1233 static void add_to_tips(struct tips *tips, const unsigned char *sha1)
1235 struct commit *commit;
1237 if (is_null_sha1(sha1))
1239 commit = lookup_commit_reference_gently(sha1, 1);
1240 if (!commit || (commit->object.flags & TMP_MARK))
1242 commit->object.flags |= TMP_MARK;
1243 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1244 tips->tip[tips->nr++] = commit;
1247 static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1249 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1250 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1251 struct string_list_item *item;
1253 struct tips sent_tips;
1256 * Collect everything we know they would have at the end of
1257 * this push, and collect all tags they have.
1259 memset(&sent_tips, 0, sizeof(sent_tips));
1260 for (ref = *dst; ref; ref = ref->next) {
1261 if (ref->peer_ref &&
1262 !is_null_sha1(ref->peer_ref->new_sha1))
1263 add_to_tips(&sent_tips, ref->peer_ref->new_sha1);
1265 add_to_tips(&sent_tips, ref->old_sha1);
1266 if (!prefixcmp(ref->name, "refs/tags/"))
1267 string_list_append(&dst_tag, ref->name);
1269 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1271 sort_string_list(&dst_tag);
1273 /* Collect tags they do not have. */
1274 for (ref = src; ref; ref = ref->next) {
1275 if (prefixcmp(ref->name, "refs/tags/"))
1276 continue; /* not a tag */
1277 if (string_list_has_string(&dst_tag, ref->name))
1278 continue; /* they already have it */
1279 if (sha1_object_info(ref->new_sha1, NULL) != OBJ_TAG)
1280 continue; /* be conservative */
1281 item = string_list_append(&src_tag, ref->name);
1284 string_list_clear(&dst_tag, 0);
1287 * At this point, src_tag lists tags that are missing from
1288 * dst, and sent_tips lists the tips we are pushing or those
1289 * that we know they already have. An element in the src_tag
1290 * that is an ancestor of any of the sent_tips needs to be
1291 * sent to the other side.
1294 for_each_string_list_item(item, &src_tag) {
1295 struct ref *ref = item->util;
1296 struct ref *dst_ref;
1297 struct commit *commit;
1299 if (is_null_sha1(ref->new_sha1))
1301 commit = lookup_commit_reference_gently(ref->new_sha1, 1);
1303 /* not pushing a commit, which is not an error */
1307 * Is this tag, which they do not have, reachable from
1308 * any of the commits we are sending?
1310 if (!in_merge_bases_many(commit, sent_tips.nr, sent_tips.tip))
1314 dst_ref = make_linked_ref(ref->name, dst_tail);
1315 hashcpy(dst_ref->new_sha1, ref->new_sha1);
1316 dst_ref->peer_ref = copy_ref(ref);
1319 string_list_clear(&src_tag, 0);
1320 free(sent_tips.tip);
1324 * Given the set of refs the local repository has, the set of refs the
1325 * remote repository has, and the refspec used for push, determine
1326 * what remote refs we will update and with what value by setting
1327 * peer_ref (which object is being pushed) and force (if the push is
1328 * forced) in elements of "dst". The function may add new elements to
1329 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1331 int match_push_refs(struct ref *src, struct ref **dst,
1332 int nr_refspec, const char **refspec, int flags)
1335 int send_all = flags & MATCH_REFS_ALL;
1336 int send_mirror = flags & MATCH_REFS_MIRROR;
1337 int send_prune = flags & MATCH_REFS_PRUNE;
1339 static const char *default_refspec[] = { ":", NULL };
1340 struct ref *ref, **dst_tail = tail_ref(dst);
1344 refspec = default_refspec;
1346 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1347 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
1349 /* pick the remainder */
1350 for (ref = src; ref; ref = ref->next) {
1351 struct ref *dst_peer;
1352 const struct refspec *pat = NULL;
1355 dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_SRC, &pat);
1359 dst_peer = find_ref_by_name(*dst, dst_name);
1361 if (dst_peer->peer_ref)
1362 /* We're already sending something to this ref. */
1365 if (pat->matching && !(send_all || send_mirror))
1367 * Remote doesn't have it, and we have no
1368 * explicit pattern, and we don't have
1369 * --all nor --mirror.
1373 /* Create a new one and link it */
1374 dst_peer = make_linked_ref(dst_name, &dst_tail);
1375 hashcpy(dst_peer->new_sha1, ref->new_sha1);
1377 dst_peer->peer_ref = copy_ref(ref);
1378 dst_peer->force = pat->force;
1383 if (flags & MATCH_REFS_FOLLOW_TAGS)
1384 add_missing_tags(src, dst, &dst_tail);
1387 /* check for missing refs on the remote */
1388 for (ref = *dst; ref; ref = ref->next) {
1392 /* We're already sending something to this ref. */
1395 src_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_DST, NULL);
1397 if (!find_ref_by_name(src, src_name))
1398 ref->peer_ref = alloc_delete_ref();
1408 void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1413 for (ref = remote_refs; ref; ref = ref->next) {
1414 int force_ref_update = ref->force || force_update;
1417 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1418 else if (!send_mirror)
1421 ref->deletion = is_null_sha1(ref->new_sha1);
1422 if (!ref->deletion &&
1423 !hashcmp(ref->old_sha1, ref->new_sha1)) {
1424 ref->status = REF_STATUS_UPTODATE;
1429 * Decide whether an individual refspec A:B can be
1430 * pushed. The push will succeed if any of the
1431 * following are true:
1433 * (1) the remote reference B does not exist
1435 * (2) the remote reference B is being removed (i.e.,
1436 * pushing :B where no source is specified)
1438 * (3) the destination is not under refs/tags/, and
1439 * if the old and new value is a commit, the new
1440 * is a descendant of the old.
1442 * (4) it is forced using the +A:B notation, or by
1443 * passing the --force argument
1446 if (!ref->deletion && !is_null_sha1(ref->old_sha1)) {
1447 int why = 0; /* why would this push require --force? */
1449 if (!prefixcmp(ref->name, "refs/tags/"))
1450 why = REF_STATUS_REJECT_ALREADY_EXISTS;
1451 else if (!has_sha1_file(ref->old_sha1))
1452 why = REF_STATUS_REJECT_FETCH_FIRST;
1453 else if (!lookup_commit_reference_gently(ref->old_sha1, 1) ||
1454 !lookup_commit_reference_gently(ref->new_sha1, 1))
1455 why = REF_STATUS_REJECT_NEEDS_FORCE;
1456 else if (!ref_newer(ref->new_sha1, ref->old_sha1))
1457 why = REF_STATUS_REJECT_NONFASTFORWARD;
1459 if (!force_ref_update)
1462 ref->forced_update = 1;
1467 struct branch *branch_get(const char *name)
1472 if (!name || !*name || !strcmp(name, "HEAD"))
1473 ret = current_branch;
1475 ret = make_branch(name, 0);
1476 if (ret && ret->remote_name) {
1477 ret->remote = remote_get(ret->remote_name);
1478 if (ret->merge_nr) {
1480 ret->merge = xcalloc(sizeof(*ret->merge),
1482 for (i = 0; i < ret->merge_nr; i++) {
1483 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1484 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1485 if (remote_find_tracking(ret->remote, ret->merge[i])
1486 && !strcmp(ret->remote_name, "."))
1487 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1494 int branch_has_merge_config(struct branch *branch)
1496 return branch && !!branch->merge;
1499 int branch_merge_matches(struct branch *branch,
1501 const char *refname)
1503 if (!branch || i < 0 || i >= branch->merge_nr)
1505 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1508 static int ignore_symref_update(const char *refname)
1510 unsigned char sha1[20];
1513 if (!resolve_ref_unsafe(refname, sha1, 0, &flag))
1514 return 0; /* non-existing refs are OK */
1515 return (flag & REF_ISSYMREF);
1518 static struct ref *get_expanded_map(const struct ref *remote_refs,
1519 const struct refspec *refspec)
1521 const struct ref *ref;
1522 struct ref *ret = NULL;
1523 struct ref **tail = &ret;
1527 for (ref = remote_refs; ref; ref = ref->next) {
1528 if (strchr(ref->name, '^'))
1529 continue; /* a dereference item */
1530 if (match_name_with_pattern(refspec->src, ref->name,
1531 refspec->dst, &expn_name) &&
1532 !ignore_symref_update(expn_name)) {
1533 struct ref *cpy = copy_ref(ref);
1535 cpy->peer_ref = alloc_ref(expn_name);
1538 cpy->peer_ref->force = 1;
1547 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1549 const struct ref *ref;
1550 for (ref = refs; ref; ref = ref->next) {
1551 if (refname_match(name, ref->name, ref_fetch_rules))
1557 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1559 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1564 return copy_ref(ref);
1567 static struct ref *get_local_ref(const char *name)
1569 if (!name || name[0] == '\0')
1572 if (!prefixcmp(name, "refs/"))
1573 return alloc_ref(name);
1575 if (!prefixcmp(name, "heads/") ||
1576 !prefixcmp(name, "tags/") ||
1577 !prefixcmp(name, "remotes/"))
1578 return alloc_ref_with_prefix("refs/", 5, name);
1580 return alloc_ref_with_prefix("refs/heads/", 11, name);
1583 int get_fetch_map(const struct ref *remote_refs,
1584 const struct refspec *refspec,
1588 struct ref *ref_map, **rmp;
1590 if (refspec->pattern) {
1591 ref_map = get_expanded_map(remote_refs, refspec);
1593 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1595 if (refspec->exact_sha1) {
1596 ref_map = alloc_ref(name);
1597 get_sha1_hex(name, ref_map->old_sha1);
1599 ref_map = get_remote_ref(remote_refs, name);
1601 if (!missing_ok && !ref_map)
1602 die("Couldn't find remote ref %s", name);
1604 ref_map->peer_ref = get_local_ref(refspec->dst);
1605 if (ref_map->peer_ref && refspec->force)
1606 ref_map->peer_ref->force = 1;
1610 for (rmp = &ref_map; *rmp; ) {
1611 if ((*rmp)->peer_ref) {
1612 if (prefixcmp((*rmp)->peer_ref->name, "refs/") ||
1613 check_refname_format((*rmp)->peer_ref->name, 0)) {
1614 struct ref *ignore = *rmp;
1615 error("* Ignoring funny ref '%s' locally",
1616 (*rmp)->peer_ref->name);
1617 *rmp = (*rmp)->next;
1618 free(ignore->peer_ref);
1623 rmp = &((*rmp)->next);
1627 tail_link_ref(ref_map, tail);
1632 int resolve_remote_symref(struct ref *ref, struct ref *list)
1636 for (; list; list = list->next)
1637 if (!strcmp(ref->symref, list->name)) {
1638 hashcpy(ref->old_sha1, list->old_sha1);
1644 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1647 struct commit_list *temp = list;
1648 temp->item->object.flags &= ~mark;
1654 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1657 struct commit *old, *new;
1658 struct commit_list *list, *used;
1662 * Both new and old must be commit-ish and new is descendant of
1663 * old. Otherwise we require --force.
1665 o = deref_tag(parse_object(old_sha1), NULL, 0);
1666 if (!o || o->type != OBJ_COMMIT)
1668 old = (struct commit *) o;
1670 o = deref_tag(parse_object(new_sha1), NULL, 0);
1671 if (!o || o->type != OBJ_COMMIT)
1673 new = (struct commit *) o;
1675 if (parse_commit(new) < 0)
1679 commit_list_insert(new, &list);
1681 new = pop_most_recent_commit(&list, TMP_MARK);
1682 commit_list_insert(new, &used);
1688 unmark_and_free(list, TMP_MARK);
1689 unmark_and_free(used, TMP_MARK);
1694 * Return true if there is anything to report, otherwise false.
1696 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1698 unsigned char sha1[20];
1699 struct commit *ours, *theirs;
1701 struct rev_info revs;
1702 const char *rev_argv[10], *base;
1706 * Nothing to report unless we are marked to build on top of
1710 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1714 * If what we used to build on no longer exists, there is
1715 * nothing to report.
1717 base = branch->merge[0]->dst;
1718 if (read_ref(base, sha1))
1720 theirs = lookup_commit_reference(sha1);
1724 if (read_ref(branch->refname, sha1))
1726 ours = lookup_commit_reference(sha1);
1730 /* are we the same? */
1734 /* Run "rev-list --left-right ours...theirs" internally... */
1736 rev_argv[rev_argc++] = NULL;
1737 rev_argv[rev_argc++] = "--left-right";
1738 rev_argv[rev_argc++] = symmetric;
1739 rev_argv[rev_argc++] = "--";
1740 rev_argv[rev_argc] = NULL;
1742 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1743 strcpy(symmetric + 40, "...");
1744 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1746 init_revisions(&revs, NULL);
1747 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1748 prepare_revision_walk(&revs);
1750 /* ... and count the commits on each side. */
1754 struct commit *c = get_revision(&revs);
1757 if (c->object.flags & SYMMETRIC_LEFT)
1763 /* clear object flags smudged by the above traversal */
1764 clear_commit_marks(ours, ALL_REV_FLAGS);
1765 clear_commit_marks(theirs, ALL_REV_FLAGS);
1770 * Return true when there is anything to report, otherwise false.
1772 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1774 int num_ours, num_theirs;
1777 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1780 base = branch->merge[0]->dst;
1781 base = shorten_unambiguous_ref(base, 0);
1784 Q_("Your branch is ahead of '%s' by %d commit.\n",
1785 "Your branch is ahead of '%s' by %d commits.\n",
1788 if (advice_status_hints)
1790 _(" (use \"git push\" to publish your local commits)\n"));
1791 } else if (!num_ours) {
1793 Q_("Your branch is behind '%s' by %d commit, "
1794 "and can be fast-forwarded.\n",
1795 "Your branch is behind '%s' by %d commits, "
1796 "and can be fast-forwarded.\n",
1799 if (advice_status_hints)
1801 _(" (use \"git pull\" to update your local branch)\n"));
1804 Q_("Your branch and '%s' have diverged,\n"
1805 "and have %d and %d different commit each, "
1807 "Your branch and '%s' have diverged,\n"
1808 "and have %d and %d different commits each, "
1811 base, num_ours, num_theirs);
1812 if (advice_status_hints)
1814 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
1819 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1821 struct ref ***local_tail = cb_data;
1825 /* we already know it starts with refs/ to get here */
1826 if (check_refname_format(refname + 5, 0))
1829 len = strlen(refname) + 1;
1830 ref = xcalloc(1, sizeof(*ref) + len);
1831 hashcpy(ref->new_sha1, sha1);
1832 memcpy(ref->name, refname, len);
1834 *local_tail = &ref->next;
1838 struct ref *get_local_heads(void)
1840 struct ref *local_refs = NULL, **local_tail = &local_refs;
1841 for_each_ref(one_local_ref, &local_tail);
1845 struct ref *guess_remote_head(const struct ref *head,
1846 const struct ref *refs,
1849 const struct ref *r;
1850 struct ref *list = NULL;
1851 struct ref **tail = &list;
1857 * Some transports support directly peeking at
1858 * where HEAD points; if that is the case, then
1859 * we don't have to guess.
1862 return copy_ref(find_ref_by_name(refs, head->symref));
1864 /* If refs/heads/master could be right, it is. */
1866 r = find_ref_by_name(refs, "refs/heads/master");
1867 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1871 /* Look for another ref that points there */
1872 for (r = refs; r; r = r->next) {
1874 !prefixcmp(r->name, "refs/heads/") &&
1875 !hashcmp(r->old_sha1, head->old_sha1)) {
1876 *tail = copy_ref(r);
1877 tail = &((*tail)->next);
1886 struct stale_heads_info {
1887 struct string_list *ref_names;
1888 struct ref **stale_refs_tail;
1889 struct refspec *refs;
1893 static int get_stale_heads_cb(const char *refname,
1894 const unsigned char *sha1, int flags, void *cb_data)
1896 struct stale_heads_info *info = cb_data;
1897 struct refspec query;
1898 memset(&query, 0, sizeof(struct refspec));
1899 query.dst = (char *)refname;
1901 if (query_refspecs(info->refs, info->ref_count, &query))
1902 return 0; /* No matches */
1905 * If we did find a suitable refspec and it's not a symref and
1906 * it's not in the list of refs that currently exist in that
1907 * remote we consider it to be stale.
1909 if (!((flags & REF_ISSYMREF) ||
1910 string_list_has_string(info->ref_names, query.src))) {
1911 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1912 hashcpy(ref->new_sha1, sha1);
1919 struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map)
1921 struct ref *ref, *stale_refs = NULL;
1922 struct string_list ref_names = STRING_LIST_INIT_NODUP;
1923 struct stale_heads_info info;
1924 info.ref_names = &ref_names;
1925 info.stale_refs_tail = &stale_refs;
1927 info.ref_count = ref_count;
1928 for (ref = fetch_map; ref; ref = ref->next)
1929 string_list_append(&ref_names, ref->name);
1930 sort_string_list(&ref_names);
1931 for_each_ref(get_stale_heads_cb, &info);
1932 string_list_clear(&ref_names, 0);