Use the matching function to generate the match results
[git/jnareb-git.git] / remote.c
blob01b8f91c5b3f493eb1ade0d08e6f9f962f7ff992
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"
9 static struct refspec s_tag_refspec = {
13 "refs/tags/",
14 "refs/tags/"
17 const struct refspec *tag_refspec = &s_tag_refspec;
19 struct counted_string {
20 size_t len;
21 const char *s;
23 struct rewrite {
24 const char *base;
25 size_t baselen;
26 struct counted_string *instead_of;
27 int instead_of_nr;
28 int instead_of_alloc;
31 static struct remote **remotes;
32 static int remotes_alloc;
33 static int remotes_nr;
35 static struct branch **branches;
36 static int branches_alloc;
37 static int branches_nr;
39 static struct branch *current_branch;
40 static const char *default_remote_name;
42 static struct rewrite **rewrite;
43 static int rewrite_alloc;
44 static int rewrite_nr;
46 #define BUF_SIZE (2048)
47 static char buffer[BUF_SIZE];
49 static const char *alias_url(const char *url)
51 int i, j;
52 char *ret;
53 struct counted_string *longest;
54 int longest_i;
56 longest = NULL;
57 longest_i = -1;
58 for (i = 0; i < rewrite_nr; i++) {
59 if (!rewrite[i])
60 continue;
61 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
62 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
63 (!longest ||
64 longest->len < rewrite[i]->instead_of[j].len)) {
65 longest = &(rewrite[i]->instead_of[j]);
66 longest_i = i;
70 if (!longest)
71 return url;
73 ret = xmalloc(rewrite[longest_i]->baselen +
74 (strlen(url) - longest->len) + 1);
75 strcpy(ret, rewrite[longest_i]->base);
76 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
77 return ret;
80 static void add_push_refspec(struct remote *remote, const char *ref)
82 ALLOC_GROW(remote->push_refspec,
83 remote->push_refspec_nr + 1,
84 remote->push_refspec_alloc);
85 remote->push_refspec[remote->push_refspec_nr++] = ref;
88 static void add_fetch_refspec(struct remote *remote, const char *ref)
90 ALLOC_GROW(remote->fetch_refspec,
91 remote->fetch_refspec_nr + 1,
92 remote->fetch_refspec_alloc);
93 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
96 static void add_url(struct remote *remote, const char *url)
98 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
99 remote->url[remote->url_nr++] = url;
102 static void add_url_alias(struct remote *remote, const char *url)
104 add_url(remote, alias_url(url));
107 static struct remote *make_remote(const char *name, int len)
109 struct remote *ret;
110 int i;
112 for (i = 0; i < remotes_nr; i++) {
113 if (len ? (!strncmp(name, remotes[i]->name, len) &&
114 !remotes[i]->name[len]) :
115 !strcmp(name, remotes[i]->name))
116 return remotes[i];
119 ret = xcalloc(1, sizeof(struct remote));
120 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
121 remotes[remotes_nr++] = ret;
122 if (len)
123 ret->name = xstrndup(name, len);
124 else
125 ret->name = xstrdup(name);
126 return ret;
129 static void add_merge(struct branch *branch, const char *name)
131 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
132 branch->merge_alloc);
133 branch->merge_name[branch->merge_nr++] = name;
136 static struct branch *make_branch(const char *name, int len)
138 struct branch *ret;
139 int i;
140 char *refname;
142 for (i = 0; i < branches_nr; i++) {
143 if (len ? (!strncmp(name, branches[i]->name, len) &&
144 !branches[i]->name[len]) :
145 !strcmp(name, branches[i]->name))
146 return branches[i];
149 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
150 ret = xcalloc(1, sizeof(struct branch));
151 branches[branches_nr++] = ret;
152 if (len)
153 ret->name = xstrndup(name, len);
154 else
155 ret->name = xstrdup(name);
156 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
157 strcpy(refname, "refs/heads/");
158 strcpy(refname + strlen("refs/heads/"), ret->name);
159 ret->refname = refname;
161 return ret;
164 static struct rewrite *make_rewrite(const char *base, int len)
166 struct rewrite *ret;
167 int i;
169 for (i = 0; i < rewrite_nr; i++) {
170 if (len
171 ? (len == rewrite[i]->baselen &&
172 !strncmp(base, rewrite[i]->base, len))
173 : !strcmp(base, rewrite[i]->base))
174 return rewrite[i];
177 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
178 ret = xcalloc(1, sizeof(struct rewrite));
179 rewrite[rewrite_nr++] = ret;
180 if (len) {
181 ret->base = xstrndup(base, len);
182 ret->baselen = len;
184 else {
185 ret->base = xstrdup(base);
186 ret->baselen = strlen(base);
188 return ret;
191 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
193 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
194 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
195 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
196 rewrite->instead_of_nr++;
199 static void read_remotes_file(struct remote *remote)
201 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
203 if (!f)
204 return;
205 remote->origin = REMOTE_REMOTES;
206 while (fgets(buffer, BUF_SIZE, f)) {
207 int value_list;
208 char *s, *p;
210 if (!prefixcmp(buffer, "URL:")) {
211 value_list = 0;
212 s = buffer + 4;
213 } else if (!prefixcmp(buffer, "Push:")) {
214 value_list = 1;
215 s = buffer + 5;
216 } else if (!prefixcmp(buffer, "Pull:")) {
217 value_list = 2;
218 s = buffer + 5;
219 } else
220 continue;
222 while (isspace(*s))
223 s++;
224 if (!*s)
225 continue;
227 p = s + strlen(s);
228 while (isspace(p[-1]))
229 *--p = 0;
231 switch (value_list) {
232 case 0:
233 add_url_alias(remote, xstrdup(s));
234 break;
235 case 1:
236 add_push_refspec(remote, xstrdup(s));
237 break;
238 case 2:
239 add_fetch_refspec(remote, xstrdup(s));
240 break;
243 fclose(f);
246 static void read_branches_file(struct remote *remote)
248 const char *slash = strchr(remote->name, '/');
249 char *frag;
250 struct strbuf branch = STRBUF_INIT;
251 int n = slash ? slash - remote->name : 1000;
252 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
253 char *s, *p;
254 int len;
256 if (!f)
257 return;
258 s = fgets(buffer, BUF_SIZE, f);
259 fclose(f);
260 if (!s)
261 return;
262 while (isspace(*s))
263 s++;
264 if (!*s)
265 return;
266 remote->origin = REMOTE_BRANCHES;
267 p = s + strlen(s);
268 while (isspace(p[-1]))
269 *--p = 0;
270 len = p - s;
271 if (slash)
272 len += strlen(slash);
273 p = xmalloc(len + 1);
274 strcpy(p, s);
275 if (slash)
276 strcat(p, slash);
279 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
280 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
281 * the partial URL obtained from the branches file plus
282 * "/netdev-2.6" and does not store it in any tracking ref.
283 * #branch specifier in the file is ignored.
285 * Otherwise, the branches file would have URL and optionally
286 * #branch specified. The "master" (or specified) branch is
287 * fetched and stored in the local branch of the same name.
289 frag = strchr(p, '#');
290 if (frag) {
291 *(frag++) = '\0';
292 strbuf_addf(&branch, "refs/heads/%s", frag);
293 } else
294 strbuf_addstr(&branch, "refs/heads/master");
295 if (!slash) {
296 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
297 } else {
298 strbuf_reset(&branch);
299 strbuf_addstr(&branch, "HEAD:");
301 add_url_alias(remote, p);
302 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
304 * Cogito compatible push: push current HEAD to remote #branch
305 * (master if missing)
307 strbuf_init(&branch, 0);
308 strbuf_addstr(&branch, "HEAD");
309 if (frag)
310 strbuf_addf(&branch, ":refs/heads/%s", frag);
311 else
312 strbuf_addstr(&branch, ":refs/heads/master");
313 add_push_refspec(remote, strbuf_detach(&branch, 0));
314 remote->fetch_tags = 1; /* always auto-follow */
317 static int handle_config(const char *key, const char *value, void *cb)
319 const char *name;
320 const char *subkey;
321 struct remote *remote;
322 struct branch *branch;
323 if (!prefixcmp(key, "branch.")) {
324 name = key + 7;
325 subkey = strrchr(name, '.');
326 if (!subkey)
327 return 0;
328 branch = make_branch(name, subkey - name);
329 if (!strcmp(subkey, ".remote")) {
330 if (!value)
331 return config_error_nonbool(key);
332 branch->remote_name = xstrdup(value);
333 if (branch == current_branch)
334 default_remote_name = branch->remote_name;
335 } else if (!strcmp(subkey, ".merge")) {
336 if (!value)
337 return config_error_nonbool(key);
338 add_merge(branch, xstrdup(value));
340 return 0;
342 if (!prefixcmp(key, "url.")) {
343 struct rewrite *rewrite;
344 name = key + 4;
345 subkey = strrchr(name, '.');
346 if (!subkey)
347 return 0;
348 rewrite = make_rewrite(name, subkey - name);
349 if (!strcmp(subkey, ".insteadof")) {
350 if (!value)
351 return config_error_nonbool(key);
352 add_instead_of(rewrite, xstrdup(value));
355 if (prefixcmp(key, "remote."))
356 return 0;
357 name = key + 7;
358 if (*name == '/') {
359 warning("Config remote shorthand cannot begin with '/': %s",
360 name);
361 return 0;
363 subkey = strrchr(name, '.');
364 if (!subkey)
365 return error("Config with no key for remote %s", name);
366 remote = make_remote(name, subkey - name);
367 remote->origin = REMOTE_CONFIG;
368 if (!strcmp(subkey, ".mirror"))
369 remote->mirror = git_config_bool(key, value);
370 else if (!strcmp(subkey, ".skipdefaultupdate"))
371 remote->skip_default_update = git_config_bool(key, value);
373 else if (!strcmp(subkey, ".url")) {
374 const char *v;
375 if (git_config_string(&v, key, value))
376 return -1;
377 add_url(remote, v);
378 } else if (!strcmp(subkey, ".push")) {
379 const char *v;
380 if (git_config_string(&v, key, value))
381 return -1;
382 add_push_refspec(remote, v);
383 } else if (!strcmp(subkey, ".fetch")) {
384 const char *v;
385 if (git_config_string(&v, key, value))
386 return -1;
387 add_fetch_refspec(remote, v);
388 } else if (!strcmp(subkey, ".receivepack")) {
389 const char *v;
390 if (git_config_string(&v, key, value))
391 return -1;
392 if (!remote->receivepack)
393 remote->receivepack = v;
394 else
395 error("more than one receivepack given, using the first");
396 } else if (!strcmp(subkey, ".uploadpack")) {
397 const char *v;
398 if (git_config_string(&v, key, value))
399 return -1;
400 if (!remote->uploadpack)
401 remote->uploadpack = v;
402 else
403 error("more than one uploadpack given, using the first");
404 } else if (!strcmp(subkey, ".tagopt")) {
405 if (!strcmp(value, "--no-tags"))
406 remote->fetch_tags = -1;
407 } else if (!strcmp(subkey, ".proxy")) {
408 return git_config_string((const char **)&remote->http_proxy,
409 key, value);
411 return 0;
414 static void alias_all_urls(void)
416 int i, j;
417 for (i = 0; i < remotes_nr; i++) {
418 if (!remotes[i])
419 continue;
420 for (j = 0; j < remotes[i]->url_nr; j++) {
421 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
426 static void read_config(void)
428 unsigned char sha1[20];
429 const char *head_ref;
430 int flag;
431 if (default_remote_name) // did this already
432 return;
433 default_remote_name = xstrdup("origin");
434 current_branch = NULL;
435 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
436 if (head_ref && (flag & REF_ISSYMREF) &&
437 !prefixcmp(head_ref, "refs/heads/")) {
438 current_branch =
439 make_branch(head_ref + strlen("refs/heads/"), 0);
441 git_config(handle_config, NULL);
442 alias_all_urls();
446 * We need to make sure the tracking branches are well formed, but a
447 * wildcard refspec in "struct refspec" must have a trailing slash. We
448 * temporarily drop the trailing '/' while calling check_ref_format(),
449 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
450 * error return is Ok for a wildcard refspec.
452 static int verify_refname(char *name, int is_glob)
454 int result, len = -1;
456 if (is_glob) {
457 len = strlen(name);
458 assert(name[len - 1] == '/');
459 name[len - 1] = '\0';
461 result = check_ref_format(name);
462 if (is_glob)
463 name[len - 1] = '/';
464 return result;
468 * This function frees a refspec array.
469 * Warning: code paths should be checked to ensure that the src
470 * and dst pointers are always freeable pointers as well
471 * as the refspec pointer itself.
473 static void free_refspecs(struct refspec *refspec, int nr_refspec)
475 int i;
477 if (!refspec)
478 return;
480 for (i = 0; i < nr_refspec; i++) {
481 free(refspec[i].src);
482 free(refspec[i].dst);
484 free(refspec);
487 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
489 int i;
490 int st;
491 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
493 for (i = 0; i < nr_refspec; i++) {
494 size_t llen;
495 int is_glob;
496 const char *lhs, *rhs;
498 llen = is_glob = 0;
500 lhs = refspec[i];
501 if (*lhs == '+') {
502 rs[i].force = 1;
503 lhs++;
506 rhs = strrchr(lhs, ':');
509 * Before going on, special case ":" (or "+:") as a refspec
510 * for matching refs.
512 if (!fetch && rhs == lhs && rhs[1] == '\0') {
513 rs[i].matching = 1;
514 continue;
517 if (rhs) {
518 size_t rlen = strlen(++rhs);
519 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
520 rs[i].dst = xstrndup(rhs, rlen - is_glob);
523 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
524 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
525 if ((rhs && !is_glob) || (!rhs && fetch))
526 goto invalid;
527 is_glob = 1;
528 llen--;
529 } else if (rhs && is_glob) {
530 goto invalid;
533 rs[i].pattern = is_glob;
534 rs[i].src = xstrndup(lhs, llen);
536 if (fetch) {
538 * LHS
539 * - empty is allowed; it means HEAD.
540 * - otherwise it must be a valid looking ref.
542 if (!*rs[i].src)
543 ; /* empty is ok */
544 else {
545 st = verify_refname(rs[i].src, is_glob);
546 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
547 goto invalid;
550 * RHS
551 * - missing is ok, and is same as empty.
552 * - empty is ok; it means not to store.
553 * - otherwise it must be a valid looking ref.
555 if (!rs[i].dst) {
556 ; /* ok */
557 } else if (!*rs[i].dst) {
558 ; /* ok */
559 } else {
560 st = verify_refname(rs[i].dst, is_glob);
561 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
562 goto invalid;
564 } else {
566 * LHS
567 * - empty is allowed; it means delete.
568 * - when wildcarded, it must be a valid looking ref.
569 * - otherwise, it must be an extended SHA-1, but
570 * there is no existing way to validate this.
572 if (!*rs[i].src)
573 ; /* empty is ok */
574 else if (is_glob) {
575 st = verify_refname(rs[i].src, is_glob);
576 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
577 goto invalid;
579 else
580 ; /* anything goes, for now */
582 * RHS
583 * - missing is allowed, but LHS then must be a
584 * valid looking ref.
585 * - empty is not allowed.
586 * - otherwise it must be a valid looking ref.
588 if (!rs[i].dst) {
589 st = verify_refname(rs[i].src, is_glob);
590 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
591 goto invalid;
592 } else if (!*rs[i].dst) {
593 goto invalid;
594 } else {
595 st = verify_refname(rs[i].dst, is_glob);
596 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
597 goto invalid;
601 return rs;
603 invalid:
604 if (verify) {
606 * nr_refspec must be greater than zero and i must be valid
607 * since it is only possible to reach this point from within
608 * the for loop above.
610 free_refspecs(rs, i+1);
611 return NULL;
613 die("Invalid refspec '%s'", refspec[i]);
616 int valid_fetch_refspec(const char *fetch_refspec_str)
618 const char *fetch_refspec[] = { fetch_refspec_str };
619 struct refspec *refspec;
621 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
622 free_refspecs(refspec, 1);
623 return !!refspec;
626 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
628 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
631 static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
633 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
636 static int valid_remote_nick(const char *name)
638 if (!name[0] || is_dot_or_dotdot(name))
639 return 0;
640 return !strchr(name, '/'); /* no slash */
643 struct remote *remote_get(const char *name)
645 struct remote *ret;
647 read_config();
648 if (!name)
649 name = default_remote_name;
650 ret = make_remote(name, 0);
651 if (valid_remote_nick(name)) {
652 if (!ret->url)
653 read_remotes_file(ret);
654 if (!ret->url)
655 read_branches_file(ret);
657 if (!ret->url)
658 add_url_alias(ret, name);
659 if (!ret->url)
660 return NULL;
661 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
662 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
663 return ret;
666 int for_each_remote(each_remote_fn fn, void *priv)
668 int i, result = 0;
669 read_config();
670 for (i = 0; i < remotes_nr && !result; i++) {
671 struct remote *r = remotes[i];
672 if (!r)
673 continue;
674 if (!r->fetch)
675 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
676 r->fetch_refspec);
677 if (!r->push)
678 r->push = parse_push_refspec(r->push_refspec_nr,
679 r->push_refspec);
680 result = fn(r, priv);
682 return result;
685 void ref_remove_duplicates(struct ref *ref_map)
687 struct ref **posn;
688 struct ref *next;
689 for (; ref_map; ref_map = ref_map->next) {
690 if (!ref_map->peer_ref)
691 continue;
692 posn = &ref_map->next;
693 while (*posn) {
694 if ((*posn)->peer_ref &&
695 !strcmp((*posn)->peer_ref->name,
696 ref_map->peer_ref->name)) {
697 if (strcmp((*posn)->name, ref_map->name))
698 die("%s tracks both %s and %s",
699 ref_map->peer_ref->name,
700 (*posn)->name, ref_map->name);
701 next = (*posn)->next;
702 free((*posn)->peer_ref);
703 free(*posn);
704 *posn = next;
705 } else {
706 posn = &(*posn)->next;
712 int remote_has_url(struct remote *remote, const char *url)
714 int i;
715 for (i = 0; i < remote->url_nr; i++) {
716 if (!strcmp(remote->url[i], url))
717 return 1;
719 return 0;
722 static int match_name_with_pattern(const char *key, const char *name,
723 const char *value, char **result)
725 size_t klen = strlen(key);
726 int ret = !strncmp(key, name, klen);
727 if (ret && value) {
728 size_t vlen = strlen(value);
729 *result = xmalloc(vlen +
730 strlen(name) -
731 klen + 1);
732 strcpy(*result, value);
733 strcpy(*result + vlen, name + klen);
735 return ret;
738 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
740 int find_src = refspec->src == NULL;
741 char *needle, **result;
742 int i;
744 if (find_src) {
745 if (!refspec->dst)
746 return error("find_tracking: need either src or dst");
747 needle = refspec->dst;
748 result = &refspec->src;
749 } else {
750 needle = refspec->src;
751 result = &refspec->dst;
754 for (i = 0; i < remote->fetch_refspec_nr; i++) {
755 struct refspec *fetch = &remote->fetch[i];
756 const char *key = find_src ? fetch->dst : fetch->src;
757 const char *value = find_src ? fetch->src : fetch->dst;
758 if (!fetch->dst)
759 continue;
760 if (fetch->pattern) {
761 if (match_name_with_pattern(key, needle, value, result)) {
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 *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
792 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
793 ret->next = NULL;
794 return ret;
797 struct ref *copy_ref_list(const struct ref *ref)
799 struct ref *ret = NULL;
800 struct ref **tail = &ret;
801 while (ref) {
802 *tail = copy_ref(ref);
803 ref = ref->next;
804 tail = &((*tail)->next);
806 return ret;
809 static void free_ref(struct ref *ref)
811 if (!ref)
812 return;
813 free(ref->remote_status);
814 free(ref->symref);
815 free(ref);
818 void free_refs(struct ref *ref)
820 struct ref *next;
821 while (ref) {
822 next = ref->next;
823 free(ref->peer_ref);
824 free_ref(ref);
825 ref = next;
829 static int count_refspec_match(const char *pattern,
830 struct ref *refs,
831 struct ref **matched_ref)
833 int patlen = strlen(pattern);
834 struct ref *matched_weak = NULL;
835 struct ref *matched = NULL;
836 int weak_match = 0;
837 int match = 0;
839 for (weak_match = match = 0; refs; refs = refs->next) {
840 char *name = refs->name;
841 int namelen = strlen(name);
843 if (!refname_match(pattern, name, ref_rev_parse_rules))
844 continue;
846 /* A match is "weak" if it is with refs outside
847 * heads or tags, and did not specify the pattern
848 * in full (e.g. "refs/remotes/origin/master") or at
849 * least from the toplevel (e.g. "remotes/origin/master");
850 * otherwise "git push $URL master" would result in
851 * ambiguity between remotes/origin/master and heads/master
852 * at the remote site.
854 if (namelen != patlen &&
855 patlen != namelen - 5 &&
856 prefixcmp(name, "refs/heads/") &&
857 prefixcmp(name, "refs/tags/")) {
858 /* We want to catch the case where only weak
859 * matches are found and there are multiple
860 * matches, and where more than one strong
861 * matches are found, as ambiguous. One
862 * strong match with zero or more weak matches
863 * are acceptable as a unique match.
865 matched_weak = refs;
866 weak_match++;
868 else {
869 matched = refs;
870 match++;
873 if (!matched) {
874 *matched_ref = matched_weak;
875 return weak_match;
877 else {
878 *matched_ref = matched;
879 return match;
883 static void tail_link_ref(struct ref *ref, struct ref ***tail)
885 **tail = ref;
886 while (ref->next)
887 ref = ref->next;
888 *tail = &ref->next;
891 static struct ref *try_explicit_object_name(const char *name)
893 unsigned char sha1[20];
894 struct ref *ref;
896 if (!*name) {
897 ref = alloc_ref("(delete)");
898 hashclr(ref->new_sha1);
899 return ref;
901 if (get_sha1(name, sha1))
902 return NULL;
903 ref = alloc_ref(name);
904 hashcpy(ref->new_sha1, sha1);
905 return ref;
908 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
910 struct ref *ret = alloc_ref(name);
911 tail_link_ref(ret, tail);
912 return ret;
915 static char *guess_ref(const char *name, struct ref *peer)
917 struct strbuf buf = STRBUF_INIT;
918 unsigned char sha1[20];
920 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
921 if (!r)
922 return NULL;
924 if (!prefixcmp(r, "refs/heads/"))
925 strbuf_addstr(&buf, "refs/heads/");
926 else if (!prefixcmp(r, "refs/tags/"))
927 strbuf_addstr(&buf, "refs/tags/");
928 else
929 return NULL;
931 strbuf_addstr(&buf, name);
932 return strbuf_detach(&buf, NULL);
935 static int match_explicit(struct ref *src, struct ref *dst,
936 struct ref ***dst_tail,
937 struct refspec *rs)
939 struct ref *matched_src, *matched_dst;
941 const char *dst_value = rs->dst;
942 char *dst_guess;
944 if (rs->pattern || rs->matching)
945 return 0;
947 matched_src = matched_dst = NULL;
948 switch (count_refspec_match(rs->src, src, &matched_src)) {
949 case 1:
950 break;
951 case 0:
952 /* The source could be in the get_sha1() format
953 * not a reference name. :refs/other is a
954 * way to delete 'other' ref at the remote end.
956 matched_src = try_explicit_object_name(rs->src);
957 if (!matched_src)
958 return error("src refspec %s does not match any.", rs->src);
959 break;
960 default:
961 return error("src refspec %s matches more than one.", rs->src);
964 if (!dst_value) {
965 unsigned char sha1[20];
966 int flag;
968 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
969 if (!dst_value ||
970 ((flag & REF_ISSYMREF) &&
971 prefixcmp(dst_value, "refs/heads/")))
972 die("%s cannot be resolved to branch.",
973 matched_src->name);
976 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
977 case 1:
978 break;
979 case 0:
980 if (!memcmp(dst_value, "refs/", 5))
981 matched_dst = make_linked_ref(dst_value, dst_tail);
982 else if((dst_guess = guess_ref(dst_value, matched_src)))
983 matched_dst = make_linked_ref(dst_guess, dst_tail);
984 else
985 error("unable to push to unqualified destination: %s\n"
986 "The destination refspec neither matches an "
987 "existing ref on the remote nor\n"
988 "begins with refs/, and we are unable to "
989 "guess a prefix based on the source ref.",
990 dst_value);
991 break;
992 default:
993 matched_dst = NULL;
994 error("dst refspec %s matches more than one.",
995 dst_value);
996 break;
998 if (!matched_dst)
999 return -1;
1000 if (matched_dst->peer_ref)
1001 return error("dst ref %s receives from more than one src.",
1002 matched_dst->name);
1003 else {
1004 matched_dst->peer_ref = matched_src;
1005 matched_dst->force = rs->force;
1007 return 0;
1010 static int match_explicit_refs(struct ref *src, struct ref *dst,
1011 struct ref ***dst_tail, struct refspec *rs,
1012 int rs_nr)
1014 int i, errs;
1015 for (i = errs = 0; i < rs_nr; i++)
1016 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1017 return errs;
1020 static const struct refspec *check_pattern_match(const struct refspec *rs,
1021 int rs_nr,
1022 const struct ref *src)
1024 int i;
1025 int matching_refs = -1;
1026 for (i = 0; i < rs_nr; i++) {
1027 if (rs[i].matching &&
1028 (matching_refs == -1 || rs[i].force)) {
1029 matching_refs = i;
1030 continue;
1033 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1034 NULL, NULL))
1035 return rs + i;
1037 if (matching_refs != -1)
1038 return rs + matching_refs;
1039 else
1040 return NULL;
1044 * Note. This is used only by "push"; refspec matching rules for
1045 * push and fetch are subtly different, so do not try to reuse it
1046 * without thinking.
1048 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1049 int nr_refspec, const char **refspec, int flags)
1051 struct refspec *rs;
1052 int send_all = flags & MATCH_REFS_ALL;
1053 int send_mirror = flags & MATCH_REFS_MIRROR;
1054 static const char *default_refspec[] = { ":", 0 };
1056 if (!nr_refspec) {
1057 nr_refspec = 1;
1058 refspec = default_refspec;
1060 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1061 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
1062 return -1;
1064 /* pick the remainder */
1065 for ( ; src; src = src->next) {
1066 struct ref *dst_peer;
1067 const struct refspec *pat = NULL;
1068 char *dst_name;
1069 if (src->peer_ref)
1070 continue;
1072 pat = check_pattern_match(rs, nr_refspec, src);
1073 if (!pat)
1074 continue;
1076 if (pat->matching) {
1078 * "matching refs"; traditionally we pushed everything
1079 * including refs outside refs/heads/ hierarchy, but
1080 * that does not make much sense these days.
1082 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1083 continue;
1084 dst_name = xstrdup(src->name);
1086 } else {
1087 const char *dst_side = pat->dst ? pat->dst : pat->src;
1088 if (!match_name_with_pattern(pat->src, src->name,
1089 dst_side, &dst_name))
1090 die("Didn't think it matches any more");
1092 dst_peer = find_ref_by_name(dst, dst_name);
1093 if (dst_peer) {
1094 if (dst_peer->peer_ref)
1095 /* We're already sending something to this ref. */
1096 goto free_name;
1098 } else {
1099 if (pat->matching && !(send_all || send_mirror))
1101 * Remote doesn't have it, and we have no
1102 * explicit pattern, and we don't have
1103 * --all nor --mirror.
1105 goto free_name;
1107 /* Create a new one and link it */
1108 dst_peer = make_linked_ref(dst_name, dst_tail);
1109 hashcpy(dst_peer->new_sha1, src->new_sha1);
1111 dst_peer->peer_ref = src;
1112 dst_peer->force = pat->force;
1113 free_name:
1114 free(dst_name);
1116 return 0;
1119 struct branch *branch_get(const char *name)
1121 struct branch *ret;
1123 read_config();
1124 if (!name || !*name || !strcmp(name, "HEAD"))
1125 ret = current_branch;
1126 else
1127 ret = make_branch(name, 0);
1128 if (ret && ret->remote_name) {
1129 ret->remote = remote_get(ret->remote_name);
1130 if (ret->merge_nr) {
1131 int i;
1132 ret->merge = xcalloc(sizeof(*ret->merge),
1133 ret->merge_nr);
1134 for (i = 0; i < ret->merge_nr; i++) {
1135 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1136 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1137 remote_find_tracking(ret->remote,
1138 ret->merge[i]);
1142 return ret;
1145 int branch_has_merge_config(struct branch *branch)
1147 return branch && !!branch->merge;
1150 int branch_merge_matches(struct branch *branch,
1151 int i,
1152 const char *refname)
1154 if (!branch || i < 0 || i >= branch->merge_nr)
1155 return 0;
1156 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1159 static struct ref *get_expanded_map(const struct ref *remote_refs,
1160 const struct refspec *refspec)
1162 const struct ref *ref;
1163 struct ref *ret = NULL;
1164 struct ref **tail = &ret;
1166 char *expn_name;
1168 for (ref = remote_refs; ref; ref = ref->next) {
1169 if (strchr(ref->name, '^'))
1170 continue; /* a dereference item */
1171 if (match_name_with_pattern(refspec->src, ref->name,
1172 refspec->dst, &expn_name)) {
1173 struct ref *cpy = copy_ref(ref);
1175 cpy->peer_ref = alloc_ref(expn_name);
1176 free(expn_name);
1177 if (refspec->force)
1178 cpy->peer_ref->force = 1;
1179 *tail = cpy;
1180 tail = &cpy->next;
1184 return ret;
1187 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1189 const struct ref *ref;
1190 for (ref = refs; ref; ref = ref->next) {
1191 if (refname_match(name, ref->name, ref_fetch_rules))
1192 return ref;
1194 return NULL;
1197 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1199 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1201 if (!ref)
1202 return NULL;
1204 return copy_ref(ref);
1207 static struct ref *get_local_ref(const char *name)
1209 if (!name)
1210 return NULL;
1212 if (!prefixcmp(name, "refs/"))
1213 return alloc_ref(name);
1215 if (!prefixcmp(name, "heads/") ||
1216 !prefixcmp(name, "tags/") ||
1217 !prefixcmp(name, "remotes/"))
1218 return alloc_ref_with_prefix("refs/", 5, name);
1220 return alloc_ref_with_prefix("refs/heads/", 11, name);
1223 int get_fetch_map(const struct ref *remote_refs,
1224 const struct refspec *refspec,
1225 struct ref ***tail,
1226 int missing_ok)
1228 struct ref *ref_map, **rmp;
1230 if (refspec->pattern) {
1231 ref_map = get_expanded_map(remote_refs, refspec);
1232 } else {
1233 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1235 ref_map = get_remote_ref(remote_refs, name);
1236 if (!missing_ok && !ref_map)
1237 die("Couldn't find remote ref %s", name);
1238 if (ref_map) {
1239 ref_map->peer_ref = get_local_ref(refspec->dst);
1240 if (ref_map->peer_ref && refspec->force)
1241 ref_map->peer_ref->force = 1;
1245 for (rmp = &ref_map; *rmp; ) {
1246 if ((*rmp)->peer_ref) {
1247 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1248 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1249 struct ref *ignore = *rmp;
1250 error("* Ignoring funny ref '%s' locally",
1251 (*rmp)->peer_ref->name);
1252 *rmp = (*rmp)->next;
1253 free(ignore->peer_ref);
1254 free(ignore);
1255 continue;
1258 rmp = &((*rmp)->next);
1261 if (ref_map)
1262 tail_link_ref(ref_map, tail);
1264 return 0;
1267 int resolve_remote_symref(struct ref *ref, struct ref *list)
1269 if (!ref->symref)
1270 return 0;
1271 for (; list; list = list->next)
1272 if (!strcmp(ref->symref, list->name)) {
1273 hashcpy(ref->old_sha1, list->old_sha1);
1274 return 0;
1276 return 1;
1280 * Return true if there is anything to report, otherwise false.
1282 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1284 unsigned char sha1[20];
1285 struct commit *ours, *theirs;
1286 char symmetric[84];
1287 struct rev_info revs;
1288 const char *rev_argv[10], *base;
1289 int rev_argc;
1292 * Nothing to report unless we are marked to build on top of
1293 * somebody else.
1295 if (!branch ||
1296 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1297 return 0;
1300 * If what we used to build on no longer exists, there is
1301 * nothing to report.
1303 base = branch->merge[0]->dst;
1304 if (!resolve_ref(base, sha1, 1, NULL))
1305 return 0;
1306 theirs = lookup_commit(sha1);
1307 if (!theirs)
1308 return 0;
1310 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1311 return 0;
1312 ours = lookup_commit(sha1);
1313 if (!ours)
1314 return 0;
1316 /* are we the same? */
1317 if (theirs == ours)
1318 return 0;
1320 /* Run "rev-list --left-right ours...theirs" internally... */
1321 rev_argc = 0;
1322 rev_argv[rev_argc++] = NULL;
1323 rev_argv[rev_argc++] = "--left-right";
1324 rev_argv[rev_argc++] = symmetric;
1325 rev_argv[rev_argc++] = "--";
1326 rev_argv[rev_argc] = NULL;
1328 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1329 strcpy(symmetric + 40, "...");
1330 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1332 init_revisions(&revs, NULL);
1333 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1334 prepare_revision_walk(&revs);
1336 /* ... and count the commits on each side. */
1337 *num_ours = 0;
1338 *num_theirs = 0;
1339 while (1) {
1340 struct commit *c = get_revision(&revs);
1341 if (!c)
1342 break;
1343 if (c->object.flags & SYMMETRIC_LEFT)
1344 (*num_ours)++;
1345 else
1346 (*num_theirs)++;
1349 /* clear object flags smudged by the above traversal */
1350 clear_commit_marks(ours, ALL_REV_FLAGS);
1351 clear_commit_marks(theirs, ALL_REV_FLAGS);
1352 return 1;
1356 * Return true when there is anything to report, otherwise false.
1358 int format_tracking_info(struct branch *branch, struct strbuf *sb)
1360 int num_ours, num_theirs;
1361 const char *base;
1363 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1364 return 0;
1366 base = branch->merge[0]->dst;
1367 if (!prefixcmp(base, "refs/remotes/")) {
1368 base += strlen("refs/remotes/");
1370 if (!num_theirs)
1371 strbuf_addf(sb, "Your branch is ahead of '%s' "
1372 "by %d commit%s.\n",
1373 base, num_ours, (num_ours == 1) ? "" : "s");
1374 else if (!num_ours)
1375 strbuf_addf(sb, "Your branch is behind '%s' "
1376 "by %d commit%s, "
1377 "and can be fast-forwarded.\n",
1378 base, num_theirs, (num_theirs == 1) ? "" : "s");
1379 else
1380 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1381 "and have %d and %d different commit(s) each, "
1382 "respectively.\n",
1383 base, num_ours, num_theirs);
1384 return 1;