t5602: Work around path mangling on MSYS
[git/mjg.git] / remote.c
blobbaa50aab6573738059a9a71a224acb6877257255
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
4 #include "commit.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "dir.h"
8 #include "tag.h"
10 static struct refspec s_tag_refspec = {
14 "refs/tags/",
15 "refs/tags/"
18 const struct refspec *tag_refspec = &s_tag_refspec;
20 struct counted_string {
21 size_t len;
22 const char *s;
24 struct rewrite {
25 const char *base;
26 size_t baselen;
27 struct counted_string *instead_of;
28 int instead_of_nr;
29 int instead_of_alloc;
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;
42 static int explicit_default_remote_name;
44 static struct rewrite **rewrite;
45 static int rewrite_alloc;
46 static int rewrite_nr;
48 #define BUF_SIZE (2048)
49 static char buffer[BUF_SIZE];
51 static const char *alias_url(const char *url)
53 int i, j;
54 char *ret;
55 struct counted_string *longest;
56 int longest_i;
58 longest = NULL;
59 longest_i = -1;
60 for (i = 0; i < rewrite_nr; i++) {
61 if (!rewrite[i])
62 continue;
63 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
64 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
65 (!longest ||
66 longest->len < rewrite[i]->instead_of[j].len)) {
67 longest = &(rewrite[i]->instead_of[j]);
68 longest_i = i;
72 if (!longest)
73 return url;
75 ret = xmalloc(rewrite[longest_i]->baselen +
76 (strlen(url) - longest->len) + 1);
77 strcpy(ret, rewrite[longest_i]->base);
78 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
79 return ret;
82 static void add_push_refspec(struct remote *remote, const char *ref)
84 ALLOC_GROW(remote->push_refspec,
85 remote->push_refspec_nr + 1,
86 remote->push_refspec_alloc);
87 remote->push_refspec[remote->push_refspec_nr++] = ref;
90 static void add_fetch_refspec(struct remote *remote, const char *ref)
92 ALLOC_GROW(remote->fetch_refspec,
93 remote->fetch_refspec_nr + 1,
94 remote->fetch_refspec_alloc);
95 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
98 static void add_url(struct remote *remote, const char *url)
100 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
101 remote->url[remote->url_nr++] = url;
104 static void add_url_alias(struct remote *remote, const char *url)
106 add_url(remote, alias_url(url));
109 static struct remote *make_remote(const char *name, int len)
111 struct remote *ret;
112 int i;
114 for (i = 0; i < remotes_nr; i++) {
115 if (len ? (!strncmp(name, remotes[i]->name, len) &&
116 !remotes[i]->name[len]) :
117 !strcmp(name, remotes[i]->name))
118 return remotes[i];
121 ret = xcalloc(1, sizeof(struct remote));
122 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
123 remotes[remotes_nr++] = ret;
124 if (len)
125 ret->name = xstrndup(name, len);
126 else
127 ret->name = xstrdup(name);
128 return ret;
131 static void add_merge(struct branch *branch, const char *name)
133 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
134 branch->merge_alloc);
135 branch->merge_name[branch->merge_nr++] = name;
138 static struct branch *make_branch(const char *name, int len)
140 struct branch *ret;
141 int i;
142 char *refname;
144 for (i = 0; i < branches_nr; i++) {
145 if (len ? (!strncmp(name, branches[i]->name, len) &&
146 !branches[i]->name[len]) :
147 !strcmp(name, branches[i]->name))
148 return branches[i];
151 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
152 ret = xcalloc(1, sizeof(struct branch));
153 branches[branches_nr++] = ret;
154 if (len)
155 ret->name = xstrndup(name, len);
156 else
157 ret->name = xstrdup(name);
158 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
159 strcpy(refname, "refs/heads/");
160 strcpy(refname + strlen("refs/heads/"), ret->name);
161 ret->refname = refname;
163 return ret;
166 static struct rewrite *make_rewrite(const char *base, int len)
168 struct rewrite *ret;
169 int i;
171 for (i = 0; i < rewrite_nr; i++) {
172 if (len
173 ? (len == rewrite[i]->baselen &&
174 !strncmp(base, rewrite[i]->base, len))
175 : !strcmp(base, rewrite[i]->base))
176 return rewrite[i];
179 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
180 ret = xcalloc(1, sizeof(struct rewrite));
181 rewrite[rewrite_nr++] = ret;
182 if (len) {
183 ret->base = xstrndup(base, len);
184 ret->baselen = len;
186 else {
187 ret->base = xstrdup(base);
188 ret->baselen = strlen(base);
190 return ret;
193 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
195 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
196 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
197 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
198 rewrite->instead_of_nr++;
201 static void read_remotes_file(struct remote *remote)
203 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
205 if (!f)
206 return;
207 remote->origin = REMOTE_REMOTES;
208 while (fgets(buffer, BUF_SIZE, f)) {
209 int value_list;
210 char *s, *p;
212 if (!prefixcmp(buffer, "URL:")) {
213 value_list = 0;
214 s = buffer + 4;
215 } else if (!prefixcmp(buffer, "Push:")) {
216 value_list = 1;
217 s = buffer + 5;
218 } else if (!prefixcmp(buffer, "Pull:")) {
219 value_list = 2;
220 s = buffer + 5;
221 } else
222 continue;
224 while (isspace(*s))
225 s++;
226 if (!*s)
227 continue;
229 p = s + strlen(s);
230 while (isspace(p[-1]))
231 *--p = 0;
233 switch (value_list) {
234 case 0:
235 add_url_alias(remote, xstrdup(s));
236 break;
237 case 1:
238 add_push_refspec(remote, xstrdup(s));
239 break;
240 case 2:
241 add_fetch_refspec(remote, xstrdup(s));
242 break;
245 fclose(f);
248 static void read_branches_file(struct remote *remote)
250 const char *slash = strchr(remote->name, '/');
251 char *frag;
252 struct strbuf branch = STRBUF_INIT;
253 int n = slash ? slash - remote->name : 1000;
254 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
255 char *s, *p;
256 int len;
258 if (!f)
259 return;
260 s = fgets(buffer, BUF_SIZE, f);
261 fclose(f);
262 if (!s)
263 return;
264 while (isspace(*s))
265 s++;
266 if (!*s)
267 return;
268 remote->origin = REMOTE_BRANCHES;
269 p = s + strlen(s);
270 while (isspace(p[-1]))
271 *--p = 0;
272 len = p - s;
273 if (slash)
274 len += strlen(slash);
275 p = xmalloc(len + 1);
276 strcpy(p, s);
277 if (slash)
278 strcat(p, slash);
281 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
282 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
283 * the partial URL obtained from the branches file plus
284 * "/netdev-2.6" and does not store it in any tracking ref.
285 * #branch specifier in the file is ignored.
287 * Otherwise, the branches file would have URL and optionally
288 * #branch specified. The "master" (or specified) branch is
289 * fetched and stored in the local branch of the same name.
291 frag = strchr(p, '#');
292 if (frag) {
293 *(frag++) = '\0';
294 strbuf_addf(&branch, "refs/heads/%s", frag);
295 } else
296 strbuf_addstr(&branch, "refs/heads/master");
297 if (!slash) {
298 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
299 } else {
300 strbuf_reset(&branch);
301 strbuf_addstr(&branch, "HEAD:");
303 add_url_alias(remote, p);
304 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
306 * Cogito compatible push: push current HEAD to remote #branch
307 * (master if missing)
309 strbuf_init(&branch, 0);
310 strbuf_addstr(&branch, "HEAD");
311 if (frag)
312 strbuf_addf(&branch, ":refs/heads/%s", frag);
313 else
314 strbuf_addstr(&branch, ":refs/heads/master");
315 add_push_refspec(remote, strbuf_detach(&branch, 0));
316 remote->fetch_tags = 1; /* always auto-follow */
319 static int handle_config(const char *key, const char *value, void *cb)
321 const char *name;
322 const char *subkey;
323 struct remote *remote;
324 struct branch *branch;
325 if (!prefixcmp(key, "branch.")) {
326 name = key + 7;
327 subkey = strrchr(name, '.');
328 if (!subkey)
329 return 0;
330 branch = make_branch(name, subkey - name);
331 if (!strcmp(subkey, ".remote")) {
332 if (!value)
333 return config_error_nonbool(key);
334 branch->remote_name = xstrdup(value);
335 if (branch == current_branch) {
336 default_remote_name = branch->remote_name;
337 explicit_default_remote_name = 1;
339 } else if (!strcmp(subkey, ".merge")) {
340 if (!value)
341 return config_error_nonbool(key);
342 add_merge(branch, xstrdup(value));
344 return 0;
346 if (!prefixcmp(key, "url.")) {
347 struct rewrite *rewrite;
348 name = key + 4;
349 subkey = strrchr(name, '.');
350 if (!subkey)
351 return 0;
352 rewrite = make_rewrite(name, subkey - name);
353 if (!strcmp(subkey, ".insteadof")) {
354 if (!value)
355 return config_error_nonbool(key);
356 add_instead_of(rewrite, xstrdup(value));
359 if (prefixcmp(key, "remote."))
360 return 0;
361 name = key + 7;
362 if (*name == '/') {
363 warning("Config remote shorthand cannot begin with '/': %s",
364 name);
365 return 0;
367 subkey = strrchr(name, '.');
368 if (!subkey)
369 return error("Config with no key for remote %s", name);
370 remote = make_remote(name, subkey - name);
371 remote->origin = REMOTE_CONFIG;
372 if (!strcmp(subkey, ".mirror"))
373 remote->mirror = git_config_bool(key, value);
374 else if (!strcmp(subkey, ".skipdefaultupdate"))
375 remote->skip_default_update = git_config_bool(key, value);
377 else if (!strcmp(subkey, ".url")) {
378 const char *v;
379 if (git_config_string(&v, key, value))
380 return -1;
381 add_url(remote, v);
382 } else if (!strcmp(subkey, ".push")) {
383 const char *v;
384 if (git_config_string(&v, key, value))
385 return -1;
386 add_push_refspec(remote, v);
387 } else if (!strcmp(subkey, ".fetch")) {
388 const char *v;
389 if (git_config_string(&v, key, value))
390 return -1;
391 add_fetch_refspec(remote, v);
392 } else if (!strcmp(subkey, ".receivepack")) {
393 const char *v;
394 if (git_config_string(&v, key, value))
395 return -1;
396 if (!remote->receivepack)
397 remote->receivepack = v;
398 else
399 error("more than one receivepack given, using the first");
400 } else if (!strcmp(subkey, ".uploadpack")) {
401 const char *v;
402 if (git_config_string(&v, key, value))
403 return -1;
404 if (!remote->uploadpack)
405 remote->uploadpack = v;
406 else
407 error("more than one uploadpack given, using the first");
408 } else if (!strcmp(subkey, ".tagopt")) {
409 if (!strcmp(value, "--no-tags"))
410 remote->fetch_tags = -1;
411 } else if (!strcmp(subkey, ".proxy")) {
412 return git_config_string((const char **)&remote->http_proxy,
413 key, value);
415 return 0;
418 static void alias_all_urls(void)
420 int i, j;
421 for (i = 0; i < remotes_nr; i++) {
422 if (!remotes[i])
423 continue;
424 for (j = 0; j < remotes[i]->url_nr; j++) {
425 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
430 static void read_config(void)
432 unsigned char sha1[20];
433 const char *head_ref;
434 int flag;
435 if (default_remote_name) // did this already
436 return;
437 default_remote_name = xstrdup("origin");
438 current_branch = NULL;
439 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
440 if (head_ref && (flag & REF_ISSYMREF) &&
441 !prefixcmp(head_ref, "refs/heads/")) {
442 current_branch =
443 make_branch(head_ref + strlen("refs/heads/"), 0);
445 git_config(handle_config, NULL);
446 alias_all_urls();
450 * We need to make sure the tracking branches are well formed, but a
451 * wildcard refspec in "struct refspec" must have a trailing slash. We
452 * temporarily drop the trailing '/' while calling check_ref_format(),
453 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
454 * error return is Ok for a wildcard refspec.
456 static int verify_refname(char *name, int is_glob)
458 int result, len = -1;
460 if (is_glob) {
461 len = strlen(name);
462 assert(name[len - 1] == '/');
463 name[len - 1] = '\0';
465 result = check_ref_format(name);
466 if (is_glob)
467 name[len - 1] = '/';
468 return result;
472 * This function frees a refspec array.
473 * Warning: code paths should be checked to ensure that the src
474 * and dst pointers are always freeable pointers as well
475 * as the refspec pointer itself.
477 static void free_refspecs(struct refspec *refspec, int nr_refspec)
479 int i;
481 if (!refspec)
482 return;
484 for (i = 0; i < nr_refspec; i++) {
485 free(refspec[i].src);
486 free(refspec[i].dst);
488 free(refspec);
491 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
493 int i;
494 int st;
495 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
497 for (i = 0; i < nr_refspec; i++) {
498 size_t llen;
499 int is_glob;
500 const char *lhs, *rhs;
502 is_glob = 0;
504 lhs = refspec[i];
505 if (*lhs == '+') {
506 rs[i].force = 1;
507 lhs++;
510 rhs = strrchr(lhs, ':');
513 * Before going on, special case ":" (or "+:") as a refspec
514 * for matching refs.
516 if (!fetch && rhs == lhs && rhs[1] == '\0') {
517 rs[i].matching = 1;
518 continue;
521 if (rhs) {
522 size_t rlen = strlen(++rhs);
523 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
524 rs[i].dst = xstrndup(rhs, rlen - is_glob);
527 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
528 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
529 if ((rhs && !is_glob) || (!rhs && fetch))
530 goto invalid;
531 is_glob = 1;
532 llen--;
533 } else if (rhs && is_glob) {
534 goto invalid;
537 rs[i].pattern = is_glob;
538 rs[i].src = xstrndup(lhs, llen);
540 if (fetch) {
542 * LHS
543 * - empty is allowed; it means HEAD.
544 * - otherwise it must be a valid looking ref.
546 if (!*rs[i].src)
547 ; /* empty is ok */
548 else {
549 st = verify_refname(rs[i].src, is_glob);
550 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
551 goto invalid;
554 * RHS
555 * - missing is ok, and is same as empty.
556 * - empty is ok; it means not to store.
557 * - otherwise it must be a valid looking ref.
559 if (!rs[i].dst) {
560 ; /* ok */
561 } else if (!*rs[i].dst) {
562 ; /* ok */
563 } else {
564 st = verify_refname(rs[i].dst, is_glob);
565 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
566 goto invalid;
568 } else {
570 * LHS
571 * - empty is allowed; it means delete.
572 * - when wildcarded, it must be a valid looking ref.
573 * - otherwise, it must be an extended SHA-1, but
574 * there is no existing way to validate this.
576 if (!*rs[i].src)
577 ; /* empty is ok */
578 else if (is_glob) {
579 st = verify_refname(rs[i].src, is_glob);
580 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
581 goto invalid;
583 else
584 ; /* anything goes, for now */
586 * RHS
587 * - missing is allowed, but LHS then must be a
588 * valid looking ref.
589 * - empty is not allowed.
590 * - otherwise it must be a valid looking ref.
592 if (!rs[i].dst) {
593 st = verify_refname(rs[i].src, is_glob);
594 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
595 goto invalid;
596 } else if (!*rs[i].dst) {
597 goto invalid;
598 } else {
599 st = verify_refname(rs[i].dst, is_glob);
600 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
601 goto invalid;
605 return rs;
607 invalid:
608 if (verify) {
610 * nr_refspec must be greater than zero and i must be valid
611 * since it is only possible to reach this point from within
612 * the for loop above.
614 free_refspecs(rs, i+1);
615 return NULL;
617 die("Invalid refspec '%s'", refspec[i]);
620 int valid_fetch_refspec(const char *fetch_refspec_str)
622 const char *fetch_refspec[] = { fetch_refspec_str };
623 struct refspec *refspec;
625 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
626 free_refspecs(refspec, 1);
627 return !!refspec;
630 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
632 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
635 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
637 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
640 static int valid_remote_nick(const char *name)
642 if (!name[0] || is_dot_or_dotdot(name))
643 return 0;
644 return !strchr(name, '/'); /* no slash */
647 struct remote *remote_get(const char *name)
649 struct remote *ret;
650 int name_given = 0;
652 read_config();
653 if (name)
654 name_given = 1;
655 else {
656 name = default_remote_name;
657 name_given = explicit_default_remote_name;
660 ret = make_remote(name, 0);
661 if (valid_remote_nick(name)) {
662 if (!ret->url)
663 read_remotes_file(ret);
664 if (!ret->url)
665 read_branches_file(ret);
667 if (name_given && !ret->url)
668 add_url_alias(ret, name);
669 if (!ret->url)
670 return NULL;
671 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
672 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
673 return ret;
676 int for_each_remote(each_remote_fn fn, void *priv)
678 int i, result = 0;
679 read_config();
680 for (i = 0; i < remotes_nr && !result; i++) {
681 struct remote *r = remotes[i];
682 if (!r)
683 continue;
684 if (!r->fetch)
685 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
686 r->fetch_refspec);
687 if (!r->push)
688 r->push = parse_push_refspec(r->push_refspec_nr,
689 r->push_refspec);
690 result = fn(r, priv);
692 return result;
695 void ref_remove_duplicates(struct ref *ref_map)
697 struct ref **posn;
698 struct ref *next;
699 for (; ref_map; ref_map = ref_map->next) {
700 if (!ref_map->peer_ref)
701 continue;
702 posn = &ref_map->next;
703 while (*posn) {
704 if ((*posn)->peer_ref &&
705 !strcmp((*posn)->peer_ref->name,
706 ref_map->peer_ref->name)) {
707 if (strcmp((*posn)->name, ref_map->name))
708 die("%s tracks both %s and %s",
709 ref_map->peer_ref->name,
710 (*posn)->name, ref_map->name);
711 next = (*posn)->next;
712 free((*posn)->peer_ref);
713 free(*posn);
714 *posn = next;
715 } else {
716 posn = &(*posn)->next;
722 int remote_has_url(struct remote *remote, const char *url)
724 int i;
725 for (i = 0; i < remote->url_nr; i++) {
726 if (!strcmp(remote->url[i], url))
727 return 1;
729 return 0;
732 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
734 int find_src = refspec->src == NULL;
735 char *needle, **result;
736 int i;
738 if (find_src) {
739 if (!refspec->dst)
740 return error("find_tracking: need either src or dst");
741 needle = refspec->dst;
742 result = &refspec->src;
743 } else {
744 needle = refspec->src;
745 result = &refspec->dst;
748 for (i = 0; i < remote->fetch_refspec_nr; i++) {
749 struct refspec *fetch = &remote->fetch[i];
750 const char *key = find_src ? fetch->dst : fetch->src;
751 const char *value = find_src ? fetch->src : fetch->dst;
752 if (!fetch->dst)
753 continue;
754 if (fetch->pattern) {
755 if (!prefixcmp(needle, key)) {
756 *result = xmalloc(strlen(value) +
757 strlen(needle) -
758 strlen(key) + 1);
759 strcpy(*result, value);
760 strcpy(*result + strlen(value),
761 needle + strlen(key));
762 refspec->force = fetch->force;
763 return 0;
765 } else if (!strcmp(needle, key)) {
766 *result = xstrdup(value);
767 refspec->force = fetch->force;
768 return 0;
771 return -1;
774 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
775 const char *name)
777 size_t len = strlen(name);
778 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
779 memcpy(ref->name, prefix, prefixlen);
780 memcpy(ref->name + prefixlen, name, len);
781 return ref;
784 struct ref *alloc_ref(const char *name)
786 return alloc_ref_with_prefix("", 0, name);
789 static struct ref *copy_ref(const struct ref *ref)
791 struct ref *cpy;
792 size_t len;
793 if (!ref)
794 return NULL;
795 len = strlen(ref->name);
796 cpy = xmalloc(sizeof(struct ref) + len + 1);
797 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
798 cpy->next = NULL;
799 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
800 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
801 cpy->peer_ref = copy_ref(ref->peer_ref);
802 return cpy;
805 struct ref *copy_ref_list(const struct ref *ref)
807 struct ref *ret = NULL;
808 struct ref **tail = &ret;
809 while (ref) {
810 *tail = copy_ref(ref);
811 ref = ref->next;
812 tail = &((*tail)->next);
814 return ret;
817 static void free_ref(struct ref *ref)
819 if (!ref)
820 return;
821 free_ref(ref->peer_ref);
822 free(ref->remote_status);
823 free(ref->symref);
824 free(ref);
827 void free_refs(struct ref *ref)
829 struct ref *next;
830 while (ref) {
831 next = ref->next;
832 free_ref(ref);
833 ref = next;
837 static int count_refspec_match(const char *pattern,
838 struct ref *refs,
839 struct ref **matched_ref)
841 int patlen = strlen(pattern);
842 struct ref *matched_weak = NULL;
843 struct ref *matched = NULL;
844 int weak_match = 0;
845 int match = 0;
847 for (weak_match = match = 0; refs; refs = refs->next) {
848 char *name = refs->name;
849 int namelen = strlen(name);
851 if (!refname_match(pattern, name, ref_rev_parse_rules))
852 continue;
854 /* A match is "weak" if it is with refs outside
855 * heads or tags, and did not specify the pattern
856 * in full (e.g. "refs/remotes/origin/master") or at
857 * least from the toplevel (e.g. "remotes/origin/master");
858 * otherwise "git push $URL master" would result in
859 * ambiguity between remotes/origin/master and heads/master
860 * at the remote site.
862 if (namelen != patlen &&
863 patlen != namelen - 5 &&
864 prefixcmp(name, "refs/heads/") &&
865 prefixcmp(name, "refs/tags/")) {
866 /* We want to catch the case where only weak
867 * matches are found and there are multiple
868 * matches, and where more than one strong
869 * matches are found, as ambiguous. One
870 * strong match with zero or more weak matches
871 * are acceptable as a unique match.
873 matched_weak = refs;
874 weak_match++;
876 else {
877 matched = refs;
878 match++;
881 if (!matched) {
882 *matched_ref = matched_weak;
883 return weak_match;
885 else {
886 *matched_ref = matched;
887 return match;
891 static void tail_link_ref(struct ref *ref, struct ref ***tail)
893 **tail = ref;
894 while (ref->next)
895 ref = ref->next;
896 *tail = &ref->next;
899 static struct ref *try_explicit_object_name(const char *name)
901 unsigned char sha1[20];
902 struct ref *ref;
904 if (!*name) {
905 ref = alloc_ref("(delete)");
906 hashclr(ref->new_sha1);
907 return ref;
909 if (get_sha1(name, sha1))
910 return NULL;
911 ref = alloc_ref(name);
912 hashcpy(ref->new_sha1, sha1);
913 return ref;
916 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
918 struct ref *ret = alloc_ref(name);
919 tail_link_ref(ret, tail);
920 return ret;
923 static char *guess_ref(const char *name, struct ref *peer)
925 struct strbuf buf = STRBUF_INIT;
926 unsigned char sha1[20];
928 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
929 if (!r)
930 return NULL;
932 if (!prefixcmp(r, "refs/heads/"))
933 strbuf_addstr(&buf, "refs/heads/");
934 else if (!prefixcmp(r, "refs/tags/"))
935 strbuf_addstr(&buf, "refs/tags/");
936 else
937 return NULL;
939 strbuf_addstr(&buf, name);
940 return strbuf_detach(&buf, NULL);
943 static int match_explicit(struct ref *src, struct ref *dst,
944 struct ref ***dst_tail,
945 struct refspec *rs)
947 struct ref *matched_src, *matched_dst;
948 int copy_src;
950 const char *dst_value = rs->dst;
951 char *dst_guess;
953 if (rs->pattern || rs->matching)
954 return 0;
956 matched_src = matched_dst = NULL;
957 switch (count_refspec_match(rs->src, src, &matched_src)) {
958 case 1:
959 copy_src = 1;
960 break;
961 case 0:
962 /* The source could be in the get_sha1() format
963 * not a reference name. :refs/other is a
964 * way to delete 'other' ref at the remote end.
966 matched_src = try_explicit_object_name(rs->src);
967 if (!matched_src)
968 return error("src refspec %s does not match any.", rs->src);
969 copy_src = 0;
970 break;
971 default:
972 return error("src refspec %s matches more than one.", rs->src);
975 if (!dst_value) {
976 unsigned char sha1[20];
977 int flag;
979 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
980 if (!dst_value ||
981 ((flag & REF_ISSYMREF) &&
982 prefixcmp(dst_value, "refs/heads/")))
983 die("%s cannot be resolved to branch.",
984 matched_src->name);
987 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
988 case 1:
989 break;
990 case 0:
991 if (!memcmp(dst_value, "refs/", 5))
992 matched_dst = make_linked_ref(dst_value, dst_tail);
993 else if((dst_guess = guess_ref(dst_value, matched_src)))
994 matched_dst = make_linked_ref(dst_guess, dst_tail);
995 else
996 error("unable to push to unqualified destination: %s\n"
997 "The destination refspec neither matches an "
998 "existing ref on the remote nor\n"
999 "begins with refs/, and we are unable to "
1000 "guess a prefix based on the source ref.",
1001 dst_value);
1002 break;
1003 default:
1004 matched_dst = NULL;
1005 error("dst refspec %s matches more than one.",
1006 dst_value);
1007 break;
1009 if (!matched_dst)
1010 return -1;
1011 if (matched_dst->peer_ref)
1012 return error("dst ref %s receives from more than one src.",
1013 matched_dst->name);
1014 else {
1015 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1016 matched_dst->force = rs->force;
1018 return 0;
1021 static int match_explicit_refs(struct ref *src, struct ref *dst,
1022 struct ref ***dst_tail, struct refspec *rs,
1023 int rs_nr)
1025 int i, errs;
1026 for (i = errs = 0; i < rs_nr; i++)
1027 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1028 return errs;
1031 static const struct refspec *check_pattern_match(const struct refspec *rs,
1032 int rs_nr,
1033 const struct ref *src)
1035 int i;
1036 int matching_refs = -1;
1037 for (i = 0; i < rs_nr; i++) {
1038 if (rs[i].matching &&
1039 (matching_refs == -1 || rs[i].force)) {
1040 matching_refs = i;
1041 continue;
1044 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
1045 return rs + i;
1047 if (matching_refs != -1)
1048 return rs + matching_refs;
1049 else
1050 return NULL;
1054 * Note. This is used only by "push"; refspec matching rules for
1055 * push and fetch are subtly different, so do not try to reuse it
1056 * without thinking.
1058 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1059 int nr_refspec, const char **refspec, int flags)
1061 struct refspec *rs;
1062 int send_all = flags & MATCH_REFS_ALL;
1063 int send_mirror = flags & MATCH_REFS_MIRROR;
1064 int errs;
1065 static const char *default_refspec[] = { ":", 0 };
1067 if (!nr_refspec) {
1068 nr_refspec = 1;
1069 refspec = default_refspec;
1071 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1072 errs = match_explicit_refs(src, dst, dst_tail, rs, nr_refspec);
1074 /* pick the remainder */
1075 for ( ; src; src = src->next) {
1076 struct ref *dst_peer;
1077 const struct refspec *pat = NULL;
1078 char *dst_name;
1079 if (src->peer_ref)
1080 continue;
1082 pat = check_pattern_match(rs, nr_refspec, src);
1083 if (!pat)
1084 continue;
1086 if (pat->matching) {
1088 * "matching refs"; traditionally we pushed everything
1089 * including refs outside refs/heads/ hierarchy, but
1090 * that does not make much sense these days.
1092 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1093 continue;
1094 dst_name = xstrdup(src->name);
1096 } else {
1097 const char *dst_side = pat->dst ? pat->dst : pat->src;
1098 dst_name = xmalloc(strlen(dst_side) +
1099 strlen(src->name) -
1100 strlen(pat->src) + 2);
1101 strcpy(dst_name, dst_side);
1102 strcat(dst_name, src->name + strlen(pat->src));
1104 dst_peer = find_ref_by_name(dst, dst_name);
1105 if (dst_peer) {
1106 if (dst_peer->peer_ref)
1107 /* We're already sending something to this ref. */
1108 goto free_name;
1110 } else {
1111 if (pat->matching && !(send_all || send_mirror))
1113 * Remote doesn't have it, and we have no
1114 * explicit pattern, and we don't have
1115 * --all nor --mirror.
1117 goto free_name;
1119 /* Create a new one and link it */
1120 dst_peer = make_linked_ref(dst_name, dst_tail);
1121 hashcpy(dst_peer->new_sha1, src->new_sha1);
1123 dst_peer->peer_ref = copy_ref(src);
1124 dst_peer->force = pat->force;
1125 free_name:
1126 free(dst_name);
1128 if (errs)
1129 return -1;
1130 return 0;
1133 struct branch *branch_get(const char *name)
1135 struct branch *ret;
1137 read_config();
1138 if (!name || !*name || !strcmp(name, "HEAD"))
1139 ret = current_branch;
1140 else
1141 ret = make_branch(name, 0);
1142 if (ret && ret->remote_name) {
1143 ret->remote = remote_get(ret->remote_name);
1144 if (ret->merge_nr) {
1145 int i;
1146 ret->merge = xcalloc(sizeof(*ret->merge),
1147 ret->merge_nr);
1148 for (i = 0; i < ret->merge_nr; i++) {
1149 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1150 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1151 remote_find_tracking(ret->remote,
1152 ret->merge[i]);
1156 return ret;
1159 int branch_has_merge_config(struct branch *branch)
1161 return branch && !!branch->merge;
1164 int branch_merge_matches(struct branch *branch,
1165 int i,
1166 const char *refname)
1168 if (!branch || i < 0 || i >= branch->merge_nr)
1169 return 0;
1170 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1173 static struct ref *get_expanded_map(const struct ref *remote_refs,
1174 const struct refspec *refspec)
1176 const struct ref *ref;
1177 struct ref *ret = NULL;
1178 struct ref **tail = &ret;
1180 int remote_prefix_len = strlen(refspec->src);
1181 int local_prefix_len = strlen(refspec->dst);
1183 for (ref = remote_refs; ref; ref = ref->next) {
1184 if (strchr(ref->name, '^'))
1185 continue; /* a dereference item */
1186 if (!prefixcmp(ref->name, refspec->src)) {
1187 const char *match;
1188 struct ref *cpy = copy_ref(ref);
1189 match = ref->name + remote_prefix_len;
1191 cpy->peer_ref = alloc_ref_with_prefix(refspec->dst,
1192 local_prefix_len, match);
1193 if (refspec->force)
1194 cpy->peer_ref->force = 1;
1195 *tail = cpy;
1196 tail = &cpy->next;
1200 return ret;
1203 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1205 const struct ref *ref;
1206 for (ref = refs; ref; ref = ref->next) {
1207 if (refname_match(name, ref->name, ref_fetch_rules))
1208 return ref;
1210 return NULL;
1213 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1215 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1217 if (!ref)
1218 return NULL;
1220 return copy_ref(ref);
1223 static struct ref *get_local_ref(const char *name)
1225 if (!name)
1226 return NULL;
1228 if (!prefixcmp(name, "refs/"))
1229 return alloc_ref(name);
1231 if (!prefixcmp(name, "heads/") ||
1232 !prefixcmp(name, "tags/") ||
1233 !prefixcmp(name, "remotes/"))
1234 return alloc_ref_with_prefix("refs/", 5, name);
1236 return alloc_ref_with_prefix("refs/heads/", 11, name);
1239 int get_fetch_map(const struct ref *remote_refs,
1240 const struct refspec *refspec,
1241 struct ref ***tail,
1242 int missing_ok)
1244 struct ref *ref_map, **rmp;
1246 if (refspec->pattern) {
1247 ref_map = get_expanded_map(remote_refs, refspec);
1248 } else {
1249 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1251 ref_map = get_remote_ref(remote_refs, name);
1252 if (!missing_ok && !ref_map)
1253 die("Couldn't find remote ref %s", name);
1254 if (ref_map) {
1255 ref_map->peer_ref = get_local_ref(refspec->dst);
1256 if (ref_map->peer_ref && refspec->force)
1257 ref_map->peer_ref->force = 1;
1261 for (rmp = &ref_map; *rmp; ) {
1262 if ((*rmp)->peer_ref) {
1263 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1264 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1265 struct ref *ignore = *rmp;
1266 error("* Ignoring funny ref '%s' locally",
1267 (*rmp)->peer_ref->name);
1268 *rmp = (*rmp)->next;
1269 free(ignore->peer_ref);
1270 free(ignore);
1271 continue;
1274 rmp = &((*rmp)->next);
1277 if (ref_map)
1278 tail_link_ref(ref_map, tail);
1280 return 0;
1283 int resolve_remote_symref(struct ref *ref, struct ref *list)
1285 if (!ref->symref)
1286 return 0;
1287 for (; list; list = list->next)
1288 if (!strcmp(ref->symref, list->name)) {
1289 hashcpy(ref->old_sha1, list->old_sha1);
1290 return 0;
1292 return 1;
1295 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1297 while (list) {
1298 struct commit_list *temp = list;
1299 temp->item->object.flags &= ~mark;
1300 list = temp->next;
1301 free(temp);
1305 int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1307 struct object *o;
1308 struct commit *old, *new;
1309 struct commit_list *list, *used;
1310 int found = 0;
1312 /* Both new and old must be commit-ish and new is descendant of
1313 * old. Otherwise we require --force.
1315 o = deref_tag(parse_object(old_sha1), NULL, 0);
1316 if (!o || o->type != OBJ_COMMIT)
1317 return 0;
1318 old = (struct commit *) o;
1320 o = deref_tag(parse_object(new_sha1), NULL, 0);
1321 if (!o || o->type != OBJ_COMMIT)
1322 return 0;
1323 new = (struct commit *) o;
1325 if (parse_commit(new) < 0)
1326 return 0;
1328 used = list = NULL;
1329 commit_list_insert(new, &list);
1330 while (list) {
1331 new = pop_most_recent_commit(&list, TMP_MARK);
1332 commit_list_insert(new, &used);
1333 if (new == old) {
1334 found = 1;
1335 break;
1338 unmark_and_free(list, TMP_MARK);
1339 unmark_and_free(used, TMP_MARK);
1340 return found;
1344 * Return true if there is anything to report, otherwise false.
1346 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1348 unsigned char sha1[20];
1349 struct commit *ours, *theirs;
1350 char symmetric[84];
1351 struct rev_info revs;
1352 const char *rev_argv[10], *base;
1353 int rev_argc;
1356 * Nothing to report unless we are marked to build on top of
1357 * somebody else.
1359 if (!branch ||
1360 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1361 return 0;
1364 * If what we used to build on no longer exists, there is
1365 * nothing to report.
1367 base = branch->merge[0]->dst;
1368 if (!resolve_ref(base, sha1, 1, NULL))
1369 return 0;
1370 theirs = lookup_commit(sha1);
1371 if (!theirs)
1372 return 0;
1374 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1375 return 0;
1376 ours = lookup_commit(sha1);
1377 if (!ours)
1378 return 0;
1380 /* are we the same? */
1381 if (theirs == ours)
1382 return 0;
1384 /* Run "rev-list --left-right ours...theirs" internally... */
1385 rev_argc = 0;
1386 rev_argv[rev_argc++] = NULL;
1387 rev_argv[rev_argc++] = "--left-right";
1388 rev_argv[rev_argc++] = symmetric;
1389 rev_argv[rev_argc++] = "--";
1390 rev_argv[rev_argc] = NULL;
1392 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1393 strcpy(symmetric + 40, "...");
1394 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1396 init_revisions(&revs, NULL);
1397 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1398 prepare_revision_walk(&revs);
1400 /* ... and count the commits on each side. */
1401 *num_ours = 0;
1402 *num_theirs = 0;
1403 while (1) {
1404 struct commit *c = get_revision(&revs);
1405 if (!c)
1406 break;
1407 if (c->object.flags & SYMMETRIC_LEFT)
1408 (*num_ours)++;
1409 else
1410 (*num_theirs)++;
1413 /* clear object flags smudged by the above traversal */
1414 clear_commit_marks(ours, ALL_REV_FLAGS);
1415 clear_commit_marks(theirs, ALL_REV_FLAGS);
1416 return 1;
1420 * Return true when there is anything to report, otherwise false.
1422 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1424 int num_ours, num_theirs;
1425 const char *base;
1427 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1428 return 0;
1430 base = branch->merge[0]->dst;
1431 if (!prefixcmp(base, "refs/remotes/")) {
1432 base += strlen("refs/remotes/");
1434 if (!num_theirs)
1435 strbuf_addf(sb, "Your branch is ahead of '%s' "
1436 "by %d commit%s.\n",
1437 base, num_ours, (num_ours == 1) ? "" : "s");
1438 else if (!num_ours)
1439 strbuf_addf(sb, "Your branch is behind '%s' "
1440 "by %d commit%s, "
1441 "and can be fast-forwarded.\n",
1442 base, num_theirs, (num_theirs == 1) ? "" : "s");
1443 else
1444 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1445 "and have %d and %d different commit(s) each, "
1446 "respectively.\n",
1447 base, num_ours, num_theirs);
1448 return 1;
1451 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1453 struct ref ***local_tail = cb_data;
1454 struct ref *ref;
1455 int len;
1457 /* we already know it starts with refs/ to get here */
1458 if (check_ref_format(refname + 5))
1459 return 0;
1461 len = strlen(refname) + 1;
1462 ref = xcalloc(1, sizeof(*ref) + len);
1463 hashcpy(ref->new_sha1, sha1);
1464 memcpy(ref->name, refname, len);
1465 **local_tail = ref;
1466 *local_tail = &ref->next;
1467 return 0;
1470 struct ref *get_local_heads(void)
1472 struct ref *local_refs, **local_tail = &local_refs;
1473 for_each_ref(one_local_ref, &local_tail);
1474 return local_refs;
1477 struct ref *guess_remote_head(const struct ref *head,
1478 const struct ref *refs,
1479 int all)
1481 const struct ref *r;
1482 struct ref *list = NULL;
1483 struct ref **tail = &list;
1485 if (!head)
1486 return NULL;
1489 * Some transports support directly peeking at
1490 * where HEAD points; if that is the case, then
1491 * we don't have to guess.
1493 if (head->symref)
1494 return copy_ref(find_ref_by_name(refs, head->symref));
1496 /* If refs/heads/master could be right, it is. */
1497 if (!all) {
1498 r = find_ref_by_name(refs, "refs/heads/master");
1499 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1500 return copy_ref(r);
1503 /* Look for another ref that points there */
1504 for (r = refs; r; r = r->next) {
1505 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1506 *tail = copy_ref(r);
1507 tail = &((*tail)->next);
1508 if (!all)
1509 break;
1513 return list;