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#include "string-list.h"
10#include "mergesort.h"
11
12enum map_direction { FROM_SRC, FROM_DST };
13
14static struct refspec s_tag_refspec = {
15 0,
16 1,
17 0,
18 0,
19 "refs/tags/*",
20 "refs/tags/*"
21};
22
23const struct refspec *tag_refspec = &s_tag_refspec;
24
25struct counted_string {
26 size_t len;
27 const char *s;
28};
29struct rewrite {
30 const char *base;
31 size_t baselen;
32 struct counted_string *instead_of;
33 int instead_of_nr;
34 int instead_of_alloc;
35};
36struct rewrites {
37 struct rewrite **rewrite;
38 int rewrite_alloc;
39 int rewrite_nr;
40};
41
42static struct remote **remotes;
43static int remotes_alloc;
44static int remotes_nr;
45
46static struct branch **branches;
47static int branches_alloc;
48static int branches_nr;
49
50static struct branch *current_branch;
51static const char *default_remote_name;
52static const char *pushremote_name;
53static int explicit_default_remote_name;
54
55static struct rewrites rewrites;
56static struct rewrites rewrites_push;
57
58#define BUF_SIZE (2048)
59static char buffer[BUF_SIZE];
60
61static int valid_remote(const struct remote *remote)
62{
63 return (!!remote->url) || (!!remote->foreign_vcs);
64}
65
66static const char *alias_url(const char *url, struct rewrites *r)
67{
68 int i, j;
69 char *ret;
70 struct counted_string *longest;
71 int longest_i;
72
73 longest = NULL;
74 longest_i = -1;
75 for (i = 0; i < r->rewrite_nr; i++) {
76 if (!r->rewrite[i])
77 continue;
78 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
79 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
80 (!longest ||
81 longest->len < r->rewrite[i]->instead_of[j].len)) {
82 longest = &(r->rewrite[i]->instead_of[j]);
83 longest_i = i;
84 }
85 }
86 }
87 if (!longest)
88 return url;
89
90 ret = xmalloc(r->rewrite[longest_i]->baselen +
91 (strlen(url) - longest->len) + 1);
92 strcpy(ret, r->rewrite[longest_i]->base);
93 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
94 return ret;
95}
96
97static void add_push_refspec(struct remote *remote, const char *ref)
98{
99 ALLOC_GROW(remote->push_refspec,
100 remote->push_refspec_nr + 1,
101 remote->push_refspec_alloc);
102 remote->push_refspec[remote->push_refspec_nr++] = ref;
103}
104
105static void add_fetch_refspec(struct remote *remote, const char *ref)
106{
107 ALLOC_GROW(remote->fetch_refspec,
108 remote->fetch_refspec_nr + 1,
109 remote->fetch_refspec_alloc);
110 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
111}
112
113static void add_url(struct remote *remote, const char *url)
114{
115 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
116 remote->url[remote->url_nr++] = url;
117}
118
119static void add_pushurl(struct remote *remote, const char *pushurl)
120{
121 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
122 remote->pushurl[remote->pushurl_nr++] = pushurl;
123}
124
125static void add_pushurl_alias(struct remote *remote, const char *url)
126{
127 const char *pushurl = alias_url(url, &rewrites_push);
128 if (pushurl != url)
129 add_pushurl(remote, pushurl);
130}
131
132static void add_url_alias(struct remote *remote, const char *url)
133{
134 add_url(remote, alias_url(url, &rewrites));
135 add_pushurl_alias(remote, url);
136}
137
138static struct remote *make_remote(const char *name, int len)
139{
140 struct remote *ret;
141 int i;
142
143 for (i = 0; i < remotes_nr; i++) {
144 if (len ? (!strncmp(name, remotes[i]->name, len) &&
145 !remotes[i]->name[len]) :
146 !strcmp(name, remotes[i]->name))
147 return remotes[i];
148 }
149
150 ret = xcalloc(1, sizeof(struct remote));
151 ret->prune = -1; /* unspecified */
152 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
153 remotes[remotes_nr++] = ret;
154 if (len)
155 ret->name = xstrndup(name, len);
156 else
157 ret->name = xstrdup(name);
158 return ret;
159}
160
161static void add_merge(struct branch *branch, const char *name)
162{
163 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
164 branch->merge_alloc);
165 branch->merge_name[branch->merge_nr++] = name;
166}
167
168static struct branch *make_branch(const char *name, int len)
169{
170 struct branch *ret;
171 int i;
172 char *refname;
173
174 for (i = 0; i < branches_nr; i++) {
175 if (len ? (!strncmp(name, branches[i]->name, len) &&
176 !branches[i]->name[len]) :
177 !strcmp(name, branches[i]->name))
178 return branches[i];
179 }
180
181 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
182 ret = xcalloc(1, sizeof(struct branch));
183 branches[branches_nr++] = ret;
184 if (len)
185 ret->name = xstrndup(name, len);
186 else
187 ret->name = xstrdup(name);
188 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
189 strcpy(refname, "refs/heads/");
190 strcpy(refname + strlen("refs/heads/"), ret->name);
191 ret->refname = refname;
192
193 return ret;
194}
195
196static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
197{
198 struct rewrite *ret;
199 int i;
200
201 for (i = 0; i < r->rewrite_nr; i++) {
202 if (len
203 ? (len == r->rewrite[i]->baselen &&
204 !strncmp(base, r->rewrite[i]->base, len))
205 : !strcmp(base, r->rewrite[i]->base))
206 return r->rewrite[i];
207 }
208
209 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
210 ret = xcalloc(1, sizeof(struct rewrite));
211 r->rewrite[r->rewrite_nr++] = ret;
212 if (len) {
213 ret->base = xstrndup(base, len);
214 ret->baselen = len;
215 }
216 else {
217 ret->base = xstrdup(base);
218 ret->baselen = strlen(base);
219 }
220 return ret;
221}
222
223static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
224{
225 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
226 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
227 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
228 rewrite->instead_of_nr++;
229}
230
231static void read_remotes_file(struct remote *remote)
232{
233 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
234
235 if (!f)
236 return;
237 remote->origin = REMOTE_REMOTES;
238 while (fgets(buffer, BUF_SIZE, f)) {
239 int value_list;
240 char *s, *p;
241
242 if (!prefixcmp(buffer, "URL:")) {
243 value_list = 0;
244 s = buffer + 4;
245 } else if (!prefixcmp(buffer, "Push:")) {
246 value_list = 1;
247 s = buffer + 5;
248 } else if (!prefixcmp(buffer, "Pull:")) {
249 value_list = 2;
250 s = buffer + 5;
251 } else
252 continue;
253
254 while (isspace(*s))
255 s++;
256 if (!*s)
257 continue;
258
259 p = s + strlen(s);
260 while (isspace(p[-1]))
261 *--p = 0;
262
263 switch (value_list) {
264 case 0:
265 add_url_alias(remote, xstrdup(s));
266 break;
267 case 1:
268 add_push_refspec(remote, xstrdup(s));
269 break;
270 case 2:
271 add_fetch_refspec(remote, xstrdup(s));
272 break;
273 }
274 }
275 fclose(f);
276}
277
278static void read_branches_file(struct remote *remote)
279{
280 const char *slash = strchr(remote->name, '/');
281 char *frag;
282 struct strbuf branch = STRBUF_INIT;
283 int n = slash ? slash - remote->name : 1000;
284 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
285 char *s, *p;
286 int len;
287
288 if (!f)
289 return;
290 s = fgets(buffer, BUF_SIZE, f);
291 fclose(f);
292 if (!s)
293 return;
294 while (isspace(*s))
295 s++;
296 if (!*s)
297 return;
298 remote->origin = REMOTE_BRANCHES;
299 p = s + strlen(s);
300 while (isspace(p[-1]))
301 *--p = 0;
302 len = p - s;
303 if (slash)
304 len += strlen(slash);
305 p = xmalloc(len + 1);
306 strcpy(p, s);
307 if (slash)
308 strcat(p, slash);
309
310 /*
311 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
312 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
313 * the partial URL obtained from the branches file plus
314 * "/netdev-2.6" and does not store it in any tracking ref.
315 * #branch specifier in the file is ignored.
316 *
317 * Otherwise, the branches file would have URL and optionally
318 * #branch specified. The "master" (or specified) branch is
319 * fetched and stored in the local branch of the same name.
320 */
321 frag = strchr(p, '#');
322 if (frag) {
323 *(frag++) = '\0';
324 strbuf_addf(&branch, "refs/heads/%s", frag);
325 } else
326 strbuf_addstr(&branch, "refs/heads/master");
327 if (!slash) {
328 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
329 } else {
330 strbuf_reset(&branch);
331 strbuf_addstr(&branch, "HEAD:");
332 }
333 add_url_alias(remote, p);
334 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
335 /*
336 * Cogito compatible push: push current HEAD to remote #branch
337 * (master if missing)
338 */
339 strbuf_init(&branch, 0);
340 strbuf_addstr(&branch, "HEAD");
341 if (frag)
342 strbuf_addf(&branch, ":refs/heads/%s", frag);
343 else
344 strbuf_addstr(&branch, ":refs/heads/master");
345 add_push_refspec(remote, strbuf_detach(&branch, NULL));
346 remote->fetch_tags = 1; /* always auto-follow */
347}
348
349static int handle_config(const char *key, const char *value, void *cb)
350{
351 const char *name;
352 const char *subkey;
353 struct remote *remote;
354 struct branch *branch;
355 if (!prefixcmp(key, "branch.")) {
356 name = key + 7;
357 subkey = strrchr(name, '.');
358 if (!subkey)
359 return 0;
360 branch = make_branch(name, subkey - name);
361 if (!strcmp(subkey, ".remote")) {
362 if (git_config_string(&branch->remote_name, key, value))
363 return -1;
364 if (branch == current_branch) {
365 default_remote_name = branch->remote_name;
366 explicit_default_remote_name = 1;
367 }
368 } else if (!strcmp(subkey, ".pushremote")) {
369 if (branch == current_branch)
370 if (git_config_string(&pushremote_name, key, value))
371 return -1;
372 } else if (!strcmp(subkey, ".merge")) {
373 if (!value)
374 return config_error_nonbool(key);
375 add_merge(branch, xstrdup(value));
376 }
377 return 0;
378 }
379 if (!prefixcmp(key, "url.")) {
380 struct rewrite *rewrite;
381 name = key + 4;
382 subkey = strrchr(name, '.');
383 if (!subkey)
384 return 0;
385 if (!strcmp(subkey, ".insteadof")) {
386 rewrite = make_rewrite(&rewrites, name, subkey - name);
387 if (!value)
388 return config_error_nonbool(key);
389 add_instead_of(rewrite, xstrdup(value));
390 } else if (!strcmp(subkey, ".pushinsteadof")) {
391 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
392 if (!value)
393 return config_error_nonbool(key);
394 add_instead_of(rewrite, xstrdup(value));
395 }
396 }
397
398 if (prefixcmp(key, "remote."))
399 return 0;
400 name = key + 7;
401
402 /* Handle remote.* variables */
403 if (!strcmp(name, "pushdefault"))
404 return git_config_string(&pushremote_name, key, value);
405
406 /* Handle remote.<name>.* variables */
407 if (*name == '/') {
408 warning("Config remote shorthand cannot begin with '/': %s",
409 name);
410 return 0;
411 }
412 subkey = strrchr(name, '.');
413 if (!subkey)
414 return 0;
415 remote = make_remote(name, subkey - name);
416 remote->origin = REMOTE_CONFIG;
417 if (!strcmp(subkey, ".mirror"))
418 remote->mirror = git_config_bool(key, value);
419 else if (!strcmp(subkey, ".skipdefaultupdate"))
420 remote->skip_default_update = git_config_bool(key, value);
421 else if (!strcmp(subkey, ".skipfetchall"))
422 remote->skip_default_update = git_config_bool(key, value);
423 else if (!strcmp(subkey, ".prune"))
424 remote->prune = git_config_bool(key, value);
425 else if (!strcmp(subkey, ".url")) {
426 const char *v;
427 if (git_config_string(&v, key, value))
428 return -1;
429 add_url(remote, v);
430 } else if (!strcmp(subkey, ".pushurl")) {
431 const char *v;
432 if (git_config_string(&v, key, value))
433 return -1;
434 add_pushurl(remote, v);
435 } else if (!strcmp(subkey, ".push")) {
436 const char *v;
437 if (git_config_string(&v, key, value))
438 return -1;
439 add_push_refspec(remote, v);
440 } else if (!strcmp(subkey, ".fetch")) {
441 const char *v;
442 if (git_config_string(&v, key, value))
443 return -1;
444 add_fetch_refspec(remote, v);
445 } else if (!strcmp(subkey, ".receivepack")) {
446 const char *v;
447 if (git_config_string(&v, key, value))
448 return -1;
449 if (!remote->receivepack)
450 remote->receivepack = v;
451 else
452 error("more than one receivepack given, using the first");
453 } else if (!strcmp(subkey, ".uploadpack")) {
454 const char *v;
455 if (git_config_string(&v, key, value))
456 return -1;
457 if (!remote->uploadpack)
458 remote->uploadpack = v;
459 else
460 error("more than one uploadpack given, using the first");
461 } else if (!strcmp(subkey, ".tagopt")) {
462 if (!strcmp(value, "--no-tags"))
463 remote->fetch_tags = -1;
464 else if (!strcmp(value, "--tags"))
465 remote->fetch_tags = 2;
466 } else if (!strcmp(subkey, ".proxy")) {
467 return git_config_string((const char **)&remote->http_proxy,
468 key, value);
469 } else if (!strcmp(subkey, ".vcs")) {
470 return git_config_string(&remote->foreign_vcs, key, value);
471 }
472 return 0;
473}
474
475static void alias_all_urls(void)
476{
477 int i, j;
478 for (i = 0; i < remotes_nr; i++) {
479 int add_pushurl_aliases;
480 if (!remotes[i])
481 continue;
482 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
483 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
484 }
485 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
486 for (j = 0; j < remotes[i]->url_nr; j++) {
487 if (add_pushurl_aliases)
488 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
489 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
490 }
491 }
492}
493
494static void read_config(void)
495{
496 unsigned char sha1[20];
497 const char *head_ref;
498 int flag;
499 if (default_remote_name) /* did this already */
500 return;
501 default_remote_name = xstrdup("origin");
502 current_branch = NULL;
503 head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
504 if (head_ref && (flag & REF_ISSYMREF) &&
505 !prefixcmp(head_ref, "refs/heads/")) {
506 current_branch =
507 make_branch(head_ref + strlen("refs/heads/"), 0);
508 }
509 git_config(handle_config, NULL);
510 alias_all_urls();
511}
512
513/*
514 * This function frees a refspec array.
515 * Warning: code paths should be checked to ensure that the src
516 * and dst pointers are always freeable pointers as well
517 * as the refspec pointer itself.
518 */
519static void free_refspecs(struct refspec *refspec, int nr_refspec)
520{
521 int i;
522
523 if (!refspec)
524 return;
525
526 for (i = 0; i < nr_refspec; i++) {
527 free(refspec[i].src);
528 free(refspec[i].dst);
529 }
530 free(refspec);
531}
532
533static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
534{
535 int i;
536 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
537
538 for (i = 0; i < nr_refspec; i++) {
539 size_t llen;
540 int is_glob;
541 const char *lhs, *rhs;
542 int flags;
543
544 is_glob = 0;
545
546 lhs = refspec[i];
547 if (*lhs == '+') {
548 rs[i].force = 1;
549 lhs++;
550 }
551
552 rhs = strrchr(lhs, ':');
553
554 /*
555 * Before going on, special case ":" (or "+:") as a refspec
556 * for pushing matching refs.
557 */
558 if (!fetch && rhs == lhs && rhs[1] == '\0') {
559 rs[i].matching = 1;
560 continue;
561 }
562
563 if (rhs) {
564 size_t rlen = strlen(++rhs);
565 is_glob = (1 <= rlen && strchr(rhs, '*'));
566 rs[i].dst = xstrndup(rhs, rlen);
567 }
568
569 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
570 if (1 <= llen && memchr(lhs, '*', llen)) {
571 if ((rhs && !is_glob) || (!rhs && fetch))
572 goto invalid;
573 is_glob = 1;
574 } else if (rhs && is_glob) {
575 goto invalid;
576 }
577
578 rs[i].pattern = is_glob;
579 rs[i].src = xstrndup(lhs, llen);
580 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
581
582 if (fetch) {
583 unsigned char unused[40];
584
585 /* LHS */
586 if (!*rs[i].src)
587 ; /* empty is ok; it means "HEAD" */
588 else if (llen == 40 && !get_sha1_hex(rs[i].src, unused))
589 rs[i].exact_sha1 = 1; /* ok */
590 else if (!check_refname_format(rs[i].src, flags))
591 ; /* valid looking ref is ok */
592 else
593 goto invalid;
594 /* RHS */
595 if (!rs[i].dst)
596 ; /* missing is ok; it is the same as empty */
597 else if (!*rs[i].dst)
598 ; /* empty is ok; it means "do not store" */
599 else if (!check_refname_format(rs[i].dst, flags))
600 ; /* valid looking ref is ok */
601 else
602 goto invalid;
603 } else {
604 /*
605 * LHS
606 * - empty is allowed; it means delete.
607 * - when wildcarded, it must be a valid looking ref.
608 * - otherwise, it must be an extended SHA-1, but
609 * there is no existing way to validate this.
610 */
611 if (!*rs[i].src)
612 ; /* empty is ok */
613 else if (is_glob) {
614 if (check_refname_format(rs[i].src, flags))
615 goto invalid;
616 }
617 else
618 ; /* anything goes, for now */
619 /*
620 * RHS
621 * - missing is allowed, but LHS then must be a
622 * valid looking ref.
623 * - empty is not allowed.
624 * - otherwise it must be a valid looking ref.
625 */
626 if (!rs[i].dst) {
627 if (check_refname_format(rs[i].src, flags))
628 goto invalid;
629 } else if (!*rs[i].dst) {
630 goto invalid;
631 } else {
632 if (check_refname_format(rs[i].dst, flags))
633 goto invalid;
634 }
635 }
636 }
637 return rs;
638
639 invalid:
640 if (verify) {
641 /*
642 * nr_refspec must be greater than zero and i must be valid
643 * since it is only possible to reach this point from within
644 * the for loop above.
645 */
646 free_refspecs(rs, i+1);
647 return NULL;
648 }
649 die("Invalid refspec '%s'", refspec[i]);
650}
651
652int valid_fetch_refspec(const char *fetch_refspec_str)
653{
654 struct refspec *refspec;
655
656 refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
657 free_refspecs(refspec, 1);
658 return !!refspec;
659}
660
661struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
662{
663 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
664}
665
666static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
667{
668 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
669}
670
671void free_refspec(int nr_refspec, struct refspec *refspec)
672{
673 int i;
674 for (i = 0; i < nr_refspec; i++) {
675 free(refspec[i].src);
676 free(refspec[i].dst);
677 }
678 free(refspec);
679}
680
681static int valid_remote_nick(const char *name)
682{
683 if (!name[0] || is_dot_or_dotdot(name))
684 return 0;
685 return !strchr(name, '/'); /* no slash */
686}
687
688static struct remote *remote_get_1(const char *name, const char *pushremote_name)
689{
690 struct remote *ret;
691 int name_given = 0;
692
693 if (name)
694 name_given = 1;
695 else {
696 if (pushremote_name) {
697 name = pushremote_name;
698 name_given = 1;
699 } else {
700 name = default_remote_name;
701 name_given = explicit_default_remote_name;
702 }
703 }
704
705 ret = make_remote(name, 0);
706 if (valid_remote_nick(name)) {
707 if (!valid_remote(ret))
708 read_remotes_file(ret);
709 if (!valid_remote(ret))
710 read_branches_file(ret);
711 }
712 if (name_given && !valid_remote(ret))
713 add_url_alias(ret, name);
714 if (!valid_remote(ret))
715 return NULL;
716 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
717 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
718 return ret;
719}
720
721struct remote *remote_get(const char *name)
722{
723 read_config();
724 return remote_get_1(name, NULL);
725}
726
727struct remote *pushremote_get(const char *name)
728{
729 read_config();
730 return remote_get_1(name, pushremote_name);
731}
732
733int remote_is_configured(const char *name)
734{
735 int i;
736 read_config();
737
738 for (i = 0; i < remotes_nr; i++)
739 if (!strcmp(name, remotes[i]->name))
740 return 1;
741 return 0;
742}
743
744int for_each_remote(each_remote_fn fn, void *priv)
745{
746 int i, result = 0;
747 read_config();
748 for (i = 0; i < remotes_nr && !result; i++) {
749 struct remote *r = remotes[i];
750 if (!r)
751 continue;
752 if (!r->fetch)
753 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
754 r->fetch_refspec);
755 if (!r->push)
756 r->push = parse_push_refspec(r->push_refspec_nr,
757 r->push_refspec);
758 result = fn(r, priv);
759 }
760 return result;
761}
762
763void ref_remove_duplicates(struct ref *ref_map)
764{
765 struct string_list refs = STRING_LIST_INIT_NODUP;
766 struct string_list_item *item = NULL;
767 struct ref *prev = NULL, *next = NULL;
768 for (; ref_map; prev = ref_map, ref_map = next) {
769 next = ref_map->next;
770 if (!ref_map->peer_ref)
771 continue;
772
773 item = string_list_lookup(&refs, ref_map->peer_ref->name);
774 if (item) {
775 if (strcmp(((struct ref *)item->util)->name,
776 ref_map->name))
777 die("%s tracks both %s and %s",
778 ref_map->peer_ref->name,
779 ((struct ref *)item->util)->name,
780 ref_map->name);
781 prev->next = ref_map->next;
782 free(ref_map->peer_ref);
783 free(ref_map);
784 ref_map = prev; /* skip this; we freed it */
785 continue;
786 }
787
788 item = string_list_insert(&refs, ref_map->peer_ref->name);
789 item->util = ref_map;
790 }
791 string_list_clear(&refs, 0);
792}
793
794int remote_has_url(struct remote *remote, const char *url)
795{
796 int i;
797 for (i = 0; i < remote->url_nr; i++) {
798 if (!strcmp(remote->url[i], url))
799 return 1;
800 }
801 return 0;
802}
803
804static int match_name_with_pattern(const char *key, const char *name,
805 const char *value, char **result)
806{
807 const char *kstar = strchr(key, '*');
808 size_t klen;
809 size_t ksuffixlen;
810 size_t namelen;
811 int ret;
812 if (!kstar)
813 die("Key '%s' of pattern had no '*'", key);
814 klen = kstar - key;
815 ksuffixlen = strlen(kstar + 1);
816 namelen = strlen(name);
817 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
818 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
819 if (ret && value) {
820 const char *vstar = strchr(value, '*');
821 size_t vlen;
822 size_t vsuffixlen;
823 if (!vstar)
824 die("Value '%s' of pattern has no '*'", value);
825 vlen = vstar - value;
826 vsuffixlen = strlen(vstar + 1);
827 *result = xmalloc(vlen + vsuffixlen +
828 strlen(name) -
829 klen - ksuffixlen + 1);
830 strncpy(*result, value, vlen);
831 strncpy(*result + vlen,
832 name + klen, namelen - klen - ksuffixlen);
833 strcpy(*result + vlen + namelen - klen - ksuffixlen,
834 vstar + 1);
835 }
836 return ret;
837}
838
839static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
840{
841 int i;
842 int find_src = !query->src;
843
844 if (find_src && !query->dst)
845 return error("query_refspecs: need either src or dst");
846
847 for (i = 0; i < ref_count; i++) {
848 struct refspec *refspec = &refs[i];
849 const char *key = find_src ? refspec->dst : refspec->src;
850 const char *value = find_src ? refspec->src : refspec->dst;
851 const char *needle = find_src ? query->dst : query->src;
852 char **result = find_src ? &query->src : &query->dst;
853
854 if (!refspec->dst)
855 continue;
856 if (refspec->pattern) {
857 if (match_name_with_pattern(key, needle, value, result)) {
858 query->force = refspec->force;
859 return 0;
860 }
861 } else if (!strcmp(needle, key)) {
862 *result = xstrdup(value);
863 query->force = refspec->force;
864 return 0;
865 }
866 }
867 return -1;
868}
869
870char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
871 const char *name)
872{
873 struct refspec query;
874
875 memset(&query, 0, sizeof(struct refspec));
876 query.src = (char *)name;
877
878 if (query_refspecs(refspecs, nr_refspec, &query))
879 return NULL;
880
881 return query.dst;
882}
883
884int remote_find_tracking(struct remote *remote, struct refspec *refspec)
885{
886 return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
887}
888
889static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
890 const char *name)
891{
892 size_t len = strlen(name);
893 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
894 memcpy(ref->name, prefix, prefixlen);
895 memcpy(ref->name + prefixlen, name, len);
896 return ref;
897}
898
899struct ref *alloc_ref(const char *name)
900{
901 return alloc_ref_with_prefix("", 0, name);
902}
903
904struct ref *copy_ref(const struct ref *ref)
905{
906 struct ref *cpy;
907 size_t len;
908 if (!ref)
909 return NULL;
910 len = strlen(ref->name);
911 cpy = xmalloc(sizeof(struct ref) + len + 1);
912 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
913 cpy->next = NULL;
914 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
915 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
916 cpy->peer_ref = copy_ref(ref->peer_ref);
917 return cpy;
918}
919
920struct ref *copy_ref_list(const struct ref *ref)
921{
922 struct ref *ret = NULL;
923 struct ref **tail = &ret;
924 while (ref) {
925 *tail = copy_ref(ref);
926 ref = ref->next;
927 tail = &((*tail)->next);
928 }
929 return ret;
930}
931
932static void free_ref(struct ref *ref)
933{
934 if (!ref)
935 return;
936 free_ref(ref->peer_ref);
937 free(ref->remote_status);
938 free(ref->symref);
939 free(ref);
940}
941
942void free_refs(struct ref *ref)
943{
944 struct ref *next;
945 while (ref) {
946 next = ref->next;
947 free_ref(ref);
948 ref = next;
949 }
950}
951
952int ref_compare_name(const void *va, const void *vb)
953{
954 const struct ref *a = va, *b = vb;
955 return strcmp(a->name, b->name);
956}
957
958static void *ref_list_get_next(const void *a)
959{
960 return ((const struct ref *)a)->next;
961}
962
963static void ref_list_set_next(void *a, void *next)
964{
965 ((struct ref *)a)->next = next;
966}
967
968void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
969{
970 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
971}
972
973static int count_refspec_match(const char *pattern,
974 struct ref *refs,
975 struct ref **matched_ref)
976{
977 int patlen = strlen(pattern);
978 struct ref *matched_weak = NULL;
979 struct ref *matched = NULL;
980 int weak_match = 0;
981 int match = 0;
982
983 for (weak_match = match = 0; refs; refs = refs->next) {
984 char *name = refs->name;
985 int namelen = strlen(name);
986
987 if (!refname_match(pattern, name, ref_rev_parse_rules))
988 continue;
989
990 /* A match is "weak" if it is with refs outside
991 * heads or tags, and did not specify the pattern
992 * in full (e.g. "refs/remotes/origin/master") or at
993 * least from the toplevel (e.g. "remotes/origin/master");
994 * otherwise "git push $URL master" would result in
995 * ambiguity between remotes/origin/master and heads/master
996 * at the remote site.
997 */
998 if (namelen != patlen &&
999 patlen != namelen - 5 &&
1000 prefixcmp(name, "refs/heads/") &&
1001 prefixcmp(name, "refs/tags/")) {
1002 /* We want to catch the case where only weak
1003 * matches are found and there are multiple
1004 * matches, and where more than one strong
1005 * matches are found, as ambiguous. One
1006 * strong match with zero or more weak matches
1007 * are acceptable as a unique match.
1008 */
1009 matched_weak = refs;
1010 weak_match++;
1011 }
1012 else {
1013 matched = refs;
1014 match++;
1015 }
1016 }
1017 if (!matched) {
1018 *matched_ref = matched_weak;
1019 return weak_match;
1020 }
1021 else {
1022 *matched_ref = matched;
1023 return match;
1024 }
1025}
1026
1027static void tail_link_ref(struct ref *ref, struct ref ***tail)
1028{
1029 **tail = ref;
1030 while (ref->next)
1031 ref = ref->next;
1032 *tail = &ref->next;
1033}
1034
1035static struct ref *alloc_delete_ref(void)
1036{
1037 struct ref *ref = alloc_ref("(delete)");
1038 hashclr(ref->new_sha1);
1039 return ref;
1040}
1041
1042static struct ref *try_explicit_object_name(const char *name)
1043{
1044 unsigned char sha1[20];
1045 struct ref *ref;
1046
1047 if (!*name)
1048 return alloc_delete_ref();
1049 if (get_sha1(name, sha1))
1050 return NULL;
1051 ref = alloc_ref(name);
1052 hashcpy(ref->new_sha1, sha1);
1053 return ref;
1054}
1055
1056static struct ref *make_linked_ref(const char *name, struct ref ***tail)
1057{
1058 struct ref *ret = alloc_ref(name);
1059 tail_link_ref(ret, tail);
1060 return ret;
1061}
1062
1063static char *guess_ref(const char *name, struct ref *peer)
1064{
1065 struct strbuf buf = STRBUF_INIT;
1066 unsigned char sha1[20];
1067
1068 const char *r = resolve_ref_unsafe(peer->name, sha1, 1, NULL);
1069 if (!r)
1070 return NULL;
1071
1072 if (!prefixcmp(r, "refs/heads/"))
1073 strbuf_addstr(&buf, "refs/heads/");
1074 else if (!prefixcmp(r, "refs/tags/"))
1075 strbuf_addstr(&buf, "refs/tags/");
1076 else
1077 return NULL;
1078
1079 strbuf_addstr(&buf, name);
1080 return strbuf_detach(&buf, NULL);
1081}
1082
1083static int match_explicit(struct ref *src, struct ref *dst,
1084 struct ref ***dst_tail,
1085 struct refspec *rs)
1086{
1087 struct ref *matched_src, *matched_dst;
1088 int copy_src;
1089
1090 const char *dst_value = rs->dst;
1091 char *dst_guess;
1092
1093 if (rs->pattern || rs->matching)
1094 return 0;
1095
1096 matched_src = matched_dst = NULL;
1097 switch (count_refspec_match(rs->src, src, &matched_src)) {
1098 case 1:
1099 copy_src = 1;
1100 break;
1101 case 0:
1102 /* The source could be in the get_sha1() format
1103 * not a reference name. :refs/other is a
1104 * way to delete 'other' ref at the remote end.
1105 */
1106 matched_src = try_explicit_object_name(rs->src);
1107 if (!matched_src)
1108 return error("src refspec %s does not match any.", rs->src);
1109 copy_src = 0;
1110 break;
1111 default:
1112 return error("src refspec %s matches more than one.", rs->src);
1113 }
1114
1115 if (!dst_value) {
1116 unsigned char sha1[20];
1117 int flag;
1118
1119 dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
1120 if (!dst_value ||
1121 ((flag & REF_ISSYMREF) &&
1122 prefixcmp(dst_value, "refs/heads/")))
1123 die("%s cannot be resolved to branch.",
1124 matched_src->name);
1125 }
1126
1127 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1128 case 1:
1129 break;
1130 case 0:
1131 if (!memcmp(dst_value, "refs/", 5))
1132 matched_dst = make_linked_ref(dst_value, dst_tail);
1133 else if (is_null_sha1(matched_src->new_sha1))
1134 error("unable to delete '%s': remote ref does not exist",
1135 dst_value);
1136 else if ((dst_guess = guess_ref(dst_value, matched_src)))
1137 matched_dst = make_linked_ref(dst_guess, dst_tail);
1138 else
1139 error("unable to push to unqualified destination: %s\n"
1140 "The destination refspec neither matches an "
1141 "existing ref on the remote nor\n"
1142 "begins with refs/, and we are unable to "
1143 "guess a prefix based on the source ref.",
1144 dst_value);
1145 break;
1146 default:
1147 matched_dst = NULL;
1148 error("dst refspec %s matches more than one.",
1149 dst_value);
1150 break;
1151 }
1152 if (!matched_dst)
1153 return -1;
1154 if (matched_dst->peer_ref)
1155 return error("dst ref %s receives from more than one src.",
1156 matched_dst->name);
1157 else {
1158 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1159 matched_dst->force = rs->force;
1160 }
1161 return 0;
1162}
1163
1164static int match_explicit_refs(struct ref *src, struct ref *dst,
1165 struct ref ***dst_tail, struct refspec *rs,
1166 int rs_nr)
1167{
1168 int i, errs;
1169 for (i = errs = 0; i < rs_nr; i++)
1170 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1171 return errs;
1172}
1173
1174static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref *ref,
1175 int send_mirror, int direction, const struct refspec **ret_pat)
1176{
1177 const struct refspec *pat;
1178 char *name;
1179 int i;
1180 int matching_refs = -1;
1181 for (i = 0; i < rs_nr; i++) {
1182 if (rs[i].matching &&
1183 (matching_refs == -1 || rs[i].force)) {
1184 matching_refs = i;
1185 continue;
1186 }
1187
1188 if (rs[i].pattern) {
1189 const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src;
1190 int match;
1191 if (direction == FROM_SRC)
1192 match = match_name_with_pattern(rs[i].src, ref->name, dst_side, &name);
1193 else
1194 match = match_name_with_pattern(dst_side, ref->name, rs[i].src, &name);
1195 if (match) {
1196 matching_refs = i;
1197 break;
1198 }
1199 }
1200 }
1201 if (matching_refs == -1)
1202 return NULL;
1203
1204 pat = rs + matching_refs;
1205 if (pat->matching) {
1206 /*
1207 * "matching refs"; traditionally we pushed everything
1208 * including refs outside refs/heads/ hierarchy, but
1209 * that does not make much sense these days.
1210 */
1211 if (!send_mirror && prefixcmp(ref->name, "refs/heads/"))
1212 return NULL;
1213 name = xstrdup(ref->name);
1214 }
1215 if (ret_pat)
1216 *ret_pat = pat;
1217 return name;
1218}
1219
1220static struct ref **tail_ref(struct ref **head)
1221{
1222 struct ref **tail = head;
1223 while (*tail)
1224 tail = &((*tail)->next);
1225 return tail;
1226}
1227
1228struct tips {
1229 struct commit **tip;
1230 int nr, alloc;
1231};
1232
1233static void add_to_tips(struct tips *tips, const unsigned char *sha1)
1234{
1235 struct commit *commit;
1236
1237 if (is_null_sha1(sha1))
1238 return;
1239 commit = lookup_commit_reference_gently(sha1, 1);
1240 if (!commit || (commit->object.flags & TMP_MARK))
1241 return;
1242 commit->object.flags |= TMP_MARK;
1243 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1244 tips->tip[tips->nr++] = commit;
1245}
1246
1247static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1248{
1249 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1250 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1251 struct string_list_item *item;
1252 struct ref *ref;
1253 struct tips sent_tips;
1254
1255 /*
1256 * Collect everything we know they would have at the end of
1257 * this push, and collect all tags they have.
1258 */
1259 memset(&sent_tips, 0, sizeof(sent_tips));
1260 for (ref = *dst; ref; ref = ref->next) {
1261 if (ref->peer_ref &&
1262 !is_null_sha1(ref->peer_ref->new_sha1))
1263 add_to_tips(&sent_tips, ref->peer_ref->new_sha1);
1264 else
1265 add_to_tips(&sent_tips, ref->old_sha1);
1266 if (!prefixcmp(ref->name, "refs/tags/"))
1267 string_list_append(&dst_tag, ref->name);
1268 }
1269 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1270
1271 sort_string_list(&dst_tag);
1272
1273 /* Collect tags they do not have. */
1274 for (ref = src; ref; ref = ref->next) {
1275 if (prefixcmp(ref->name, "refs/tags/"))
1276 continue; /* not a tag */
1277 if (string_list_has_string(&dst_tag, ref->name))
1278 continue; /* they already have it */
1279 if (sha1_object_info(ref->new_sha1, NULL) != OBJ_TAG)
1280 continue; /* be conservative */
1281 item = string_list_append(&src_tag, ref->name);
1282 item->util = ref;
1283 }
1284 string_list_clear(&dst_tag, 0);
1285
1286 /*
1287 * At this point, src_tag lists tags that are missing from
1288 * dst, and sent_tips lists the tips we are pushing or those
1289 * that we know they already have. An element in the src_tag
1290 * that is an ancestor of any of the sent_tips needs to be
1291 * sent to the other side.
1292 */
1293 if (sent_tips.nr) {
1294 for_each_string_list_item(item, &src_tag) {
1295 struct ref *ref = item->util;
1296 struct ref *dst_ref;
1297 struct commit *commit;
1298
1299 if (is_null_sha1(ref->new_sha1))
1300 continue;
1301 commit = lookup_commit_reference_gently(ref->new_sha1, 1);
1302 if (!commit)
1303 /* not pushing a commit, which is not an error */
1304 continue;
1305
1306 /*
1307 * Is this tag, which they do not have, reachable from
1308 * any of the commits we are sending?
1309 */
1310 if (!in_merge_bases_many(commit, sent_tips.nr, sent_tips.tip))
1311 continue;
1312
1313 /* Add it in */
1314 dst_ref = make_linked_ref(ref->name, dst_tail);
1315 hashcpy(dst_ref->new_sha1, ref->new_sha1);
1316 dst_ref->peer_ref = copy_ref(ref);
1317 }
1318 }
1319 string_list_clear(&src_tag, 0);
1320 free(sent_tips.tip);
1321}
1322
1323/*
1324 * Given the set of refs the local repository has, the set of refs the
1325 * remote repository has, and the refspec used for push, determine
1326 * what remote refs we will update and with what value by setting
1327 * peer_ref (which object is being pushed) and force (if the push is
1328 * forced) in elements of "dst". The function may add new elements to
1329 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1330 */
1331int match_push_refs(struct ref *src, struct ref **dst,
1332 int nr_refspec, const char **refspec, int flags)
1333{
1334 struct refspec *rs;
1335 int send_all = flags & MATCH_REFS_ALL;
1336 int send_mirror = flags & MATCH_REFS_MIRROR;
1337 int send_prune = flags & MATCH_REFS_PRUNE;
1338 int errs;
1339 static const char *default_refspec[] = { ":", NULL };
1340 struct ref *ref, **dst_tail = tail_ref(dst);
1341
1342 if (!nr_refspec) {
1343 nr_refspec = 1;
1344 refspec = default_refspec;
1345 }
1346 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
1347 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
1348
1349 /* pick the remainder */
1350 for (ref = src; ref; ref = ref->next) {
1351 struct ref *dst_peer;
1352 const struct refspec *pat = NULL;
1353 char *dst_name;
1354
1355 dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_SRC, &pat);
1356 if (!dst_name)
1357 continue;
1358
1359 dst_peer = find_ref_by_name(*dst, dst_name);
1360 if (dst_peer) {
1361 if (dst_peer->peer_ref)
1362 /* We're already sending something to this ref. */
1363 goto free_name;
1364 } else {
1365 if (pat->matching && !(send_all || send_mirror))
1366 /*
1367 * Remote doesn't have it, and we have no
1368 * explicit pattern, and we don't have
1369 * --all nor --mirror.
1370 */
1371 goto free_name;
1372
1373 /* Create a new one and link it */
1374 dst_peer = make_linked_ref(dst_name, &dst_tail);
1375 hashcpy(dst_peer->new_sha1, ref->new_sha1);
1376 }
1377 dst_peer->peer_ref = copy_ref(ref);
1378 dst_peer->force = pat->force;
1379 free_name:
1380 free(dst_name);
1381 }
1382
1383 if (flags & MATCH_REFS_FOLLOW_TAGS)
1384 add_missing_tags(src, dst, &dst_tail);
1385
1386 if (send_prune) {
1387 /* check for missing refs on the remote */
1388 for (ref = *dst; ref; ref = ref->next) {
1389 char *src_name;
1390
1391 if (ref->peer_ref)
1392 /* We're already sending something to this ref. */
1393 continue;
1394
1395 src_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_DST, NULL);
1396 if (src_name) {
1397 if (!find_ref_by_name(src, src_name))
1398 ref->peer_ref = alloc_delete_ref();
1399 free(src_name);
1400 }
1401 }
1402 }
1403 if (errs)
1404 return -1;
1405 return 0;
1406}
1407
1408void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1409 int force_update)
1410{
1411 struct ref *ref;
1412
1413 for (ref = remote_refs; ref; ref = ref->next) {
1414 int force_ref_update = ref->force || force_update;
1415
1416 if (ref->peer_ref)
1417 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1418 else if (!send_mirror)
1419 continue;
1420
1421 ref->deletion = is_null_sha1(ref->new_sha1);
1422 if (!ref->deletion &&
1423 !hashcmp(ref->old_sha1, ref->new_sha1)) {
1424 ref->status = REF_STATUS_UPTODATE;
1425 continue;
1426 }
1427
1428 /*
1429 * Decide whether an individual refspec A:B can be
1430 * pushed. The push will succeed if any of the
1431 * following are true:
1432 *
1433 * (1) the remote reference B does not exist
1434 *
1435 * (2) the remote reference B is being removed (i.e.,
1436 * pushing :B where no source is specified)
1437 *
1438 * (3) the destination is not under refs/tags/, and
1439 * if the old and new value is a commit, the new
1440 * is a descendant of the old.
1441 *
1442 * (4) it is forced using the +A:B notation, or by
1443 * passing the --force argument
1444 */
1445
1446 if (!ref->deletion && !is_null_sha1(ref->old_sha1)) {
1447 int why = 0; /* why would this push require --force? */
1448
1449 if (!prefixcmp(ref->name, "refs/tags/"))
1450 why = REF_STATUS_REJECT_ALREADY_EXISTS;
1451 else if (!has_sha1_file(ref->old_sha1))
1452 why = REF_STATUS_REJECT_FETCH_FIRST;
1453 else if (!lookup_commit_reference_gently(ref->old_sha1, 1) ||
1454 !lookup_commit_reference_gently(ref->new_sha1, 1))
1455 why = REF_STATUS_REJECT_NEEDS_FORCE;
1456 else if (!ref_newer(ref->new_sha1, ref->old_sha1))
1457 why = REF_STATUS_REJECT_NONFASTFORWARD;
1458
1459 if (!force_ref_update)
1460 ref->status = why;
1461 else if (why)
1462 ref->forced_update = 1;
1463 }
1464 }
1465}
1466
1467struct branch *branch_get(const char *name)
1468{
1469 struct branch *ret;
1470
1471 read_config();
1472 if (!name || !*name || !strcmp(name, "HEAD"))
1473 ret = current_branch;
1474 else
1475 ret = make_branch(name, 0);
1476 if (ret && ret->remote_name) {
1477 ret->remote = remote_get(ret->remote_name);
1478 if (ret->merge_nr) {
1479 int i;
1480 ret->merge = xcalloc(sizeof(*ret->merge),
1481 ret->merge_nr);
1482 for (i = 0; i < ret->merge_nr; i++) {
1483 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1484 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1485 if (remote_find_tracking(ret->remote, ret->merge[i])
1486 && !strcmp(ret->remote_name, "."))
1487 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1488 }
1489 }
1490 }
1491 return ret;
1492}
1493
1494int branch_has_merge_config(struct branch *branch)
1495{
1496 return branch && !!branch->merge;
1497}
1498
1499int branch_merge_matches(struct branch *branch,
1500 int i,
1501 const char *refname)
1502{
1503 if (!branch || i < 0 || i >= branch->merge_nr)
1504 return 0;
1505 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
1506}
1507
1508static int ignore_symref_update(const char *refname)
1509{
1510 unsigned char sha1[20];
1511 int flag;
1512
1513 if (!resolve_ref_unsafe(refname, sha1, 0, &flag))
1514 return 0; /* non-existing refs are OK */
1515 return (flag & REF_ISSYMREF);
1516}
1517
1518static struct ref *get_expanded_map(const struct ref *remote_refs,
1519 const struct refspec *refspec)
1520{
1521 const struct ref *ref;
1522 struct ref *ret = NULL;
1523 struct ref **tail = &ret;
1524
1525 char *expn_name;
1526
1527 for (ref = remote_refs; ref; ref = ref->next) {
1528 if (strchr(ref->name, '^'))
1529 continue; /* a dereference item */
1530 if (match_name_with_pattern(refspec->src, ref->name,
1531 refspec->dst, &expn_name) &&
1532 !ignore_symref_update(expn_name)) {
1533 struct ref *cpy = copy_ref(ref);
1534
1535 cpy->peer_ref = alloc_ref(expn_name);
1536 free(expn_name);
1537 if (refspec->force)
1538 cpy->peer_ref->force = 1;
1539 *tail = cpy;
1540 tail = &cpy->next;
1541 }
1542 }
1543
1544 return ret;
1545}
1546
1547static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
1548{
1549 const struct ref *ref;
1550 for (ref = refs; ref; ref = ref->next) {
1551 if (refname_match(name, ref->name, ref_fetch_rules))
1552 return ref;
1553 }
1554 return NULL;
1555}
1556
1557struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
1558{
1559 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
1560
1561 if (!ref)
1562 return NULL;
1563
1564 return copy_ref(ref);
1565}
1566
1567static struct ref *get_local_ref(const char *name)
1568{
1569 if (!name || name[0] == '\0')
1570 return NULL;
1571
1572 if (!prefixcmp(name, "refs/"))
1573 return alloc_ref(name);
1574
1575 if (!prefixcmp(name, "heads/") ||
1576 !prefixcmp(name, "tags/") ||
1577 !prefixcmp(name, "remotes/"))
1578 return alloc_ref_with_prefix("refs/", 5, name);
1579
1580 return alloc_ref_with_prefix("refs/heads/", 11, name);
1581}
1582
1583int get_fetch_map(const struct ref *remote_refs,
1584 const struct refspec *refspec,
1585 struct ref ***tail,
1586 int missing_ok)
1587{
1588 struct ref *ref_map, **rmp;
1589
1590 if (refspec->pattern) {
1591 ref_map = get_expanded_map(remote_refs, refspec);
1592 } else {
1593 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1594
1595 if (refspec->exact_sha1) {
1596 ref_map = alloc_ref(name);
1597 get_sha1_hex(name, ref_map->old_sha1);
1598 } else {
1599 ref_map = get_remote_ref(remote_refs, name);
1600 }
1601 if (!missing_ok && !ref_map)
1602 die("Couldn't find remote ref %s", name);
1603 if (ref_map) {
1604 ref_map->peer_ref = get_local_ref(refspec->dst);
1605 if (ref_map->peer_ref && refspec->force)
1606 ref_map->peer_ref->force = 1;
1607 }
1608 }
1609
1610 for (rmp = &ref_map; *rmp; ) {
1611 if ((*rmp)->peer_ref) {
1612 if (prefixcmp((*rmp)->peer_ref->name, "refs/") ||
1613 check_refname_format((*rmp)->peer_ref->name, 0)) {
1614 struct ref *ignore = *rmp;
1615 error("* Ignoring funny ref '%s' locally",
1616 (*rmp)->peer_ref->name);
1617 *rmp = (*rmp)->next;
1618 free(ignore->peer_ref);
1619 free(ignore);
1620 continue;
1621 }
1622 }
1623 rmp = &((*rmp)->next);
1624 }
1625
1626 if (ref_map)
1627 tail_link_ref(ref_map, tail);
1628
1629 return 0;
1630}
1631
1632int resolve_remote_symref(struct ref *ref, struct ref *list)
1633{
1634 if (!ref->symref)
1635 return 0;
1636 for (; list; list = list->next)
1637 if (!strcmp(ref->symref, list->name)) {
1638 hashcpy(ref->old_sha1, list->old_sha1);
1639 return 0;
1640 }
1641 return 1;
1642}
1643
1644static void unmark_and_free(struct commit_list *list, unsigned int mark)
1645{
1646 while (list) {
1647 struct commit_list *temp = list;
1648 temp->item->object.flags &= ~mark;
1649 list = temp->next;
1650 free(temp);
1651 }
1652}
1653
1654int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1655{
1656 struct object *o;
1657 struct commit *old, *new;
1658 struct commit_list *list, *used;
1659 int found = 0;
1660
1661 /*
1662 * Both new and old must be commit-ish and new is descendant of
1663 * old. Otherwise we require --force.
1664 */
1665 o = deref_tag(parse_object(old_sha1), NULL, 0);
1666 if (!o || o->type != OBJ_COMMIT)
1667 return 0;
1668 old = (struct commit *) o;
1669
1670 o = deref_tag(parse_object(new_sha1), NULL, 0);
1671 if (!o || o->type != OBJ_COMMIT)
1672 return 0;
1673 new = (struct commit *) o;
1674
1675 if (parse_commit(new) < 0)
1676 return 0;
1677
1678 used = list = NULL;
1679 commit_list_insert(new, &list);
1680 while (list) {
1681 new = pop_most_recent_commit(&list, TMP_MARK);
1682 commit_list_insert(new, &used);
1683 if (new == old) {
1684 found = 1;
1685 break;
1686 }
1687 }
1688 unmark_and_free(list, TMP_MARK);
1689 unmark_and_free(used, TMP_MARK);
1690 return found;
1691}
1692
1693/*
1694 * Return true if there is anything to report, otherwise false.
1695 */
1696int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1697{
1698 unsigned char sha1[20];
1699 struct commit *ours, *theirs;
1700 char symmetric[84];
1701 struct rev_info revs;
1702 const char *rev_argv[10], *base;
1703 int rev_argc;
1704
1705 /*
1706 * Nothing to report unless we are marked to build on top of
1707 * somebody else.
1708 */
1709 if (!branch ||
1710 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1711 return 0;
1712
1713 /*
1714 * If what we used to build on no longer exists, there is
1715 * nothing to report.
1716 */
1717 base = branch->merge[0]->dst;
1718 if (read_ref(base, sha1))
1719 return 0;
1720 theirs = lookup_commit_reference(sha1);
1721 if (!theirs)
1722 return 0;
1723
1724 if (read_ref(branch->refname, sha1))
1725 return 0;
1726 ours = lookup_commit_reference(sha1);
1727 if (!ours)
1728 return 0;
1729
1730 /* are we the same? */
1731 if (theirs == ours)
1732 return 0;
1733
1734 /* Run "rev-list --left-right ours...theirs" internally... */
1735 rev_argc = 0;
1736 rev_argv[rev_argc++] = NULL;
1737 rev_argv[rev_argc++] = "--left-right";
1738 rev_argv[rev_argc++] = symmetric;
1739 rev_argv[rev_argc++] = "--";
1740 rev_argv[rev_argc] = NULL;
1741
1742 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1743 strcpy(symmetric + 40, "...");
1744 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1745
1746 init_revisions(&revs, NULL);
1747 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1748 prepare_revision_walk(&revs);
1749
1750 /* ... and count the commits on each side. */
1751 *num_ours = 0;
1752 *num_theirs = 0;
1753 while (1) {
1754 struct commit *c = get_revision(&revs);
1755 if (!c)
1756 break;
1757 if (c->object.flags & SYMMETRIC_LEFT)
1758 (*num_ours)++;
1759 else
1760 (*num_theirs)++;
1761 }
1762
1763 /* clear object flags smudged by the above traversal */
1764 clear_commit_marks(ours, ALL_REV_FLAGS);
1765 clear_commit_marks(theirs, ALL_REV_FLAGS);
1766 return 1;
1767}
1768
1769/*
1770 * Return true when there is anything to report, otherwise false.
1771 */
1772int format_tracking_info(struct branch *branch, struct strbuf *sb)
1773{
1774 int num_ours, num_theirs;
1775 const char *base;
1776
1777 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1778 return 0;
1779
1780 base = branch->merge[0]->dst;
1781 base = shorten_unambiguous_ref(base, 0);
1782 if (!num_theirs) {
1783 strbuf_addf(sb,
1784 Q_("Your branch is ahead of '%s' by %d commit.\n",
1785 "Your branch is ahead of '%s' by %d commits.\n",
1786 num_ours),
1787 base, num_ours);
1788 if (advice_status_hints)
1789 strbuf_addf(sb,
1790 _(" (use \"git push\" to publish your local commits)\n"));
1791 } else if (!num_ours) {
1792 strbuf_addf(sb,
1793 Q_("Your branch is behind '%s' by %d commit, "
1794 "and can be fast-forwarded.\n",
1795 "Your branch is behind '%s' by %d commits, "
1796 "and can be fast-forwarded.\n",
1797 num_theirs),
1798 base, num_theirs);
1799 if (advice_status_hints)
1800 strbuf_addf(sb,
1801 _(" (use \"git pull\" to update your local branch)\n"));
1802 } else {
1803 strbuf_addf(sb,
1804 Q_("Your branch and '%s' have diverged,\n"
1805 "and have %d and %d different commit each, "
1806 "respectively.\n",
1807 "Your branch and '%s' have diverged,\n"
1808 "and have %d and %d different commits each, "
1809 "respectively.\n",
1810 num_theirs),
1811 base, num_ours, num_theirs);
1812 if (advice_status_hints)
1813 strbuf_addf(sb,
1814 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
1815 }
1816 return 1;
1817}
1818
1819static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1820{
1821 struct ref ***local_tail = cb_data;
1822 struct ref *ref;
1823 int len;
1824
1825 /* we already know it starts with refs/ to get here */
1826 if (check_refname_format(refname + 5, 0))
1827 return 0;
1828
1829 len = strlen(refname) + 1;
1830 ref = xcalloc(1, sizeof(*ref) + len);
1831 hashcpy(ref->new_sha1, sha1);
1832 memcpy(ref->name, refname, len);
1833 **local_tail = ref;
1834 *local_tail = &ref->next;
1835 return 0;
1836}
1837
1838struct ref *get_local_heads(void)
1839{
1840 struct ref *local_refs = NULL, **local_tail = &local_refs;
1841 for_each_ref(one_local_ref, &local_tail);
1842 return local_refs;
1843}
1844
1845struct ref *guess_remote_head(const struct ref *head,
1846 const struct ref *refs,
1847 int all)
1848{
1849 const struct ref *r;
1850 struct ref *list = NULL;
1851 struct ref **tail = &list;
1852
1853 if (!head)
1854 return NULL;
1855
1856 /*
1857 * Some transports support directly peeking at
1858 * where HEAD points; if that is the case, then
1859 * we don't have to guess.
1860 */
1861 if (head->symref)
1862 return copy_ref(find_ref_by_name(refs, head->symref));
1863
1864 /* If refs/heads/master could be right, it is. */
1865 if (!all) {
1866 r = find_ref_by_name(refs, "refs/heads/master");
1867 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1868 return copy_ref(r);
1869 }
1870
1871 /* Look for another ref that points there */
1872 for (r = refs; r; r = r->next) {
1873 if (r != head &&
1874 !prefixcmp(r->name, "refs/heads/") &&
1875 !hashcmp(r->old_sha1, head->old_sha1)) {
1876 *tail = copy_ref(r);
1877 tail = &((*tail)->next);
1878 if (!all)
1879 break;
1880 }
1881 }
1882
1883 return list;
1884}
1885
1886struct stale_heads_info {
1887 struct string_list *ref_names;
1888 struct ref **stale_refs_tail;
1889 struct refspec *refs;
1890 int ref_count;
1891};
1892
1893static int get_stale_heads_cb(const char *refname,
1894 const unsigned char *sha1, int flags, void *cb_data)
1895{
1896 struct stale_heads_info *info = cb_data;
1897 struct refspec query;
1898 memset(&query, 0, sizeof(struct refspec));
1899 query.dst = (char *)refname;
1900
1901 if (query_refspecs(info->refs, info->ref_count, &query))
1902 return 0; /* No matches */
1903
1904 /*
1905 * If we did find a suitable refspec and it's not a symref and
1906 * it's not in the list of refs that currently exist in that
1907 * remote we consider it to be stale.
1908 */
1909 if (!((flags & REF_ISSYMREF) ||
1910 string_list_has_string(info->ref_names, query.src))) {
1911 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1912 hashcpy(ref->new_sha1, sha1);
1913 }
1914
1915 free(query.src);
1916 return 0;
1917}
1918
1919struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map)
1920{
1921 struct ref *ref, *stale_refs = NULL;
1922 struct string_list ref_names = STRING_LIST_INIT_NODUP;
1923 struct stale_heads_info info;
1924 info.ref_names = &ref_names;
1925 info.stale_refs_tail = &stale_refs;
1926 info.refs = refs;
1927 info.ref_count = ref_count;
1928 for (ref = fetch_map; ref; ref = ref->next)
1929 string_list_append(&ref_names, ref->name);
1930 sort_string_list(&ref_names);
1931 for_each_ref(get_stale_heads_cb, &info);
1932 string_list_clear(&ref_names, 0);
1933 return stale_refs;
1934}