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