Merge branch 'bc/repack' into next
[git/platforms/storm.git] / remote.c
blob05041ed0d2195cedb9f1e93d332d9d7adde35081
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 static struct refspec s_tag_refspec = {
6 0,
7 1,
8 "refs/tags/",
9 "refs/tags/"
12 const struct refspec *tag_refspec = &s_tag_refspec;
14 struct counted_string {
15 size_t len;
16 const char *s;
18 struct rewrite {
19 const char *base;
20 size_t baselen;
21 struct counted_string *instead_of;
22 int instead_of_nr;
23 int instead_of_alloc;
26 static struct remote **remotes;
27 static int remotes_alloc;
28 static int remotes_nr;
30 static struct branch **branches;
31 static int branches_alloc;
32 static int branches_nr;
34 static struct branch *current_branch;
35 static const char *default_remote_name;
37 static struct rewrite **rewrite;
38 static int rewrite_alloc;
39 static int rewrite_nr;
41 #define BUF_SIZE (2048)
42 static char buffer[BUF_SIZE];
44 static const char *alias_url(const char *url)
46 int i, j;
47 char *ret;
48 struct counted_string *longest;
49 int longest_i;
51 longest = NULL;
52 longest_i = -1;
53 for (i = 0; i < rewrite_nr; i++) {
54 if (!rewrite[i])
55 continue;
56 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
57 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
58 (!longest ||
59 longest->len < rewrite[i]->instead_of[j].len)) {
60 longest = &(rewrite[i]->instead_of[j]);
61 longest_i = i;
65 if (!longest)
66 return url;
68 ret = malloc(rewrite[longest_i]->baselen +
69 (strlen(url) - longest->len) + 1);
70 strcpy(ret, rewrite[longest_i]->base);
71 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
72 return ret;
75 static void add_push_refspec(struct remote *remote, const char *ref)
77 ALLOC_GROW(remote->push_refspec,
78 remote->push_refspec_nr + 1,
79 remote->push_refspec_alloc);
80 remote->push_refspec[remote->push_refspec_nr++] = ref;
83 static void add_fetch_refspec(struct remote *remote, const char *ref)
85 ALLOC_GROW(remote->fetch_refspec,
86 remote->fetch_refspec_nr + 1,
87 remote->fetch_refspec_alloc);
88 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
91 static void add_url(struct remote *remote, const char *url)
93 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
94 remote->url[remote->url_nr++] = url;
97 static void add_url_alias(struct remote *remote, const char *url)
99 add_url(remote, alias_url(url));
102 static struct remote *make_remote(const char *name, int len)
104 struct remote *ret;
105 int i;
107 for (i = 0; i < remotes_nr; i++) {
108 if (len ? (!strncmp(name, remotes[i]->name, len) &&
109 !remotes[i]->name[len]) :
110 !strcmp(name, remotes[i]->name))
111 return remotes[i];
114 ret = xcalloc(1, sizeof(struct remote));
115 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
116 remotes[remotes_nr++] = ret;
117 if (len)
118 ret->name = xstrndup(name, len);
119 else
120 ret->name = xstrdup(name);
121 return ret;
124 static void add_merge(struct branch *branch, const char *name)
126 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
127 branch->merge_alloc);
128 branch->merge_name[branch->merge_nr++] = name;
131 static struct branch *make_branch(const char *name, int len)
133 struct branch *ret;
134 int i;
135 char *refname;
137 for (i = 0; i < branches_nr; i++) {
138 if (len ? (!strncmp(name, branches[i]->name, len) &&
139 !branches[i]->name[len]) :
140 !strcmp(name, branches[i]->name))
141 return branches[i];
144 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
145 ret = xcalloc(1, sizeof(struct branch));
146 branches[branches_nr++] = ret;
147 if (len)
148 ret->name = xstrndup(name, len);
149 else
150 ret->name = xstrdup(name);
151 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
152 strcpy(refname, "refs/heads/");
153 strcpy(refname + strlen("refs/heads/"), ret->name);
154 ret->refname = refname;
156 return ret;
159 static struct rewrite *make_rewrite(const char *base, int len)
161 struct rewrite *ret;
162 int i;
164 for (i = 0; i < rewrite_nr; i++) {
165 if (len
166 ? (len == rewrite[i]->baselen &&
167 !strncmp(base, rewrite[i]->base, len))
168 : !strcmp(base, rewrite[i]->base))
169 return rewrite[i];
172 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
173 ret = xcalloc(1, sizeof(struct rewrite));
174 rewrite[rewrite_nr++] = ret;
175 if (len) {
176 ret->base = xstrndup(base, len);
177 ret->baselen = len;
179 else {
180 ret->base = xstrdup(base);
181 ret->baselen = strlen(base);
183 return ret;
186 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
188 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
189 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
190 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
191 rewrite->instead_of_nr++;
194 static void read_remotes_file(struct remote *remote)
196 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
198 if (!f)
199 return;
200 while (fgets(buffer, BUF_SIZE, f)) {
201 int value_list;
202 char *s, *p;
204 if (!prefixcmp(buffer, "URL:")) {
205 value_list = 0;
206 s = buffer + 4;
207 } else if (!prefixcmp(buffer, "Push:")) {
208 value_list = 1;
209 s = buffer + 5;
210 } else if (!prefixcmp(buffer, "Pull:")) {
211 value_list = 2;
212 s = buffer + 5;
213 } else
214 continue;
216 while (isspace(*s))
217 s++;
218 if (!*s)
219 continue;
221 p = s + strlen(s);
222 while (isspace(p[-1]))
223 *--p = 0;
225 switch (value_list) {
226 case 0:
227 add_url_alias(remote, xstrdup(s));
228 break;
229 case 1:
230 add_push_refspec(remote, xstrdup(s));
231 break;
232 case 2:
233 add_fetch_refspec(remote, xstrdup(s));
234 break;
237 fclose(f);
240 static void read_branches_file(struct remote *remote)
242 const char *slash = strchr(remote->name, '/');
243 char *frag;
244 struct strbuf branch;
245 int n = slash ? slash - remote->name : 1000;
246 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
247 char *s, *p;
248 int len;
250 if (!f)
251 return;
252 s = fgets(buffer, BUF_SIZE, f);
253 fclose(f);
254 if (!s)
255 return;
256 while (isspace(*s))
257 s++;
258 if (!*s)
259 return;
260 p = s + strlen(s);
261 while (isspace(p[-1]))
262 *--p = 0;
263 len = p - s;
264 if (slash)
265 len += strlen(slash);
266 p = xmalloc(len + 1);
267 strcpy(p, s);
268 if (slash)
269 strcat(p, slash);
272 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
273 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
274 * the partial URL obtained from the branches file plus
275 * "/netdev-2.6" and does not store it in any tracking ref.
276 * #branch specifier in the file is ignored.
278 * Otherwise, the branches file would have URL and optionally
279 * #branch specified. The "master" (or specified) branch is
280 * fetched and stored in the local branch of the same name.
282 strbuf_init(&branch, 0);
283 frag = strchr(p, '#');
284 if (frag) {
285 *(frag++) = '\0';
286 strbuf_addf(&branch, "refs/heads/%s", frag);
287 } else
288 strbuf_addstr(&branch, "refs/heads/master");
289 if (!slash) {
290 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
291 } else {
292 strbuf_reset(&branch);
293 strbuf_addstr(&branch, "HEAD:");
295 add_url_alias(remote, p);
296 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
297 remote->fetch_tags = 1; /* always auto-follow */
300 static int handle_config(const char *key, const char *value)
302 const char *name;
303 const char *subkey;
304 struct remote *remote;
305 struct branch *branch;
306 if (!prefixcmp(key, "branch.")) {
307 name = key + 7;
308 subkey = strrchr(name, '.');
309 if (!subkey)
310 return 0;
311 branch = make_branch(name, subkey - name);
312 if (!strcmp(subkey, ".remote")) {
313 if (!value)
314 return config_error_nonbool(key);
315 branch->remote_name = xstrdup(value);
316 if (branch == current_branch)
317 default_remote_name = branch->remote_name;
318 } else if (!strcmp(subkey, ".merge")) {
319 if (!value)
320 return config_error_nonbool(key);
321 add_merge(branch, xstrdup(value));
323 return 0;
325 if (!prefixcmp(key, "url.")) {
326 struct rewrite *rewrite;
327 name = key + 4;
328 subkey = strrchr(name, '.');
329 if (!subkey)
330 return 0;
331 rewrite = make_rewrite(name, subkey - name);
332 if (!strcmp(subkey, ".insteadof")) {
333 if (!value)
334 return config_error_nonbool(key);
335 add_instead_of(rewrite, xstrdup(value));
338 if (prefixcmp(key, "remote."))
339 return 0;
340 name = key + 7;
341 subkey = strrchr(name, '.');
342 if (!subkey)
343 return error("Config with no key for remote %s", name);
344 if (*subkey == '/') {
345 warning("Config remote shorthand cannot begin with '/': %s", name);
346 return 0;
348 remote = make_remote(name, subkey - name);
349 if (!strcmp(subkey, ".mirror"))
350 remote->mirror = git_config_bool(key, value);
351 else if (!strcmp(subkey, ".skipdefaultupdate"))
352 remote->skip_default_update = git_config_bool(key, value);
354 else if (!strcmp(subkey, ".url")) {
355 const char *v;
356 if (git_config_string(&v, key, value))
357 return -1;
358 add_url(remote, v);
359 } else if (!strcmp(subkey, ".push")) {
360 const char *v;
361 if (git_config_string(&v, key, value))
362 return -1;
363 add_push_refspec(remote, v);
364 } else if (!strcmp(subkey, ".fetch")) {
365 const char *v;
366 if (git_config_string(&v, key, value))
367 return -1;
368 add_fetch_refspec(remote, v);
369 } else if (!strcmp(subkey, ".receivepack")) {
370 const char *v;
371 if (git_config_string(&v, key, value))
372 return -1;
373 if (!remote->receivepack)
374 remote->receivepack = v;
375 else
376 error("more than one receivepack given, using the first");
377 } else if (!strcmp(subkey, ".uploadpack")) {
378 const char *v;
379 if (git_config_string(&v, key, value))
380 return -1;
381 if (!remote->uploadpack)
382 remote->uploadpack = v;
383 else
384 error("more than one uploadpack given, using the first");
385 } else if (!strcmp(subkey, ".tagopt")) {
386 if (!strcmp(value, "--no-tags"))
387 remote->fetch_tags = -1;
388 } else if (!strcmp(subkey, ".proxy")) {
389 return git_config_string((const char **)&remote->http_proxy,
390 key, value);
392 return 0;
395 static void alias_all_urls(void)
397 int i, j;
398 for (i = 0; i < remotes_nr; i++) {
399 if (!remotes[i])
400 continue;
401 for (j = 0; j < remotes[i]->url_nr; j++) {
402 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
407 static void read_config(void)
409 unsigned char sha1[20];
410 const char *head_ref;
411 int flag;
412 if (default_remote_name) // did this already
413 return;
414 default_remote_name = xstrdup("origin");
415 current_branch = NULL;
416 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
417 if (head_ref && (flag & REF_ISSYMREF) &&
418 !prefixcmp(head_ref, "refs/heads/")) {
419 current_branch =
420 make_branch(head_ref + strlen("refs/heads/"), 0);
422 git_config(handle_config);
423 alias_all_urls();
426 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
428 int i;
429 int st;
430 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
432 for (i = 0; i < nr_refspec; i++) {
433 size_t llen, rlen;
434 int is_glob;
435 const char *lhs, *rhs;
437 llen = rlen = is_glob = 0;
439 lhs = refspec[i];
440 if (*lhs == '+') {
441 rs[i].force = 1;
442 lhs++;
445 rhs = strrchr(lhs, ':');
446 if (rhs) {
447 rhs++;
448 rlen = strlen(rhs);
449 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
450 if (is_glob)
451 rlen -= 2;
452 rs[i].dst = xstrndup(rhs, rlen);
455 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
456 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
457 if ((rhs && !is_glob) || (!rhs && fetch))
458 goto invalid;
459 is_glob = 1;
460 llen -= 2;
461 } else if (rhs && is_glob) {
462 goto invalid;
465 rs[i].pattern = is_glob;
466 rs[i].src = xstrndup(lhs, llen);
468 if (fetch) {
470 * LHS
471 * - empty is allowed; it means HEAD.
472 * - otherwise it must be a valid looking ref.
474 if (!*rs[i].src)
475 ; /* empty is ok */
476 else {
477 st = check_ref_format(rs[i].src);
478 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
479 goto invalid;
482 * RHS
483 * - missing is ok, and is same as empty.
484 * - empty is ok; it means not to store.
485 * - otherwise it must be a valid looking ref.
487 if (!rs[i].dst) {
488 ; /* ok */
489 } else if (!*rs[i].dst) {
490 ; /* ok */
491 } else {
492 st = check_ref_format(rs[i].dst);
493 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
494 goto invalid;
496 } else {
498 * LHS
499 * - empty is allowed; it means delete.
500 * - when wildcarded, it must be a valid looking ref.
501 * - otherwise, it must be an extended SHA-1, but
502 * there is no existing way to validate this.
504 if (!*rs[i].src)
505 ; /* empty is ok */
506 else if (is_glob) {
507 st = check_ref_format(rs[i].src);
508 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
509 goto invalid;
511 else
512 ; /* anything goes, for now */
514 * RHS
515 * - missing is allowed, but LHS then must be a
516 * valid looking ref.
517 * - empty is not allowed.
518 * - otherwise it must be a valid looking ref.
520 if (!rs[i].dst) {
521 st = check_ref_format(rs[i].src);
522 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
523 goto invalid;
524 } else if (!*rs[i].dst) {
525 goto invalid;
526 } else {
527 st = check_ref_format(rs[i].dst);
528 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
529 goto invalid;
533 return rs;
535 invalid:
536 if (verify) {
537 free(rs);
538 return NULL;
540 die("Invalid refspec '%s'", refspec[i]);
543 int valid_fetch_refspec(const char *fetch_refspec_str)
545 const char *fetch_refspec[] = { fetch_refspec_str };
546 struct refspec *refspec;
548 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
549 if (refspec)
550 free(refspec);
551 return !!refspec;
554 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
556 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
559 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
561 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
564 static int valid_remote_nick(const char *name)
566 if (!name[0] || /* not empty */
567 (name[0] == '.' && /* not "." */
568 (!name[1] || /* not ".." */
569 (name[1] == '.' && !name[2]))))
570 return 0;
571 return !strchr(name, '/'); /* no slash */
574 struct remote *remote_get(const char *name)
576 struct remote *ret;
578 read_config();
579 if (!name)
580 name = default_remote_name;
581 ret = make_remote(name, 0);
582 if (valid_remote_nick(name)) {
583 if (!ret->url)
584 read_remotes_file(ret);
585 if (!ret->url)
586 read_branches_file(ret);
588 if (!ret->url)
589 add_url_alias(ret, name);
590 if (!ret->url)
591 return NULL;
592 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
593 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
594 return ret;
597 int for_each_remote(each_remote_fn fn, void *priv)
599 int i, result = 0;
600 read_config();
601 for (i = 0; i < remotes_nr && !result; i++) {
602 struct remote *r = remotes[i];
603 if (!r)
604 continue;
605 if (!r->fetch)
606 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
607 r->fetch_refspec);
608 if (!r->push)
609 r->push = parse_push_refspec(r->push_refspec_nr,
610 r->push_refspec);
611 result = fn(r, priv);
613 return result;
616 void ref_remove_duplicates(struct ref *ref_map)
618 struct ref **posn;
619 struct ref *next;
620 for (; ref_map; ref_map = ref_map->next) {
621 if (!ref_map->peer_ref)
622 continue;
623 posn = &ref_map->next;
624 while (*posn) {
625 if ((*posn)->peer_ref &&
626 !strcmp((*posn)->peer_ref->name,
627 ref_map->peer_ref->name)) {
628 if (strcmp((*posn)->name, ref_map->name))
629 die("%s tracks both %s and %s",
630 ref_map->peer_ref->name,
631 (*posn)->name, ref_map->name);
632 next = (*posn)->next;
633 free((*posn)->peer_ref);
634 free(*posn);
635 *posn = next;
636 } else {
637 posn = &(*posn)->next;
643 int remote_has_url(struct remote *remote, const char *url)
645 int i;
646 for (i = 0; i < remote->url_nr; i++) {
647 if (!strcmp(remote->url[i], url))
648 return 1;
650 return 0;
653 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
655 int find_src = refspec->src == NULL;
656 char *needle, **result;
657 int i;
659 if (find_src) {
660 if (!refspec->dst)
661 return error("find_tracking: need either src or dst");
662 needle = refspec->dst;
663 result = &refspec->src;
664 } else {
665 needle = refspec->src;
666 result = &refspec->dst;
669 for (i = 0; i < remote->fetch_refspec_nr; i++) {
670 struct refspec *fetch = &remote->fetch[i];
671 const char *key = find_src ? fetch->dst : fetch->src;
672 const char *value = find_src ? fetch->src : fetch->dst;
673 if (!fetch->dst)
674 continue;
675 if (fetch->pattern) {
676 if (!prefixcmp(needle, key) &&
677 needle[strlen(key)] == '/') {
678 *result = xmalloc(strlen(value) +
679 strlen(needle) -
680 strlen(key) + 1);
681 strcpy(*result, value);
682 strcpy(*result + strlen(value),
683 needle + strlen(key));
684 refspec->force = fetch->force;
685 return 0;
687 } else if (!strcmp(needle, key)) {
688 *result = xstrdup(value);
689 refspec->force = fetch->force;
690 return 0;
693 return -1;
696 struct ref *alloc_ref(unsigned namelen)
698 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
699 memset(ret, 0, sizeof(struct ref) + namelen);
700 return ret;
703 struct ref *alloc_ref_from_str(const char* str)
705 struct ref *ret = alloc_ref(strlen(str) + 1);
706 strcpy(ret->name, str);
707 return ret;
710 static struct ref *copy_ref(const struct ref *ref)
712 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
713 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
714 ret->next = NULL;
715 return ret;
718 struct ref *copy_ref_list(const struct ref *ref)
720 struct ref *ret = NULL;
721 struct ref **tail = &ret;
722 while (ref) {
723 *tail = copy_ref(ref);
724 ref = ref->next;
725 tail = &((*tail)->next);
727 return ret;
730 void free_ref(struct ref *ref)
732 if (!ref)
733 return;
734 free(ref->remote_status);
735 free(ref->symref);
736 free(ref);
739 void free_refs(struct ref *ref)
741 struct ref *next;
742 while (ref) {
743 next = ref->next;
744 free(ref->peer_ref);
745 free_ref(ref);
746 ref = next;
750 static int count_refspec_match(const char *pattern,
751 struct ref *refs,
752 struct ref **matched_ref)
754 int patlen = strlen(pattern);
755 struct ref *matched_weak = NULL;
756 struct ref *matched = NULL;
757 int weak_match = 0;
758 int match = 0;
760 for (weak_match = match = 0; refs; refs = refs->next) {
761 char *name = refs->name;
762 int namelen = strlen(name);
764 if (!refname_match(pattern, name, ref_rev_parse_rules))
765 continue;
767 /* A match is "weak" if it is with refs outside
768 * heads or tags, and did not specify the pattern
769 * in full (e.g. "refs/remotes/origin/master") or at
770 * least from the toplevel (e.g. "remotes/origin/master");
771 * otherwise "git push $URL master" would result in
772 * ambiguity between remotes/origin/master and heads/master
773 * at the remote site.
775 if (namelen != patlen &&
776 patlen != namelen - 5 &&
777 prefixcmp(name, "refs/heads/") &&
778 prefixcmp(name, "refs/tags/")) {
779 /* We want to catch the case where only weak
780 * matches are found and there are multiple
781 * matches, and where more than one strong
782 * matches are found, as ambiguous. One
783 * strong match with zero or more weak matches
784 * are acceptable as a unique match.
786 matched_weak = refs;
787 weak_match++;
789 else {
790 matched = refs;
791 match++;
794 if (!matched) {
795 *matched_ref = matched_weak;
796 return weak_match;
798 else {
799 *matched_ref = matched;
800 return match;
804 static void tail_link_ref(struct ref *ref, struct ref ***tail)
806 **tail = ref;
807 while (ref->next)
808 ref = ref->next;
809 *tail = &ref->next;
812 static struct ref *try_explicit_object_name(const char *name)
814 unsigned char sha1[20];
815 struct ref *ref;
817 if (!*name) {
818 ref = alloc_ref(20);
819 strcpy(ref->name, "(delete)");
820 hashclr(ref->new_sha1);
821 return ref;
823 if (get_sha1(name, sha1))
824 return NULL;
825 ref = alloc_ref_from_str(name);
826 hashcpy(ref->new_sha1, sha1);
827 return ref;
830 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
832 struct ref *ret = alloc_ref_from_str(name);
833 tail_link_ref(ret, tail);
834 return ret;
837 static char *guess_ref(const char *name, struct ref *peer)
839 struct strbuf buf = STRBUF_INIT;
840 unsigned char sha1[20];
842 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
843 if (!r)
844 return NULL;
846 if (!prefixcmp(r, "refs/heads/"))
847 strbuf_addstr(&buf, "refs/heads/");
848 else if (!prefixcmp(r, "refs/tags/"))
849 strbuf_addstr(&buf, "refs/tags/");
850 else
851 return NULL;
853 strbuf_addstr(&buf, name);
854 return strbuf_detach(&buf, NULL);
857 static int match_explicit(struct ref *src, struct ref *dst,
858 struct ref ***dst_tail,
859 struct refspec *rs,
860 int errs)
862 struct ref *matched_src, *matched_dst;
864 const char *dst_value = rs->dst;
865 char *dst_guess;
867 if (rs->pattern)
868 return errs;
870 matched_src = matched_dst = NULL;
871 switch (count_refspec_match(rs->src, src, &matched_src)) {
872 case 1:
873 break;
874 case 0:
875 /* The source could be in the get_sha1() format
876 * not a reference name. :refs/other is a
877 * way to delete 'other' ref at the remote end.
879 matched_src = try_explicit_object_name(rs->src);
880 if (!matched_src)
881 error("src refspec %s does not match any.", rs->src);
882 break;
883 default:
884 matched_src = NULL;
885 error("src refspec %s matches more than one.", rs->src);
886 break;
889 if (!matched_src)
890 errs = 1;
892 if (!dst_value) {
893 unsigned char sha1[20];
894 int flag;
896 if (!matched_src)
897 return errs;
898 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
899 if (!dst_value ||
900 ((flag & REF_ISSYMREF) &&
901 prefixcmp(dst_value, "refs/heads/")))
902 die("%s cannot be resolved to branch.",
903 matched_src->name);
906 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
907 case 1:
908 break;
909 case 0:
910 if (!memcmp(dst_value, "refs/", 5))
911 matched_dst = make_linked_ref(dst_value, dst_tail);
912 else if((dst_guess = guess_ref(dst_value, matched_src)))
913 matched_dst = make_linked_ref(dst_guess, dst_tail);
914 else
915 error("unable to push to unqualified destination: %s\n"
916 "The destination refspec neither matches an "
917 "existing ref on the remote nor\n"
918 "begins with refs/, and we are unable to "
919 "guess a prefix based on the source ref.",
920 dst_value);
921 break;
922 default:
923 matched_dst = NULL;
924 error("dst refspec %s matches more than one.",
925 dst_value);
926 break;
928 if (errs || !matched_dst)
929 return 1;
930 if (matched_dst->peer_ref) {
931 errs = 1;
932 error("dst ref %s receives from more than one src.",
933 matched_dst->name);
935 else {
936 matched_dst->peer_ref = matched_src;
937 matched_dst->force = rs->force;
939 return errs;
942 static int match_explicit_refs(struct ref *src, struct ref *dst,
943 struct ref ***dst_tail, struct refspec *rs,
944 int rs_nr)
946 int i, errs;
947 for (i = errs = 0; i < rs_nr; i++)
948 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
949 return -errs;
952 static const struct refspec *check_pattern_match(const struct refspec *rs,
953 int rs_nr,
954 const struct ref *src)
956 int i;
957 for (i = 0; i < rs_nr; i++) {
958 if (rs[i].pattern &&
959 !prefixcmp(src->name, rs[i].src) &&
960 src->name[strlen(rs[i].src)] == '/')
961 return rs + i;
963 return NULL;
967 * Note. This is used only by "push"; refspec matching rules for
968 * push and fetch are subtly different, so do not try to reuse it
969 * without thinking.
971 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
972 int nr_refspec, const char **refspec, int flags)
974 struct refspec *rs =
975 parse_push_refspec(nr_refspec, (const char **) refspec);
976 int send_all = flags & MATCH_REFS_ALL;
977 int send_mirror = flags & MATCH_REFS_MIRROR;
979 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
980 return -1;
982 /* pick the remainder */
983 for ( ; src; src = src->next) {
984 struct ref *dst_peer;
985 const struct refspec *pat = NULL;
986 char *dst_name;
987 if (src->peer_ref)
988 continue;
989 if (nr_refspec) {
990 pat = check_pattern_match(rs, nr_refspec, src);
991 if (!pat)
992 continue;
994 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
996 * "matching refs"; traditionally we pushed everything
997 * including refs outside refs/heads/ hierarchy, but
998 * that does not make much sense these days.
1000 continue;
1002 if (pat) {
1003 const char *dst_side = pat->dst ? pat->dst : pat->src;
1004 dst_name = xmalloc(strlen(dst_side) +
1005 strlen(src->name) -
1006 strlen(pat->src) + 2);
1007 strcpy(dst_name, dst_side);
1008 strcat(dst_name, src->name + strlen(pat->src));
1009 } else
1010 dst_name = xstrdup(src->name);
1011 dst_peer = find_ref_by_name(dst, dst_name);
1012 if (dst_peer && dst_peer->peer_ref)
1013 /* We're already sending something to this ref. */
1014 goto free_name;
1016 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
1018 * Remote doesn't have it, and we have no
1019 * explicit pattern, and we don't have
1020 * --all nor --mirror.
1022 goto free_name;
1023 if (!dst_peer) {
1024 /* Create a new one and link it */
1025 dst_peer = make_linked_ref(dst_name, dst_tail);
1026 hashcpy(dst_peer->new_sha1, src->new_sha1);
1028 dst_peer->peer_ref = src;
1029 if (pat)
1030 dst_peer->force = pat->force;
1031 free_name:
1032 free(dst_name);
1034 return 0;
1037 struct branch *branch_get(const char *name)
1039 struct branch *ret;
1041 read_config();
1042 if (!name || !*name || !strcmp(name, "HEAD"))
1043 ret = current_branch;
1044 else
1045 ret = make_branch(name, 0);
1046 if (ret && ret->remote_name) {
1047 ret->remote = remote_get(ret->remote_name);
1048 if (ret->merge_nr) {
1049 int i;
1050 ret->merge = xcalloc(sizeof(*ret->merge),
1051 ret->merge_nr);
1052 for (i = 0; i < ret->merge_nr; i++) {
1053 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1054 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1055 remote_find_tracking(ret->remote,
1056 ret->merge[i]);
1060 return ret;
1063 int branch_has_merge_config(struct branch *branch)
1065 return branch && !!branch->merge;
1068 int branch_merge_matches(struct branch *branch,
1069 int i,
1070 const char *refname)
1072 if (!branch || i < 0 || i >= branch->merge_nr)
1073 return 0;
1074 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1077 static struct ref *get_expanded_map(const struct ref *remote_refs,
1078 const struct refspec *refspec)
1080 const struct ref *ref;
1081 struct ref *ret = NULL;
1082 struct ref **tail = &ret;
1084 int remote_prefix_len = strlen(refspec->src);
1085 int local_prefix_len = strlen(refspec->dst);
1087 for (ref = remote_refs; ref; ref = ref->next) {
1088 if (strchr(ref->name, '^'))
1089 continue; /* a dereference item */
1090 if (!prefixcmp(ref->name, refspec->src)) {
1091 const char *match;
1092 struct ref *cpy = copy_ref(ref);
1093 match = ref->name + remote_prefix_len;
1095 cpy->peer_ref = alloc_ref(local_prefix_len +
1096 strlen(match) + 1);
1097 sprintf(cpy->peer_ref->name, "%s%s",
1098 refspec->dst, match);
1099 if (refspec->force)
1100 cpy->peer_ref->force = 1;
1101 *tail = cpy;
1102 tail = &cpy->next;
1106 return ret;
1109 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1111 const struct ref *ref;
1112 for (ref = refs; ref; ref = ref->next) {
1113 if (refname_match(name, ref->name, ref_fetch_rules))
1114 return ref;
1116 return NULL;
1119 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1121 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1123 if (!ref)
1124 return NULL;
1126 return copy_ref(ref);
1129 static struct ref *get_local_ref(const char *name)
1131 struct ref *ret;
1132 if (!name)
1133 return NULL;
1135 if (!prefixcmp(name, "refs/")) {
1136 return alloc_ref_from_str(name);
1139 if (!prefixcmp(name, "heads/") ||
1140 !prefixcmp(name, "tags/") ||
1141 !prefixcmp(name, "remotes/")) {
1142 ret = alloc_ref(strlen(name) + 6);
1143 sprintf(ret->name, "refs/%s", name);
1144 return ret;
1147 ret = alloc_ref(strlen(name) + 12);
1148 sprintf(ret->name, "refs/heads/%s", name);
1149 return ret;
1152 int get_fetch_map(const struct ref *remote_refs,
1153 const struct refspec *refspec,
1154 struct ref ***tail,
1155 int missing_ok)
1157 struct ref *ref_map, **rmp;
1159 if (refspec->pattern) {
1160 ref_map = get_expanded_map(remote_refs, refspec);
1161 } else {
1162 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1164 ref_map = get_remote_ref(remote_refs, name);
1165 if (!missing_ok && !ref_map)
1166 die("Couldn't find remote ref %s", name);
1167 if (ref_map) {
1168 ref_map->peer_ref = get_local_ref(refspec->dst);
1169 if (ref_map->peer_ref && refspec->force)
1170 ref_map->peer_ref->force = 1;
1174 for (rmp = &ref_map; *rmp; ) {
1175 if ((*rmp)->peer_ref) {
1176 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1177 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1178 struct ref *ignore = *rmp;
1179 error("* Ignoring funny ref '%s' locally",
1180 (*rmp)->peer_ref->name);
1181 *rmp = (*rmp)->next;
1182 free(ignore->peer_ref);
1183 free(ignore);
1184 continue;
1187 rmp = &((*rmp)->next);
1190 if (ref_map)
1191 tail_link_ref(ref_map, tail);
1193 return 0;
1196 int resolve_remote_symref(struct ref *ref, struct ref *list)
1198 if (!ref->symref)
1199 return 0;
1200 for (; list; list = list->next)
1201 if (!strcmp(ref->symref, list->name)) {
1202 hashcpy(ref->old_sha1, list->old_sha1);
1203 return 0;
1205 return 1;