Merge branch 'dh/encoding-trace-optim'
[git/gitster.git] / remote.c
blobf43cf5e7a4d2432113400c28a45e58cc2230b36b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "abspath.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "remote.h"
10 #include "urlmatch.h"
11 #include "refs.h"
12 #include "refspec.h"
13 #include "object-name.h"
14 #include "object-store-ll.h"
15 #include "path.h"
16 #include "commit.h"
17 #include "diff.h"
18 #include "revision.h"
19 #include "dir.h"
20 #include "setup.h"
21 #include "string-list.h"
22 #include "strvec.h"
23 #include "commit-reach.h"
24 #include "advice.h"
25 #include "connect.h"
26 #include "parse-options.h"
28 enum map_direction { FROM_SRC, FROM_DST };
30 struct counted_string {
31 size_t len;
32 const char *s;
35 static int valid_remote(const struct remote *remote)
37 return !!remote->url.nr;
40 static char *alias_url(const char *url, struct rewrites *r)
42 int i, j;
43 struct counted_string *longest;
44 int longest_i;
46 longest = NULL;
47 longest_i = -1;
48 for (i = 0; i < r->rewrite_nr; i++) {
49 if (!r->rewrite[i])
50 continue;
51 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
52 if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
53 (!longest ||
54 longest->len < r->rewrite[i]->instead_of[j].len)) {
55 longest = &(r->rewrite[i]->instead_of[j]);
56 longest_i = i;
60 if (!longest)
61 return NULL;
63 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
66 static void add_url(struct remote *remote, const char *url)
68 if (*url)
69 strvec_push(&remote->url, url);
70 else
71 strvec_clear(&remote->url);
74 static void add_pushurl(struct remote *remote, const char *pushurl)
76 if (*pushurl)
77 strvec_push(&remote->pushurl, pushurl);
78 else
79 strvec_clear(&remote->pushurl);
82 static void add_pushurl_alias(struct remote_state *remote_state,
83 struct remote *remote, const char *url)
85 char *alias = alias_url(url, &remote_state->rewrites_push);
86 if (alias)
87 add_pushurl(remote, alias);
88 free(alias);
91 static void add_url_alias(struct remote_state *remote_state,
92 struct remote *remote, const char *url)
94 char *alias = alias_url(url, &remote_state->rewrites);
95 add_url(remote, alias ? alias : url);
96 add_pushurl_alias(remote_state, remote, url);
97 free(alias);
100 struct remotes_hash_key {
101 const char *str;
102 int len;
105 static int remotes_hash_cmp(const void *cmp_data UNUSED,
106 const struct hashmap_entry *eptr,
107 const struct hashmap_entry *entry_or_key,
108 const void *keydata)
110 const struct remote *a, *b;
111 const struct remotes_hash_key *key = keydata;
113 a = container_of(eptr, const struct remote, ent);
114 b = container_of(entry_or_key, const struct remote, ent);
116 if (key)
117 return !!xstrncmpz(a->name, key->str, key->len);
118 else
119 return strcmp(a->name, b->name);
122 static struct remote *make_remote(struct remote_state *remote_state,
123 const char *name, int len)
125 struct remote *ret;
126 struct remotes_hash_key lookup;
127 struct hashmap_entry lookup_entry, *e;
129 if (!len)
130 len = strlen(name);
132 lookup.str = name;
133 lookup.len = len;
134 hashmap_entry_init(&lookup_entry, memhash(name, len));
136 e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup);
137 if (e)
138 return container_of(e, struct remote, ent);
140 CALLOC_ARRAY(ret, 1);
141 ret->prune = -1; /* unspecified */
142 ret->prune_tags = -1; /* unspecified */
143 ret->name = xstrndup(name, len);
144 refspec_init(&ret->push, REFSPEC_PUSH);
145 refspec_init(&ret->fetch, REFSPEC_FETCH);
147 ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
148 remote_state->remotes_alloc);
149 remote_state->remotes[remote_state->remotes_nr++] = ret;
151 hashmap_entry_init(&ret->ent, lookup_entry.hash);
152 if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent))
153 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
154 return ret;
157 static void remote_clear(struct remote *remote)
159 free((char *)remote->name);
160 free((char *)remote->foreign_vcs);
162 strvec_clear(&remote->url);
163 strvec_clear(&remote->pushurl);
165 free((char *)remote->receivepack);
166 free((char *)remote->uploadpack);
167 FREE_AND_NULL(remote->http_proxy);
168 FREE_AND_NULL(remote->http_proxy_authmethod);
171 static void add_merge(struct branch *branch, const char *name)
173 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
174 branch->merge_alloc);
175 branch->merge_name[branch->merge_nr++] = name;
178 struct branches_hash_key {
179 const char *str;
180 int len;
183 static int branches_hash_cmp(const void *cmp_data UNUSED,
184 const struct hashmap_entry *eptr,
185 const struct hashmap_entry *entry_or_key,
186 const void *keydata)
188 const struct branch *a, *b;
189 const struct branches_hash_key *key = keydata;
191 a = container_of(eptr, const struct branch, ent);
192 b = container_of(entry_or_key, const struct branch, ent);
194 if (key)
195 return !!xstrncmpz(a->name, key->str, key->len);
196 else
197 return strcmp(a->name, b->name);
200 static struct branch *find_branch(struct remote_state *remote_state,
201 const char *name, size_t len)
203 struct branches_hash_key lookup;
204 struct hashmap_entry lookup_entry, *e;
206 lookup.str = name;
207 lookup.len = len;
208 hashmap_entry_init(&lookup_entry, memhash(name, len));
210 e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup);
211 if (e)
212 return container_of(e, struct branch, ent);
214 return NULL;
217 static void die_on_missing_branch(struct repository *repo,
218 struct branch *branch)
220 /* branch == NULL is always valid because it represents detached HEAD. */
221 if (branch &&
222 branch != find_branch(repo->remote_state, branch->name,
223 strlen(branch->name)))
224 die("branch %s was not found in the repository", branch->name);
227 static struct branch *make_branch(struct remote_state *remote_state,
228 const char *name, size_t len)
230 struct branch *ret;
232 ret = find_branch(remote_state, name, len);
233 if (ret)
234 return ret;
236 CALLOC_ARRAY(ret, 1);
237 ret->name = xstrndup(name, len);
238 ret->refname = xstrfmt("refs/heads/%s", ret->name);
240 hashmap_entry_init(&ret->ent, memhash(name, len));
241 if (hashmap_put_entry(&remote_state->branches_hash, ret, ent))
242 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
243 return ret;
246 static struct rewrite *make_rewrite(struct rewrites *r,
247 const char *base, size_t len)
249 struct rewrite *ret;
250 int i;
252 for (i = 0; i < r->rewrite_nr; i++) {
253 if (len == r->rewrite[i]->baselen &&
254 !strncmp(base, r->rewrite[i]->base, len))
255 return r->rewrite[i];
258 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
259 CALLOC_ARRAY(ret, 1);
260 r->rewrite[r->rewrite_nr++] = ret;
261 ret->base = xstrndup(base, len);
262 ret->baselen = len;
263 return ret;
266 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
268 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
269 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
270 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
271 rewrite->instead_of_nr++;
274 static const char *skip_spaces(const char *s)
276 while (isspace(*s))
277 s++;
278 return s;
281 static void read_remotes_file(struct remote_state *remote_state,
282 struct remote *remote)
284 struct strbuf buf = STRBUF_INIT;
285 FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
287 if (!f)
288 return;
289 remote->configured_in_repo = 1;
290 remote->origin = REMOTE_REMOTES;
291 while (strbuf_getline(&buf, f) != EOF) {
292 const char *v;
294 strbuf_rtrim(&buf);
296 if (skip_prefix(buf.buf, "URL:", &v))
297 add_url_alias(remote_state, remote,
298 skip_spaces(v));
299 else if (skip_prefix(buf.buf, "Push:", &v))
300 refspec_append(&remote->push, skip_spaces(v));
301 else if (skip_prefix(buf.buf, "Pull:", &v))
302 refspec_append(&remote->fetch, skip_spaces(v));
304 strbuf_release(&buf);
305 fclose(f);
308 static void read_branches_file(struct remote_state *remote_state,
309 struct remote *remote)
311 char *frag, *to_free = NULL;
312 struct strbuf buf = STRBUF_INIT;
313 FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
315 if (!f)
316 return;
318 strbuf_getline_lf(&buf, f);
319 fclose(f);
320 strbuf_trim(&buf);
321 if (!buf.len) {
322 strbuf_release(&buf);
323 return;
326 remote->configured_in_repo = 1;
327 remote->origin = REMOTE_BRANCHES;
330 * The branches file would have URL and optionally
331 * #branch specified. The default (or specified) branch is
332 * fetched and stored in the local branch matching the
333 * remote name.
335 frag = strchr(buf.buf, '#');
336 if (frag)
337 *(frag++) = '\0';
338 else
339 frag = to_free = repo_default_branch_name(the_repository, 0);
341 add_url_alias(remote_state, remote, buf.buf);
342 refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
343 frag, remote->name);
346 * Cogito compatible push: push current HEAD to remote #branch
347 * (master if missing)
349 refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
350 remote->fetch_tags = 1; /* always auto-follow */
352 strbuf_release(&buf);
353 free(to_free);
356 static int handle_config(const char *key, const char *value,
357 const struct config_context *ctx, void *cb)
359 const char *name;
360 size_t namelen;
361 const char *subkey;
362 struct remote *remote;
363 struct branch *branch;
364 struct remote_state *remote_state = cb;
365 const struct key_value_info *kvi = ctx->kvi;
367 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
368 /* There is no subsection. */
369 if (!name)
370 return 0;
371 /* There is a subsection, but it is empty. */
372 if (!namelen)
373 return -1;
374 branch = make_branch(remote_state, name, namelen);
375 if (!strcmp(subkey, "remote")) {
376 return git_config_string(&branch->remote_name, key, value);
377 } else if (!strcmp(subkey, "pushremote")) {
378 return git_config_string(&branch->pushremote_name, key, value);
379 } else if (!strcmp(subkey, "merge")) {
380 if (!value)
381 return config_error_nonbool(key);
382 add_merge(branch, xstrdup(value));
384 return 0;
386 if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
387 struct rewrite *rewrite;
388 if (!name)
389 return 0;
390 if (!strcmp(subkey, "insteadof")) {
391 if (!value)
392 return config_error_nonbool(key);
393 rewrite = make_rewrite(&remote_state->rewrites, name,
394 namelen);
395 add_instead_of(rewrite, xstrdup(value));
396 } else if (!strcmp(subkey, "pushinsteadof")) {
397 if (!value)
398 return config_error_nonbool(key);
399 rewrite = make_rewrite(&remote_state->rewrites_push,
400 name, namelen);
401 add_instead_of(rewrite, xstrdup(value));
405 if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
406 return 0;
408 /* Handle remote.* variables */
409 if (!name && !strcmp(subkey, "pushdefault"))
410 return git_config_string(&remote_state->pushremote_name, key,
411 value);
413 if (!name)
414 return 0;
415 /* Handle remote.<name>.* variables */
416 if (*name == '/') {
417 warning(_("config remote shorthand cannot begin with '/': %s"),
418 name);
419 return 0;
421 remote = make_remote(remote_state, name, namelen);
422 remote->origin = REMOTE_CONFIG;
423 if (kvi->scope == CONFIG_SCOPE_LOCAL ||
424 kvi->scope == CONFIG_SCOPE_WORKTREE)
425 remote->configured_in_repo = 1;
426 if (!strcmp(subkey, "mirror"))
427 remote->mirror = git_config_bool(key, value);
428 else if (!strcmp(subkey, "skipdefaultupdate"))
429 remote->skip_default_update = git_config_bool(key, value);
430 else if (!strcmp(subkey, "skipfetchall"))
431 remote->skip_default_update = git_config_bool(key, value);
432 else if (!strcmp(subkey, "prune"))
433 remote->prune = git_config_bool(key, value);
434 else if (!strcmp(subkey, "prunetags"))
435 remote->prune_tags = git_config_bool(key, value);
436 else if (!strcmp(subkey, "url")) {
437 if (!value)
438 return config_error_nonbool(key);
439 add_url(remote, value);
440 } else if (!strcmp(subkey, "pushurl")) {
441 if (!value)
442 return config_error_nonbool(key);
443 add_pushurl(remote, value);
444 } else if (!strcmp(subkey, "push")) {
445 char *v;
446 if (git_config_string(&v, key, value))
447 return -1;
448 refspec_append(&remote->push, v);
449 free(v);
450 } else if (!strcmp(subkey, "fetch")) {
451 char *v;
452 if (git_config_string(&v, key, value))
453 return -1;
454 refspec_append(&remote->fetch, v);
455 free(v);
456 } else if (!strcmp(subkey, "receivepack")) {
457 char *v;
458 if (git_config_string(&v, key, value))
459 return -1;
460 if (!remote->receivepack)
461 remote->receivepack = v;
462 else
463 error(_("more than one receivepack given, using the first"));
464 } else if (!strcmp(subkey, "uploadpack")) {
465 char *v;
466 if (git_config_string(&v, key, value))
467 return -1;
468 if (!remote->uploadpack)
469 remote->uploadpack = v;
470 else
471 error(_("more than one uploadpack given, using the first"));
472 } else if (!strcmp(subkey, "tagopt")) {
473 if (!strcmp(value, "--no-tags"))
474 remote->fetch_tags = -1;
475 else if (!strcmp(value, "--tags"))
476 remote->fetch_tags = 2;
477 } else if (!strcmp(subkey, "proxy")) {
478 return git_config_string(&remote->http_proxy,
479 key, value);
480 } else if (!strcmp(subkey, "proxyauthmethod")) {
481 return git_config_string(&remote->http_proxy_authmethod,
482 key, value);
483 } else if (!strcmp(subkey, "vcs")) {
484 return git_config_string(&remote->foreign_vcs, key, value);
486 return 0;
489 static void alias_all_urls(struct remote_state *remote_state)
491 int i, j;
492 for (i = 0; i < remote_state->remotes_nr; i++) {
493 int add_pushurl_aliases;
494 if (!remote_state->remotes[i])
495 continue;
496 for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) {
497 char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j],
498 &remote_state->rewrites);
499 if (alias)
500 strvec_replace(&remote_state->remotes[i]->pushurl,
501 j, alias);
503 add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0;
504 for (j = 0; j < remote_state->remotes[i]->url.nr; j++) {
505 char *alias;
506 if (add_pushurl_aliases)
507 add_pushurl_alias(
508 remote_state, remote_state->remotes[i],
509 remote_state->remotes[i]->url.v[j]);
510 alias = alias_url(remote_state->remotes[i]->url.v[j],
511 &remote_state->rewrites);
512 if (alias)
513 strvec_replace(&remote_state->remotes[i]->url,
514 j, alias);
519 static void read_config(struct repository *repo, int early)
521 int flag;
523 if (repo->remote_state->initialized)
524 return;
525 repo->remote_state->initialized = 1;
527 repo->remote_state->current_branch = NULL;
528 if (startup_info->have_repository && !early) {
529 const char *head_ref = refs_resolve_ref_unsafe(
530 get_main_ref_store(repo), "HEAD", 0, NULL, &flag);
531 if (head_ref && (flag & REF_ISSYMREF) &&
532 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
533 repo->remote_state->current_branch = make_branch(
534 repo->remote_state, head_ref, strlen(head_ref));
537 repo_config(repo, handle_config, repo->remote_state);
538 alias_all_urls(repo->remote_state);
541 static int valid_remote_nick(const char *name)
543 if (!name[0] || is_dot_or_dotdot(name))
544 return 0;
546 /* remote nicknames cannot contain slashes */
547 while (*name)
548 if (is_dir_sep(*name++))
549 return 0;
550 return 1;
553 static const char *remotes_remote_for_branch(struct remote_state *remote_state,
554 struct branch *branch,
555 int *explicit)
557 if (branch && branch->remote_name) {
558 if (explicit)
559 *explicit = 1;
560 return branch->remote_name;
562 if (explicit)
563 *explicit = 0;
564 if (remote_state->remotes_nr == 1)
565 return remote_state->remotes[0]->name;
566 return "origin";
569 const char *remote_for_branch(struct branch *branch, int *explicit)
571 read_config(the_repository, 0);
572 die_on_missing_branch(the_repository, branch);
574 return remotes_remote_for_branch(the_repository->remote_state, branch,
575 explicit);
578 static const char *
579 remotes_pushremote_for_branch(struct remote_state *remote_state,
580 struct branch *branch, int *explicit)
582 if (branch && branch->pushremote_name) {
583 if (explicit)
584 *explicit = 1;
585 return branch->pushremote_name;
587 if (remote_state->pushremote_name) {
588 if (explicit)
589 *explicit = 1;
590 return remote_state->pushremote_name;
592 return remotes_remote_for_branch(remote_state, branch, explicit);
595 const char *pushremote_for_branch(struct branch *branch, int *explicit)
597 read_config(the_repository, 0);
598 die_on_missing_branch(the_repository, branch);
600 return remotes_pushremote_for_branch(the_repository->remote_state,
601 branch, explicit);
604 static struct remote *remotes_remote_get(struct remote_state *remote_state,
605 const char *name);
607 const char *remote_ref_for_branch(struct branch *branch, int for_push)
609 read_config(the_repository, 0);
610 die_on_missing_branch(the_repository, branch);
612 if (branch) {
613 if (!for_push) {
614 if (branch->merge_nr) {
615 return branch->merge_name[0];
617 } else {
618 const char *dst,
619 *remote_name = remotes_pushremote_for_branch(
620 the_repository->remote_state, branch,
621 NULL);
622 struct remote *remote = remotes_remote_get(
623 the_repository->remote_state, remote_name);
625 if (remote && remote->push.nr &&
626 (dst = apply_refspecs(&remote->push,
627 branch->refname))) {
628 return dst;
632 return NULL;
635 static void validate_remote_url(struct remote *remote)
637 int i;
638 const char *value;
639 struct strbuf redacted = STRBUF_INIT;
640 int warn_not_die;
642 if (git_config_get_string_tmp("transfer.credentialsinurl", &value))
643 return;
645 if (!strcmp("warn", value))
646 warn_not_die = 1;
647 else if (!strcmp("die", value))
648 warn_not_die = 0;
649 else if (!strcmp("allow", value))
650 return;
651 else
652 die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value);
654 for (i = 0; i < remote->url.nr; i++) {
655 struct url_info url_info = { 0 };
657 if (!url_normalize(remote->url.v[i], &url_info) ||
658 !url_info.passwd_off)
659 goto loop_cleanup;
661 strbuf_reset(&redacted);
662 strbuf_add(&redacted, url_info.url, url_info.passwd_off);
663 strbuf_addstr(&redacted, "<redacted>");
664 strbuf_addstr(&redacted,
665 url_info.url + url_info.passwd_off + url_info.passwd_len);
667 if (warn_not_die)
668 warning(_("URL '%s' uses plaintext credentials"), redacted.buf);
669 else
670 die(_("URL '%s' uses plaintext credentials"), redacted.buf);
672 loop_cleanup:
673 free(url_info.url);
676 strbuf_release(&redacted);
679 static struct remote *
680 remotes_remote_get_1(struct remote_state *remote_state, const char *name,
681 const char *(*get_default)(struct remote_state *,
682 struct branch *, int *))
684 struct remote *ret;
685 int name_given = 0;
687 if (name)
688 name_given = 1;
689 else
690 name = get_default(remote_state, remote_state->current_branch,
691 &name_given);
693 ret = make_remote(remote_state, name, 0);
694 if (valid_remote_nick(name) && have_git_dir()) {
695 if (!valid_remote(ret))
696 read_remotes_file(remote_state, ret);
697 if (!valid_remote(ret))
698 read_branches_file(remote_state, ret);
700 if (name_given && !valid_remote(ret))
701 add_url_alias(remote_state, ret, name);
702 if (!valid_remote(ret))
703 return NULL;
705 validate_remote_url(ret);
707 return ret;
710 static inline struct remote *
711 remotes_remote_get(struct remote_state *remote_state, const char *name)
713 return remotes_remote_get_1(remote_state, name,
714 remotes_remote_for_branch);
717 struct remote *remote_get(const char *name)
719 read_config(the_repository, 0);
720 return remotes_remote_get(the_repository->remote_state, name);
723 struct remote *remote_get_early(const char *name)
725 read_config(the_repository, 1);
726 return remotes_remote_get(the_repository->remote_state, name);
729 static inline struct remote *
730 remotes_pushremote_get(struct remote_state *remote_state, const char *name)
732 return remotes_remote_get_1(remote_state, name,
733 remotes_pushremote_for_branch);
736 struct remote *pushremote_get(const char *name)
738 read_config(the_repository, 0);
739 return remotes_pushremote_get(the_repository->remote_state, name);
742 int remote_is_configured(struct remote *remote, int in_repo)
744 if (!remote)
745 return 0;
746 if (in_repo)
747 return remote->configured_in_repo;
748 return !!remote->origin;
751 int for_each_remote(each_remote_fn fn, void *priv)
753 int i, result = 0;
754 read_config(the_repository, 0);
755 for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
756 i++) {
757 struct remote *remote =
758 the_repository->remote_state->remotes[i];
759 if (!remote)
760 continue;
761 result = fn(remote, priv);
763 return result;
766 static void handle_duplicate(struct ref *ref1, struct ref *ref2)
768 if (strcmp(ref1->name, ref2->name)) {
769 if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
770 ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
771 die(_("Cannot fetch both %s and %s to %s"),
772 ref1->name, ref2->name, ref2->peer_ref->name);
773 } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
774 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
775 warning(_("%s usually tracks %s, not %s"),
776 ref2->peer_ref->name, ref2->name, ref1->name);
777 } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
778 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
779 die(_("%s tracks both %s and %s"),
780 ref2->peer_ref->name, ref1->name, ref2->name);
781 } else {
783 * This last possibility doesn't occur because
784 * FETCH_HEAD_IGNORE entries always appear at
785 * the end of the list.
787 BUG("Internal error");
790 free(ref2->peer_ref);
791 free(ref2);
794 struct ref *ref_remove_duplicates(struct ref *ref_map)
796 struct string_list refs = STRING_LIST_INIT_NODUP;
797 struct ref *retval = NULL;
798 struct ref **p = &retval;
800 while (ref_map) {
801 struct ref *ref = ref_map;
803 ref_map = ref_map->next;
804 ref->next = NULL;
806 if (!ref->peer_ref) {
807 *p = ref;
808 p = &ref->next;
809 } else {
810 struct string_list_item *item =
811 string_list_insert(&refs, ref->peer_ref->name);
813 if (item->util) {
814 /* Entry already existed */
815 handle_duplicate((struct ref *)item->util, ref);
816 } else {
817 *p = ref;
818 p = &ref->next;
819 item->util = ref;
824 string_list_clear(&refs, 0);
825 return retval;
828 int remote_has_url(struct remote *remote, const char *url)
830 int i;
831 for (i = 0; i < remote->url.nr; i++) {
832 if (!strcmp(remote->url.v[i], url))
833 return 1;
835 return 0;
838 struct strvec *push_url_of_remote(struct remote *remote)
840 return remote->pushurl.nr ? &remote->pushurl : &remote->url;
843 static int match_name_with_pattern(const char *key, const char *name,
844 const char *value, char **result)
846 const char *kstar = strchr(key, '*');
847 size_t klen;
848 size_t ksuffixlen;
849 size_t namelen;
850 int ret;
851 if (!kstar)
852 die(_("key '%s' of pattern had no '*'"), key);
853 klen = kstar - key;
854 ksuffixlen = strlen(kstar + 1);
855 namelen = strlen(name);
856 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
857 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
858 if (ret && value) {
859 struct strbuf sb = STRBUF_INIT;
860 const char *vstar = strchr(value, '*');
861 if (!vstar)
862 die(_("value '%s' of pattern has no '*'"), value);
863 strbuf_add(&sb, value, vstar - value);
864 strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
865 strbuf_addstr(&sb, vstar + 1);
866 *result = strbuf_detach(&sb, NULL);
868 return ret;
871 static int refspec_match(const struct refspec_item *refspec,
872 const char *name)
874 if (refspec->pattern)
875 return match_name_with_pattern(refspec->src, name, NULL, NULL);
877 return !strcmp(refspec->src, name);
880 int omit_name_by_refspec(const char *name, struct refspec *rs)
882 int i;
884 for (i = 0; i < rs->nr; i++) {
885 if (rs->items[i].negative && refspec_match(&rs->items[i], name))
886 return 1;
888 return 0;
891 struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
893 struct ref **tail;
895 for (tail = &ref_map; *tail; ) {
896 struct ref *ref = *tail;
898 if (omit_name_by_refspec(ref->name, rs)) {
899 *tail = ref->next;
900 free(ref->peer_ref);
901 free(ref);
902 } else
903 tail = &ref->next;
906 return ref_map;
909 static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
911 int i, matched_negative = 0;
912 int find_src = !query->src;
913 struct string_list reversed = STRING_LIST_INIT_DUP;
914 const char *needle = find_src ? query->dst : query->src;
917 * Check whether the queried ref matches any negative refpsec. If so,
918 * then we should ultimately treat this as not matching the query at
919 * all.
921 * Note that negative refspecs always match the source, but the query
922 * item uses the destination. To handle this, we apply pattern
923 * refspecs in reverse to figure out if the query source matches any
924 * of the negative refspecs.
926 * The first loop finds and expands all positive refspecs
927 * matched by the queried ref.
929 * The second loop checks if any of the results of the first loop
930 * match any negative refspec.
932 for (i = 0; i < rs->nr; i++) {
933 struct refspec_item *refspec = &rs->items[i];
934 char *expn_name;
936 if (refspec->negative)
937 continue;
939 /* Note the reversal of src and dst */
940 if (refspec->pattern) {
941 const char *key = refspec->dst ? refspec->dst : refspec->src;
942 const char *value = refspec->src;
944 if (match_name_with_pattern(key, needle, value, &expn_name))
945 string_list_append_nodup(&reversed, expn_name);
946 } else if (refspec->matching) {
947 /* For the special matching refspec, any query should match */
948 string_list_append(&reversed, needle);
949 } else if (!refspec->src) {
950 BUG("refspec->src should not be null here");
951 } else if (!strcmp(needle, refspec->src)) {
952 string_list_append(&reversed, refspec->src);
956 for (i = 0; !matched_negative && i < reversed.nr; i++) {
957 if (omit_name_by_refspec(reversed.items[i].string, rs))
958 matched_negative = 1;
961 string_list_clear(&reversed, 0);
963 return matched_negative;
966 static void query_refspecs_multiple(struct refspec *rs,
967 struct refspec_item *query,
968 struct string_list *results)
970 int i;
971 int find_src = !query->src;
973 if (find_src && !query->dst)
974 BUG("query_refspecs_multiple: need either src or dst");
976 if (query_matches_negative_refspec(rs, query))
977 return;
979 for (i = 0; i < rs->nr; i++) {
980 struct refspec_item *refspec = &rs->items[i];
981 const char *key = find_src ? refspec->dst : refspec->src;
982 const char *value = find_src ? refspec->src : refspec->dst;
983 const char *needle = find_src ? query->dst : query->src;
984 char **result = find_src ? &query->src : &query->dst;
986 if (!refspec->dst || refspec->negative)
987 continue;
988 if (refspec->pattern) {
989 if (match_name_with_pattern(key, needle, value, result))
990 string_list_append_nodup(results, *result);
991 } else if (!strcmp(needle, key)) {
992 string_list_append(results, value);
997 int query_refspecs(struct refspec *rs, struct refspec_item *query)
999 int i;
1000 int find_src = !query->src;
1001 const char *needle = find_src ? query->dst : query->src;
1002 char **result = find_src ? &query->src : &query->dst;
1004 if (find_src && !query->dst)
1005 BUG("query_refspecs: need either src or dst");
1007 if (query_matches_negative_refspec(rs, query))
1008 return -1;
1010 for (i = 0; i < rs->nr; i++) {
1011 struct refspec_item *refspec = &rs->items[i];
1012 const char *key = find_src ? refspec->dst : refspec->src;
1013 const char *value = find_src ? refspec->src : refspec->dst;
1015 if (!refspec->dst || refspec->negative)
1016 continue;
1017 if (refspec->pattern) {
1018 if (match_name_with_pattern(key, needle, value, result)) {
1019 query->force = refspec->force;
1020 return 0;
1022 } else if (!strcmp(needle, key)) {
1023 *result = xstrdup(value);
1024 query->force = refspec->force;
1025 return 0;
1028 return -1;
1031 char *apply_refspecs(struct refspec *rs, const char *name)
1033 struct refspec_item query;
1035 memset(&query, 0, sizeof(struct refspec_item));
1036 query.src = (char *)name;
1038 if (query_refspecs(rs, &query))
1039 return NULL;
1041 return query.dst;
1044 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
1046 return query_refspecs(&remote->fetch, refspec);
1049 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
1050 const char *name)
1052 size_t len = strlen(name);
1053 struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
1054 memcpy(ref->name, prefix, prefixlen);
1055 memcpy(ref->name + prefixlen, name, len);
1056 return ref;
1059 struct ref *alloc_ref(const char *name)
1061 return alloc_ref_with_prefix("", 0, name);
1064 struct ref *copy_ref(const struct ref *ref)
1066 struct ref *cpy;
1067 size_t len;
1068 if (!ref)
1069 return NULL;
1070 len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
1071 cpy = xmalloc(len);
1072 memcpy(cpy, ref, len);
1073 cpy->next = NULL;
1074 cpy->symref = xstrdup_or_null(ref->symref);
1075 cpy->remote_status = xstrdup_or_null(ref->remote_status);
1076 cpy->peer_ref = copy_ref(ref->peer_ref);
1077 return cpy;
1080 struct ref *copy_ref_list(const struct ref *ref)
1082 struct ref *ret = NULL;
1083 struct ref **tail = &ret;
1084 while (ref) {
1085 *tail = copy_ref(ref);
1086 ref = ref->next;
1087 tail = &((*tail)->next);
1089 return ret;
1092 void free_one_ref(struct ref *ref)
1094 if (!ref)
1095 return;
1096 free_one_ref(ref->peer_ref);
1097 free(ref->remote_status);
1098 free(ref->symref);
1099 free(ref);
1102 void free_refs(struct ref *ref)
1104 struct ref *next;
1105 while (ref) {
1106 next = ref->next;
1107 free_one_ref(ref);
1108 ref = next;
1112 int count_refspec_match(const char *pattern,
1113 struct ref *refs,
1114 struct ref **matched_ref)
1116 int patlen = strlen(pattern);
1117 struct ref *matched_weak = NULL;
1118 struct ref *matched = NULL;
1119 int weak_match = 0;
1120 int match = 0;
1122 for (weak_match = match = 0; refs; refs = refs->next) {
1123 char *name = refs->name;
1124 int namelen = strlen(name);
1126 if (!refname_match(pattern, name))
1127 continue;
1129 /* A match is "weak" if it is with refs outside
1130 * heads or tags, and did not specify the pattern
1131 * in full (e.g. "refs/remotes/origin/master") or at
1132 * least from the toplevel (e.g. "remotes/origin/master");
1133 * otherwise "git push $URL master" would result in
1134 * ambiguity between remotes/origin/master and heads/master
1135 * at the remote site.
1137 if (namelen != patlen &&
1138 patlen != namelen - 5 &&
1139 !starts_with(name, "refs/heads/") &&
1140 !starts_with(name, "refs/tags/")) {
1141 /* We want to catch the case where only weak
1142 * matches are found and there are multiple
1143 * matches, and where more than one strong
1144 * matches are found, as ambiguous. One
1145 * strong match with zero or more weak matches
1146 * are acceptable as a unique match.
1148 matched_weak = refs;
1149 weak_match++;
1151 else {
1152 matched = refs;
1153 match++;
1156 if (!matched) {
1157 if (matched_ref)
1158 *matched_ref = matched_weak;
1159 return weak_match;
1161 else {
1162 if (matched_ref)
1163 *matched_ref = matched;
1164 return match;
1168 static void tail_link_ref(struct ref *ref, struct ref ***tail)
1170 **tail = ref;
1171 while (ref->next)
1172 ref = ref->next;
1173 *tail = &ref->next;
1176 static struct ref *alloc_delete_ref(void)
1178 struct ref *ref = alloc_ref("(delete)");
1179 oidclr(&ref->new_oid, the_repository->hash_algo);
1180 return ref;
1183 static int try_explicit_object_name(const char *name,
1184 struct ref **match)
1186 struct object_id oid;
1188 if (!*name) {
1189 if (match)
1190 *match = alloc_delete_ref();
1191 return 0;
1194 if (repo_get_oid(the_repository, name, &oid))
1195 return -1;
1197 if (match) {
1198 *match = alloc_ref(name);
1199 oidcpy(&(*match)->new_oid, &oid);
1201 return 0;
1204 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
1206 struct ref *ret = alloc_ref(name);
1207 tail_link_ref(ret, tail);
1208 return ret;
1211 static char *guess_ref(const char *name, struct ref *peer)
1213 struct strbuf buf = STRBUF_INIT;
1215 const char *r = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1216 peer->name,
1217 RESOLVE_REF_READING,
1218 NULL, NULL);
1219 if (!r)
1220 return NULL;
1222 if (starts_with(r, "refs/heads/")) {
1223 strbuf_addstr(&buf, "refs/heads/");
1224 } else if (starts_with(r, "refs/tags/")) {
1225 strbuf_addstr(&buf, "refs/tags/");
1226 } else {
1227 return NULL;
1230 strbuf_addstr(&buf, name);
1231 return strbuf_detach(&buf, NULL);
1234 static int match_explicit_lhs(struct ref *src,
1235 struct refspec_item *rs,
1236 struct ref **match,
1237 int *allocated_match)
1239 switch (count_refspec_match(rs->src, src, match)) {
1240 case 1:
1241 if (allocated_match)
1242 *allocated_match = 0;
1243 return 0;
1244 case 0:
1245 /* The source could be in the get_sha1() format
1246 * not a reference name. :refs/other is a
1247 * way to delete 'other' ref at the remote end.
1249 if (try_explicit_object_name(rs->src, match) < 0)
1250 return error(_("src refspec %s does not match any"), rs->src);
1251 if (allocated_match)
1252 *allocated_match = 1;
1253 return 0;
1254 default:
1255 return error(_("src refspec %s matches more than one"), rs->src);
1259 static void show_push_unqualified_ref_name_error(const char *dst_value,
1260 const char *matched_src_name)
1262 struct object_id oid;
1263 enum object_type type;
1266 * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
1267 * <remote> <src>:<dst>" push, and "being pushed ('%s')" is
1268 * the <src>.
1270 error(_("The destination you provided is not a full refname (i.e.,\n"
1271 "starting with \"refs/\"). We tried to guess what you meant by:\n"
1272 "\n"
1273 "- Looking for a ref that matches '%s' on the remote side.\n"
1274 "- Checking if the <src> being pushed ('%s')\n"
1275 " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n"
1276 " refs/{heads,tags}/ prefix on the remote side.\n"
1277 "\n"
1278 "Neither worked, so we gave up. You must fully qualify the ref."),
1279 dst_value, matched_src_name);
1281 if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME))
1282 return;
1284 if (repo_get_oid(the_repository, matched_src_name, &oid))
1285 BUG("'%s' is not a valid object, "
1286 "match_explicit_lhs() should catch this!",
1287 matched_src_name);
1288 type = oid_object_info(the_repository, &oid, NULL);
1289 if (type == OBJ_COMMIT) {
1290 advise(_("The <src> part of the refspec is a commit object.\n"
1291 "Did you mean to create a new branch by pushing to\n"
1292 "'%s:refs/heads/%s'?"),
1293 matched_src_name, dst_value);
1294 } else if (type == OBJ_TAG) {
1295 advise(_("The <src> part of the refspec is a tag object.\n"
1296 "Did you mean to create a new tag by pushing to\n"
1297 "'%s:refs/tags/%s'?"),
1298 matched_src_name, dst_value);
1299 } else if (type == OBJ_TREE) {
1300 advise(_("The <src> part of the refspec is a tree object.\n"
1301 "Did you mean to tag a new tree by pushing to\n"
1302 "'%s:refs/tags/%s'?"),
1303 matched_src_name, dst_value);
1304 } else if (type == OBJ_BLOB) {
1305 advise(_("The <src> part of the refspec is a blob object.\n"
1306 "Did you mean to tag a new blob by pushing to\n"
1307 "'%s:refs/tags/%s'?"),
1308 matched_src_name, dst_value);
1309 } else {
1310 BUG("'%s' should be commit/tag/tree/blob, is '%d'",
1311 matched_src_name, type);
1315 static int match_explicit(struct ref *src, struct ref *dst,
1316 struct ref ***dst_tail,
1317 struct refspec_item *rs)
1319 struct ref *matched_src, *matched_dst;
1320 int allocated_src;
1322 const char *dst_value = rs->dst;
1323 char *dst_guess;
1325 if (rs->pattern || rs->matching || rs->negative)
1326 return 0;
1328 matched_src = matched_dst = NULL;
1329 if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
1330 return -1;
1332 if (!dst_value) {
1333 int flag;
1335 dst_value = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1336 matched_src->name,
1337 RESOLVE_REF_READING,
1338 NULL, &flag);
1339 if (!dst_value ||
1340 ((flag & REF_ISSYMREF) &&
1341 !starts_with(dst_value, "refs/heads/")))
1342 die(_("%s cannot be resolved to branch"),
1343 matched_src->name);
1346 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1347 case 1:
1348 break;
1349 case 0:
1350 if (starts_with(dst_value, "refs/")) {
1351 matched_dst = make_linked_ref(dst_value, dst_tail);
1352 } else if (is_null_oid(&matched_src->new_oid)) {
1353 error(_("unable to delete '%s': remote ref does not exist"),
1354 dst_value);
1355 } else if ((dst_guess = guess_ref(dst_value, matched_src))) {
1356 matched_dst = make_linked_ref(dst_guess, dst_tail);
1357 free(dst_guess);
1358 } else {
1359 show_push_unqualified_ref_name_error(dst_value,
1360 matched_src->name);
1362 break;
1363 default:
1364 matched_dst = NULL;
1365 error(_("dst refspec %s matches more than one"),
1366 dst_value);
1367 break;
1369 if (!matched_dst)
1370 return -1;
1371 if (matched_dst->peer_ref)
1372 return error(_("dst ref %s receives from more than one src"),
1373 matched_dst->name);
1374 else {
1375 matched_dst->peer_ref = allocated_src ?
1376 matched_src :
1377 copy_ref(matched_src);
1378 matched_dst->force = rs->force;
1380 return 0;
1383 static int match_explicit_refs(struct ref *src, struct ref *dst,
1384 struct ref ***dst_tail, struct refspec *rs)
1386 int i, errs;
1387 for (i = errs = 0; i < rs->nr; i++)
1388 errs += match_explicit(src, dst, dst_tail, &rs->items[i]);
1389 return errs;
1392 static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
1393 int send_mirror, int direction,
1394 const struct refspec_item **ret_pat)
1396 const struct refspec_item *pat;
1397 char *name;
1398 int i;
1399 int matching_refs = -1;
1400 for (i = 0; i < rs->nr; i++) {
1401 const struct refspec_item *item = &rs->items[i];
1403 if (item->negative)
1404 continue;
1406 if (item->matching &&
1407 (matching_refs == -1 || item->force)) {
1408 matching_refs = i;
1409 continue;
1412 if (item->pattern) {
1413 const char *dst_side = item->dst ? item->dst : item->src;
1414 int match;
1415 if (direction == FROM_SRC)
1416 match = match_name_with_pattern(item->src, ref->name, dst_side, &name);
1417 else
1418 match = match_name_with_pattern(dst_side, ref->name, item->src, &name);
1419 if (match) {
1420 matching_refs = i;
1421 break;
1425 if (matching_refs == -1)
1426 return NULL;
1428 pat = &rs->items[matching_refs];
1429 if (pat->matching) {
1431 * "matching refs"; traditionally we pushed everything
1432 * including refs outside refs/heads/ hierarchy, but
1433 * that does not make much sense these days.
1435 if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
1436 return NULL;
1437 name = xstrdup(ref->name);
1439 if (ret_pat)
1440 *ret_pat = pat;
1441 return name;
1444 static struct ref **tail_ref(struct ref **head)
1446 struct ref **tail = head;
1447 while (*tail)
1448 tail = &((*tail)->next);
1449 return tail;
1452 struct tips {
1453 struct commit **tip;
1454 int nr, alloc;
1457 static void add_to_tips(struct tips *tips, const struct object_id *oid)
1459 struct commit *commit;
1461 if (is_null_oid(oid))
1462 return;
1463 commit = lookup_commit_reference_gently(the_repository, oid, 1);
1464 if (!commit || (commit->object.flags & TMP_MARK))
1465 return;
1466 commit->object.flags |= TMP_MARK;
1467 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1468 tips->tip[tips->nr++] = commit;
1471 static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1473 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1474 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1475 struct string_list_item *item;
1476 struct ref *ref;
1477 struct tips sent_tips;
1480 * Collect everything we know they would have at the end of
1481 * this push, and collect all tags they have.
1483 memset(&sent_tips, 0, sizeof(sent_tips));
1484 for (ref = *dst; ref; ref = ref->next) {
1485 if (ref->peer_ref &&
1486 !is_null_oid(&ref->peer_ref->new_oid))
1487 add_to_tips(&sent_tips, &ref->peer_ref->new_oid);
1488 else
1489 add_to_tips(&sent_tips, &ref->old_oid);
1490 if (starts_with(ref->name, "refs/tags/"))
1491 string_list_append(&dst_tag, ref->name);
1493 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1495 string_list_sort(&dst_tag);
1497 /* Collect tags they do not have. */
1498 for (ref = src; ref; ref = ref->next) {
1499 if (!starts_with(ref->name, "refs/tags/"))
1500 continue; /* not a tag */
1501 if (string_list_has_string(&dst_tag, ref->name))
1502 continue; /* they already have it */
1503 if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG)
1504 continue; /* be conservative */
1505 item = string_list_append(&src_tag, ref->name);
1506 item->util = ref;
1508 string_list_clear(&dst_tag, 0);
1511 * At this point, src_tag lists tags that are missing from
1512 * dst, and sent_tips lists the tips we are pushing or those
1513 * that we know they already have. An element in the src_tag
1514 * that is an ancestor of any of the sent_tips needs to be
1515 * sent to the other side.
1517 if (sent_tips.nr) {
1518 const int reachable_flag = 1;
1519 struct commit_list *found_commits;
1520 struct commit **src_commits;
1521 int nr_src_commits = 0, alloc_src_commits = 16;
1522 ALLOC_ARRAY(src_commits, alloc_src_commits);
1524 for_each_string_list_item(item, &src_tag) {
1525 struct ref *ref = item->util;
1526 struct commit *commit;
1528 if (is_null_oid(&ref->new_oid))
1529 continue;
1530 commit = lookup_commit_reference_gently(the_repository,
1531 &ref->new_oid,
1533 if (!commit)
1534 /* not pushing a commit, which is not an error */
1535 continue;
1537 ALLOC_GROW(src_commits, nr_src_commits + 1, alloc_src_commits);
1538 src_commits[nr_src_commits++] = commit;
1541 found_commits = get_reachable_subset(sent_tips.tip, sent_tips.nr,
1542 src_commits, nr_src_commits,
1543 reachable_flag);
1545 for_each_string_list_item(item, &src_tag) {
1546 struct ref *dst_ref;
1547 struct ref *ref = item->util;
1548 struct commit *commit;
1550 if (is_null_oid(&ref->new_oid))
1551 continue;
1552 commit = lookup_commit_reference_gently(the_repository,
1553 &ref->new_oid,
1555 if (!commit)
1556 /* not pushing a commit, which is not an error */
1557 continue;
1560 * Is this tag, which they do not have, reachable from
1561 * any of the commits we are sending?
1563 if (!(commit->object.flags & reachable_flag))
1564 continue;
1566 /* Add it in */
1567 dst_ref = make_linked_ref(ref->name, dst_tail);
1568 oidcpy(&dst_ref->new_oid, &ref->new_oid);
1569 dst_ref->peer_ref = copy_ref(ref);
1572 clear_commit_marks_many(nr_src_commits, src_commits, reachable_flag);
1573 free(src_commits);
1574 free_commit_list(found_commits);
1577 string_list_clear(&src_tag, 0);
1578 free(sent_tips.tip);
1581 struct ref *find_ref_by_name(const struct ref *list, const char *name)
1583 for ( ; list; list = list->next)
1584 if (!strcmp(list->name, name))
1585 return (struct ref *)list;
1586 return NULL;
1589 static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
1591 for ( ; ref; ref = ref->next)
1592 string_list_append_nodup(ref_index, ref->name)->util = ref;
1594 string_list_sort(ref_index);
1598 * Given only the set of local refs, sanity-check the set of push
1599 * refspecs. We can't catch all errors that match_push_refs would,
1600 * but we can catch some errors early before even talking to the
1601 * remote side.
1603 int check_push_refs(struct ref *src, struct refspec *rs)
1605 int ret = 0;
1606 int i;
1608 for (i = 0; i < rs->nr; i++) {
1609 struct refspec_item *item = &rs->items[i];
1611 if (item->pattern || item->matching || item->negative)
1612 continue;
1614 ret |= match_explicit_lhs(src, item, NULL, NULL);
1617 return ret;
1621 * Given the set of refs the local repository has, the set of refs the
1622 * remote repository has, and the refspec used for push, determine
1623 * what remote refs we will update and with what value by setting
1624 * peer_ref (which object is being pushed) and force (if the push is
1625 * forced) in elements of "dst". The function may add new elements to
1626 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1628 int match_push_refs(struct ref *src, struct ref **dst,
1629 struct refspec *rs, int flags)
1631 int send_all = flags & MATCH_REFS_ALL;
1632 int send_mirror = flags & MATCH_REFS_MIRROR;
1633 int send_prune = flags & MATCH_REFS_PRUNE;
1634 int errs;
1635 struct ref *ref, **dst_tail = tail_ref(dst);
1636 struct string_list dst_ref_index = STRING_LIST_INIT_NODUP;
1638 /* If no refspec is provided, use the default ":" */
1639 if (!rs->nr)
1640 refspec_append(rs, ":");
1642 errs = match_explicit_refs(src, *dst, &dst_tail, rs);
1644 /* pick the remainder */
1645 for (ref = src; ref; ref = ref->next) {
1646 struct string_list_item *dst_item;
1647 struct ref *dst_peer;
1648 const struct refspec_item *pat = NULL;
1649 char *dst_name;
1651 dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat);
1652 if (!dst_name)
1653 continue;
1655 if (!dst_ref_index.nr)
1656 prepare_ref_index(&dst_ref_index, *dst);
1658 dst_item = string_list_lookup(&dst_ref_index, dst_name);
1659 dst_peer = dst_item ? dst_item->util : NULL;
1660 if (dst_peer) {
1661 if (dst_peer->peer_ref)
1662 /* We're already sending something to this ref. */
1663 goto free_name;
1664 } else {
1665 if (pat->matching && !(send_all || send_mirror))
1667 * Remote doesn't have it, and we have no
1668 * explicit pattern, and we don't have
1669 * --all or --mirror.
1671 goto free_name;
1673 /* Create a new one and link it */
1674 dst_peer = make_linked_ref(dst_name, &dst_tail);
1675 oidcpy(&dst_peer->new_oid, &ref->new_oid);
1676 string_list_insert(&dst_ref_index,
1677 dst_peer->name)->util = dst_peer;
1679 dst_peer->peer_ref = copy_ref(ref);
1680 dst_peer->force = pat->force;
1681 free_name:
1682 free(dst_name);
1685 string_list_clear(&dst_ref_index, 0);
1687 if (flags & MATCH_REFS_FOLLOW_TAGS)
1688 add_missing_tags(src, dst, &dst_tail);
1690 if (send_prune) {
1691 struct string_list src_ref_index = STRING_LIST_INIT_NODUP;
1692 /* check for missing refs on the remote */
1693 for (ref = *dst; ref; ref = ref->next) {
1694 char *src_name;
1696 if (ref->peer_ref)
1697 /* We're already sending something to this ref. */
1698 continue;
1700 src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL);
1701 if (src_name) {
1702 if (!src_ref_index.nr)
1703 prepare_ref_index(&src_ref_index, src);
1704 if (!string_list_has_string(&src_ref_index,
1705 src_name))
1706 ref->peer_ref = alloc_delete_ref();
1707 free(src_name);
1710 string_list_clear(&src_ref_index, 0);
1713 *dst = apply_negative_refspecs(*dst, rs);
1715 if (errs)
1716 return -1;
1717 return 0;
1720 void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1721 int force_update)
1723 struct ref *ref;
1725 for (ref = remote_refs; ref; ref = ref->next) {
1726 int force_ref_update = ref->force || force_update;
1727 int reject_reason = 0;
1729 if (ref->peer_ref)
1730 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
1731 else if (!send_mirror)
1732 continue;
1734 ref->deletion = is_null_oid(&ref->new_oid);
1735 if (!ref->deletion &&
1736 oideq(&ref->old_oid, &ref->new_oid)) {
1737 ref->status = REF_STATUS_UPTODATE;
1738 continue;
1742 * If the remote ref has moved and is now different
1743 * from what we expect, reject any push.
1745 * It also is an error if the user told us to check
1746 * with the remote-tracking branch to find the value
1747 * to expect, but we did not have such a tracking
1748 * branch.
1750 * If the tip of the remote-tracking ref is unreachable
1751 * from any reflog entry of its local ref indicating a
1752 * possible update since checkout; reject the push.
1754 if (ref->expect_old_sha1) {
1755 if (!oideq(&ref->old_oid, &ref->old_oid_expect))
1756 reject_reason = REF_STATUS_REJECT_STALE;
1757 else if (ref->check_reachable && ref->unreachable)
1758 reject_reason =
1759 REF_STATUS_REJECT_REMOTE_UPDATED;
1760 else
1762 * If the ref isn't stale, and is reachable
1763 * from one of the reflog entries of
1764 * the local branch, force the update.
1766 force_ref_update = 1;
1770 * If the update isn't already rejected then check
1771 * the usual "must fast-forward" rules.
1773 * Decide whether an individual refspec A:B can be
1774 * pushed. The push will succeed if any of the
1775 * following are true:
1777 * (1) the remote reference B does not exist
1779 * (2) the remote reference B is being removed (i.e.,
1780 * pushing :B where no source is specified)
1782 * (3) the destination is not under refs/tags/, and
1783 * if the old and new value is a commit, the new
1784 * is a descendant of the old.
1786 * (4) it is forced using the +A:B notation, or by
1787 * passing the --force argument
1790 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
1791 if (starts_with(ref->name, "refs/tags/"))
1792 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
1793 else if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
1794 reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
1795 else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) ||
1796 !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1))
1797 reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
1798 else if (!ref_newer(&ref->new_oid, &ref->old_oid))
1799 reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
1803 * "--force" will defeat any rejection implemented
1804 * by the rules above.
1806 if (!force_ref_update)
1807 ref->status = reject_reason;
1808 else if (reject_reason)
1809 ref->forced_update = 1;
1813 static void set_merge(struct remote_state *remote_state, struct branch *ret)
1815 struct remote *remote;
1816 char *ref;
1817 struct object_id oid;
1818 int i;
1820 if (!ret)
1821 return; /* no branch */
1822 if (ret->merge)
1823 return; /* already run */
1824 if (!ret->remote_name || !ret->merge_nr) {
1826 * no merge config; let's make sure we don't confuse callers
1827 * with a non-zero merge_nr but a NULL merge
1829 ret->merge_nr = 0;
1830 return;
1833 remote = remotes_remote_get(remote_state, ret->remote_name);
1835 CALLOC_ARRAY(ret->merge, ret->merge_nr);
1836 for (i = 0; i < ret->merge_nr; i++) {
1837 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1838 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
1839 if (!remote_find_tracking(remote, ret->merge[i]) ||
1840 strcmp(ret->remote_name, "."))
1841 continue;
1842 if (repo_dwim_ref(the_repository, ret->merge_name[i],
1843 strlen(ret->merge_name[i]), &oid, &ref,
1844 0) == 1)
1845 ret->merge[i]->dst = ref;
1846 else
1847 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1851 struct branch *branch_get(const char *name)
1853 struct branch *ret;
1855 read_config(the_repository, 0);
1856 if (!name || !*name || !strcmp(name, "HEAD"))
1857 ret = the_repository->remote_state->current_branch;
1858 else
1859 ret = make_branch(the_repository->remote_state, name,
1860 strlen(name));
1861 set_merge(the_repository->remote_state, ret);
1862 return ret;
1865 int branch_has_merge_config(struct branch *branch)
1867 return branch && !!branch->merge;
1870 int branch_merge_matches(struct branch *branch,
1871 int i,
1872 const char *refname)
1874 if (!branch || i < 0 || i >= branch->merge_nr)
1875 return 0;
1876 return refname_match(branch->merge[i]->src, refname);
1879 __attribute__((format (printf,2,3)))
1880 static const char *error_buf(struct strbuf *err, const char *fmt, ...)
1882 if (err) {
1883 va_list ap;
1884 va_start(ap, fmt);
1885 strbuf_vaddf(err, fmt, ap);
1886 va_end(ap);
1888 return NULL;
1891 const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
1893 if (!branch)
1894 return error_buf(err, _("HEAD does not point to a branch"));
1896 if (!branch->merge || !branch->merge[0]) {
1898 * no merge config; is it because the user didn't define any,
1899 * or because it is not a real branch, and get_branch
1900 * auto-vivified it?
1902 if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname))
1903 return error_buf(err, _("no such branch: '%s'"),
1904 branch->name);
1905 return error_buf(err,
1906 _("no upstream configured for branch '%s'"),
1907 branch->name);
1910 if (!branch->merge[0]->dst)
1911 return error_buf(err,
1912 _("upstream branch '%s' not stored as a remote-tracking branch"),
1913 branch->merge[0]->src);
1915 return branch->merge[0]->dst;
1918 static const char *tracking_for_push_dest(struct remote *remote,
1919 const char *refname,
1920 struct strbuf *err)
1922 char *ret;
1924 ret = apply_refspecs(&remote->fetch, refname);
1925 if (!ret)
1926 return error_buf(err,
1927 _("push destination '%s' on remote '%s' has no local tracking branch"),
1928 refname, remote->name);
1929 return ret;
1932 static const char *branch_get_push_1(struct remote_state *remote_state,
1933 struct branch *branch, struct strbuf *err)
1935 struct remote *remote;
1937 remote = remotes_remote_get(
1938 remote_state,
1939 remotes_pushremote_for_branch(remote_state, branch, NULL));
1940 if (!remote)
1941 return error_buf(err,
1942 _("branch '%s' has no remote for pushing"),
1943 branch->name);
1945 if (remote->push.nr) {
1946 char *dst;
1947 const char *ret;
1949 dst = apply_refspecs(&remote->push, branch->refname);
1950 if (!dst)
1951 return error_buf(err,
1952 _("push refspecs for '%s' do not include '%s'"),
1953 remote->name, branch->name);
1955 ret = tracking_for_push_dest(remote, dst, err);
1956 free(dst);
1957 return ret;
1960 if (remote->mirror)
1961 return tracking_for_push_dest(remote, branch->refname, err);
1963 switch (push_default) {
1964 case PUSH_DEFAULT_NOTHING:
1965 return error_buf(err, _("push has no destination (push.default is 'nothing')"));
1967 case PUSH_DEFAULT_MATCHING:
1968 case PUSH_DEFAULT_CURRENT:
1969 return tracking_for_push_dest(remote, branch->refname, err);
1971 case PUSH_DEFAULT_UPSTREAM:
1972 return branch_get_upstream(branch, err);
1974 case PUSH_DEFAULT_UNSPECIFIED:
1975 case PUSH_DEFAULT_SIMPLE:
1977 const char *up, *cur;
1979 up = branch_get_upstream(branch, err);
1980 if (!up)
1981 return NULL;
1982 cur = tracking_for_push_dest(remote, branch->refname, err);
1983 if (!cur)
1984 return NULL;
1985 if (strcmp(cur, up))
1986 return error_buf(err,
1987 _("cannot resolve 'simple' push to a single destination"));
1988 return cur;
1992 BUG("unhandled push situation");
1995 const char *branch_get_push(struct branch *branch, struct strbuf *err)
1997 read_config(the_repository, 0);
1998 die_on_missing_branch(the_repository, branch);
2000 if (!branch)
2001 return error_buf(err, _("HEAD does not point to a branch"));
2003 if (!branch->push_tracking_ref)
2004 branch->push_tracking_ref = branch_get_push_1(
2005 the_repository->remote_state, branch, err);
2006 return branch->push_tracking_ref;
2009 static int ignore_symref_update(const char *refname, struct strbuf *scratch)
2011 return !refs_read_symbolic_ref(get_main_ref_store(the_repository), refname, scratch);
2015 * Create and return a list of (struct ref) consisting of copies of
2016 * each remote_ref that matches refspec. refspec must be a pattern.
2017 * Fill in the copies' peer_ref to describe the local tracking refs to
2018 * which they map. Omit any references that would map to an existing
2019 * local symbolic ref.
2021 static struct ref *get_expanded_map(const struct ref *remote_refs,
2022 const struct refspec_item *refspec)
2024 struct strbuf scratch = STRBUF_INIT;
2025 const struct ref *ref;
2026 struct ref *ret = NULL;
2027 struct ref **tail = &ret;
2029 for (ref = remote_refs; ref; ref = ref->next) {
2030 char *expn_name = NULL;
2032 strbuf_reset(&scratch);
2034 if (strchr(ref->name, '^'))
2035 continue; /* a dereference item */
2036 if (match_name_with_pattern(refspec->src, ref->name,
2037 refspec->dst, &expn_name) &&
2038 !ignore_symref_update(expn_name, &scratch)) {
2039 struct ref *cpy = copy_ref(ref);
2041 cpy->peer_ref = alloc_ref(expn_name);
2042 if (refspec->force)
2043 cpy->peer_ref->force = 1;
2044 *tail = cpy;
2045 tail = &cpy->next;
2047 free(expn_name);
2050 strbuf_release(&scratch);
2051 return ret;
2054 static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
2056 const struct ref *ref;
2057 const struct ref *best_match = NULL;
2058 int best_score = 0;
2060 for (ref = refs; ref; ref = ref->next) {
2061 int score = refname_match(name, ref->name);
2063 if (best_score < score) {
2064 best_match = ref;
2065 best_score = score;
2068 return best_match;
2071 struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
2073 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
2075 if (!ref)
2076 return NULL;
2078 return copy_ref(ref);
2081 static struct ref *get_local_ref(const char *name)
2083 if (!name || name[0] == '\0')
2084 return NULL;
2086 if (starts_with(name, "refs/"))
2087 return alloc_ref(name);
2089 if (starts_with(name, "heads/") ||
2090 starts_with(name, "tags/") ||
2091 starts_with(name, "remotes/"))
2092 return alloc_ref_with_prefix("refs/", 5, name);
2094 return alloc_ref_with_prefix("refs/heads/", 11, name);
2097 int get_fetch_map(const struct ref *remote_refs,
2098 const struct refspec_item *refspec,
2099 struct ref ***tail,
2100 int missing_ok)
2102 struct ref *ref_map, **rmp;
2104 if (refspec->negative)
2105 return 0;
2107 if (refspec->pattern) {
2108 ref_map = get_expanded_map(remote_refs, refspec);
2109 } else {
2110 const char *name = refspec->src[0] ? refspec->src : "HEAD";
2112 if (refspec->exact_sha1) {
2113 ref_map = alloc_ref(name);
2114 get_oid_hex(name, &ref_map->old_oid);
2115 ref_map->exact_oid = 1;
2116 } else {
2117 ref_map = get_remote_ref(remote_refs, name);
2119 if (!missing_ok && !ref_map)
2120 die(_("couldn't find remote ref %s"), name);
2121 if (ref_map) {
2122 ref_map->peer_ref = get_local_ref(refspec->dst);
2123 if (ref_map->peer_ref && refspec->force)
2124 ref_map->peer_ref->force = 1;
2128 for (rmp = &ref_map; *rmp; ) {
2129 if ((*rmp)->peer_ref) {
2130 if (!starts_with((*rmp)->peer_ref->name, "refs/") ||
2131 check_refname_format((*rmp)->peer_ref->name, 0)) {
2132 struct ref *ignore = *rmp;
2133 error(_("* Ignoring funny ref '%s' locally"),
2134 (*rmp)->peer_ref->name);
2135 *rmp = (*rmp)->next;
2136 free(ignore->peer_ref);
2137 free(ignore);
2138 continue;
2141 rmp = &((*rmp)->next);
2144 if (ref_map)
2145 tail_link_ref(ref_map, tail);
2147 return 0;
2150 int resolve_remote_symref(struct ref *ref, struct ref *list)
2152 if (!ref->symref)
2153 return 0;
2154 for (; list; list = list->next)
2155 if (!strcmp(ref->symref, list->name)) {
2156 oidcpy(&ref->old_oid, &list->old_oid);
2157 return 0;
2159 return 1;
2163 * Compute the commit ahead/behind values for the pair branch_name, base.
2165 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2166 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2167 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2168 * set to zero).
2170 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
2171 * does not exist). Returns 0 if the commits are identical. Returns 1 if
2172 * commits are different.
2175 static int stat_branch_pair(const char *branch_name, const char *base,
2176 int *num_ours, int *num_theirs,
2177 enum ahead_behind_flags abf)
2179 struct object_id oid;
2180 struct commit *ours, *theirs;
2181 struct rev_info revs;
2182 struct setup_revision_opt opt = {
2183 .free_removed_argv_elements = 1,
2185 struct strvec argv = STRVEC_INIT;
2187 /* Cannot stat if what we used to build on no longer exists */
2188 if (refs_read_ref(get_main_ref_store(the_repository), base, &oid))
2189 return -1;
2190 theirs = lookup_commit_reference(the_repository, &oid);
2191 if (!theirs)
2192 return -1;
2194 if (refs_read_ref(get_main_ref_store(the_repository), branch_name, &oid))
2195 return -1;
2196 ours = lookup_commit_reference(the_repository, &oid);
2197 if (!ours)
2198 return -1;
2200 *num_theirs = *num_ours = 0;
2202 /* are we the same? */
2203 if (theirs == ours)
2204 return 0;
2205 if (abf == AHEAD_BEHIND_QUICK)
2206 return 1;
2207 if (abf != AHEAD_BEHIND_FULL)
2208 BUG("stat_branch_pair: invalid abf '%d'", abf);
2210 /* Run "rev-list --left-right ours...theirs" internally... */
2211 strvec_push(&argv, ""); /* ignored */
2212 strvec_push(&argv, "--left-right");
2213 strvec_pushf(&argv, "%s...%s",
2214 oid_to_hex(&ours->object.oid),
2215 oid_to_hex(&theirs->object.oid));
2216 strvec_push(&argv, "--");
2218 repo_init_revisions(the_repository, &revs, NULL);
2219 setup_revisions(argv.nr, argv.v, &revs, &opt);
2220 if (prepare_revision_walk(&revs))
2221 die(_("revision walk setup failed"));
2223 /* ... and count the commits on each side. */
2224 while (1) {
2225 struct commit *c = get_revision(&revs);
2226 if (!c)
2227 break;
2228 if (c->object.flags & SYMMETRIC_LEFT)
2229 (*num_ours)++;
2230 else
2231 (*num_theirs)++;
2234 /* clear object flags smudged by the above traversal */
2235 clear_commit_marks(ours, ALL_REV_FLAGS);
2236 clear_commit_marks(theirs, ALL_REV_FLAGS);
2238 strvec_clear(&argv);
2239 release_revisions(&revs);
2240 return 1;
2244 * Lookup the tracking branch for the given branch and if present, optionally
2245 * compute the commit ahead/behind values for the pair.
2247 * If for_push is true, the tracking branch refers to the push branch,
2248 * otherwise it refers to the upstream branch.
2250 * The name of the tracking branch (or NULL if it is not defined) is
2251 * returned via *tracking_name, if it is not itself NULL.
2253 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2254 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2255 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2256 * set to zero).
2258 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
2259 * upstream defined, or ref does not exist). Returns 0 if the commits are
2260 * identical. Returns 1 if commits are different.
2262 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
2263 const char **tracking_name, int for_push,
2264 enum ahead_behind_flags abf)
2266 const char *base;
2268 /* Cannot stat unless we are marked to build on top of somebody else. */
2269 base = for_push ? branch_get_push(branch, NULL) :
2270 branch_get_upstream(branch, NULL);
2271 if (tracking_name)
2272 *tracking_name = base;
2273 if (!base)
2274 return -1;
2276 return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
2280 * Return true when there is anything to report, otherwise false.
2282 int format_tracking_info(struct branch *branch, struct strbuf *sb,
2283 enum ahead_behind_flags abf,
2284 int show_divergence_advice)
2286 int ours, theirs, sti;
2287 const char *full_base;
2288 char *base;
2289 int upstream_is_gone = 0;
2291 sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
2292 if (sti < 0) {
2293 if (!full_base)
2294 return 0;
2295 upstream_is_gone = 1;
2298 base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2299 full_base, 0);
2300 if (upstream_is_gone) {
2301 strbuf_addf(sb,
2302 _("Your branch is based on '%s', but the upstream is gone.\n"),
2303 base);
2304 if (advice_enabled(ADVICE_STATUS_HINTS))
2305 strbuf_addstr(sb,
2306 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2307 } else if (!sti) {
2308 strbuf_addf(sb,
2309 _("Your branch is up to date with '%s'.\n"),
2310 base);
2311 } else if (abf == AHEAD_BEHIND_QUICK) {
2312 strbuf_addf(sb,
2313 _("Your branch and '%s' refer to different commits.\n"),
2314 base);
2315 if (advice_enabled(ADVICE_STATUS_HINTS))
2316 strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
2317 "git status --ahead-behind");
2318 } else if (!theirs) {
2319 strbuf_addf(sb,
2320 Q_("Your branch is ahead of '%s' by %d commit.\n",
2321 "Your branch is ahead of '%s' by %d commits.\n",
2322 ours),
2323 base, ours);
2324 if (advice_enabled(ADVICE_STATUS_HINTS))
2325 strbuf_addstr(sb,
2326 _(" (use \"git push\" to publish your local commits)\n"));
2327 } else if (!ours) {
2328 strbuf_addf(sb,
2329 Q_("Your branch is behind '%s' by %d commit, "
2330 "and can be fast-forwarded.\n",
2331 "Your branch is behind '%s' by %d commits, "
2332 "and can be fast-forwarded.\n",
2333 theirs),
2334 base, theirs);
2335 if (advice_enabled(ADVICE_STATUS_HINTS))
2336 strbuf_addstr(sb,
2337 _(" (use \"git pull\" to update your local branch)\n"));
2338 } else {
2339 strbuf_addf(sb,
2340 Q_("Your branch and '%s' have diverged,\n"
2341 "and have %d and %d different commit each, "
2342 "respectively.\n",
2343 "Your branch and '%s' have diverged,\n"
2344 "and have %d and %d different commits each, "
2345 "respectively.\n",
2346 ours + theirs),
2347 base, ours, theirs);
2348 if (show_divergence_advice &&
2349 advice_enabled(ADVICE_STATUS_HINTS))
2350 strbuf_addstr(sb,
2351 _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
2353 free(base);
2354 return 1;
2357 static int one_local_ref(const char *refname, const struct object_id *oid,
2358 int flag UNUSED,
2359 void *cb_data)
2361 struct ref ***local_tail = cb_data;
2362 struct ref *ref;
2364 /* we already know it starts with refs/ to get here */
2365 if (check_refname_format(refname + 5, 0))
2366 return 0;
2368 ref = alloc_ref(refname);
2369 oidcpy(&ref->new_oid, oid);
2370 **local_tail = ref;
2371 *local_tail = &ref->next;
2372 return 0;
2375 struct ref *get_local_heads(void)
2377 struct ref *local_refs = NULL, **local_tail = &local_refs;
2379 refs_for_each_ref(get_main_ref_store(the_repository), one_local_ref,
2380 &local_tail);
2381 return local_refs;
2384 struct ref *guess_remote_head(const struct ref *head,
2385 const struct ref *refs,
2386 int all)
2388 const struct ref *r;
2389 struct ref *list = NULL;
2390 struct ref **tail = &list;
2392 if (!head)
2393 return NULL;
2396 * Some transports support directly peeking at
2397 * where HEAD points; if that is the case, then
2398 * we don't have to guess.
2400 if (head->symref)
2401 return copy_ref(find_ref_by_name(refs, head->symref));
2403 /* If a remote branch exists with the default branch name, let's use it. */
2404 if (!all) {
2405 char *default_branch = repo_default_branch_name(the_repository, 0);
2406 char *ref = xstrfmt("refs/heads/%s", default_branch);
2408 r = find_ref_by_name(refs, ref);
2409 free(ref);
2410 free(default_branch);
2412 if (r && oideq(&r->old_oid, &head->old_oid))
2413 return copy_ref(r);
2415 /* Fall back to the hard-coded historical default */
2416 r = find_ref_by_name(refs, "refs/heads/master");
2417 if (r && oideq(&r->old_oid, &head->old_oid))
2418 return copy_ref(r);
2421 /* Look for another ref that points there */
2422 for (r = refs; r; r = r->next) {
2423 if (r != head &&
2424 starts_with(r->name, "refs/heads/") &&
2425 oideq(&r->old_oid, &head->old_oid)) {
2426 *tail = copy_ref(r);
2427 tail = &((*tail)->next);
2428 if (!all)
2429 break;
2433 return list;
2436 struct stale_heads_info {
2437 struct string_list *ref_names;
2438 struct ref **stale_refs_tail;
2439 struct refspec *rs;
2442 static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
2443 int flags, void *cb_data)
2445 struct stale_heads_info *info = cb_data;
2446 struct string_list matches = STRING_LIST_INIT_DUP;
2447 struct refspec_item query;
2448 int i, stale = 1;
2449 memset(&query, 0, sizeof(struct refspec_item));
2450 query.dst = (char *)refname;
2452 query_refspecs_multiple(info->rs, &query, &matches);
2453 if (matches.nr == 0)
2454 goto clean_exit; /* No matches */
2457 * If we did find a suitable refspec and it's not a symref and
2458 * it's not in the list of refs that currently exist in that
2459 * remote, we consider it to be stale. In order to deal with
2460 * overlapping refspecs, we need to go over all of the
2461 * matching refs.
2463 if (flags & REF_ISSYMREF)
2464 goto clean_exit;
2466 for (i = 0; stale && i < matches.nr; i++)
2467 if (string_list_has_string(info->ref_names, matches.items[i].string))
2468 stale = 0;
2470 if (stale) {
2471 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
2472 oidcpy(&ref->new_oid, oid);
2475 clean_exit:
2476 string_list_clear(&matches, 0);
2477 return 0;
2480 struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map)
2482 struct ref *ref, *stale_refs = NULL;
2483 struct string_list ref_names = STRING_LIST_INIT_NODUP;
2484 struct stale_heads_info info;
2486 info.ref_names = &ref_names;
2487 info.stale_refs_tail = &stale_refs;
2488 info.rs = rs;
2489 for (ref = fetch_map; ref; ref = ref->next)
2490 string_list_append(&ref_names, ref->name);
2491 string_list_sort(&ref_names);
2492 refs_for_each_ref(get_main_ref_store(the_repository),
2493 get_stale_heads_cb, &info);
2494 string_list_clear(&ref_names, 0);
2495 return stale_refs;
2499 * Compare-and-swap
2501 static void clear_cas_option(struct push_cas_option *cas)
2503 int i;
2505 for (i = 0; i < cas->nr; i++)
2506 free(cas->entry[i].refname);
2507 free(cas->entry);
2508 memset(cas, 0, sizeof(*cas));
2511 static struct push_cas *add_cas_entry(struct push_cas_option *cas,
2512 const char *refname,
2513 size_t refnamelen)
2515 struct push_cas *entry;
2516 ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
2517 entry = &cas->entry[cas->nr++];
2518 memset(entry, 0, sizeof(*entry));
2519 entry->refname = xmemdupz(refname, refnamelen);
2520 return entry;
2523 static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
2525 const char *colon;
2526 struct push_cas *entry;
2528 if (unset) {
2529 /* "--no-<option>" */
2530 clear_cas_option(cas);
2531 return 0;
2534 if (!arg) {
2535 /* just "--<option>" */
2536 cas->use_tracking_for_rest = 1;
2537 return 0;
2540 /* "--<option>=refname" or "--<option>=refname:value" */
2541 colon = strchrnul(arg, ':');
2542 entry = add_cas_entry(cas, arg, colon - arg);
2543 if (!*colon)
2544 entry->use_tracking = 1;
2545 else if (!colon[1])
2546 oidclr(&entry->expect, the_repository->hash_algo);
2547 else if (repo_get_oid(the_repository, colon + 1, &entry->expect))
2548 return error(_("cannot parse expected object name '%s'"),
2549 colon + 1);
2550 return 0;
2553 int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
2555 return parse_push_cas_option(opt->value, arg, unset);
2558 int is_empty_cas(const struct push_cas_option *cas)
2560 return !cas->use_tracking_for_rest && !cas->nr;
2564 * Look at remote.fetch refspec and see if we have a remote
2565 * tracking branch for the refname there. Fill the name of
2566 * the remote-tracking branch in *dst_refname, and the name
2567 * of the commit object at its tip in oid[].
2568 * If we cannot do so, return negative to signal an error.
2570 static int remote_tracking(struct remote *remote, const char *refname,
2571 struct object_id *oid, char **dst_refname)
2573 char *dst;
2575 dst = apply_refspecs(&remote->fetch, refname);
2576 if (!dst)
2577 return -1; /* no tracking ref for refname at remote */
2578 if (refs_read_ref(get_main_ref_store(the_repository), dst, oid))
2579 return -1; /* we know what the tracking ref is but we cannot read it */
2581 *dst_refname = dst;
2582 return 0;
2586 * The struct "reflog_commit_array" and related helper functions
2587 * are used for collecting commits into an array during reflog
2588 * traversals in "check_and_collect_until()".
2590 struct reflog_commit_array {
2591 struct commit **item;
2592 size_t nr, alloc;
2595 #define REFLOG_COMMIT_ARRAY_INIT { 0 }
2597 /* Append a commit to the array. */
2598 static void append_commit(struct reflog_commit_array *arr,
2599 struct commit *commit)
2601 ALLOC_GROW(arr->item, arr->nr + 1, arr->alloc);
2602 arr->item[arr->nr++] = commit;
2605 /* Free and reset the array. */
2606 static void free_commit_array(struct reflog_commit_array *arr)
2608 FREE_AND_NULL(arr->item);
2609 arr->nr = arr->alloc = 0;
2612 struct check_and_collect_until_cb_data {
2613 struct commit *remote_commit;
2614 struct reflog_commit_array *local_commits;
2615 timestamp_t remote_reflog_timestamp;
2618 /* Get the timestamp of the latest entry. */
2619 static int peek_reflog(struct object_id *o_oid UNUSED,
2620 struct object_id *n_oid UNUSED,
2621 const char *ident UNUSED,
2622 timestamp_t timestamp, int tz UNUSED,
2623 const char *message UNUSED, void *cb_data)
2625 timestamp_t *ts = cb_data;
2626 *ts = timestamp;
2627 return 1;
2630 static int check_and_collect_until(struct object_id *o_oid UNUSED,
2631 struct object_id *n_oid,
2632 const char *ident UNUSED,
2633 timestamp_t timestamp, int tz UNUSED,
2634 const char *message UNUSED, void *cb_data)
2636 struct commit *commit;
2637 struct check_and_collect_until_cb_data *cb = cb_data;
2639 /* An entry was found. */
2640 if (oideq(n_oid, &cb->remote_commit->object.oid))
2641 return 1;
2643 if ((commit = lookup_commit_reference(the_repository, n_oid)))
2644 append_commit(cb->local_commits, commit);
2647 * If the reflog entry timestamp is older than the remote ref's
2648 * latest reflog entry, there is no need to check or collect
2649 * entries older than this one.
2651 if (timestamp < cb->remote_reflog_timestamp)
2652 return -1;
2654 return 0;
2657 #define MERGE_BASES_BATCH_SIZE 8
2660 * Iterate through the reflog of the local ref to check if there is an entry
2661 * for the given remote-tracking ref; runs until the timestamp of an entry is
2662 * older than latest timestamp of remote-tracking ref's reflog. Any commits
2663 * are that seen along the way are collected into an array to check if the
2664 * remote-tracking ref is reachable from any of them.
2666 static int is_reachable_in_reflog(const char *local, const struct ref *remote)
2668 timestamp_t date;
2669 struct commit *commit;
2670 struct commit **chunk;
2671 struct check_and_collect_until_cb_data cb;
2672 struct reflog_commit_array arr = REFLOG_COMMIT_ARRAY_INIT;
2673 size_t size = 0;
2674 int ret = 0;
2676 commit = lookup_commit_reference(the_repository, &remote->old_oid);
2677 if (!commit)
2678 goto cleanup_return;
2681 * Get the timestamp from the latest entry
2682 * of the remote-tracking ref's reflog.
2684 refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository),
2685 remote->tracking_ref, peek_reflog,
2686 &date);
2688 cb.remote_commit = commit;
2689 cb.local_commits = &arr;
2690 cb.remote_reflog_timestamp = date;
2691 ret = refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository),
2692 local, check_and_collect_until,
2693 &cb);
2695 /* We found an entry in the reflog. */
2696 if (ret > 0)
2697 goto cleanup_return;
2700 * Check if the remote commit is reachable from any
2701 * of the commits in the collected array, in batches.
2703 for (chunk = arr.item; chunk < arr.item + arr.nr; chunk += size) {
2704 size = arr.item + arr.nr - chunk;
2705 if (MERGE_BASES_BATCH_SIZE < size)
2706 size = MERGE_BASES_BATCH_SIZE;
2708 if ((ret = repo_in_merge_bases_many(the_repository, commit, size, chunk, 0)))
2709 break;
2712 cleanup_return:
2713 free_commit_array(&arr);
2714 return ret;
2718 * Check for reachability of a remote-tracking
2719 * ref in the reflog entries of its local ref.
2721 static void check_if_includes_upstream(struct ref *remote)
2723 struct ref *local = get_local_ref(remote->name);
2724 if (!local)
2725 return;
2727 if (is_reachable_in_reflog(local->name, remote) <= 0)
2728 remote->unreachable = 1;
2731 static void apply_cas(struct push_cas_option *cas,
2732 struct remote *remote,
2733 struct ref *ref)
2735 int i;
2737 /* Find an explicit --<option>=<name>[:<value>] entry */
2738 for (i = 0; i < cas->nr; i++) {
2739 struct push_cas *entry = &cas->entry[i];
2740 if (!refname_match(entry->refname, ref->name))
2741 continue;
2742 ref->expect_old_sha1 = 1;
2743 if (!entry->use_tracking)
2744 oidcpy(&ref->old_oid_expect, &entry->expect);
2745 else if (remote_tracking(remote, ref->name,
2746 &ref->old_oid_expect,
2747 &ref->tracking_ref))
2748 oidclr(&ref->old_oid_expect, the_repository->hash_algo);
2749 else
2750 ref->check_reachable = cas->use_force_if_includes;
2751 return;
2754 /* Are we using "--<option>" to cover all? */
2755 if (!cas->use_tracking_for_rest)
2756 return;
2758 ref->expect_old_sha1 = 1;
2759 if (remote_tracking(remote, ref->name,
2760 &ref->old_oid_expect,
2761 &ref->tracking_ref))
2762 oidclr(&ref->old_oid_expect, the_repository->hash_algo);
2763 else
2764 ref->check_reachable = cas->use_force_if_includes;
2767 void apply_push_cas(struct push_cas_option *cas,
2768 struct remote *remote,
2769 struct ref *remote_refs)
2771 struct ref *ref;
2772 for (ref = remote_refs; ref; ref = ref->next) {
2773 apply_cas(cas, remote, ref);
2776 * If "compare-and-swap" is in "use_tracking[_for_rest]"
2777 * mode, and if "--force-if-includes" was specified, run
2778 * the check.
2780 if (ref->check_reachable)
2781 check_if_includes_upstream(ref);
2785 struct remote_state *remote_state_new(void)
2787 struct remote_state *r = xmalloc(sizeof(*r));
2789 memset(r, 0, sizeof(*r));
2791 hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);
2792 hashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);
2793 return r;
2796 void remote_state_clear(struct remote_state *remote_state)
2798 int i;
2800 for (i = 0; i < remote_state->remotes_nr; i++)
2801 remote_clear(remote_state->remotes[i]);
2802 FREE_AND_NULL(remote_state->remotes);
2803 remote_state->remotes_alloc = 0;
2804 remote_state->remotes_nr = 0;
2806 hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent);
2807 hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent);
2811 * Returns 1 if it was the last chop before ':'.
2813 static int chop_last_dir(char **remoteurl, int is_relative)
2815 char *rfind = find_last_dir_sep(*remoteurl);
2816 if (rfind) {
2817 *rfind = '\0';
2818 return 0;
2821 rfind = strrchr(*remoteurl, ':');
2822 if (rfind) {
2823 *rfind = '\0';
2824 return 1;
2827 if (is_relative || !strcmp(".", *remoteurl))
2828 die(_("cannot strip one component off url '%s'"),
2829 *remoteurl);
2831 free(*remoteurl);
2832 *remoteurl = xstrdup(".");
2833 return 0;
2836 char *relative_url(const char *remote_url, const char *url,
2837 const char *up_path)
2839 int is_relative = 0;
2840 int colonsep = 0;
2841 char *out;
2842 char *remoteurl;
2843 struct strbuf sb = STRBUF_INIT;
2844 size_t len;
2846 if (!url_is_local_not_ssh(url) || is_absolute_path(url))
2847 return xstrdup(url);
2849 len = strlen(remote_url);
2850 if (!len)
2851 BUG("invalid empty remote_url");
2853 remoteurl = xstrdup(remote_url);
2854 if (is_dir_sep(remoteurl[len-1]))
2855 remoteurl[len-1] = '\0';
2857 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
2858 is_relative = 0;
2859 else {
2860 is_relative = 1;
2862 * Prepend a './' to ensure all relative
2863 * remoteurls start with './' or '../'
2865 if (!starts_with_dot_slash_native(remoteurl) &&
2866 !starts_with_dot_dot_slash_native(remoteurl)) {
2867 strbuf_reset(&sb);
2868 strbuf_addf(&sb, "./%s", remoteurl);
2869 free(remoteurl);
2870 remoteurl = strbuf_detach(&sb, NULL);
2874 * When the url starts with '../', remove that and the
2875 * last directory in remoteurl.
2877 while (*url) {
2878 if (starts_with_dot_dot_slash_native(url)) {
2879 url += 3;
2880 colonsep |= chop_last_dir(&remoteurl, is_relative);
2881 } else if (starts_with_dot_slash_native(url))
2882 url += 2;
2883 else
2884 break;
2886 strbuf_reset(&sb);
2887 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
2888 if (ends_with(url, "/"))
2889 strbuf_setlen(&sb, sb.len - 1);
2890 free(remoteurl);
2892 if (starts_with_dot_slash_native(sb.buf))
2893 out = xstrdup(sb.buf + 2);
2894 else
2895 out = xstrdup(sb.buf);
2897 if (!up_path || !is_relative) {
2898 strbuf_release(&sb);
2899 return out;
2902 strbuf_reset(&sb);
2903 strbuf_addf(&sb, "%s%s", up_path, out);
2904 free(out);
2905 return strbuf_detach(&sb, NULL);