Code

refspec: allow colon-less wildcard "refs/category/*"
[git.git] / remote.c
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 struct counted_string {
6         size_t len;
7         const char *s;
8 };
9 struct rewrite {
10         const char *base;
11         size_t baselen;
12         struct counted_string *instead_of;
13         int instead_of_nr;
14         int instead_of_alloc;
15 };
17 static struct remote **remotes;
18 static int remotes_alloc;
19 static int remotes_nr;
21 static struct branch **branches;
22 static int branches_alloc;
23 static int branches_nr;
25 static struct branch *current_branch;
26 static const char *default_remote_name;
28 static struct rewrite **rewrite;
29 static int rewrite_alloc;
30 static int rewrite_nr;
32 #define BUF_SIZE (2048)
33 static char buffer[BUF_SIZE];
35 static const char *alias_url(const char *url)
36 {
37         int i, j;
38         char *ret;
39         struct counted_string *longest;
40         int longest_i;
42         longest = NULL;
43         longest_i = -1;
44         for (i = 0; i < rewrite_nr; i++) {
45                 if (!rewrite[i])
46                         continue;
47                 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
48                         if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
49                             (!longest ||
50                              longest->len < rewrite[i]->instead_of[j].len)) {
51                                 longest = &(rewrite[i]->instead_of[j]);
52                                 longest_i = i;
53                         }
54                 }
55         }
56         if (!longest)
57                 return url;
59         ret = malloc(rewrite[longest_i]->baselen +
60                      (strlen(url) - longest->len) + 1);
61         strcpy(ret, rewrite[longest_i]->base);
62         strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
63         return ret;
64 }
66 static void add_push_refspec(struct remote *remote, const char *ref)
67 {
68         ALLOC_GROW(remote->push_refspec,
69                    remote->push_refspec_nr + 1,
70                    remote->push_refspec_alloc);
71         remote->push_refspec[remote->push_refspec_nr++] = ref;
72 }
74 static void add_fetch_refspec(struct remote *remote, const char *ref)
75 {
76         ALLOC_GROW(remote->fetch_refspec,
77                    remote->fetch_refspec_nr + 1,
78                    remote->fetch_refspec_alloc);
79         remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
80 }
82 static void add_url(struct remote *remote, const char *url)
83 {
84         ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
85         remote->url[remote->url_nr++] = url;
86 }
88 static void add_url_alias(struct remote *remote, const char *url)
89 {
90         add_url(remote, alias_url(url));
91 }
93 static struct remote *make_remote(const char *name, int len)
94 {
95         struct remote *ret;
96         int i;
98         for (i = 0; i < remotes_nr; i++) {
99                 if (len ? (!strncmp(name, remotes[i]->name, len) &&
100                            !remotes[i]->name[len]) :
101                     !strcmp(name, remotes[i]->name))
102                         return remotes[i];
103         }
105         ret = xcalloc(1, sizeof(struct remote));
106         ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
107         remotes[remotes_nr++] = ret;
108         if (len)
109                 ret->name = xstrndup(name, len);
110         else
111                 ret->name = xstrdup(name);
112         return ret;
115 static void add_merge(struct branch *branch, const char *name)
117         ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
118                    branch->merge_alloc);
119         branch->merge_name[branch->merge_nr++] = name;
122 static struct branch *make_branch(const char *name, int len)
124         struct branch *ret;
125         int i;
126         char *refname;
128         for (i = 0; i < branches_nr; i++) {
129                 if (len ? (!strncmp(name, branches[i]->name, len) &&
130                            !branches[i]->name[len]) :
131                     !strcmp(name, branches[i]->name))
132                         return branches[i];
133         }
135         ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
136         ret = xcalloc(1, sizeof(struct branch));
137         branches[branches_nr++] = ret;
138         if (len)
139                 ret->name = xstrndup(name, len);
140         else
141                 ret->name = xstrdup(name);
142         refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
143         strcpy(refname, "refs/heads/");
144         strcpy(refname + strlen("refs/heads/"), ret->name);
145         ret->refname = refname;
147         return ret;
150 static struct rewrite *make_rewrite(const char *base, int len)
152         struct rewrite *ret;
153         int i;
155         for (i = 0; i < rewrite_nr; i++) {
156                 if (len
157                     ? (len == rewrite[i]->baselen &&
158                        !strncmp(base, rewrite[i]->base, len))
159                     : !strcmp(base, rewrite[i]->base))
160                         return rewrite[i];
161         }
163         ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
164         ret = xcalloc(1, sizeof(struct rewrite));
165         rewrite[rewrite_nr++] = ret;
166         if (len) {
167                 ret->base = xstrndup(base, len);
168                 ret->baselen = len;
169         }
170         else {
171                 ret->base = xstrdup(base);
172                 ret->baselen = strlen(base);
173         }
174         return ret;
177 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
179         ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
180         rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
181         rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
182         rewrite->instead_of_nr++;
185 static void read_remotes_file(struct remote *remote)
187         FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
189         if (!f)
190                 return;
191         while (fgets(buffer, BUF_SIZE, f)) {
192                 int value_list;
193                 char *s, *p;
195                 if (!prefixcmp(buffer, "URL:")) {
196                         value_list = 0;
197                         s = buffer + 4;
198                 } else if (!prefixcmp(buffer, "Push:")) {
199                         value_list = 1;
200                         s = buffer + 5;
201                 } else if (!prefixcmp(buffer, "Pull:")) {
202                         value_list = 2;
203                         s = buffer + 5;
204                 } else
205                         continue;
207                 while (isspace(*s))
208                         s++;
209                 if (!*s)
210                         continue;
212                 p = s + strlen(s);
213                 while (isspace(p[-1]))
214                         *--p = 0;
216                 switch (value_list) {
217                 case 0:
218                         add_url_alias(remote, xstrdup(s));
219                         break;
220                 case 1:
221                         add_push_refspec(remote, xstrdup(s));
222                         break;
223                 case 2:
224                         add_fetch_refspec(remote, xstrdup(s));
225                         break;
226                 }
227         }
228         fclose(f);
231 static void read_branches_file(struct remote *remote)
233         const char *slash = strchr(remote->name, '/');
234         char *frag;
235         char *branch;
236         int n = slash ? slash - remote->name : 1000;
237         FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
238         char *s, *p;
239         int len;
241         if (!f)
242                 return;
243         s = fgets(buffer, BUF_SIZE, f);
244         fclose(f);
245         if (!s)
246                 return;
247         while (isspace(*s))
248                 s++;
249         if (!*s)
250                 return;
251         p = s + strlen(s);
252         while (isspace(p[-1]))
253                 *--p = 0;
254         len = p - s;
255         if (slash)
256                 len += strlen(slash);
257         p = xmalloc(len + 1);
258         strcpy(p, s);
259         if (slash)
260                 strcat(p, slash);
261         frag = strchr(p, '#');
262         if (frag) {
263                 *(frag++) = '\0';
264                 branch = xmalloc(strlen(frag) + 12);
265                 strcpy(branch, "refs/heads/");
266                 strcat(branch, frag);
267         } else {
268                 branch = "refs/heads/master";
269         }
270         add_url_alias(remote, p);
271         add_fetch_refspec(remote, branch);
272         remote->fetch_tags = 1; /* always auto-follow */
275 static int handle_config(const char *key, const char *value)
277         const char *name;
278         const char *subkey;
279         struct remote *remote;
280         struct branch *branch;
281         if (!prefixcmp(key, "branch.")) {
282                 name = key + 7;
283                 subkey = strrchr(name, '.');
284                 if (!subkey)
285                         return 0;
286                 branch = make_branch(name, subkey - name);
287                 if (!strcmp(subkey, ".remote")) {
288                         if (!value)
289                                 return config_error_nonbool(key);
290                         branch->remote_name = xstrdup(value);
291                         if (branch == current_branch)
292                                 default_remote_name = branch->remote_name;
293                 } else if (!strcmp(subkey, ".merge")) {
294                         if (!value)
295                                 return config_error_nonbool(key);
296                         add_merge(branch, xstrdup(value));
297                 }
298                 return 0;
299         }
300         if (!prefixcmp(key, "url.")) {
301                 struct rewrite *rewrite;
302                 name = key + 5;
303                 subkey = strrchr(name, '.');
304                 if (!subkey)
305                         return 0;
306                 rewrite = make_rewrite(name, subkey - name);
307                 if (!strcmp(subkey, ".insteadof")) {
308                         if (!value)
309                                 return config_error_nonbool(key);
310                         add_instead_of(rewrite, xstrdup(value));
311                 }
312         }
313         if (prefixcmp(key,  "remote."))
314                 return 0;
315         name = key + 7;
316         subkey = strrchr(name, '.');
317         if (!subkey)
318                 return error("Config with no key for remote %s", name);
319         if (*subkey == '/') {
320                 warning("Config remote shorthand cannot begin with '/': %s", name);
321                 return 0;
322         }
323         remote = make_remote(name, subkey - name);
324         if (!value) {
325                 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
326                  * [remote "frotz"]
327                  *      disabled
328                  * is a valid way to set it to true; we get NULL in value so
329                  * we need to handle it here.
330                  *
331                  * if (!strcmp(subkey, ".disabled")) {
332                  *      val = git_config_bool(key, value);
333                  *      return 0;
334                  * } else
335                  *
336                  */
337                 return 0; /* ignore unknown booleans */
338         }
339         if (!strcmp(subkey, ".url")) {
340                 add_url(remote, xstrdup(value));
341         } else if (!strcmp(subkey, ".push")) {
342                 add_push_refspec(remote, xstrdup(value));
343         } else if (!strcmp(subkey, ".fetch")) {
344                 add_fetch_refspec(remote, xstrdup(value));
345         } else if (!strcmp(subkey, ".receivepack")) {
346                 if (!remote->receivepack)
347                         remote->receivepack = xstrdup(value);
348                 else
349                         error("more than one receivepack given, using the first");
350         } else if (!strcmp(subkey, ".uploadpack")) {
351                 if (!remote->uploadpack)
352                         remote->uploadpack = xstrdup(value);
353                 else
354                         error("more than one uploadpack given, using the first");
355         } else if (!strcmp(subkey, ".tagopt")) {
356                 if (!strcmp(value, "--no-tags"))
357                         remote->fetch_tags = -1;
358         } else if (!strcmp(subkey, ".proxy")) {
359                 remote->http_proxy = xstrdup(value);
360         } else if (!strcmp(subkey, ".skipdefaultupdate"))
361                 remote->skip_default_update = 1;
362         return 0;
365 static void alias_all_urls(void)
367         int i, j;
368         for (i = 0; i < remotes_nr; i++) {
369                 if (!remotes[i])
370                         continue;
371                 for (j = 0; j < remotes[i]->url_nr; j++) {
372                         remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
373                 }
374         }
377 static void read_config(void)
379         unsigned char sha1[20];
380         const char *head_ref;
381         int flag;
382         if (default_remote_name) // did this already
383                 return;
384         default_remote_name = xstrdup("origin");
385         current_branch = NULL;
386         head_ref = resolve_ref("HEAD", sha1, 0, &flag);
387         if (head_ref && (flag & REF_ISSYMREF) &&
388             !prefixcmp(head_ref, "refs/heads/")) {
389                 current_branch =
390                         make_branch(head_ref + strlen("refs/heads/"), 0);
391         }
392         git_config(handle_config);
393         alias_all_urls();
396 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
398         int i;
399         int st;
400         struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
402         for (i = 0; i < nr_refspec; i++) {
403                 size_t llen, rlen;
404                 int is_glob;
405                 const char *lhs, *rhs;
407                 llen = rlen = is_glob = 0;
409                 lhs = refspec[i];
410                 if (*lhs == '+') {
411                         rs[i].force = 1;
412                         lhs++;
413                 }
415                 rhs = strrchr(lhs, ':');
416                 if (rhs) {
417                         rhs++;
418                         rlen = strlen(rhs);
419                         is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
420                         if (is_glob)
421                                 rlen -= 2;
422                         rs[i].dst = xstrndup(rhs, rlen);
423                 }
425                 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
426                 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
427                         if ((rhs && !is_glob) || (!rhs && fetch))
428                                 goto invalid;
429                         is_glob = 1;
430                         llen -= 2;
431                 } else if (rhs && is_glob) {
432                         goto invalid;
433                 }
435                 rs[i].pattern = is_glob;
436                 rs[i].src = xstrndup(lhs, llen);
438                 if (fetch) {
439                         /*
440                          * LHS
441                          * - empty is allowed; it means HEAD.
442                          * - otherwise it must be a valid looking ref.
443                          */
444                         if (!*rs[i].src)
445                                 ; /* empty is ok */
446                         else {
447                                 st = check_ref_format(rs[i].src);
448                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
449                                         goto invalid;
450                         }
451                         /*
452                          * RHS
453                          * - missing is ok, and is same as empty.
454                          * - empty is ok; it means not to store.
455                          * - otherwise it must be a valid looking ref.
456                          */
457                         if (!rs[i].dst) {
458                                 ; /* ok */
459                         } else if (!*rs[i].dst) {
460                                 ; /* ok */
461                         } else {
462                                 st = check_ref_format(rs[i].dst);
463                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
464                                         goto invalid;
465                         }
466                 } else {
467                         /*
468                          * LHS
469                          * - empty is allowed; it means delete.
470                          * - when wildcarded, it must be a valid looking ref.
471                          * - otherwise, it must be an extended SHA-1, but
472                          *   there is no existing way to validate this.
473                          */
474                         if (!*rs[i].src)
475                                 ; /* empty is ok */
476                         else if (is_glob) {
477                                 st = check_ref_format(rs[i].src);
478                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
479                                         goto invalid;
480                         }
481                         else
482                                 ; /* anything goes, for now */
483                         /*
484                          * RHS
485                          * - missing is allowed, but LHS then must be a
486                          *   valid looking ref.
487                          * - empty is not allowed.
488                          * - otherwise it must be a valid looking ref.
489                          */
490                         if (!rs[i].dst) {
491                                 st = check_ref_format(rs[i].src);
492                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
493                                         goto invalid;
494                         } else if (!*rs[i].dst) {
495                                 goto invalid;
496                         } else {
497                                 st = check_ref_format(rs[i].dst);
498                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
499                                         goto invalid;
500                         }
501                 }
502         }
503         return rs;
505  invalid:
506         die("Invalid refspec '%s'", refspec[i]);
509 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
511         return parse_refspec_internal(nr_refspec, refspec, 1);
514 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
516         return parse_refspec_internal(nr_refspec, refspec, 0);
519 static int valid_remote_nick(const char *name)
521         if (!name[0] || /* not empty */
522             (name[0] == '.' && /* not "." */
523              (!name[1] || /* not ".." */
524               (name[1] == '.' && !name[2]))))
525                 return 0;
526         return !strchr(name, '/'); /* no slash */
529 struct remote *remote_get(const char *name)
531         struct remote *ret;
533         read_config();
534         if (!name)
535                 name = default_remote_name;
536         ret = make_remote(name, 0);
537         if (valid_remote_nick(name)) {
538                 if (!ret->url)
539                         read_remotes_file(ret);
540                 if (!ret->url)
541                         read_branches_file(ret);
542         }
543         if (!ret->url)
544                 add_url_alias(ret, name);
545         if (!ret->url)
546                 return NULL;
547         ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
548         ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
549         return ret;
552 int for_each_remote(each_remote_fn fn, void *priv)
554         int i, result = 0;
555         read_config();
556         for (i = 0; i < remotes_nr && !result; i++) {
557                 struct remote *r = remotes[i];
558                 if (!r)
559                         continue;
560                 if (!r->fetch)
561                         r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
562                                                        r->fetch_refspec);
563                 if (!r->push)
564                         r->push = parse_push_refspec(r->push_refspec_nr,
565                                                      r->push_refspec);
566                 result = fn(r, priv);
567         }
568         return result;
571 void ref_remove_duplicates(struct ref *ref_map)
573         struct ref **posn;
574         struct ref *next;
575         for (; ref_map; ref_map = ref_map->next) {
576                 if (!ref_map->peer_ref)
577                         continue;
578                 posn = &ref_map->next;
579                 while (*posn) {
580                         if ((*posn)->peer_ref &&
581                             !strcmp((*posn)->peer_ref->name,
582                                     ref_map->peer_ref->name)) {
583                                 if (strcmp((*posn)->name, ref_map->name))
584                                         die("%s tracks both %s and %s",
585                                             ref_map->peer_ref->name,
586                                             (*posn)->name, ref_map->name);
587                                 next = (*posn)->next;
588                                 free((*posn)->peer_ref);
589                                 free(*posn);
590                                 *posn = next;
591                         } else {
592                                 posn = &(*posn)->next;
593                         }
594                 }
595         }
598 int remote_has_url(struct remote *remote, const char *url)
600         int i;
601         for (i = 0; i < remote->url_nr; i++) {
602                 if (!strcmp(remote->url[i], url))
603                         return 1;
604         }
605         return 0;
608 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
610         int find_src = refspec->src == NULL;
611         char *needle, **result;
612         int i;
614         if (find_src) {
615                 if (!refspec->dst)
616                         return error("find_tracking: need either src or dst");
617                 needle = refspec->dst;
618                 result = &refspec->src;
619         } else {
620                 needle = refspec->src;
621                 result = &refspec->dst;
622         }
624         for (i = 0; i < remote->fetch_refspec_nr; i++) {
625                 struct refspec *fetch = &remote->fetch[i];
626                 const char *key = find_src ? fetch->dst : fetch->src;
627                 const char *value = find_src ? fetch->src : fetch->dst;
628                 if (!fetch->dst)
629                         continue;
630                 if (fetch->pattern) {
631                         if (!prefixcmp(needle, key) &&
632                             needle[strlen(key)] == '/') {
633                                 *result = xmalloc(strlen(value) +
634                                                   strlen(needle) -
635                                                   strlen(key) + 1);
636                                 strcpy(*result, value);
637                                 strcpy(*result + strlen(value),
638                                        needle + strlen(key));
639                                 refspec->force = fetch->force;
640                                 return 0;
641                         }
642                 } else if (!strcmp(needle, key)) {
643                         *result = xstrdup(value);
644                         refspec->force = fetch->force;
645                         return 0;
646                 }
647         }
648         return -1;
651 struct ref *alloc_ref(unsigned namelen)
653         struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
654         memset(ret, 0, sizeof(struct ref) + namelen);
655         return ret;
658 static struct ref *copy_ref(const struct ref *ref)
660         struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
661         memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
662         ret->next = NULL;
663         return ret;
666 struct ref *copy_ref_list(const struct ref *ref)
668         struct ref *ret = NULL;
669         struct ref **tail = &ret;
670         while (ref) {
671                 *tail = copy_ref(ref);
672                 ref = ref->next;
673                 tail = &((*tail)->next);
674         }
675         return ret;
678 void free_refs(struct ref *ref)
680         struct ref *next;
681         while (ref) {
682                 next = ref->next;
683                 free(ref->peer_ref);
684                 free(ref);
685                 ref = next;
686         }
689 static int count_refspec_match(const char *pattern,
690                                struct ref *refs,
691                                struct ref **matched_ref)
693         int patlen = strlen(pattern);
694         struct ref *matched_weak = NULL;
695         struct ref *matched = NULL;
696         int weak_match = 0;
697         int match = 0;
699         for (weak_match = match = 0; refs; refs = refs->next) {
700                 char *name = refs->name;
701                 int namelen = strlen(name);
703                 if (!refname_match(pattern, name, ref_rev_parse_rules))
704                         continue;
706                 /* A match is "weak" if it is with refs outside
707                  * heads or tags, and did not specify the pattern
708                  * in full (e.g. "refs/remotes/origin/master") or at
709                  * least from the toplevel (e.g. "remotes/origin/master");
710                  * otherwise "git push $URL master" would result in
711                  * ambiguity between remotes/origin/master and heads/master
712                  * at the remote site.
713                  */
714                 if (namelen != patlen &&
715                     patlen != namelen - 5 &&
716                     prefixcmp(name, "refs/heads/") &&
717                     prefixcmp(name, "refs/tags/")) {
718                         /* We want to catch the case where only weak
719                          * matches are found and there are multiple
720                          * matches, and where more than one strong
721                          * matches are found, as ambiguous.  One
722                          * strong match with zero or more weak matches
723                          * are acceptable as a unique match.
724                          */
725                         matched_weak = refs;
726                         weak_match++;
727                 }
728                 else {
729                         matched = refs;
730                         match++;
731                 }
732         }
733         if (!matched) {
734                 *matched_ref = matched_weak;
735                 return weak_match;
736         }
737         else {
738                 *matched_ref = matched;
739                 return match;
740         }
743 static void tail_link_ref(struct ref *ref, struct ref ***tail)
745         **tail = ref;
746         while (ref->next)
747                 ref = ref->next;
748         *tail = &ref->next;
751 static struct ref *try_explicit_object_name(const char *name)
753         unsigned char sha1[20];
754         struct ref *ref;
755         int len;
757         if (!*name) {
758                 ref = alloc_ref(20);
759                 strcpy(ref->name, "(delete)");
760                 hashclr(ref->new_sha1);
761                 return ref;
762         }
763         if (get_sha1(name, sha1))
764                 return NULL;
765         len = strlen(name) + 1;
766         ref = alloc_ref(len);
767         memcpy(ref->name, name, len);
768         hashcpy(ref->new_sha1, sha1);
769         return ref;
772 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
774         struct ref *ret;
775         size_t len;
777         len = strlen(name) + 1;
778         ret = alloc_ref(len);
779         memcpy(ret->name, name, len);
780         tail_link_ref(ret, tail);
781         return ret;
784 static int match_explicit(struct ref *src, struct ref *dst,
785                           struct ref ***dst_tail,
786                           struct refspec *rs,
787                           int errs)
789         struct ref *matched_src, *matched_dst;
791         const char *dst_value = rs->dst;
793         if (rs->pattern)
794                 return errs;
796         matched_src = matched_dst = NULL;
797         switch (count_refspec_match(rs->src, src, &matched_src)) {
798         case 1:
799                 break;
800         case 0:
801                 /* The source could be in the get_sha1() format
802                  * not a reference name.  :refs/other is a
803                  * way to delete 'other' ref at the remote end.
804                  */
805                 matched_src = try_explicit_object_name(rs->src);
806                 if (!matched_src)
807                         error("src refspec %s does not match any.", rs->src);
808                 break;
809         default:
810                 matched_src = NULL;
811                 error("src refspec %s matches more than one.", rs->src);
812                 break;
813         }
815         if (!matched_src)
816                 errs = 1;
818         if (!dst_value) {
819                 unsigned char sha1[20];
820                 int flag;
822                 if (!matched_src)
823                         return errs;
824                 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
825                 if (!dst_value ||
826                     ((flag & REF_ISSYMREF) &&
827                      prefixcmp(dst_value, "refs/heads/")))
828                         die("%s cannot be resolved to branch.",
829                             matched_src->name);
830         }
832         switch (count_refspec_match(dst_value, dst, &matched_dst)) {
833         case 1:
834                 break;
835         case 0:
836                 if (!memcmp(dst_value, "refs/", 5))
837                         matched_dst = make_linked_ref(dst_value, dst_tail);
838                 else
839                         error("dst refspec %s does not match any "
840                               "existing ref on the remote and does "
841                               "not start with refs/.", dst_value);
842                 break;
843         default:
844                 matched_dst = NULL;
845                 error("dst refspec %s matches more than one.",
846                       dst_value);
847                 break;
848         }
849         if (errs || !matched_dst)
850                 return 1;
851         if (matched_dst->peer_ref) {
852                 errs = 1;
853                 error("dst ref %s receives from more than one src.",
854                       matched_dst->name);
855         }
856         else {
857                 matched_dst->peer_ref = matched_src;
858                 matched_dst->force = rs->force;
859         }
860         return errs;
863 static int match_explicit_refs(struct ref *src, struct ref *dst,
864                                struct ref ***dst_tail, struct refspec *rs,
865                                int rs_nr)
867         int i, errs;
868         for (i = errs = 0; i < rs_nr; i++)
869                 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
870         return -errs;
873 static const struct refspec *check_pattern_match(const struct refspec *rs,
874                                                  int rs_nr,
875                                                  const struct ref *src)
877         int i;
878         for (i = 0; i < rs_nr; i++) {
879                 if (rs[i].pattern &&
880                     !prefixcmp(src->name, rs[i].src) &&
881                     src->name[strlen(rs[i].src)] == '/')
882                         return rs + i;
883         }
884         return NULL;
887 /*
888  * Note. This is used only by "push"; refspec matching rules for
889  * push and fetch are subtly different, so do not try to reuse it
890  * without thinking.
891  */
892 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
893                int nr_refspec, const char **refspec, int flags)
895         struct refspec *rs =
896                 parse_push_refspec(nr_refspec, (const char **) refspec);
897         int send_all = flags & MATCH_REFS_ALL;
898         int send_mirror = flags & MATCH_REFS_MIRROR;
900         if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
901                 return -1;
903         /* pick the remainder */
904         for ( ; src; src = src->next) {
905                 struct ref *dst_peer;
906                 const struct refspec *pat = NULL;
907                 char *dst_name;
908                 if (src->peer_ref)
909                         continue;
910                 if (nr_refspec) {
911                         pat = check_pattern_match(rs, nr_refspec, src);
912                         if (!pat)
913                                 continue;
914                 }
915                 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
916                         /*
917                          * "matching refs"; traditionally we pushed everything
918                          * including refs outside refs/heads/ hierarchy, but
919                          * that does not make much sense these days.
920                          */
921                         continue;
923                 if (pat) {
924                         const char *dst_side = pat->dst ? pat->dst : pat->src;
925                         dst_name = xmalloc(strlen(dst_side) +
926                                            strlen(src->name) -
927                                            strlen(pat->src) + 2);
928                         strcpy(dst_name, dst_side);
929                         strcat(dst_name, src->name + strlen(pat->src));
930                 } else
931                         dst_name = xstrdup(src->name);
932                 dst_peer = find_ref_by_name(dst, dst_name);
933                 if (dst_peer && dst_peer->peer_ref)
934                         /* We're already sending something to this ref. */
935                         goto free_name;
937                 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
938                         /*
939                          * Remote doesn't have it, and we have no
940                          * explicit pattern, and we don't have
941                          * --all nor --mirror.
942                          */
943                         goto free_name;
944                 if (!dst_peer) {
945                         /* Create a new one and link it */
946                         dst_peer = make_linked_ref(dst_name, dst_tail);
947                         hashcpy(dst_peer->new_sha1, src->new_sha1);
948                 }
949                 dst_peer->peer_ref = src;
950                 if (pat)
951                         dst_peer->force = pat->force;
952         free_name:
953                 free(dst_name);
954         }
955         return 0;
958 struct branch *branch_get(const char *name)
960         struct branch *ret;
962         read_config();
963         if (!name || !*name || !strcmp(name, "HEAD"))
964                 ret = current_branch;
965         else
966                 ret = make_branch(name, 0);
967         if (ret && ret->remote_name) {
968                 ret->remote = remote_get(ret->remote_name);
969                 if (ret->merge_nr) {
970                         int i;
971                         ret->merge = xcalloc(sizeof(*ret->merge),
972                                              ret->merge_nr);
973                         for (i = 0; i < ret->merge_nr; i++) {
974                                 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
975                                 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
976                                 remote_find_tracking(ret->remote,
977                                                      ret->merge[i]);
978                         }
979                 }
980         }
981         return ret;
984 int branch_has_merge_config(struct branch *branch)
986         return branch && !!branch->merge;
989 int branch_merge_matches(struct branch *branch,
990                                  int i,
991                                  const char *refname)
993         if (!branch || i < 0 || i >= branch->merge_nr)
994                 return 0;
995         return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
998 static struct ref *get_expanded_map(const struct ref *remote_refs,
999                                     const struct refspec *refspec)
1001         const struct ref *ref;
1002         struct ref *ret = NULL;
1003         struct ref **tail = &ret;
1005         int remote_prefix_len = strlen(refspec->src);
1006         int local_prefix_len = strlen(refspec->dst);
1008         for (ref = remote_refs; ref; ref = ref->next) {
1009                 if (strchr(ref->name, '^'))
1010                         continue; /* a dereference item */
1011                 if (!prefixcmp(ref->name, refspec->src)) {
1012                         const char *match;
1013                         struct ref *cpy = copy_ref(ref);
1014                         match = ref->name + remote_prefix_len;
1016                         cpy->peer_ref = alloc_ref(local_prefix_len +
1017                                                   strlen(match) + 1);
1018                         sprintf(cpy->peer_ref->name, "%s%s",
1019                                 refspec->dst, match);
1020                         if (refspec->force)
1021                                 cpy->peer_ref->force = 1;
1022                         *tail = cpy;
1023                         tail = &cpy->next;
1024                 }
1025         }
1027         return ret;
1030 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1032         const struct ref *ref;
1033         for (ref = refs; ref; ref = ref->next) {
1034                 if (refname_match(name, ref->name, ref_fetch_rules))
1035                         return ref;
1036         }
1037         return NULL;
1040 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1042         const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1044         if (!ref)
1045                 return NULL;
1047         return copy_ref(ref);
1050 static struct ref *get_local_ref(const char *name)
1052         struct ref *ret;
1053         if (!name)
1054                 return NULL;
1056         if (!prefixcmp(name, "refs/")) {
1057                 ret = alloc_ref(strlen(name) + 1);
1058                 strcpy(ret->name, name);
1059                 return ret;
1060         }
1062         if (!prefixcmp(name, "heads/") ||
1063             !prefixcmp(name, "tags/") ||
1064             !prefixcmp(name, "remotes/")) {
1065                 ret = alloc_ref(strlen(name) + 6);
1066                 sprintf(ret->name, "refs/%s", name);
1067                 return ret;
1068         }
1070         ret = alloc_ref(strlen(name) + 12);
1071         sprintf(ret->name, "refs/heads/%s", name);
1072         return ret;
1075 int get_fetch_map(const struct ref *remote_refs,
1076                   const struct refspec *refspec,
1077                   struct ref ***tail,
1078                   int missing_ok)
1080         struct ref *ref_map, **rmp;
1082         if (refspec->pattern) {
1083                 ref_map = get_expanded_map(remote_refs, refspec);
1084         } else {
1085                 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1087                 ref_map = get_remote_ref(remote_refs, name);
1088                 if (!missing_ok && !ref_map)
1089                         die("Couldn't find remote ref %s", name);
1090                 if (ref_map) {
1091                         ref_map->peer_ref = get_local_ref(refspec->dst);
1092                         if (ref_map->peer_ref && refspec->force)
1093                                 ref_map->peer_ref->force = 1;
1094                 }
1095         }
1097         for (rmp = &ref_map; *rmp; ) {
1098                 if ((*rmp)->peer_ref) {
1099                         int st = check_ref_format((*rmp)->peer_ref->name + 5);
1100                         if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1101                                 struct ref *ignore = *rmp;
1102                                 error("* Ignoring funny ref '%s' locally",
1103                                       (*rmp)->peer_ref->name);
1104                                 *rmp = (*rmp)->next;
1105                                 free(ignore->peer_ref);
1106                                 free(ignore);
1107                                 continue;
1108                         }
1109                 }
1110                 rmp = &((*rmp)->next);
1111         }
1113         if (ref_map)
1114                 tail_link_ref(ref_map, tail);
1116         return 0;