Windows: Implement a custom spawnve().
[git/git-bigfiles.git] / remote.c
blobff2c8021678fee5ef12060d35c6eb1960e8792c7
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 static struct refspec s_tag_refspec = {
6 0,
7 1,
8 0,
9 "refs/tags/",
10 "refs/tags/"
13 const struct refspec *tag_refspec = &s_tag_refspec;
15 struct counted_string {
16 size_t len;
17 const char *s;
19 struct rewrite {
20 const char *base;
21 size_t baselen;
22 struct counted_string *instead_of;
23 int instead_of_nr;
24 int instead_of_alloc;
27 static struct remote **remotes;
28 static int remotes_alloc;
29 static int remotes_nr;
31 static struct branch **branches;
32 static int branches_alloc;
33 static int branches_nr;
35 static struct branch *current_branch;
36 static const char *default_remote_name;
38 static struct rewrite **rewrite;
39 static int rewrite_alloc;
40 static int rewrite_nr;
42 #define BUF_SIZE (2048)
43 static char buffer[BUF_SIZE];
45 static const char *alias_url(const char *url)
47 int i, j;
48 char *ret;
49 struct counted_string *longest;
50 int longest_i;
52 longest = NULL;
53 longest_i = -1;
54 for (i = 0; i < rewrite_nr; i++) {
55 if (!rewrite[i])
56 continue;
57 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
58 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
59 (!longest ||
60 longest->len < rewrite[i]->instead_of[j].len)) {
61 longest = &(rewrite[i]->instead_of[j]);
62 longest_i = i;
66 if (!longest)
67 return url;
69 ret = malloc(rewrite[longest_i]->baselen +
70 (strlen(url) - longest->len) + 1);
71 strcpy(ret, rewrite[longest_i]->base);
72 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
73 return ret;
76 static void add_push_refspec(struct remote *remote, const char *ref)
78 ALLOC_GROW(remote->push_refspec,
79 remote->push_refspec_nr + 1,
80 remote->push_refspec_alloc);
81 remote->push_refspec[remote->push_refspec_nr++] = ref;
84 static void add_fetch_refspec(struct remote *remote, const char *ref)
86 ALLOC_GROW(remote->fetch_refspec,
87 remote->fetch_refspec_nr + 1,
88 remote->fetch_refspec_alloc);
89 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
92 static void add_url(struct remote *remote, const char *url)
94 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
95 remote->url[remote->url_nr++] = url;
98 static void add_url_alias(struct remote *remote, const char *url)
100 add_url(remote, alias_url(url));
103 static struct remote *make_remote(const char *name, int len)
105 struct remote *ret;
106 int i;
108 for (i = 0; i < remotes_nr; i++) {
109 if (len ? (!strncmp(name, remotes[i]->name, len) &&
110 !remotes[i]->name[len]) :
111 !strcmp(name, remotes[i]->name))
112 return remotes[i];
115 ret = xcalloc(1, sizeof(struct remote));
116 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
117 remotes[remotes_nr++] = ret;
118 if (len)
119 ret->name = xstrndup(name, len);
120 else
121 ret->name = xstrdup(name);
122 return ret;
125 static void add_merge(struct branch *branch, const char *name)
127 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
128 branch->merge_alloc);
129 branch->merge_name[branch->merge_nr++] = name;
132 static struct branch *make_branch(const char *name, int len)
134 struct branch *ret;
135 int i;
136 char *refname;
138 for (i = 0; i < branches_nr; i++) {
139 if (len ? (!strncmp(name, branches[i]->name, len) &&
140 !branches[i]->name[len]) :
141 !strcmp(name, branches[i]->name))
142 return branches[i];
145 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
146 ret = xcalloc(1, sizeof(struct branch));
147 branches[branches_nr++] = ret;
148 if (len)
149 ret->name = xstrndup(name, len);
150 else
151 ret->name = xstrdup(name);
152 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
153 strcpy(refname, "refs/heads/");
154 strcpy(refname + strlen("refs/heads/"), ret->name);
155 ret->refname = refname;
157 return ret;
160 static struct rewrite *make_rewrite(const char *base, int len)
162 struct rewrite *ret;
163 int i;
165 for (i = 0; i < rewrite_nr; i++) {
166 if (len
167 ? (len == rewrite[i]->baselen &&
168 !strncmp(base, rewrite[i]->base, len))
169 : !strcmp(base, rewrite[i]->base))
170 return rewrite[i];
173 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
174 ret = xcalloc(1, sizeof(struct rewrite));
175 rewrite[rewrite_nr++] = ret;
176 if (len) {
177 ret->base = xstrndup(base, len);
178 ret->baselen = len;
180 else {
181 ret->base = xstrdup(base);
182 ret->baselen = strlen(base);
184 return ret;
187 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
189 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
190 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
191 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
192 rewrite->instead_of_nr++;
195 static void read_remotes_file(struct remote *remote)
197 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
199 if (!f)
200 return;
201 while (fgets(buffer, BUF_SIZE, f)) {
202 int value_list;
203 char *s, *p;
205 if (!prefixcmp(buffer, "URL:")) {
206 value_list = 0;
207 s = buffer + 4;
208 } else if (!prefixcmp(buffer, "Push:")) {
209 value_list = 1;
210 s = buffer + 5;
211 } else if (!prefixcmp(buffer, "Pull:")) {
212 value_list = 2;
213 s = buffer + 5;
214 } else
215 continue;
217 while (isspace(*s))
218 s++;
219 if (!*s)
220 continue;
222 p = s + strlen(s);
223 while (isspace(p[-1]))
224 *--p = 0;
226 switch (value_list) {
227 case 0:
228 add_url_alias(remote, xstrdup(s));
229 break;
230 case 1:
231 add_push_refspec(remote, xstrdup(s));
232 break;
233 case 2:
234 add_fetch_refspec(remote, xstrdup(s));
235 break;
238 fclose(f);
241 static void read_branches_file(struct remote *remote)
243 const char *slash = strchr(remote->name, '/');
244 char *frag;
245 struct strbuf branch;
246 int n = slash ? slash - remote->name : 1000;
247 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
248 char *s, *p;
249 int len;
251 if (!f)
252 return;
253 s = fgets(buffer, BUF_SIZE, f);
254 fclose(f);
255 if (!s)
256 return;
257 while (isspace(*s))
258 s++;
259 if (!*s)
260 return;
261 p = s + strlen(s);
262 while (isspace(p[-1]))
263 *--p = 0;
264 len = p - s;
265 if (slash)
266 len += strlen(slash);
267 p = xmalloc(len + 1);
268 strcpy(p, s);
269 if (slash)
270 strcat(p, slash);
273 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
274 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
275 * the partial URL obtained from the branches file plus
276 * "/netdev-2.6" and does not store it in any tracking ref.
277 * #branch specifier in the file is ignored.
279 * Otherwise, the branches file would have URL and optionally
280 * #branch specified. The "master" (or specified) branch is
281 * fetched and stored in the local branch of the same name.
283 strbuf_init(&branch, 0);
284 frag = strchr(p, '#');
285 if (frag) {
286 *(frag++) = '\0';
287 strbuf_addf(&branch, "refs/heads/%s", frag);
288 } else
289 strbuf_addstr(&branch, "refs/heads/master");
290 if (!slash) {
291 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
292 } else {
293 strbuf_reset(&branch);
294 strbuf_addstr(&branch, "HEAD:");
296 add_url_alias(remote, p);
297 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
298 remote->fetch_tags = 1; /* always auto-follow */
301 static int handle_config(const char *key, const char *value, void *cb)
303 const char *name;
304 const char *subkey;
305 struct remote *remote;
306 struct branch *branch;
307 if (!prefixcmp(key, "branch.")) {
308 name = key + 7;
309 subkey = strrchr(name, '.');
310 if (!subkey)
311 return 0;
312 branch = make_branch(name, subkey - name);
313 if (!strcmp(subkey, ".remote")) {
314 if (!value)
315 return config_error_nonbool(key);
316 branch->remote_name = xstrdup(value);
317 if (branch == current_branch)
318 default_remote_name = branch->remote_name;
319 } else if (!strcmp(subkey, ".merge")) {
320 if (!value)
321 return config_error_nonbool(key);
322 add_merge(branch, xstrdup(value));
324 return 0;
326 if (!prefixcmp(key, "url.")) {
327 struct rewrite *rewrite;
328 name = key + 4;
329 subkey = strrchr(name, '.');
330 if (!subkey)
331 return 0;
332 rewrite = make_rewrite(name, subkey - name);
333 if (!strcmp(subkey, ".insteadof")) {
334 if (!value)
335 return config_error_nonbool(key);
336 add_instead_of(rewrite, xstrdup(value));
339 if (prefixcmp(key, "remote."))
340 return 0;
341 name = key + 7;
342 subkey = strrchr(name, '.');
343 if (!subkey)
344 return error("Config with no key for remote %s", name);
345 if (*subkey == '/') {
346 warning("Config remote shorthand cannot begin with '/': %s", name);
347 return 0;
349 remote = make_remote(name, subkey - name);
350 if (!strcmp(subkey, ".mirror"))
351 remote->mirror = git_config_bool(key, value);
352 else if (!strcmp(subkey, ".skipdefaultupdate"))
353 remote->skip_default_update = git_config_bool(key, value);
355 else if (!strcmp(subkey, ".url")) {
356 const char *v;
357 if (git_config_string(&v, key, value))
358 return -1;
359 add_url(remote, v);
360 } else if (!strcmp(subkey, ".push")) {
361 const char *v;
362 if (git_config_string(&v, key, value))
363 return -1;
364 add_push_refspec(remote, v);
365 } else if (!strcmp(subkey, ".fetch")) {
366 const char *v;
367 if (git_config_string(&v, key, value))
368 return -1;
369 add_fetch_refspec(remote, v);
370 } else if (!strcmp(subkey, ".receivepack")) {
371 const char *v;
372 if (git_config_string(&v, key, value))
373 return -1;
374 if (!remote->receivepack)
375 remote->receivepack = v;
376 else
377 error("more than one receivepack given, using the first");
378 } else if (!strcmp(subkey, ".uploadpack")) {
379 const char *v;
380 if (git_config_string(&v, key, value))
381 return -1;
382 if (!remote->uploadpack)
383 remote->uploadpack = v;
384 else
385 error("more than one uploadpack given, using the first");
386 } else if (!strcmp(subkey, ".tagopt")) {
387 if (!strcmp(value, "--no-tags"))
388 remote->fetch_tags = -1;
389 } else if (!strcmp(subkey, ".proxy")) {
390 return git_config_string((const char **)&remote->http_proxy,
391 key, value);
393 return 0;
396 static void alias_all_urls(void)
398 int i, j;
399 for (i = 0; i < remotes_nr; i++) {
400 if (!remotes[i])
401 continue;
402 for (j = 0; j < remotes[i]->url_nr; j++) {
403 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
408 static void read_config(void)
410 unsigned char sha1[20];
411 const char *head_ref;
412 int flag;
413 if (default_remote_name) // did this already
414 return;
415 default_remote_name = xstrdup("origin");
416 current_branch = NULL;
417 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
418 if (head_ref && (flag & REF_ISSYMREF) &&
419 !prefixcmp(head_ref, "refs/heads/")) {
420 current_branch =
421 make_branch(head_ref + strlen("refs/heads/"), 0);
423 git_config(handle_config, NULL);
424 alias_all_urls();
427 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
429 int i;
430 int st;
431 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
433 for (i = 0; i < nr_refspec; i++) {
434 size_t llen, rlen;
435 int is_glob;
436 const char *lhs, *rhs;
438 llen = rlen = is_glob = 0;
440 lhs = refspec[i];
441 if (*lhs == '+') {
442 rs[i].force = 1;
443 lhs++;
446 rhs = strrchr(lhs, ':');
449 * Before going on, special case ":" (or "+:") as a refspec
450 * for matching refs.
452 if (!fetch && rhs == lhs && rhs[1] == '\0') {
453 rs[i].matching = 1;
454 continue;
457 if (rhs) {
458 rhs++;
459 rlen = strlen(rhs);
460 is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
461 if (is_glob)
462 rlen -= 2;
463 rs[i].dst = xstrndup(rhs, rlen);
466 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
467 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
468 if ((rhs && !is_glob) || (!rhs && fetch))
469 goto invalid;
470 is_glob = 1;
471 llen -= 2;
472 } else if (rhs && is_glob) {
473 goto invalid;
476 rs[i].pattern = is_glob;
477 rs[i].src = xstrndup(lhs, llen);
479 if (fetch) {
481 * LHS
482 * - empty is allowed; it means HEAD.
483 * - otherwise it must be a valid looking ref.
485 if (!*rs[i].src)
486 ; /* empty is ok */
487 else {
488 st = check_ref_format(rs[i].src);
489 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
490 goto invalid;
493 * RHS
494 * - missing is ok, and is same as empty.
495 * - empty is ok; it means not to store.
496 * - otherwise it must be a valid looking ref.
498 if (!rs[i].dst) {
499 ; /* ok */
500 } else if (!*rs[i].dst) {
501 ; /* ok */
502 } else {
503 st = check_ref_format(rs[i].dst);
504 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
505 goto invalid;
507 } else {
509 * LHS
510 * - empty is allowed; it means delete.
511 * - when wildcarded, it must be a valid looking ref.
512 * - otherwise, it must be an extended SHA-1, but
513 * there is no existing way to validate this.
515 if (!*rs[i].src)
516 ; /* empty is ok */
517 else if (is_glob) {
518 st = check_ref_format(rs[i].src);
519 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
520 goto invalid;
522 else
523 ; /* anything goes, for now */
525 * RHS
526 * - missing is allowed, but LHS then must be a
527 * valid looking ref.
528 * - empty is not allowed.
529 * - otherwise it must be a valid looking ref.
531 if (!rs[i].dst) {
532 st = check_ref_format(rs[i].src);
533 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
534 goto invalid;
535 } else if (!*rs[i].dst) {
536 goto invalid;
537 } else {
538 st = check_ref_format(rs[i].dst);
539 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
540 goto invalid;
544 return rs;
546 invalid:
547 if (verify) {
548 free(rs);
549 return NULL;
551 die("Invalid refspec '%s'", refspec[i]);
554 int valid_fetch_refspec(const char *fetch_refspec_str)
556 const char *fetch_refspec[] = { fetch_refspec_str };
557 struct refspec *refspec;
559 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
560 if (refspec)
561 free(refspec);
562 return !!refspec;
565 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
567 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
570 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
572 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
575 static int valid_remote_nick(const char *name)
577 if (!name[0] || /* not empty */
578 (name[0] == '.' && /* not "." */
579 (!name[1] || /* not ".." */
580 (name[1] == '.' && !name[2]))))
581 return 0;
582 return !strchr(name, '/'); /* no slash */
585 struct remote *remote_get(const char *name)
587 struct remote *ret;
589 read_config();
590 if (!name)
591 name = default_remote_name;
592 ret = make_remote(name, 0);
593 if (valid_remote_nick(name)) {
594 if (!ret->url)
595 read_remotes_file(ret);
596 if (!ret->url)
597 read_branches_file(ret);
599 if (!ret->url)
600 add_url_alias(ret, name);
601 if (!ret->url)
602 return NULL;
603 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
604 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
605 return ret;
608 int for_each_remote(each_remote_fn fn, void *priv)
610 int i, result = 0;
611 read_config();
612 for (i = 0; i < remotes_nr && !result; i++) {
613 struct remote *r = remotes[i];
614 if (!r)
615 continue;
616 if (!r->fetch)
617 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
618 r->fetch_refspec);
619 if (!r->push)
620 r->push = parse_push_refspec(r->push_refspec_nr,
621 r->push_refspec);
622 result = fn(r, priv);
624 return result;
627 void ref_remove_duplicates(struct ref *ref_map)
629 struct ref **posn;
630 struct ref *next;
631 for (; ref_map; ref_map = ref_map->next) {
632 if (!ref_map->peer_ref)
633 continue;
634 posn = &ref_map->next;
635 while (*posn) {
636 if ((*posn)->peer_ref &&
637 !strcmp((*posn)->peer_ref->name,
638 ref_map->peer_ref->name)) {
639 if (strcmp((*posn)->name, ref_map->name))
640 die("%s tracks both %s and %s",
641 ref_map->peer_ref->name,
642 (*posn)->name, ref_map->name);
643 next = (*posn)->next;
644 free((*posn)->peer_ref);
645 free(*posn);
646 *posn = next;
647 } else {
648 posn = &(*posn)->next;
654 int remote_has_url(struct remote *remote, const char *url)
656 int i;
657 for (i = 0; i < remote->url_nr; i++) {
658 if (!strcmp(remote->url[i], url))
659 return 1;
661 return 0;
664 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
666 int find_src = refspec->src == NULL;
667 char *needle, **result;
668 int i;
670 if (find_src) {
671 if (!refspec->dst)
672 return error("find_tracking: need either src or dst");
673 needle = refspec->dst;
674 result = &refspec->src;
675 } else {
676 needle = refspec->src;
677 result = &refspec->dst;
680 for (i = 0; i < remote->fetch_refspec_nr; i++) {
681 struct refspec *fetch = &remote->fetch[i];
682 const char *key = find_src ? fetch->dst : fetch->src;
683 const char *value = find_src ? fetch->src : fetch->dst;
684 if (!fetch->dst)
685 continue;
686 if (fetch->pattern) {
687 if (!prefixcmp(needle, key) &&
688 needle[strlen(key)] == '/') {
689 *result = xmalloc(strlen(value) +
690 strlen(needle) -
691 strlen(key) + 1);
692 strcpy(*result, value);
693 strcpy(*result + strlen(value),
694 needle + strlen(key));
695 refspec->force = fetch->force;
696 return 0;
698 } else if (!strcmp(needle, key)) {
699 *result = xstrdup(value);
700 refspec->force = fetch->force;
701 return 0;
704 return -1;
707 struct ref *alloc_ref(unsigned namelen)
709 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
710 memset(ret, 0, sizeof(struct ref) + namelen);
711 return ret;
714 struct ref *alloc_ref_from_str(const char* str)
716 struct ref *ret = alloc_ref(strlen(str) + 1);
717 strcpy(ret->name, str);
718 return ret;
721 static struct ref *copy_ref(const struct ref *ref)
723 struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
724 memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
725 ret->next = NULL;
726 return ret;
729 struct ref *copy_ref_list(const struct ref *ref)
731 struct ref *ret = NULL;
732 struct ref **tail = &ret;
733 while (ref) {
734 *tail = copy_ref(ref);
735 ref = ref->next;
736 tail = &((*tail)->next);
738 return ret;
741 void free_ref(struct ref *ref)
743 if (!ref)
744 return;
745 free(ref->remote_status);
746 free(ref->symref);
747 free(ref);
750 void free_refs(struct ref *ref)
752 struct ref *next;
753 while (ref) {
754 next = ref->next;
755 free(ref->peer_ref);
756 free_ref(ref);
757 ref = next;
761 static int count_refspec_match(const char *pattern,
762 struct ref *refs,
763 struct ref **matched_ref)
765 int patlen = strlen(pattern);
766 struct ref *matched_weak = NULL;
767 struct ref *matched = NULL;
768 int weak_match = 0;
769 int match = 0;
771 for (weak_match = match = 0; refs; refs = refs->next) {
772 char *name = refs->name;
773 int namelen = strlen(name);
775 if (!refname_match(pattern, name, ref_rev_parse_rules))
776 continue;
778 /* A match is "weak" if it is with refs outside
779 * heads or tags, and did not specify the pattern
780 * in full (e.g. "refs/remotes/origin/master") or at
781 * least from the toplevel (e.g. "remotes/origin/master");
782 * otherwise "git push $URL master" would result in
783 * ambiguity between remotes/origin/master and heads/master
784 * at the remote site.
786 if (namelen != patlen &&
787 patlen != namelen - 5 &&
788 prefixcmp(name, "refs/heads/") &&
789 prefixcmp(name, "refs/tags/")) {
790 /* We want to catch the case where only weak
791 * matches are found and there are multiple
792 * matches, and where more than one strong
793 * matches are found, as ambiguous. One
794 * strong match with zero or more weak matches
795 * are acceptable as a unique match.
797 matched_weak = refs;
798 weak_match++;
800 else {
801 matched = refs;
802 match++;
805 if (!matched) {
806 *matched_ref = matched_weak;
807 return weak_match;
809 else {
810 *matched_ref = matched;
811 return match;
815 static void tail_link_ref(struct ref *ref, struct ref ***tail)
817 **tail = ref;
818 while (ref->next)
819 ref = ref->next;
820 *tail = &ref->next;
823 static struct ref *try_explicit_object_name(const char *name)
825 unsigned char sha1[20];
826 struct ref *ref;
828 if (!*name) {
829 ref = alloc_ref(20);
830 strcpy(ref->name, "(delete)");
831 hashclr(ref->new_sha1);
832 return ref;
834 if (get_sha1(name, sha1))
835 return NULL;
836 ref = alloc_ref_from_str(name);
837 hashcpy(ref->new_sha1, sha1);
838 return ref;
841 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
843 struct ref *ret = alloc_ref_from_str(name);
844 tail_link_ref(ret, tail);
845 return ret;
848 static char *guess_ref(const char *name, struct ref *peer)
850 struct strbuf buf = STRBUF_INIT;
851 unsigned char sha1[20];
853 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
854 if (!r)
855 return NULL;
857 if (!prefixcmp(r, "refs/heads/"))
858 strbuf_addstr(&buf, "refs/heads/");
859 else if (!prefixcmp(r, "refs/tags/"))
860 strbuf_addstr(&buf, "refs/tags/");
861 else
862 return NULL;
864 strbuf_addstr(&buf, name);
865 return strbuf_detach(&buf, NULL);
868 static int match_explicit(struct ref *src, struct ref *dst,
869 struct ref ***dst_tail,
870 struct refspec *rs)
872 struct ref *matched_src, *matched_dst;
874 const char *dst_value = rs->dst;
875 char *dst_guess;
877 if (rs->pattern || rs->matching)
878 return 0;
880 matched_src = matched_dst = NULL;
881 switch (count_refspec_match(rs->src, src, &matched_src)) {
882 case 1:
883 break;
884 case 0:
885 /* The source could be in the get_sha1() format
886 * not a reference name. :refs/other is a
887 * way to delete 'other' ref at the remote end.
889 matched_src = try_explicit_object_name(rs->src);
890 if (!matched_src)
891 return error("src refspec %s does not match any.", rs->src);
892 break;
893 default:
894 return error("src refspec %s matches more than one.", rs->src);
897 if (!dst_value) {
898 unsigned char sha1[20];
899 int flag;
901 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
902 if (!dst_value ||
903 ((flag & REF_ISSYMREF) &&
904 prefixcmp(dst_value, "refs/heads/")))
905 die("%s cannot be resolved to branch.",
906 matched_src->name);
909 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
910 case 1:
911 break;
912 case 0:
913 if (!memcmp(dst_value, "refs/", 5))
914 matched_dst = make_linked_ref(dst_value, dst_tail);
915 else if((dst_guess = guess_ref(dst_value, matched_src)))
916 matched_dst = make_linked_ref(dst_guess, dst_tail);
917 else
918 error("unable to push to unqualified destination: %s\n"
919 "The destination refspec neither matches an "
920 "existing ref on the remote nor\n"
921 "begins with refs/, and we are unable to "
922 "guess a prefix based on the source ref.",
923 dst_value);
924 break;
925 default:
926 matched_dst = NULL;
927 error("dst refspec %s matches more than one.",
928 dst_value);
929 break;
931 if (!matched_dst)
932 return -1;
933 if (matched_dst->peer_ref)
934 return error("dst ref %s receives from more than one src.",
935 matched_dst->name);
936 else {
937 matched_dst->peer_ref = matched_src;
938 matched_dst->force = rs->force;
940 return 0;
943 static int match_explicit_refs(struct ref *src, struct ref *dst,
944 struct ref ***dst_tail, struct refspec *rs,
945 int rs_nr)
947 int i, errs;
948 for (i = errs = 0; i < rs_nr; i++)
949 errs += match_explicit(src, dst, dst_tail, &rs[i]);
950 return errs;
953 static const struct refspec *check_pattern_match(const struct refspec *rs,
954 int rs_nr,
955 const struct ref *src)
957 int i;
958 int matching_refs = -1;
959 for (i = 0; i < rs_nr; i++) {
960 if (rs[i].matching &&
961 (matching_refs == -1 || rs[i].force)) {
962 matching_refs = i;
963 continue;
966 if (rs[i].pattern &&
967 !prefixcmp(src->name, rs[i].src) &&
968 src->name[strlen(rs[i].src)] == '/')
969 return rs + i;
971 if (matching_refs != -1)
972 return rs + matching_refs;
973 else
974 return NULL;
978 * Note. This is used only by "push"; refspec matching rules for
979 * push and fetch are subtly different, so do not try to reuse it
980 * without thinking.
982 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
983 int nr_refspec, const char **refspec, int flags)
985 struct refspec *rs;
986 int send_all = flags & MATCH_REFS_ALL;
987 int send_mirror = flags & MATCH_REFS_MIRROR;
988 static const char *default_refspec[] = { ":", 0 };
990 if (!nr_refspec) {
991 nr_refspec = 1;
992 refspec = default_refspec;
994 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
995 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
996 return -1;
998 /* pick the remainder */
999 for ( ; src; src = src->next) {
1000 struct ref *dst_peer;
1001 const struct refspec *pat = NULL;
1002 char *dst_name;
1003 if (src->peer_ref)
1004 continue;
1006 pat = check_pattern_match(rs, nr_refspec, src);
1007 if (!pat)
1008 continue;
1010 if (pat->matching) {
1012 * "matching refs"; traditionally we pushed everything
1013 * including refs outside refs/heads/ hierarchy, but
1014 * that does not make much sense these days.
1016 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1017 continue;
1018 dst_name = xstrdup(src->name);
1020 } else {
1021 const char *dst_side = pat->dst ? pat->dst : pat->src;
1022 dst_name = xmalloc(strlen(dst_side) +
1023 strlen(src->name) -
1024 strlen(pat->src) + 2);
1025 strcpy(dst_name, dst_side);
1026 strcat(dst_name, src->name + strlen(pat->src));
1028 dst_peer = find_ref_by_name(dst, dst_name);
1029 if (dst_peer) {
1030 if (dst_peer->peer_ref)
1031 /* We're already sending something to this ref. */
1032 goto free_name;
1034 } else {
1035 if (pat->matching && !(send_all || send_mirror))
1037 * Remote doesn't have it, and we have no
1038 * explicit pattern, and we don't have
1039 * --all nor --mirror.
1041 goto free_name;
1043 /* Create a new one and link it */
1044 dst_peer = make_linked_ref(dst_name, dst_tail);
1045 hashcpy(dst_peer->new_sha1, src->new_sha1);
1047 dst_peer->peer_ref = src;
1048 dst_peer->force = pat->force;
1049 free_name:
1050 free(dst_name);
1052 return 0;
1055 struct branch *branch_get(const char *name)
1057 struct branch *ret;
1059 read_config();
1060 if (!name || !*name || !strcmp(name, "HEAD"))
1061 ret = current_branch;
1062 else
1063 ret = make_branch(name, 0);
1064 if (ret && ret->remote_name) {
1065 ret->remote = remote_get(ret->remote_name);
1066 if (ret->merge_nr) {
1067 int i;
1068 ret->merge = xcalloc(sizeof(*ret->merge),
1069 ret->merge_nr);
1070 for (i = 0; i < ret->merge_nr; i++) {
1071 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1072 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1073 remote_find_tracking(ret->remote,
1074 ret->merge[i]);
1078 return ret;
1081 int branch_has_merge_config(struct branch *branch)
1083 return branch && !!branch->merge;
1086 int branch_merge_matches(struct branch *branch,
1087 int i,
1088 const char *refname)
1090 if (!branch || i < 0 || i >= branch->merge_nr)
1091 return 0;
1092 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1095 static struct ref *get_expanded_map(const struct ref *remote_refs,
1096 const struct refspec *refspec)
1098 const struct ref *ref;
1099 struct ref *ret = NULL;
1100 struct ref **tail = &ret;
1102 int remote_prefix_len = strlen(refspec->src);
1103 int local_prefix_len = strlen(refspec->dst);
1105 for (ref = remote_refs; ref; ref = ref->next) {
1106 if (strchr(ref->name, '^'))
1107 continue; /* a dereference item */
1108 if (!prefixcmp(ref->name, refspec->src)) {
1109 const char *match;
1110 struct ref *cpy = copy_ref(ref);
1111 match = ref->name + remote_prefix_len;
1113 cpy->peer_ref = alloc_ref(local_prefix_len +
1114 strlen(match) + 1);
1115 sprintf(cpy->peer_ref->name, "%s%s",
1116 refspec->dst, match);
1117 if (refspec->force)
1118 cpy->peer_ref->force = 1;
1119 *tail = cpy;
1120 tail = &cpy->next;
1124 return ret;
1127 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1129 const struct ref *ref;
1130 for (ref = refs; ref; ref = ref->next) {
1131 if (refname_match(name, ref->name, ref_fetch_rules))
1132 return ref;
1134 return NULL;
1137 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1139 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1141 if (!ref)
1142 return NULL;
1144 return copy_ref(ref);
1147 static struct ref *get_local_ref(const char *name)
1149 struct ref *ret;
1150 if (!name)
1151 return NULL;
1153 if (!prefixcmp(name, "refs/")) {
1154 return alloc_ref_from_str(name);
1157 if (!prefixcmp(name, "heads/") ||
1158 !prefixcmp(name, "tags/") ||
1159 !prefixcmp(name, "remotes/")) {
1160 ret = alloc_ref(strlen(name) + 6);
1161 sprintf(ret->name, "refs/%s", name);
1162 return ret;
1165 ret = alloc_ref(strlen(name) + 12);
1166 sprintf(ret->name, "refs/heads/%s", name);
1167 return ret;
1170 int get_fetch_map(const struct ref *remote_refs,
1171 const struct refspec *refspec,
1172 struct ref ***tail,
1173 int missing_ok)
1175 struct ref *ref_map, **rmp;
1177 if (refspec->pattern) {
1178 ref_map = get_expanded_map(remote_refs, refspec);
1179 } else {
1180 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1182 ref_map = get_remote_ref(remote_refs, name);
1183 if (!missing_ok && !ref_map)
1184 die("Couldn't find remote ref %s", name);
1185 if (ref_map) {
1186 ref_map->peer_ref = get_local_ref(refspec->dst);
1187 if (ref_map->peer_ref && refspec->force)
1188 ref_map->peer_ref->force = 1;
1192 for (rmp = &ref_map; *rmp; ) {
1193 if ((*rmp)->peer_ref) {
1194 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1195 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1196 struct ref *ignore = *rmp;
1197 error("* Ignoring funny ref '%s' locally",
1198 (*rmp)->peer_ref->name);
1199 *rmp = (*rmp)->next;
1200 free(ignore->peer_ref);
1201 free(ignore);
1202 continue;
1205 rmp = &((*rmp)->next);
1208 if (ref_map)
1209 tail_link_ref(ref_map, tail);
1211 return 0;
1214 int resolve_remote_symref(struct ref *ref, struct ref *list)
1216 if (!ref->symref)
1217 return 0;
1218 for (; list; list = list->next)
1219 if (!strcmp(ref->symref, list->name)) {
1220 hashcpy(ref->old_sha1, list->old_sha1);
1221 return 0;
1223 return 1;