1#include "cache.h"
2#include "remote.h"
3#include "refs.h"
4#include "commit.h"
5#include "diff.h"
6#include "revision.h"
7#include "dir.h"
8#include "tag.h"
9
10static struct refspec s_tag_refspec = {
11 0,
12 1,
13 0,
14 "refs/tags/*",
15 "refs/tags/*"
16};
17
18const struct refspec *tag_refspec = &s_tag_refspec;
19
20struct counted_string {
21 size_t len;
22 const char *s;
23};
24struct rewrite {
25 const char *base;
26 size_t baselen;
27 struct counted_string *instead_of;
28 int instead_of_nr;
29 int instead_of_alloc;
30};
31
32static struct remote **remotes;
33static int remotes_alloc;
34static int remotes_nr;
35
36static struct branch **branches;
37static int branches_alloc;
38static int branches_nr;
39
40static struct branch *current_branch;
41static const char *default_remote_name;
42static int explicit_default_remote_name;
43
44static struct rewrite **rewrite;
45static int rewrite_alloc;
46static int rewrite_nr;
47
48#define BUF_SIZE (2048)
49static char buffer[BUF_SIZE];
50
51static const char *alias_url(const char *url)
52{
53 int i, j;
54 char *ret;
55 struct counted_string *longest;
56 int longest_i;
57
58 longest = NULL;
59 longest_i = -1;
60 for (i = 0; i < rewrite_nr; i++) {
61 if (!rewrite[i])
62 continue;
63 for (j = 0; j < rewrite[i]->instead_of_nr; j++) {
64 if (!prefixcmp(url, rewrite[i]->instead_of[j].s) &&
65 (!longest ||
66 longest->len < rewrite[i]->instead_of[j].len)) {
67 longest = &(rewrite[i]->instead_of[j]);
68 longest_i = i;
69 }
70 }
71 }
72 if (!longest)
73 return url;
74
75 ret = xmalloc(rewrite[longest_i]->baselen +
76 (strlen(url) - longest->len) + 1);
77 strcpy(ret, rewrite[longest_i]->base);
78 strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
79 return ret;
80}
81
82static void add_push_refspec(struct remote *remote, const char *ref)
83{
84 ALLOC_GROW(remote->push_refspec,
85 remote->push_refspec_nr + 1,
86 remote->push_refspec_alloc);
87 remote->push_refspec[remote->push_refspec_nr++] = ref;
88}
89
90static void add_fetch_refspec(struct remote *remote, const char *ref)
91{
92 ALLOC_GROW(remote->fetch_refspec,
93 remote->fetch_refspec_nr + 1,
94 remote->fetch_refspec_alloc);
95 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
96}
97
98static void add_url(struct remote *remote, const char *url)
99{
100 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
101 remote->url[remote->url_nr++] = url;
102}
103
104static void add_url_alias(struct remote *remote, const char *url)
105{
106 add_url(remote, alias_url(url));
107}
108
109static struct remote *make_remote(const char *name, int len)
110{
111 struct remote *ret;
112 int i;
113
114 for (i = 0; i < remotes_nr; i++) {
115 if (len ? (!strncmp(name, remotes[i]->name, len) &&
116 !remotes[i]->name[len]) :
117 !strcmp(name, remotes[i]->name))
118 return remotes[i];
119 }
120
121 ret = xcalloc(1, sizeof(struct remote));
122 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
123 remotes[remotes_nr++] = ret;
124 if (len)
125 ret->name = xstrndup(name, len);
126 else
127 ret->name = xstrdup(name);
128 return ret;
129}
130
131static void add_merge(struct branch *branch, const char *name)
132{
133 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
134 branch->merge_alloc);
135 branch->merge_name[branch->merge_nr++] = name;
136}
137
138static struct branch *make_branch(const char *name, int len)
139{
140 struct branch *ret;
141 int i;
142 char *refname;
143
144 for (i = 0; i < branches_nr; i++) {
145 if (len ? (!strncmp(name, branches[i]->name, len) &&
146 !branches[i]->name[len]) :
147 !strcmp(name, branches[i]->name))
148 return branches[i];
149 }
150
151 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
152 ret = xcalloc(1, sizeof(struct branch));
153 branches[branches_nr++] = ret;
154 if (len)
155 ret->name = xstrndup(name, len);
156 else
157 ret->name = xstrdup(name);
158 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
159 strcpy(refname, "refs/heads/");
160 strcpy(refname + strlen("refs/heads/"), ret->name);
161 ret->refname = refname;
162
163 return ret;
164}
165
166static struct rewrite *make_rewrite(const char *base, int len)
167{
168 struct rewrite *ret;
169 int i;
170
171 for (i = 0; i < rewrite_nr; i++) {
172 if (len
173 ? (len == rewrite[i]->baselen &&
174 !strncmp(base, rewrite[i]->base, len))
175 : !strcmp(base, rewrite[i]->base))
176 return rewrite[i];
177 }
178
179 ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc);
180 ret = xcalloc(1, sizeof(struct rewrite));
181 rewrite[rewrite_nr++] = ret;
182 if (len) {
183 ret->base = xstrndup(base, len);
184 ret->baselen = len;
185 }
186 else {
187 ret->base = xstrdup(base);
188 ret->baselen = strlen(base);
189 }
190 return ret;
191}
192
193static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
194{
195 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
196 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
197 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
198 rewrite->instead_of_nr++;
199}
200
201static void read_remotes_file(struct remote *remote)
202{
203 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
204
205 if (!f)
206 return;
207 remote->origin = REMOTE_REMOTES;
208 while (fgets(buffer, BUF_SIZE, f)) {
209 int value_list;
210 char *s, *p;
211
212 if (!prefixcmp(buffer, "URL:")) {
213 value_list = 0;
214 s = buffer + 4;
215 } else if (!prefixcmp(buffer, "Push:")) {
216 value_list = 1;
217 s = buffer + 5;
218 } else if (!prefixcmp(buffer, "Pull:")) {
219 value_list = 2;
220 s = buffer + 5;
221 } else
222 continue;
223
224 while (isspace(*s))
225 s++;
226 if (!*s)
227 continue;
228
229 p = s + strlen(s);
230 while (isspace(p[-1]))
231 *--p = 0;
232
233 switch (value_list) {
234 case 0:
235 add_url_alias(remote, xstrdup(s));
236 break;
237 case 1:
238 add_push_refspec(remote, xstrdup(s));
239 break;
240 case 2:
241 add_fetch_refspec(remote, xstrdup(s));
242 break;
243 }
244 }
245 fclose(f);
246}
247
248static void read_branches_file(struct remote *remote)
249{
250 const char *slash = strchr(remote->name, '/');
251 char *frag;
252 struct strbuf branch = STRBUF_INIT;
253 int n = slash ? slash - remote->name : 1000;
254 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
255 char *s, *p;
256 int len;
257
258 if (!f)
259 return;
260 s = fgets(buffer, BUF_SIZE, f);
261 fclose(f);
262 if (!s)
263 return;
264 while (isspace(*s))
265 s++;
266 if (!*s)
267 return;
268 remote->origin = REMOTE_BRANCHES;
269 p = s + strlen(s);
270 while (isspace(p[-1]))
271 *--p = 0;
272 len = p - s;
273 if (slash)
274 len += strlen(slash);
275 p = xmalloc(len + 1);
276 strcpy(p, s);
277 if (slash)
278 strcat(p, slash);
279
280 /*
281 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
282 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
283 * the partial URL obtained from the branches file plus
284 * "/netdev-2.6" and does not store it in any tracking ref.
285 * #branch specifier in the file is ignored.
286 *
287 * Otherwise, the branches file would have URL and optionally
288 * #branch specified. The "master" (or specified) branch is
289 * fetched and stored in the local branch of the same name.
290 */
291 frag = strchr(p, '#');
292 if (frag) {
293 *(frag++) = '\0';
294 strbuf_addf(&branch, "refs/heads/%s", frag);
295 } else
296 strbuf_addstr(&branch, "refs/heads/master");
297 if (!slash) {
298 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
299 } else {
300 strbuf_reset(&branch);
301 strbuf_addstr(&branch, "HEAD:");
302 }
303 add_url_alias(remote, p);
304 add_fetch_refspec(remote, strbuf_detach(&branch, 0));
305 /*
306 * Cogito compatible push: push current HEAD to remote #branch
307 * (master if missing)
308 */
309 strbuf_init(&branch, 0);
310 strbuf_addstr(&branch, "HEAD");
311 if (frag)
312 strbuf_addf(&branch, ":refs/heads/%s", frag);
313 else
314 strbuf_addstr(&branch, ":refs/heads/master");
315 add_push_refspec(remote, strbuf_detach(&branch, 0));
316 remote->fetch_tags = 1; /* always auto-follow */
317}
318
319static int handle_config(const char *key, const char *value, void *cb)
320{
321 const char *name;
322 const char *subkey;
323 struct remote *remote;
324 struct branch *branch;
325 if (!prefixcmp(key, "branch.")) {
326 name = key + 7;
327 subkey = strrchr(name, '.');
328 if (!subkey)
329 return 0;
330 branch = make_branch(name, subkey - name);
331 if (!strcmp(subkey, ".remote")) {
332 if (!value)
333 return config_error_nonbool(key);
334 branch->remote_name = xstrdup(value);
335 if (branch == current_branch) {
336 default_remote_name = branch->remote_name;
337 explicit_default_remote_name = 1;
338 }
339 } else if (!strcmp(subkey, ".merge")) {
340 if (!value)
341 return config_error_nonbool(key);
342 add_merge(branch, xstrdup(value));
343 }
344 return 0;
345 }
346 if (!prefixcmp(key, "url.")) {
347 struct rewrite *rewrite;
348 name = key + 4;
349 subkey = strrchr(name, '.');
350 if (!subkey)
351 return 0;
352 rewrite = make_rewrite(name, subkey - name);
353 if (!strcmp(subkey, ".insteadof")) {
354 if (!value)
355 return config_error_nonbool(key);
356 add_instead_of(rewrite, xstrdup(value));
357 }
358 }
359 if (prefixcmp(key, "remote."))
360 return 0;
361 name = key + 7;
362 if (*name == '/') {
363 warning("Config remote shorthand cannot begin with '/': %s",
364 name);
365 return 0;
366 }
367 subkey = strrchr(name, '.');
368 if (!subkey)
369 return 0;
370 remote = make_remote(name, subkey - name);
371 remote->origin = REMOTE_CONFIG;
372 if (!strcmp(subkey, ".mirror"))
373 remote->mirror = git_config_bool(key, value);
374 else if (!strcmp(subkey, ".skipdefaultupdate"))
375 remote->skip_default_update = git_config_bool(key, value);
376
377 else if (!strcmp(subkey, ".url")) {
378 const char *v;
379 if (git_config_string(&v, key, value))
380 return -1;
381 add_url(remote, v);
382 } else if (!strcmp(subkey, ".push")) {
383 const char *v;
384 if (git_config_string(&v, key, value))
385 return -1;
386 add_push_refspec(remote, v);
387 } else if (!strcmp(subkey, ".fetch")) {
388 const char *v;
389 if (git_config_string(&v, key, value))
390 return -1;
391 add_fetch_refspec(remote, v);
392 } else if (!strcmp(subkey, ".receivepack")) {
393 const char *v;
394 if (git_config_string(&v, key, value))
395 return -1;
396 if (!remote->receivepack)
397 remote->receivepack = v;
398 else
399 error("more than one receivepack given, using the first");
400 } else if (!strcmp(subkey, ".uploadpack")) {
401 const char *v;
402 if (git_config_string(&v, key, value))
403 return -1;
404 if (!remote->uploadpack)
405 remote->uploadpack = v;
406 else
407 error("more than one uploadpack given, using the first");
408 } else if (!strcmp(subkey, ".tagopt")) {
409 if (!strcmp(value, "--no-tags"))
410 remote->fetch_tags = -1;
411 } else if (!strcmp(subkey, ".proxy")) {
412 return git_config_string((const char **)&remote->http_proxy,
413 key, value);
414 }
415 return 0;
416}
417
418static void alias_all_urls(void)
419{
420 int i, j;
421 for (i = 0; i < remotes_nr; i++) {
422 if (!remotes[i])
423 continue;
424 for (j = 0; j < remotes[i]->url_nr; j++) {
425 remotes[i]->url[j] = alias_url(remotes[i]->url[j]);
426 }
427 }
428}
429
430static void read_config(void)
431{
432 unsigned char sha1[20];
433 const char *head_ref;
434 int flag;
435 if (default_remote_name) // did this already
436 return;
437 default_remote_name = xstrdup("origin");
438 current_branch = NULL;
439 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
440 if (head_ref && (flag & REF_ISSYMREF) &&
441 !prefixcmp(head_ref, "refs/heads/")) {
442 current_branch =
443 make_branch(head_ref + strlen("refs/heads/"), 0);
444 }
445 git_config(handle_config, NULL);
446 alias_all_urls();
447}
448
449/*
450 * We need to make sure the tracking branches are well formed, but a
451 * wildcard refspec in "struct refspec" must have a trailing slash. We
452 * temporarily drop the trailing '/' while calling check_ref_format(),
453 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
454 * error return is Ok for a wildcard refspec.
455 */
456static int verify_refname(char *name, int is_glob)
457{
458 int result;
459
460 result = check_ref_format(name);
461 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
462 result = CHECK_REF_FORMAT_OK;
463 return result;
464}
465
466/*
467 * This function frees a refspec array.
468 * Warning: code paths should be checked to ensure that the src
469 * and dst pointers are always freeable pointers as well
470 * as the refspec pointer itself.
471 */
472static void free_refspecs(struct refspec *refspec, int nr_refspec)
473{
474 int i;
475
476 if (!refspec)
477 return;
478
479 for (i = 0; i < nr_refspec; i++) {
480 free(refspec[i].src);
481 free(refspec[i].dst);
482 }
483 free(refspec);
484}
485
486static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
487{
488 int i;
489 int st;
490 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
491
492 for (i = 0; i < nr_refspec; i++) {
493 size_t llen;
494 int is_glob;
495 const char *lhs, *rhs;
496
497 is_glob = 0;
498
499 lhs = refspec[i];
500 if (*lhs == '+') {
501 rs[i].force = 1;
502 lhs++;
503 }
504
505 rhs = strrchr(lhs, ':');
506
507 /*
508 * Before going on, special case ":" (or "+:") as a refspec
509 * for matching refs.
510 */
511 if (!fetch && rhs == lhs && rhs[1] == '\0') {
512 rs[i].matching = 1;
513 continue;
514 }
515
516 if (rhs) {
517 size_t rlen = strlen(++rhs);
518 is_glob = (1 <= rlen && strchr(rhs, '*'));
519 rs[i].dst = xstrndup(rhs, rlen);
520 }
521
522 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
523 if (1 <= llen && memchr(lhs, '*', llen)) {
524 if ((rhs && !is_glob) || (!rhs && fetch))
525 goto invalid;
526 is_glob = 1;
527 } else if (rhs && is_glob) {
528 goto invalid;
529 }
530
531 rs[i].pattern = is_glob;
532 rs[i].src = xstrndup(lhs, llen);
533
534 if (fetch) {
535 /*
536 * LHS
537 * - empty is allowed; it means HEAD.
538 * - otherwise it must be a valid looking ref.
539 */
540 if (!*rs[i].src)
541 ; /* empty is ok */
542 else {
543 st = verify_refname(rs[i].src, is_glob);
544 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
545 goto invalid;
546 }
547 /*
548 * RHS
549 * - missing is ok, and is same as empty.
550 * - empty is ok; it means not to store.
551 * - otherwise it must be a valid looking ref.
552 */
553 if (!rs[i].dst) {
554 ; /* ok */
555 } else if (!*rs[i].dst) {
556 ; /* ok */
557 } else {
558 st = verify_refname(rs[i].dst, is_glob);
559 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
560 goto invalid;
561 }
562 } else {
563 /*
564 * LHS
565 * - empty is allowed; it means delete.
566 * - when wildcarded, it must be a valid looking ref.
567 * - otherwise, it must be an extended SHA-1, but
568 * there is no existing way to validate this.
569 */
570 if (!*rs[i].src)
571 ; /* empty is ok */
572 else if (is_glob) {
573 st = verify_refname(rs[i].src, is_glob);
574 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
575 goto invalid;
576 }
577 else
578 ; /* anything goes, for now */
579 /*
580 * RHS
581 * - missing is allowed, but LHS then must be a
582 * valid looking ref.
583 * - empty is not allowed.
584 * - otherwise it must be a valid looking ref.
585 */
586 if (!rs[i].dst) {
587 st = verify_refname(rs[i].src, is_glob);
588 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
589 goto invalid;
590 } else if (!*rs[i].dst) {
591 goto invalid;
592 } else {
593 st = verify_refname(rs[i].dst, is_glob);
594 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
595 goto invalid;
596 }
597 }
598 }
599 return rs;
600
601 invalid:
602 if (verify) {
603 /*
604 * nr_refspec must be greater than zero and i must be valid
605 * since it is only possible to reach this point from within
606 * the for loop above.
607 */
608 free_refspecs(rs, i+1);
609 return NULL;
610 }
611 die("Invalid refspec '%s'", refspec[i]);
612}
613
614int valid_fetch_refspec(const char *fetch_refspec_str)
615{
616 const char *fetch_refspec[] = { fetch_refspec_str };
617 struct refspec *refspec;
618
619 refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
620 free_refspecs(refspec, 1);
621 return !!refspec;
622}
623
624struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
625{
626 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
627}
628
629static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
630{
631 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
632}
633
634static int valid_remote_nick(const char *name)
635{
636 if (!name[0] || is_dot_or_dotdot(name))
637 return 0;
638 return !strchr(name, '/'); /* no slash */
639}
640
641struct remote *remote_get(const char *name)
642{
643 struct remote *ret;
644 int name_given = 0;
645
646 read_config();
647 if (name)
648 name_given = 1;
649 else {
650 name = default_remote_name;
651 name_given = explicit_default_remote_name;
652 }
653
654 ret = make_remote(name, 0);
655 if (valid_remote_nick(name)) {
656 if (!ret->url)
657 read_remotes_file(ret);
658 if (!ret->url)
659 read_branches_file(ret);
660 }
661 if (name_given && !ret->url)
662 add_url_alias(ret, name);
663 if (!ret->url)
664 return NULL;
665 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
666 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
667 return ret;
668}
669
670int remote_is_configured(const char *name)
671{
672 int i;
673 read_config();
674
675 for (i = 0; i < remotes_nr; i++)
676 if (!strcmp(name, remotes[i]->name))
677 return 1;
678 return 0;
679}
680
681int for_each_remote(each_remote_fn fn, void *priv)
682{
683 int i, result = 0;
684 read_config();
685 for (i = 0; i < remotes_nr && !result; i++) {
686 struct remote *r = remotes[i];
687 if (!r)
688 continue;
689 if (!r->fetch)
690 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
691 r->fetch_refspec);
692 if (!r->push)
693 r->push = parse_push_refspec(r->push_refspec_nr,
694 r->push_refspec);
695 result = fn(r, priv);
696 }
697 return result;
698}
699
700void ref_remove_duplicates(struct ref *ref_map)
701{
702 struct ref **posn;
703 struct ref *next;
704 for (; ref_map; ref_map = ref_map->next) {
705 if (!ref_map->peer_ref)
706 continue;
707 posn = &ref_map->next;
708 while (*posn) {
709 if ((*posn)->peer_ref &&
710 !strcmp((*posn)->peer_ref->name,
711 ref_map->peer_ref->name)) {
712 if (strcmp((*posn)->name, ref_map->name))
713 die("%s tracks both %s and %s",
714 ref_map->peer_ref->name,
715 (*posn)->name, ref_map->name);
716 next = (*posn)->next;
717 free((*posn)->peer_ref);
718 free(*posn);
719 *posn = next;
720 } else {
721 posn = &(*posn)->next;
722 }
723 }
724 }
725}
726
727int remote_has_url(struct remote *remote, const char *url)
728{
729 int i;
730 for (i = 0; i < remote->url_nr; i++) {
731 if (!strcmp(remote->url[i], url))
732 return 1;
733 }
734 return 0;
735}
736
737static int match_name_with_pattern(const char *key, const char *name,
738 const char *value, char **result)
739{
740 const char *kstar = strchr(key, '*');
741 size_t klen;
742 size_t ksuffixlen;
743 size_t namelen;
744 int ret;
745 if (!kstar)
746 die("Key '%s' of pattern had no '*'", key);
747 klen = kstar - key;
748 ksuffixlen = strlen(kstar + 1);
749 namelen = strlen(name);
750 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
751 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
752 if (ret && value) {
753 const char *vstar = strchr(value, '*');
754 size_t vlen;
755 size_t vsuffixlen;
756 if (!vstar)
757 die("Value '%s' of pattern has no '*'", value);
758 vlen = vstar - value;
759 vsuffixlen = strlen(vstar + 1);
760 *result = xmalloc(vlen + vsuffixlen +
761 strlen(name) -
762 klen - ksuffixlen + 1);
763 strncpy(*result, value, vlen);
764 strncpy(*result + vlen,
765 name + klen, namelen - klen - ksuffixlen);
766 strcpy(*result + vlen + namelen - klen - ksuffixlen,
767 vstar + 1);
768 }
769 return ret;
770}
771
772int remote_find_tracking(struct remote *remote, struct refspec *refspec)
773{
774 int find_src = refspec->src == NULL;
775 char *needle, **result;
776 int i;
777
778 if (find_src) {
779 if (!refspec->dst)
780 return error("find_tracking: need either src or dst");
781 needle = refspec->dst;
782 result = &refspec->src;
783 } else {
784 needle = refspec->src;
785 result = &refspec->dst;
786 }
787
788 for (i = 0; i < remote->fetch_refspec_nr; i++) {
789 struct refspec *fetch = &remote->fetch[i];
790 const char *key = find_src ? fetch->dst : fetch->src;
791 const char *value = find_src ? fetch->src : fetch->dst;
792 if (!fetch->dst)
793 continue;
794 if (fetch->pattern) {
795 if (match_name_with_pattern(key, needle, value, result)) {
796 refspec->force = fetch->force;
797 return 0;
798 }
799 } else if (!strcmp(needle, key)) {
800 *result = xstrdup(value);
801 refspec->force = fetch->force;
802 return 0;
803 }
804 }
805 return -1;
806}
807
808static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
809 const char *name)
810{
811 size_t len = strlen(name);
812 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
813 memcpy(ref->name, prefix, prefixlen);
814 memcpy(ref->name + prefixlen, name, len);
815 return ref;
816}
817
818struct ref *alloc_ref(const char *name)
819{
820 return alloc_ref_with_prefix("", 0, name);
821}
822
823static struct ref *copy_ref(const struct ref *ref)
824{
825 struct ref *cpy;
826 size_t len;
827 if (!ref)
828 return NULL;
829 len = strlen(ref->name);
830 cpy = xmalloc(sizeof(struct ref) + len + 1);
831 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
832 cpy->next = NULL;
833 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
834 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
835 cpy->peer_ref = copy_ref(ref->peer_ref);
836 return cpy;
837}
838
839struct ref *copy_ref_list(const struct ref *ref)
840{
841 struct ref *ret = NULL;
842 struct ref **tail = &ret;
843 while (ref) {
844 *tail = copy_ref(ref);
845 ref = ref->next;
846 tail = &((*tail)->next);
847 }
848 return ret;
849}
850
851static void free_ref(struct ref *ref)
852{
853 if (!ref)
854 return;
855 free_ref(ref->peer_ref);
856 free(ref->remote_status);
857 free(ref->symref);
858 free(ref);
859}
860
861void free_refs(struct ref *ref)
862{
863 struct ref *next;
864 while (ref) {
865 next = ref->next;
866 free_ref(ref);
867 ref = next;
868 }
869}
870
871static int count_refspec_match(const char *pattern,
872 struct ref *refs,
873 struct ref **matched_ref)
874{
875 int patlen = strlen(pattern);
876 struct ref *matched_weak = NULL;
877 struct ref *matched = NULL;
878 int weak_match = 0;
879 int match = 0;
880
881 for (weak_match = match = 0; refs; refs = refs->next) {
882 char *name = refs->name;
883 int namelen = strlen(name);
884
885 if (!refname_match(pattern, name, ref_rev_parse_rules))
886 continue;
887
888 /* A match is "weak" if it is with refs outside
889 * heads or tags, and did not specify the pattern
890 * in full (e.g. "refs/remotes/origin/master") or at
891 * least from the toplevel (e.g. "remotes/origin/master");
892 * otherwise "git push $URL master" would result in
893 * ambiguity between remotes/origin/master and heads/master
894 * at the remote site.
895 */
896 if (namelen != patlen &&
897 patlen != namelen - 5 &&
898 prefixcmp(name, "refs/heads/") &&
899 prefixcmp(name, "refs/tags/")) {
900 /* We want to catch the case where only weak
901 * matches are found and there are multiple
902 * matches, and where more than one strong
903 * matches are found, as ambiguous. One
904 * strong match with zero or more weak matches
905 * are acceptable as a unique match.
906 */
907 matched_weak = refs;
908 weak_match++;
909 }
910 else {
911 matched = refs;
912 match++;
913 }
914 }
915 if (!matched) {
916 *matched_ref = matched_weak;
917 return weak_match;
918 }
919 else {
920 *matched_ref = matched;
921 return match;
922 }
923}
924
925static void tail_link_ref(struct ref *ref, struct ref ***tail)
926{
927 **tail = ref;
928 while (ref->next)
929 ref = ref->next;
930 *tail = &ref->next;
931}
932
933static struct ref *try_explicit_object_name(const char *name)
934{
935 unsigned char sha1[20];
936 struct ref *ref;
937
938 if (!*name) {
939 ref = alloc_ref("(delete)");
940 hashclr(ref->new_sha1);
941 return ref;
942 }
943 if (get_sha1(name, sha1))
944 return NULL;
945 ref = alloc_ref(name);
946 hashcpy(ref->new_sha1, sha1);
947 return ref;
948}
949
950static struct ref *make_linked_ref(const char *name, struct ref ***tail)
951{
952 struct ref *ret = alloc_ref(name);
953 tail_link_ref(ret, tail);
954 return ret;
955}
956
957static char *guess_ref(const char *name, struct ref *peer)
958{
959 struct strbuf buf = STRBUF_INIT;
960 unsigned char sha1[20];
961
962 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
963 if (!r)
964 return NULL;
965
966 if (!prefixcmp(r, "refs/heads/"))
967 strbuf_addstr(&buf, "refs/heads/");
968 else if (!prefixcmp(r, "refs/tags/"))
969 strbuf_addstr(&buf, "refs/tags/");
970 else
971 return NULL;
972
973 strbuf_addstr(&buf, name);
974 return strbuf_detach(&buf, NULL);
975}
976
977static int match_explicit(struct ref *src, struct ref *dst,
978 struct ref ***dst_tail,
979 struct refspec *rs)
980{
981 struct ref *matched_src, *matched_dst;
982 int copy_src;
983
984 const char *dst_value = rs->dst;
985 char *dst_guess;
986
987 if (rs->pattern || rs->matching)
988 return 0;
989
990 matched_src = matched_dst = NULL;
991 switch (count_refspec_match(rs->src, src, &matched_src)) {
992 case 1:
993 copy_src = 1;
994 break;
995 case 0:
996 /* The source could be in the get_sha1() format
997 * not a reference name. :refs/other is a
998 * way to delete 'other' ref at the remote end.
999 */
1000 matched_src = try_explicit_object_name(rs->src);
1001 if (!matched_src)
1002 return error("src refspec %s does not match any.", rs->src);
1003 copy_src = 0;
1004 break;
1005 default:
1006 return error("src refspec %s matches more than one.", rs->src);
1007 }
1008
1009 if (!dst_value) {
1010 unsigned char sha1[20];
1011 int flag;
1012
1013 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1014 if (!dst_value ||
1015 ((flag & REF_ISSYMREF) &&
1016 prefixcmp(dst_value, "refs/heads/")))
1017 die("%s cannot be resolved to branch.",
1018 matched_src->name);
1019 }
1020
1021 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1022 case 1:
1023 break;
1024 case 0:
1025 if (!memcmp(dst_value, "refs/", 5))
1026 matched_dst = make_linked_ref(dst_value, dst_tail);
1027 else if((dst_guess = guess_ref(dst_value, matched_src)))
1028 matched_dst = make_linked_ref(dst_guess, dst_tail);
1029 else
1030 error("unable to push to unqualified destination: %s\n"
1031 "The destination refspec neither matches an "
1032 "existing ref on the remote nor\n"
1033 "begins with refs/, and we are unable to "
1034 "guess a prefix based on the source ref.",
1035 dst_value);
1036 break;
1037 default:
1038 matched_dst = NULL;
1039 error("dst refspec %s matches more than one.",
1040 dst_value);
1041 break;
1042 }
1043 if (!matched_dst)
1044 return -1;
1045 if (matched_dst->peer_ref)
1046 return error("dst ref %s receives from more than one src.",
1047 matched_dst->name);
1048 else {
1049 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1050 matched_dst->force = rs->force;
1051 }
1052 return 0;
1053}
1054
1055static int match_explicit_refs(struct ref *src, struct ref *dst,
1056 struct ref ***dst_tail, struct refspec *rs,
1057 int rs_nr)
1058{
1059 int i, errs;
1060 for (i = errs = 0; i < rs_nr; i++)
1061 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1062 return errs;
1063}
1064
1065static const struct refspec *check_pattern_match(const struct refspec *rs,
1066 int rs_nr,
1067 const struct ref *src)
1068{
1069 int i;
1070 int matching_refs = -1;
1071 for (i = 0; i < rs_nr; i++) {
1072 if (rs[i].matching &&
1073 (matching_refs == -1 || rs[i].force)) {
1074 matching_refs = i;
1075 continue;
1076 }
1077
1078 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1079 NULL, NULL))
1080 return rs + i;
1081 }
1082 if (matching_refs != -1)
1083 return rs + matching_refs;
1084 else
1085 return NULL;
1086}
1087
1088/*
1089 * Note. This is used only by "push"; refspec matching rules for
1090 * push and fetch are subtly different, so do not try to reuse it
1091 * without thinking.
1092 */
1093int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1094 int nr_refspec, const char **refspec, int flags)
1095{
1096 struct refspec *rs;
1097 int send_all = flags & MATCH_REFS_ALL;
1098 int send_mirror = flags & MATCH_REFS_MIRROR;
1099 int errs;
1100 static const char *default_refspec[] = { ":", 0 };
1101
1102 if (!nr_refspec) {
1103 nr_refspec = 1;
1104 refspec = default_refspec;
1105 }
1106 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1107 errs = match_explicit_refs(src, dst, dst_tail, rs, nr_refspec);
1108
1109 /* pick the remainder */
1110 for ( ; src; src = src->next) {
1111 struct ref *dst_peer;
1112 const struct refspec *pat = NULL;
1113 char *dst_name;
1114 if (src->peer_ref)
1115 continue;
1116
1117 pat = check_pattern_match(rs, nr_refspec, src);
1118 if (!pat)
1119 continue;
1120
1121 if (pat->matching) {
1122 /*
1123 * "matching refs"; traditionally we pushed everything
1124 * including refs outside refs/heads/ hierarchy, but
1125 * that does not make much sense these days.
1126 */
1127 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1128 continue;
1129 dst_name = xstrdup(src->name);
1130
1131 } else {
1132 const char *dst_side = pat->dst ? pat->dst : pat->src;
1133 if (!match_name_with_pattern(pat->src, src->name,
1134 dst_side, &dst_name))
1135 die("Didn't think it matches any more");
1136 }
1137 dst_peer = find_ref_by_name(dst, dst_name);
1138 if (dst_peer) {
1139 if (dst_peer->peer_ref)
1140 /* We're already sending something to this ref. */
1141 goto free_name;
1142
1143 } else {
1144 if (pat->matching && !(send_all || send_mirror))
1145 /*
1146 * Remote doesn't have it, and we have no
1147 * explicit pattern, and we don't have
1148 * --all nor --mirror.
1149 */
1150 goto free_name;
1151
1152 /* Create a new one and link it */
1153 dst_peer = make_linked_ref(dst_name, dst_tail);
1154 hashcpy(dst_peer->new_sha1, src->new_sha1);
1155 }
1156 dst_peer->peer_ref = copy_ref(src);
1157 dst_peer->force = pat->force;
1158 free_name:
1159 free(dst_name);
1160 }
1161 if (errs)
1162 return -1;
1163 return 0;
1164}
1165
1166struct branch *branch_get(const char *name)
1167{
1168 struct branch *ret;
1169
1170 read_config();
1171 if (!name || !*name || !strcmp(name, "HEAD"))
1172 ret = current_branch;
1173 else
1174 ret = make_branch(name, 0);
1175 if (ret && ret->remote_name) {
1176 ret->remote = remote_get(ret->remote_name);
1177 if (ret->merge_nr) {
1178 int i;
1179 ret->merge = xcalloc(sizeof(*ret->merge),
1180 ret->merge_nr);
1181 for (i = 0; i < ret->merge_nr; i++) {
1182 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1183 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1184 if (remote_find_tracking(ret->remote, ret->merge[i])
1185 && !strcmp(ret->remote_name, "."))
1186 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1187 }
1188 }
1189 }
1190 return ret;
1191}
1192
1193int branch_has_merge_config(struct branch *branch)
1194{
1195 return branch && !!branch->merge;
1196}
1197
1198int branch_merge_matches(struct branch *branch,
1199 int i,
1200 const char *refname)
1201{
1202 if (!branch || i < 0 || i >= branch->merge_nr)
1203 return 0;
1204 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1205}
1206
1207static struct ref *get_expanded_map(const struct ref *remote_refs,
1208 const struct refspec *refspec)
1209{
1210 const struct ref *ref;
1211 struct ref *ret = NULL;
1212 struct ref **tail = &ret;
1213
1214 char *expn_name;
1215
1216 for (ref = remote_refs; ref; ref = ref->next) {
1217 if (strchr(ref->name, '^'))
1218 continue; /* a dereference item */
1219 if (match_name_with_pattern(refspec->src, ref->name,
1220 refspec->dst, &expn_name)) {
1221 struct ref *cpy = copy_ref(ref);
1222
1223 cpy->peer_ref = alloc_ref(expn_name);
1224 free(expn_name);
1225 if (refspec->force)
1226 cpy->peer_ref->force = 1;
1227 *tail = cpy;
1228 tail = &cpy->next;
1229 }
1230 }
1231
1232 return ret;
1233}
1234
1235static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1236{
1237 const struct ref *ref;
1238 for (ref = refs; ref; ref = ref->next) {
1239 if (refname_match(name, ref->name, ref_fetch_rules))
1240 return ref;
1241 }
1242 return NULL;
1243}
1244
1245struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1246{
1247 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1248
1249 if (!ref)
1250 return NULL;
1251
1252 return copy_ref(ref);
1253}
1254
1255static struct ref *get_local_ref(const char *name)
1256{
1257 if (!name)
1258 return NULL;
1259
1260 if (!prefixcmp(name, "refs/"))
1261 return alloc_ref(name);
1262
1263 if (!prefixcmp(name, "heads/") ||
1264 !prefixcmp(name, "tags/") ||
1265 !prefixcmp(name, "remotes/"))
1266 return alloc_ref_with_prefix("refs/", 5, name);
1267
1268 return alloc_ref_with_prefix("refs/heads/", 11, name);
1269}
1270
1271int get_fetch_map(const struct ref *remote_refs,
1272 const struct refspec *refspec,
1273 struct ref ***tail,
1274 int missing_ok)
1275{
1276 struct ref *ref_map, **rmp;
1277
1278 if (refspec->pattern) {
1279 ref_map = get_expanded_map(remote_refs, refspec);
1280 } else {
1281 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1282
1283 ref_map = get_remote_ref(remote_refs, name);
1284 if (!missing_ok && !ref_map)
1285 die("Couldn't find remote ref %s", name);
1286 if (ref_map) {
1287 ref_map->peer_ref = get_local_ref(refspec->dst);
1288 if (ref_map->peer_ref && refspec->force)
1289 ref_map->peer_ref->force = 1;
1290 }
1291 }
1292
1293 for (rmp = &ref_map; *rmp; ) {
1294 if ((*rmp)->peer_ref) {
1295 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1296 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1297 struct ref *ignore = *rmp;
1298 error("* Ignoring funny ref '%s' locally",
1299 (*rmp)->peer_ref->name);
1300 *rmp = (*rmp)->next;
1301 free(ignore->peer_ref);
1302 free(ignore);
1303 continue;
1304 }
1305 }
1306 rmp = &((*rmp)->next);
1307 }
1308
1309 if (ref_map)
1310 tail_link_ref(ref_map, tail);
1311
1312 return 0;
1313}
1314
1315int resolve_remote_symref(struct ref *ref, struct ref *list)
1316{
1317 if (!ref->symref)
1318 return 0;
1319 for (; list; list = list->next)
1320 if (!strcmp(ref->symref, list->name)) {
1321 hashcpy(ref->old_sha1, list->old_sha1);
1322 return 0;
1323 }
1324 return 1;
1325}
1326
1327static void unmark_and_free(struct commit_list *list, unsigned int mark)
1328{
1329 while (list) {
1330 struct commit_list *temp = list;
1331 temp->item->object.flags &= ~mark;
1332 list = temp->next;
1333 free(temp);
1334 }
1335}
1336
1337int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1338{
1339 struct object *o;
1340 struct commit *old, *new;
1341 struct commit_list *list, *used;
1342 int found = 0;
1343
1344 /* Both new and old must be commit-ish and new is descendant of
1345 * old. Otherwise we require --force.
1346 */
1347 o = deref_tag(parse_object(old_sha1), NULL, 0);
1348 if (!o || o->type != OBJ_COMMIT)
1349 return 0;
1350 old = (struct commit *) o;
1351
1352 o = deref_tag(parse_object(new_sha1), NULL, 0);
1353 if (!o || o->type != OBJ_COMMIT)
1354 return 0;
1355 new = (struct commit *) o;
1356
1357 if (parse_commit(new) < 0)
1358 return 0;
1359
1360 used = list = NULL;
1361 commit_list_insert(new, &list);
1362 while (list) {
1363 new = pop_most_recent_commit(&list, TMP_MARK);
1364 commit_list_insert(new, &used);
1365 if (new == old) {
1366 found = 1;
1367 break;
1368 }
1369 }
1370 unmark_and_free(list, TMP_MARK);
1371 unmark_and_free(used, TMP_MARK);
1372 return found;
1373}
1374
1375/*
1376 * Return true if there is anything to report, otherwise false.
1377 */
1378int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1379{
1380 unsigned char sha1[20];
1381 struct commit *ours, *theirs;
1382 char symmetric[84];
1383 struct rev_info revs;
1384 const char *rev_argv[10], *base;
1385 int rev_argc;
1386
1387 /*
1388 * Nothing to report unless we are marked to build on top of
1389 * somebody else.
1390 */
1391 if (!branch ||
1392 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1393 return 0;
1394
1395 /*
1396 * If what we used to build on no longer exists, there is
1397 * nothing to report.
1398 */
1399 base = branch->merge[0]->dst;
1400 if (!resolve_ref(base, sha1, 1, NULL))
1401 return 0;
1402 theirs = lookup_commit(sha1);
1403 if (!theirs)
1404 return 0;
1405
1406 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1407 return 0;
1408 ours = lookup_commit(sha1);
1409 if (!ours)
1410 return 0;
1411
1412 /* are we the same? */
1413 if (theirs == ours)
1414 return 0;
1415
1416 /* Run "rev-list --left-right ours...theirs" internally... */
1417 rev_argc = 0;
1418 rev_argv[rev_argc++] = NULL;
1419 rev_argv[rev_argc++] = "--left-right";
1420 rev_argv[rev_argc++] = symmetric;
1421 rev_argv[rev_argc++] = "--";
1422 rev_argv[rev_argc] = NULL;
1423
1424 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1425 strcpy(symmetric + 40, "...");
1426 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1427
1428 init_revisions(&revs, NULL);
1429 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1430 prepare_revision_walk(&revs);
1431
1432 /* ... and count the commits on each side. */
1433 *num_ours = 0;
1434 *num_theirs = 0;
1435 while (1) {
1436 struct commit *c = get_revision(&revs);
1437 if (!c)
1438 break;
1439 if (c->object.flags & SYMMETRIC_LEFT)
1440 (*num_ours)++;
1441 else
1442 (*num_theirs)++;
1443 }
1444
1445 /* clear object flags smudged by the above traversal */
1446 clear_commit_marks(ours, ALL_REV_FLAGS);
1447 clear_commit_marks(theirs, ALL_REV_FLAGS);
1448 return 1;
1449}
1450
1451/*
1452 * Return true when there is anything to report, otherwise false.
1453 */
1454int format_tracking_info(struct branch *branch, struct strbuf *sb)
1455{
1456 int num_ours, num_theirs;
1457 const char *base;
1458
1459 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1460 return 0;
1461
1462 base = branch->merge[0]->dst;
1463 base = shorten_unambiguous_ref(base, 0);
1464 if (!num_theirs)
1465 strbuf_addf(sb, "Your branch is ahead of '%s' "
1466 "by %d commit%s.\n",
1467 base, num_ours, (num_ours == 1) ? "" : "s");
1468 else if (!num_ours)
1469 strbuf_addf(sb, "Your branch is behind '%s' "
1470 "by %d commit%s, "
1471 "and can be fast-forwarded.\n",
1472 base, num_theirs, (num_theirs == 1) ? "" : "s");
1473 else
1474 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1475 "and have %d and %d different commit(s) each, "
1476 "respectively.\n",
1477 base, num_ours, num_theirs);
1478 return 1;
1479}
1480
1481static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1482{
1483 struct ref ***local_tail = cb_data;
1484 struct ref *ref;
1485 int len;
1486
1487 /* we already know it starts with refs/ to get here */
1488 if (check_ref_format(refname + 5))
1489 return 0;
1490
1491 len = strlen(refname) + 1;
1492 ref = xcalloc(1, sizeof(*ref) + len);
1493 hashcpy(ref->new_sha1, sha1);
1494 memcpy(ref->name, refname, len);
1495 **local_tail = ref;
1496 *local_tail = &ref->next;
1497 return 0;
1498}
1499
1500struct ref *get_local_heads(void)
1501{
1502 struct ref *local_refs = NULL, **local_tail = &local_refs;
1503 for_each_ref(one_local_ref, &local_tail);
1504 return local_refs;
1505}
1506
1507struct ref *guess_remote_head(const struct ref *head,
1508 const struct ref *refs,
1509 int all)
1510{
1511 const struct ref *r;
1512 struct ref *list = NULL;
1513 struct ref **tail = &list;
1514
1515 if (!head)
1516 return NULL;
1517
1518 /*
1519 * Some transports support directly peeking at
1520 * where HEAD points; if that is the case, then
1521 * we don't have to guess.
1522 */
1523 if (head->symref)
1524 return copy_ref(find_ref_by_name(refs, head->symref));
1525
1526 /* If refs/heads/master could be right, it is. */
1527 if (!all) {
1528 r = find_ref_by_name(refs, "refs/heads/master");
1529 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1530 return copy_ref(r);
1531 }
1532
1533 /* Look for another ref that points there */
1534 for (r = refs; r; r = r->next) {
1535 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1536 *tail = copy_ref(r);
1537 tail = &((*tail)->next);
1538 if (!all)
1539 break;
1540 }
1541 }
1542
1543 return list;
1544}