Code

Merge branch 'lt/core-optim'
[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         struct strbuf 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);
262         /*
263          * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
264          * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
265          * the partial URL obtained from the branches file plus
266          * "/netdev-2.6" and does not store it in any tracking ref.
267          * #branch specifier in the file is ignored.
268          *
269          * Otherwise, the branches file would have URL and optionally
270          * #branch specified.  The "master" (or specified) branch is
271          * fetched and stored in the local branch of the same name.
272          */
273         strbuf_init(&branch, 0);
274         frag = strchr(p, '#');
275         if (frag) {
276                 *(frag++) = '\0';
277                 strbuf_addf(&branch, "refs/heads/%s", frag);
278         } else
279                 strbuf_addstr(&branch, "refs/heads/master");
280         if (!slash) {
281                 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
282         } else {
283                 strbuf_reset(&branch);
284                 strbuf_addstr(&branch, "HEAD:");
285         }
286         add_url_alias(remote, p);
287         add_fetch_refspec(remote, strbuf_detach(&branch, 0));
288         remote->fetch_tags = 1; /* always auto-follow */
291 static int handle_config(const char *key, const char *value)
293         const char *name;
294         const char *subkey;
295         struct remote *remote;
296         struct branch *branch;
297         if (!prefixcmp(key, "branch.")) {
298                 name = key + 7;
299                 subkey = strrchr(name, '.');
300                 if (!subkey)
301                         return 0;
302                 branch = make_branch(name, subkey - name);
303                 if (!strcmp(subkey, ".remote")) {
304                         if (!value)
305                                 return config_error_nonbool(key);
306                         branch->remote_name = xstrdup(value);
307                         if (branch == current_branch)
308                                 default_remote_name = branch->remote_name;
309                 } else if (!strcmp(subkey, ".merge")) {
310                         if (!value)
311                                 return config_error_nonbool(key);
312                         add_merge(branch, xstrdup(value));
313                 }
314                 return 0;
315         }
316         if (!prefixcmp(key, "url.")) {
317                 struct rewrite *rewrite;
318                 name = key + 4;
319                 subkey = strrchr(name, '.');
320                 if (!subkey)
321                         return 0;
322                 rewrite = make_rewrite(name, subkey - name);
323                 if (!strcmp(subkey, ".insteadof")) {
324                         if (!value)
325                                 return config_error_nonbool(key);
326                         add_instead_of(rewrite, xstrdup(value));
327                 }
328         }
329         if (prefixcmp(key,  "remote."))
330                 return 0;
331         name = key + 7;
332         subkey = strrchr(name, '.');
333         if (!subkey)
334                 return error("Config with no key for remote %s", name);
335         if (*subkey == '/') {
336                 warning("Config remote shorthand cannot begin with '/': %s", name);
337                 return 0;
338         }
339         remote = make_remote(name, subkey - name);
340         if (!strcmp(subkey, ".mirror"))
341                 remote->mirror = git_config_bool(key, value);
342         else if (!strcmp(subkey, ".skipdefaultupdate"))
343                 remote->skip_default_update = git_config_bool(key, value);
345         else if (!strcmp(subkey, ".url")) {
346                 const char *v;
347                 if (git_config_string(&v, key, value))
348                         return -1;
349                 add_url(remote, v);
350         } else if (!strcmp(subkey, ".push")) {
351                 const char *v;
352                 if (git_config_string(&v, key, value))
353                         return -1;
354                 add_push_refspec(remote, v);
355         } else if (!strcmp(subkey, ".fetch")) {
356                 const char *v;
357                 if (git_config_string(&v, key, value))
358                         return -1;
359                 add_fetch_refspec(remote, v);
360         } else if (!strcmp(subkey, ".receivepack")) {
361                 const char *v;
362                 if (git_config_string(&v, key, value))
363                         return -1;
364                 if (!remote->receivepack)
365                         remote->receivepack = v;
366                 else
367                         error("more than one receivepack given, using the first");
368         } else if (!strcmp(subkey, ".uploadpack")) {
369                 const char *v;
370                 if (git_config_string(&v, key, value))
371                         return -1;
372                 if (!remote->uploadpack)
373                         remote->uploadpack = v;
374                 else
375                         error("more than one uploadpack given, using the first");
376         } else if (!strcmp(subkey, ".tagopt")) {
377                 if (!strcmp(value, "--no-tags"))
378                         remote->fetch_tags = -1;
379         } else if (!strcmp(subkey, ".proxy")) {
380                 return git_config_string((const char **)&remote->http_proxy,
381                                          key, value);
382         }
383         return 0;
386 static void alias_all_urls(void)
388         int i, j;
389         for (i = 0; i < remotes_nr; i++) {
390                 if (!remotes[i])
391                         continue;
392                 for (j = 0; j < remotes[i]->url_nr; j++) {
393                         remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
394                 }
395         }
398 static void read_config(void)
400         unsigned char sha1[20];
401         const char *head_ref;
402         int flag;
403         if (default_remote_name) // did this already
404                 return;
405         default_remote_name = xstrdup("origin");
406         current_branch = NULL;
407         head_ref = resolve_ref("HEAD", sha1, 0, &flag);
408         if (head_ref && (flag & REF_ISSYMREF) &&
409             !prefixcmp(head_ref, "refs/heads/")) {
410                 current_branch =
411                         make_branch(head_ref + strlen("refs/heads/"), 0);
412         }
413         git_config(handle_config);
414         alias_all_urls();
417 static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
419         int i;
420         int st;
421         struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
423         for (i = 0; i < nr_refspec; i++) {
424                 size_t llen, rlen;
425                 int is_glob;
426                 const char *lhs, *rhs;
428                 llen = rlen = is_glob = 0;
430                 lhs = refspec[i];
431                 if (*lhs == '+') {
432                         rs[i].force = 1;
433                         lhs++;
434                 }
436                 rhs = strrchr(lhs, ':');
437                 if (rhs) {
438                         rhs++;
439                         rlen = strlen(rhs);
440                         is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
441                         if (is_glob)
442                                 rlen -= 2;
443                         rs[i].dst = xstrndup(rhs, rlen);
444                 }
446                 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
447                 if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
448                         if ((rhs && !is_glob) || (!rhs && fetch))
449                                 goto invalid;
450                         is_glob = 1;
451                         llen -= 2;
452                 } else if (rhs && is_glob) {
453                         goto invalid;
454                 }
456                 rs[i].pattern = is_glob;
457                 rs[i].src = xstrndup(lhs, llen);
459                 if (fetch) {
460                         /*
461                          * LHS
462                          * - empty is allowed; it means HEAD.
463                          * - otherwise it must be a valid looking ref.
464                          */
465                         if (!*rs[i].src)
466                                 ; /* empty is ok */
467                         else {
468                                 st = check_ref_format(rs[i].src);
469                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
470                                         goto invalid;
471                         }
472                         /*
473                          * RHS
474                          * - missing is ok, and is same as empty.
475                          * - empty is ok; it means not to store.
476                          * - otherwise it must be a valid looking ref.
477                          */
478                         if (!rs[i].dst) {
479                                 ; /* ok */
480                         } else if (!*rs[i].dst) {
481                                 ; /* ok */
482                         } else {
483                                 st = check_ref_format(rs[i].dst);
484                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
485                                         goto invalid;
486                         }
487                 } else {
488                         /*
489                          * LHS
490                          * - empty is allowed; it means delete.
491                          * - when wildcarded, it must be a valid looking ref.
492                          * - otherwise, it must be an extended SHA-1, but
493                          *   there is no existing way to validate this.
494                          */
495                         if (!*rs[i].src)
496                                 ; /* empty is ok */
497                         else if (is_glob) {
498                                 st = check_ref_format(rs[i].src);
499                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
500                                         goto invalid;
501                         }
502                         else
503                                 ; /* anything goes, for now */
504                         /*
505                          * RHS
506                          * - missing is allowed, but LHS then must be a
507                          *   valid looking ref.
508                          * - empty is not allowed.
509                          * - otherwise it must be a valid looking ref.
510                          */
511                         if (!rs[i].dst) {
512                                 st = check_ref_format(rs[i].src);
513                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
514                                         goto invalid;
515                         } else if (!*rs[i].dst) {
516                                 goto invalid;
517                         } else {
518                                 st = check_ref_format(rs[i].dst);
519                                 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
520                                         goto invalid;
521                         }
522                 }
523         }
524         return rs;
526  invalid:
527         if (verify) {
528                 free(rs);
529                 return NULL;
530         }
531         die("Invalid refspec '%s'", refspec[i]);
534 int valid_fetch_refspec(const char *fetch_refspec_str)
536         const char *fetch_refspec[] = { fetch_refspec_str };
537         struct refspec *refspec;
539         refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
540         if (refspec)
541                 free(refspec);
542         return !!refspec;
545 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
547         return parse_refspec_internal(nr_refspec, refspec, 1, 0);
550 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
552         return parse_refspec_internal(nr_refspec, refspec, 0, 0);
555 static int valid_remote_nick(const char *name)
557         if (!name[0] || /* not empty */
558             (name[0] == '.' && /* not "." */
559              (!name[1] || /* not ".." */
560               (name[1] == '.' && !name[2]))))
561                 return 0;
562         return !strchr(name, '/'); /* no slash */
565 struct remote *remote_get(const char *name)
567         struct remote *ret;
569         read_config();
570         if (!name)
571                 name = default_remote_name;
572         ret = make_remote(name, 0);
573         if (valid_remote_nick(name)) {
574                 if (!ret->url)
575                         read_remotes_file(ret);
576                 if (!ret->url)
577                         read_branches_file(ret);
578         }
579         if (!ret->url)
580                 add_url_alias(ret, name);
581         if (!ret->url)
582                 return NULL;
583         ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
584         ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
585         return ret;
588 int for_each_remote(each_remote_fn fn, void *priv)
590         int i, result = 0;
591         read_config();
592         for (i = 0; i < remotes_nr && !result; i++) {
593                 struct remote *r = remotes[i];
594                 if (!r)
595                         continue;
596                 if (!r->fetch)
597                         r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
598                                                        r->fetch_refspec);
599                 if (!r->push)
600                         r->push = parse_push_refspec(r->push_refspec_nr,
601                                                      r->push_refspec);
602                 result = fn(r, priv);
603         }
604         return result;
607 void ref_remove_duplicates(struct ref *ref_map)
609         struct ref **posn;
610         struct ref *next;
611         for (; ref_map; ref_map = ref_map->next) {
612                 if (!ref_map->peer_ref)
613                         continue;
614                 posn = &ref_map->next;
615                 while (*posn) {
616                         if ((*posn)->peer_ref &&
617                             !strcmp((*posn)->peer_ref->name,
618                                     ref_map->peer_ref->name)) {
619                                 if (strcmp((*posn)->name, ref_map->name))
620                                         die("%s tracks both %s and %s",
621                                             ref_map->peer_ref->name,
622                                             (*posn)->name, ref_map->name);
623                                 next = (*posn)->next;
624                                 free((*posn)->peer_ref);
625                                 free(*posn);
626                                 *posn = next;
627                         } else {
628                                 posn = &(*posn)->next;
629                         }
630                 }
631         }
634 int remote_has_url(struct remote *remote, const char *url)
636         int i;
637         for (i = 0; i < remote->url_nr; i++) {
638                 if (!strcmp(remote->url[i], url))
639                         return 1;
640         }
641         return 0;
644 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
646         int find_src = refspec->src == NULL;
647         char *needle, **result;
648         int i;
650         if (find_src) {
651                 if (!refspec->dst)
652                         return error("find_tracking: need either src or dst");
653                 needle = refspec->dst;
654                 result = &refspec->src;
655         } else {
656                 needle = refspec->src;
657                 result = &refspec->dst;
658         }
660         for (i = 0; i < remote->fetch_refspec_nr; i++) {
661                 struct refspec *fetch = &remote->fetch[i];
662                 const char *key = find_src ? fetch->dst : fetch->src;
663                 const char *value = find_src ? fetch->src : fetch->dst;
664                 if (!fetch->dst)
665                         continue;
666                 if (fetch->pattern) {
667                         if (!prefixcmp(needle, key) &&
668                             needle[strlen(key)] == '/') {
669                                 *result = xmalloc(strlen(value) +
670                                                   strlen(needle) -
671                                                   strlen(key) + 1);
672                                 strcpy(*result, value);
673                                 strcpy(*result + strlen(value),
674                                        needle + strlen(key));
675                                 refspec->force = fetch->force;
676                                 return 0;
677                         }
678                 } else if (!strcmp(needle, key)) {
679                         *result = xstrdup(value);
680                         refspec->force = fetch->force;
681                         return 0;
682                 }
683         }
684         return -1;
687 struct ref *alloc_ref(unsigned namelen)
689         struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
690         memset(ret, 0, sizeof(struct ref) + namelen);
691         return ret;
694 struct ref *alloc_ref_from_str(const char* str)
696         struct ref *ret = alloc_ref(strlen(str) + 1);
697         strcpy(ret->name, str);
698         return ret;
701 static struct ref *copy_ref(const struct ref *ref)
703         struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
704         memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
705         ret->next = NULL;
706         return ret;
709 struct ref *copy_ref_list(const struct ref *ref)
711         struct ref *ret = NULL;
712         struct ref **tail = &ret;
713         while (ref) {
714                 *tail = copy_ref(ref);
715                 ref = ref->next;
716                 tail = &((*tail)->next);
717         }
718         return ret;
721 void free_ref(struct ref *ref)
723         if (!ref)
724                 return;
725         free(ref->remote_status);
726         free(ref->symref);
727         free(ref);
730 void free_refs(struct ref *ref)
732         struct ref *next;
733         while (ref) {
734                 next = ref->next;
735                 free(ref->peer_ref);
736                 free_ref(ref);
737                 ref = next;
738         }
741 static int count_refspec_match(const char *pattern,
742                                struct ref *refs,
743                                struct ref **matched_ref)
745         int patlen = strlen(pattern);
746         struct ref *matched_weak = NULL;
747         struct ref *matched = NULL;
748         int weak_match = 0;
749         int match = 0;
751         for (weak_match = match = 0; refs; refs = refs->next) {
752                 char *name = refs->name;
753                 int namelen = strlen(name);
755                 if (!refname_match(pattern, name, ref_rev_parse_rules))
756                         continue;
758                 /* A match is "weak" if it is with refs outside
759                  * heads or tags, and did not specify the pattern
760                  * in full (e.g. "refs/remotes/origin/master") or at
761                  * least from the toplevel (e.g. "remotes/origin/master");
762                  * otherwise "git push $URL master" would result in
763                  * ambiguity between remotes/origin/master and heads/master
764                  * at the remote site.
765                  */
766                 if (namelen != patlen &&
767                     patlen != namelen - 5 &&
768                     prefixcmp(name, "refs/heads/") &&
769                     prefixcmp(name, "refs/tags/")) {
770                         /* We want to catch the case where only weak
771                          * matches are found and there are multiple
772                          * matches, and where more than one strong
773                          * matches are found, as ambiguous.  One
774                          * strong match with zero or more weak matches
775                          * are acceptable as a unique match.
776                          */
777                         matched_weak = refs;
778                         weak_match++;
779                 }
780                 else {
781                         matched = refs;
782                         match++;
783                 }
784         }
785         if (!matched) {
786                 *matched_ref = matched_weak;
787                 return weak_match;
788         }
789         else {
790                 *matched_ref = matched;
791                 return match;
792         }
795 static void tail_link_ref(struct ref *ref, struct ref ***tail)
797         **tail = ref;
798         while (ref->next)
799                 ref = ref->next;
800         *tail = &ref->next;
803 static struct ref *try_explicit_object_name(const char *name)
805         unsigned char sha1[20];
806         struct ref *ref;
808         if (!*name) {
809                 ref = alloc_ref(20);
810                 strcpy(ref->name, "(delete)");
811                 hashclr(ref->new_sha1);
812                 return ref;
813         }
814         if (get_sha1(name, sha1))
815                 return NULL;
816         ref = alloc_ref_from_str(name);
817         hashcpy(ref->new_sha1, sha1);
818         return ref;
821 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
823         struct ref *ret = alloc_ref_from_str(name);
824         tail_link_ref(ret, tail);
825         return ret;
828 static char *guess_ref(const char *name, struct ref *peer)
830         struct strbuf buf = STRBUF_INIT;
831         unsigned char sha1[20];
833         const char *r = resolve_ref(peer->name, sha1, 1, NULL);
834         if (!r)
835                 return NULL;
837         if (!prefixcmp(r, "refs/heads/"))
838                 strbuf_addstr(&buf, "refs/heads/");
839         else if (!prefixcmp(r, "refs/tags/"))
840                 strbuf_addstr(&buf, "refs/tags/");
841         else
842                 return NULL;
844         strbuf_addstr(&buf, name);
845         return strbuf_detach(&buf, NULL);
848 static int match_explicit(struct ref *src, struct ref *dst,
849                           struct ref ***dst_tail,
850                           struct refspec *rs,
851                           int errs)
853         struct ref *matched_src, *matched_dst;
855         const char *dst_value = rs->dst;
856         char *dst_guess;
858         if (rs->pattern)
859                 return errs;
861         matched_src = matched_dst = NULL;
862         switch (count_refspec_match(rs->src, src, &matched_src)) {
863         case 1:
864                 break;
865         case 0:
866                 /* The source could be in the get_sha1() format
867                  * not a reference name.  :refs/other is a
868                  * way to delete 'other' ref at the remote end.
869                  */
870                 matched_src = try_explicit_object_name(rs->src);
871                 if (!matched_src)
872                         error("src refspec %s does not match any.", rs->src);
873                 break;
874         default:
875                 matched_src = NULL;
876                 error("src refspec %s matches more than one.", rs->src);
877                 break;
878         }
880         if (!matched_src)
881                 errs = 1;
883         if (!dst_value) {
884                 unsigned char sha1[20];
885                 int flag;
887                 if (!matched_src)
888                         return errs;
889                 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
890                 if (!dst_value ||
891                     ((flag & REF_ISSYMREF) &&
892                      prefixcmp(dst_value, "refs/heads/")))
893                         die("%s cannot be resolved to branch.",
894                             matched_src->name);
895         }
897         switch (count_refspec_match(dst_value, dst, &matched_dst)) {
898         case 1:
899                 break;
900         case 0:
901                 if (!memcmp(dst_value, "refs/", 5))
902                         matched_dst = make_linked_ref(dst_value, dst_tail);
903                 else if((dst_guess = guess_ref(dst_value, matched_src)))
904                         matched_dst = make_linked_ref(dst_guess, dst_tail);
905                 else
906                         error("unable to push to unqualified destination: %s\n"
907                               "The destination refspec neither matches an "
908                               "existing ref on the remote nor\n"
909                               "begins with refs/, and we are unable to "
910                               "guess a prefix based on the source ref.",
911                               dst_value);
912                 break;
913         default:
914                 matched_dst = NULL;
915                 error("dst refspec %s matches more than one.",
916                       dst_value);
917                 break;
918         }
919         if (errs || !matched_dst)
920                 return 1;
921         if (matched_dst->peer_ref) {
922                 errs = 1;
923                 error("dst ref %s receives from more than one src.",
924                       matched_dst->name);
925         }
926         else {
927                 matched_dst->peer_ref = matched_src;
928                 matched_dst->force = rs->force;
929         }
930         return errs;
933 static int match_explicit_refs(struct ref *src, struct ref *dst,
934                                struct ref ***dst_tail, struct refspec *rs,
935                                int rs_nr)
937         int i, errs;
938         for (i = errs = 0; i < rs_nr; i++)
939                 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
940         return -errs;
943 static const struct refspec *check_pattern_match(const struct refspec *rs,
944                                                  int rs_nr,
945                                                  const struct ref *src)
947         int i;
948         for (i = 0; i < rs_nr; i++) {
949                 if (rs[i].pattern &&
950                     !prefixcmp(src->name, rs[i].src) &&
951                     src->name[strlen(rs[i].src)] == '/')
952                         return rs + i;
953         }
954         return NULL;
957 /*
958  * Note. This is used only by "push"; refspec matching rules for
959  * push and fetch are subtly different, so do not try to reuse it
960  * without thinking.
961  */
962 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
963                int nr_refspec, const char **refspec, int flags)
965         struct refspec *rs =
966                 parse_push_refspec(nr_refspec, (const char **) refspec);
967         int send_all = flags & MATCH_REFS_ALL;
968         int send_mirror = flags & MATCH_REFS_MIRROR;
970         if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
971                 return -1;
973         /* pick the remainder */
974         for ( ; src; src = src->next) {
975                 struct ref *dst_peer;
976                 const struct refspec *pat = NULL;
977                 char *dst_name;
978                 if (src->peer_ref)
979                         continue;
980                 if (nr_refspec) {
981                         pat = check_pattern_match(rs, nr_refspec, src);
982                         if (!pat)
983                                 continue;
984                 }
985                 else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
986                         /*
987                          * "matching refs"; traditionally we pushed everything
988                          * including refs outside refs/heads/ hierarchy, but
989                          * that does not make much sense these days.
990                          */
991                         continue;
993                 if (pat) {
994                         const char *dst_side = pat->dst ? pat->dst : pat->src;
995                         dst_name = xmalloc(strlen(dst_side) +
996                                            strlen(src->name) -
997                                            strlen(pat->src) + 2);
998                         strcpy(dst_name, dst_side);
999                         strcat(dst_name, src->name + strlen(pat->src));
1000                 } else
1001                         dst_name = xstrdup(src->name);
1002                 dst_peer = find_ref_by_name(dst, dst_name);
1003                 if (dst_peer && dst_peer->peer_ref)
1004                         /* We're already sending something to this ref. */
1005                         goto free_name;
1007                 if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
1008                         /*
1009                          * Remote doesn't have it, and we have no
1010                          * explicit pattern, and we don't have
1011                          * --all nor --mirror.
1012                          */
1013                         goto free_name;
1014                 if (!dst_peer) {
1015                         /* Create a new one and link it */
1016                         dst_peer = make_linked_ref(dst_name, dst_tail);
1017                         hashcpy(dst_peer->new_sha1, src->new_sha1);
1018                 }
1019                 dst_peer->peer_ref = src;
1020                 if (pat)
1021                         dst_peer->force = pat->force;
1022         free_name:
1023                 free(dst_name);
1024         }
1025         return 0;
1028 struct branch *branch_get(const char *name)
1030         struct branch *ret;
1032         read_config();
1033         if (!name || !*name || !strcmp(name, "HEAD"))
1034                 ret = current_branch;
1035         else
1036                 ret = make_branch(name, 0);
1037         if (ret && ret->remote_name) {
1038                 ret->remote = remote_get(ret->remote_name);
1039                 if (ret->merge_nr) {
1040                         int i;
1041                         ret->merge = xcalloc(sizeof(*ret->merge),
1042                                              ret->merge_nr);
1043                         for (i = 0; i < ret->merge_nr; i++) {
1044                                 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1045                                 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1046                                 remote_find_tracking(ret->remote,
1047                                                      ret->merge[i]);
1048                         }
1049                 }
1050         }
1051         return ret;
1054 int branch_has_merge_config(struct branch *branch)
1056         return branch && !!branch->merge;
1059 int branch_merge_matches(struct branch *branch,
1060                                  int i,
1061                                  const char *refname)
1063         if (!branch || i < 0 || i >= branch->merge_nr)
1064                 return 0;
1065         return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1068 static struct ref *get_expanded_map(const struct ref *remote_refs,
1069                                     const struct refspec *refspec)
1071         const struct ref *ref;
1072         struct ref *ret = NULL;
1073         struct ref **tail = &ret;
1075         int remote_prefix_len = strlen(refspec->src);
1076         int local_prefix_len = strlen(refspec->dst);
1078         for (ref = remote_refs; ref; ref = ref->next) {
1079                 if (strchr(ref->name, '^'))
1080                         continue; /* a dereference item */
1081                 if (!prefixcmp(ref->name, refspec->src)) {
1082                         const char *match;
1083                         struct ref *cpy = copy_ref(ref);
1084                         match = ref->name + remote_prefix_len;
1086                         cpy->peer_ref = alloc_ref(local_prefix_len +
1087                                                   strlen(match) + 1);
1088                         sprintf(cpy->peer_ref->name, "%s%s",
1089                                 refspec->dst, match);
1090                         if (refspec->force)
1091                                 cpy->peer_ref->force = 1;
1092                         *tail = cpy;
1093                         tail = &cpy->next;
1094                 }
1095         }
1097         return ret;
1100 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1102         const struct ref *ref;
1103         for (ref = refs; ref; ref = ref->next) {
1104                 if (refname_match(name, ref->name, ref_fetch_rules))
1105                         return ref;
1106         }
1107         return NULL;
1110 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1112         const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1114         if (!ref)
1115                 return NULL;
1117         return copy_ref(ref);
1120 static struct ref *get_local_ref(const char *name)
1122         struct ref *ret;
1123         if (!name)
1124                 return NULL;
1126         if (!prefixcmp(name, "refs/")) {
1127                 return alloc_ref_from_str(name);
1128         }
1130         if (!prefixcmp(name, "heads/") ||
1131             !prefixcmp(name, "tags/") ||
1132             !prefixcmp(name, "remotes/")) {
1133                 ret = alloc_ref(strlen(name) + 6);
1134                 sprintf(ret->name, "refs/%s", name);
1135                 return ret;
1136         }
1138         ret = alloc_ref(strlen(name) + 12);
1139         sprintf(ret->name, "refs/heads/%s", name);
1140         return ret;
1143 int get_fetch_map(const struct ref *remote_refs,
1144                   const struct refspec *refspec,
1145                   struct ref ***tail,
1146                   int missing_ok)
1148         struct ref *ref_map, **rmp;
1150         if (refspec->pattern) {
1151                 ref_map = get_expanded_map(remote_refs, refspec);
1152         } else {
1153                 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1155                 ref_map = get_remote_ref(remote_refs, name);
1156                 if (!missing_ok && !ref_map)
1157                         die("Couldn't find remote ref %s", name);
1158                 if (ref_map) {
1159                         ref_map->peer_ref = get_local_ref(refspec->dst);
1160                         if (ref_map->peer_ref && refspec->force)
1161                                 ref_map->peer_ref->force = 1;
1162                 }
1163         }
1165         for (rmp = &ref_map; *rmp; ) {
1166                 if ((*rmp)->peer_ref) {
1167                         int st = check_ref_format((*rmp)->peer_ref->name + 5);
1168                         if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1169                                 struct ref *ignore = *rmp;
1170                                 error("* Ignoring funny ref '%s' locally",
1171                                       (*rmp)->peer_ref->name);
1172                                 *rmp = (*rmp)->next;
1173                                 free(ignore->peer_ref);
1174                                 free(ignore);
1175                                 continue;
1176                         }
1177                 }
1178                 rmp = &((*rmp)->next);
1179         }
1181         if (ref_map)
1182                 tail_link_ref(ref_map, tail);
1184         return 0;
1187 int resolve_remote_symref(struct ref *ref, struct ref *list)
1189         if (!ref->symref)
1190                 return 0;
1191         for (; list; list = list->next)
1192                 if (!strcmp(ref->symref, list->name)) {
1193                         hashcpy(ref->old_sha1, list->old_sha1);
1194                         return 0;
1195                 }
1196         return 1;