6 #include "object-store.h"
12 #include "string-list.h"
13 #include "mergesort.h"
15 #include "commit-reach.h"
18 enum map_direction
{ FROM_SRC
, FROM_DST
};
20 struct counted_string
{
25 static int valid_remote(const struct remote
*remote
)
27 return (!!remote
->url
) || (!!remote
->foreign_vcs
);
30 static const char *alias_url(const char *url
, struct rewrites
*r
)
33 struct counted_string
*longest
;
38 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
41 for (j
= 0; j
< r
->rewrite
[i
]->instead_of_nr
; j
++) {
42 if (starts_with(url
, r
->rewrite
[i
]->instead_of
[j
].s
) &&
44 longest
->len
< r
->rewrite
[i
]->instead_of
[j
].len
)) {
45 longest
= &(r
->rewrite
[i
]->instead_of
[j
]);
53 return xstrfmt("%s%s", r
->rewrite
[longest_i
]->base
, url
+ longest
->len
);
56 static void add_url(struct remote
*remote
, const char *url
)
58 ALLOC_GROW(remote
->url
, remote
->url_nr
+ 1, remote
->url_alloc
);
59 remote
->url
[remote
->url_nr
++] = url
;
62 static void add_pushurl(struct remote
*remote
, const char *pushurl
)
64 ALLOC_GROW(remote
->pushurl
, remote
->pushurl_nr
+ 1, remote
->pushurl_alloc
);
65 remote
->pushurl
[remote
->pushurl_nr
++] = pushurl
;
68 static void add_pushurl_alias(struct remote
*remote
, const char *url
)
71 alias_url(url
, &the_repository
->remote_state
->rewrites_push
);
73 add_pushurl(remote
, pushurl
);
76 static void add_url_alias(struct remote
*remote
, const char *url
)
79 alias_url(url
, &the_repository
->remote_state
->rewrites
));
80 add_pushurl_alias(remote
, url
);
83 struct remotes_hash_key
{
88 static int remotes_hash_cmp(const void *unused_cmp_data
,
89 const struct hashmap_entry
*eptr
,
90 const struct hashmap_entry
*entry_or_key
,
93 const struct remote
*a
, *b
;
94 const struct remotes_hash_key
*key
= keydata
;
96 a
= container_of(eptr
, const struct remote
, ent
);
97 b
= container_of(entry_or_key
, const struct remote
, ent
);
100 return strncmp(a
->name
, key
->str
, key
->len
) || a
->name
[key
->len
];
102 return strcmp(a
->name
, b
->name
);
105 static struct remote
*make_remote(const char *name
, int len
)
108 struct remotes_hash_key lookup
;
109 struct hashmap_entry lookup_entry
, *e
;
116 hashmap_entry_init(&lookup_entry
, memhash(name
, len
));
118 e
= hashmap_get(&the_repository
->remote_state
->remotes_hash
,
119 &lookup_entry
, &lookup
);
121 return container_of(e
, struct remote
, ent
);
123 CALLOC_ARRAY(ret
, 1);
124 ret
->prune
= -1; /* unspecified */
125 ret
->prune_tags
= -1; /* unspecified */
126 ret
->name
= xstrndup(name
, len
);
127 refspec_init(&ret
->push
, REFSPEC_PUSH
);
128 refspec_init(&ret
->fetch
, REFSPEC_FETCH
);
130 ALLOC_GROW(the_repository
->remote_state
->remotes
,
131 the_repository
->remote_state
->remotes_nr
+ 1,
132 the_repository
->remote_state
->remotes_alloc
);
133 the_repository
->remote_state
134 ->remotes
[the_repository
->remote_state
->remotes_nr
++] = ret
;
136 hashmap_entry_init(&ret
->ent
, lookup_entry
.hash
);
137 if (hashmap_put_entry(&the_repository
->remote_state
->remotes_hash
, ret
,
139 BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
143 static void remote_clear(struct remote
*remote
)
147 free((char *)remote
->name
);
148 free((char *)remote
->foreign_vcs
);
150 for (i
= 0; i
< remote
->url_nr
; i
++) {
151 free((char *)remote
->url
[i
]);
153 FREE_AND_NULL(remote
->pushurl
);
155 for (i
= 0; i
< remote
->pushurl_nr
; i
++) {
156 free((char *)remote
->pushurl
[i
]);
158 FREE_AND_NULL(remote
->pushurl
);
159 free((char *)remote
->receivepack
);
160 free((char *)remote
->uploadpack
);
161 FREE_AND_NULL(remote
->http_proxy
);
162 FREE_AND_NULL(remote
->http_proxy_authmethod
);
165 static void add_merge(struct branch
*branch
, const char *name
)
167 ALLOC_GROW(branch
->merge_name
, branch
->merge_nr
+ 1,
168 branch
->merge_alloc
);
169 branch
->merge_name
[branch
->merge_nr
++] = name
;
172 static struct branch
*make_branch(const char *name
, size_t len
)
177 for (i
= 0; i
< the_repository
->remote_state
->branches_nr
; i
++) {
179 the_repository
->remote_state
->branches
[i
]->name
,
181 !the_repository
->remote_state
->branches
[i
]->name
[len
])
182 return the_repository
->remote_state
->branches
[i
];
185 ALLOC_GROW(the_repository
->remote_state
->branches
,
186 the_repository
->remote_state
->branches_nr
+ 1,
187 the_repository
->remote_state
->branches_alloc
);
188 CALLOC_ARRAY(ret
, 1);
189 the_repository
->remote_state
190 ->branches
[the_repository
->remote_state
->branches_nr
++] = ret
;
191 ret
->name
= xstrndup(name
, len
);
192 ret
->refname
= xstrfmt("refs/heads/%s", ret
->name
);
197 static struct rewrite
*make_rewrite(struct rewrites
*r
,
198 const char *base
, size_t len
)
203 for (i
= 0; i
< r
->rewrite_nr
; i
++) {
204 if (len
== r
->rewrite
[i
]->baselen
&&
205 !strncmp(base
, r
->rewrite
[i
]->base
, len
))
206 return r
->rewrite
[i
];
209 ALLOC_GROW(r
->rewrite
, r
->rewrite_nr
+ 1, r
->rewrite_alloc
);
210 CALLOC_ARRAY(ret
, 1);
211 r
->rewrite
[r
->rewrite_nr
++] = ret
;
212 ret
->base
= xstrndup(base
, len
);
217 static void add_instead_of(struct rewrite
*rewrite
, const char *instead_of
)
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
++;
225 static const char *skip_spaces(const char *s
)
232 static void read_remotes_file(struct remote
*remote
)
234 struct strbuf buf
= STRBUF_INIT
;
235 FILE *f
= fopen_or_warn(git_path("remotes/%s", remote
->name
), "r");
239 remote
->configured_in_repo
= 1;
240 remote
->origin
= REMOTE_REMOTES
;
241 while (strbuf_getline(&buf
, f
) != EOF
) {
246 if (skip_prefix(buf
.buf
, "URL:", &v
))
247 add_url_alias(remote
, xstrdup(skip_spaces(v
)));
248 else if (skip_prefix(buf
.buf
, "Push:", &v
))
249 refspec_append(&remote
->push
, skip_spaces(v
));
250 else if (skip_prefix(buf
.buf
, "Pull:", &v
))
251 refspec_append(&remote
->fetch
, skip_spaces(v
));
253 strbuf_release(&buf
);
257 static void read_branches_file(struct remote
*remote
)
260 struct strbuf buf
= STRBUF_INIT
;
261 FILE *f
= fopen_or_warn(git_path("branches/%s", remote
->name
), "r");
266 strbuf_getline_lf(&buf
, f
);
270 strbuf_release(&buf
);
274 remote
->configured_in_repo
= 1;
275 remote
->origin
= REMOTE_BRANCHES
;
278 * The branches file would have URL and optionally
279 * #branch specified. The default (or specified) branch is
280 * fetched and stored in the local branch matching the
283 frag
= strchr(buf
.buf
, '#');
287 frag
= (char *)git_default_branch_name(0);
289 add_url_alias(remote
, strbuf_detach(&buf
, NULL
));
290 refspec_appendf(&remote
->fetch
, "refs/heads/%s:refs/heads/%s",
294 * Cogito compatible push: push current HEAD to remote #branch
295 * (master if missing)
297 refspec_appendf(&remote
->push
, "HEAD:refs/heads/%s", frag
);
298 remote
->fetch_tags
= 1; /* always auto-follow */
301 static int handle_config(const char *key
, const char *value
, void *cb
)
306 struct remote
*remote
;
307 struct branch
*branch
;
308 if (parse_config_key(key
, "branch", &name
, &namelen
, &subkey
) >= 0) {
311 branch
= make_branch(name
, namelen
);
312 if (!strcmp(subkey
, "remote")) {
313 return git_config_string(&branch
->remote_name
, key
, value
);
314 } else if (!strcmp(subkey
, "pushremote")) {
315 return git_config_string(&branch
->pushremote_name
, key
, value
);
316 } else if (!strcmp(subkey
, "merge")) {
318 return config_error_nonbool(key
);
319 add_merge(branch
, xstrdup(value
));
323 if (parse_config_key(key
, "url", &name
, &namelen
, &subkey
) >= 0) {
324 struct rewrite
*rewrite
;
327 if (!strcmp(subkey
, "insteadof")) {
329 return config_error_nonbool(key
);
330 rewrite
= make_rewrite(
331 &the_repository
->remote_state
->rewrites
, name
,
333 add_instead_of(rewrite
, xstrdup(value
));
334 } else if (!strcmp(subkey
, "pushinsteadof")) {
336 return config_error_nonbool(key
);
337 rewrite
= make_rewrite(
338 &the_repository
->remote_state
->rewrites_push
,
340 add_instead_of(rewrite
, xstrdup(value
));
344 if (parse_config_key(key
, "remote", &name
, &namelen
, &subkey
) < 0)
347 /* Handle remote.* variables */
348 if (!name
&& !strcmp(subkey
, "pushdefault"))
349 return git_config_string(
350 &the_repository
->remote_state
->pushremote_name
, key
,
355 /* Handle remote.<name>.* variables */
357 warning(_("config remote shorthand cannot begin with '/': %s"),
361 remote
= make_remote(name
, namelen
);
362 remote
->origin
= REMOTE_CONFIG
;
363 if (current_config_scope() == CONFIG_SCOPE_LOCAL
||
364 current_config_scope() == CONFIG_SCOPE_WORKTREE
)
365 remote
->configured_in_repo
= 1;
366 if (!strcmp(subkey
, "mirror"))
367 remote
->mirror
= git_config_bool(key
, value
);
368 else if (!strcmp(subkey
, "skipdefaultupdate"))
369 remote
->skip_default_update
= git_config_bool(key
, value
);
370 else if (!strcmp(subkey
, "skipfetchall"))
371 remote
->skip_default_update
= git_config_bool(key
, value
);
372 else if (!strcmp(subkey
, "prune"))
373 remote
->prune
= git_config_bool(key
, value
);
374 else if (!strcmp(subkey
, "prunetags"))
375 remote
->prune_tags
= git_config_bool(key
, value
);
376 else if (!strcmp(subkey
, "url")) {
378 if (git_config_string(&v
, key
, value
))
381 } else if (!strcmp(subkey
, "pushurl")) {
383 if (git_config_string(&v
, key
, value
))
385 add_pushurl(remote
, v
);
386 } else if (!strcmp(subkey
, "push")) {
388 if (git_config_string(&v
, key
, value
))
390 refspec_append(&remote
->push
, v
);
392 } else if (!strcmp(subkey
, "fetch")) {
394 if (git_config_string(&v
, key
, value
))
396 refspec_append(&remote
->fetch
, v
);
398 } else if (!strcmp(subkey
, "receivepack")) {
400 if (git_config_string(&v
, key
, value
))
402 if (!remote
->receivepack
)
403 remote
->receivepack
= v
;
405 error(_("more than one receivepack given, using the first"));
406 } else if (!strcmp(subkey
, "uploadpack")) {
408 if (git_config_string(&v
, key
, value
))
410 if (!remote
->uploadpack
)
411 remote
->uploadpack
= v
;
413 error(_("more than one uploadpack given, using the first"));
414 } else if (!strcmp(subkey
, "tagopt")) {
415 if (!strcmp(value
, "--no-tags"))
416 remote
->fetch_tags
= -1;
417 else if (!strcmp(value
, "--tags"))
418 remote
->fetch_tags
= 2;
419 } else if (!strcmp(subkey
, "proxy")) {
420 return git_config_string((const char **)&remote
->http_proxy
,
422 } else if (!strcmp(subkey
, "proxyauthmethod")) {
423 return git_config_string((const char **)&remote
->http_proxy_authmethod
,
425 } else if (!strcmp(subkey
, "vcs")) {
426 return git_config_string(&remote
->foreign_vcs
, key
, value
);
431 static void alias_all_urls(void)
434 for (i
= 0; i
< the_repository
->remote_state
->remotes_nr
; i
++) {
435 int add_pushurl_aliases
;
436 if (!the_repository
->remote_state
->remotes
[i
])
439 j
< the_repository
->remote_state
->remotes
[i
]->pushurl_nr
;
441 the_repository
->remote_state
->remotes
[i
]->pushurl
[j
] =
443 the_repository
->remote_state
->remotes
[i
]
445 &the_repository
->remote_state
->rewrites
);
447 add_pushurl_aliases
=
448 the_repository
->remote_state
->remotes
[i
]->pushurl_nr
==
451 j
< the_repository
->remote_state
->remotes
[i
]->url_nr
;
453 if (add_pushurl_aliases
)
455 the_repository
->remote_state
->remotes
[i
],
456 the_repository
->remote_state
->remotes
[i
]
458 the_repository
->remote_state
->remotes
[i
]
459 ->url
[j
] = alias_url(
460 the_repository
->remote_state
->remotes
[i
]->url
[j
],
461 &the_repository
->remote_state
->rewrites
);
466 static void read_config(void)
475 the_repository
->remote_state
->current_branch
= NULL
;
476 if (startup_info
->have_repository
) {
477 const char *head_ref
= resolve_ref_unsafe("HEAD", 0, NULL
, &flag
);
478 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
479 skip_prefix(head_ref
, "refs/heads/", &head_ref
)) {
480 the_repository
->remote_state
->current_branch
=
481 make_branch(head_ref
, strlen(head_ref
));
484 git_config(handle_config
, NULL
);
488 static int valid_remote_nick(const char *name
)
490 if (!name
[0] || is_dot_or_dotdot(name
))
493 /* remote nicknames cannot contain slashes */
495 if (is_dir_sep(*name
++))
500 const char *remote_for_branch(struct branch
*branch
, int *explicit)
502 if (branch
&& branch
->remote_name
) {
505 return branch
->remote_name
;
512 const char *pushremote_for_branch(struct branch
*branch
, int *explicit)
514 if (branch
&& branch
->pushremote_name
) {
517 return branch
->pushremote_name
;
519 if (the_repository
->remote_state
->pushremote_name
) {
522 return the_repository
->remote_state
->pushremote_name
;
524 return remote_for_branch(branch
, explicit);
527 const char *remote_ref_for_branch(struct branch
*branch
, int for_push
)
531 if (branch
->merge_nr
) {
532 return branch
->merge_name
[0];
535 const char *dst
, *remote_name
=
536 pushremote_for_branch(branch
, NULL
);
537 struct remote
*remote
= remote_get(remote_name
);
539 if (remote
&& remote
->push
.nr
&&
540 (dst
= apply_refspecs(&remote
->push
,
549 static struct remote
*remote_get_1(const char *name
,
550 const char *(*get_default
)(struct branch
*, int *))
560 name
= get_default(the_repository
->remote_state
->current_branch
,
563 ret
= make_remote(name
, 0);
564 if (valid_remote_nick(name
) && have_git_dir()) {
565 if (!valid_remote(ret
))
566 read_remotes_file(ret
);
567 if (!valid_remote(ret
))
568 read_branches_file(ret
);
570 if (name_given
&& !valid_remote(ret
))
571 add_url_alias(ret
, name
);
572 if (!valid_remote(ret
))
577 struct remote
*remote_get(const char *name
)
579 return remote_get_1(name
, remote_for_branch
);
582 struct remote
*pushremote_get(const char *name
)
584 return remote_get_1(name
, pushremote_for_branch
);
587 int remote_is_configured(struct remote
*remote
, int in_repo
)
592 return remote
->configured_in_repo
;
593 return !!remote
->origin
;
596 int for_each_remote(each_remote_fn fn
, void *priv
)
600 for (i
= 0; i
< the_repository
->remote_state
->remotes_nr
&& !result
;
602 struct remote
*remote
=
603 the_repository
->remote_state
->remotes
[i
];
606 result
= fn(remote
, priv
);
611 static void handle_duplicate(struct ref
*ref1
, struct ref
*ref2
)
613 if (strcmp(ref1
->name
, ref2
->name
)) {
614 if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
615 ref2
->fetch_head_status
!= FETCH_HEAD_IGNORE
) {
616 die(_("Cannot fetch both %s and %s to %s"),
617 ref1
->name
, ref2
->name
, ref2
->peer_ref
->name
);
618 } else if (ref1
->fetch_head_status
!= FETCH_HEAD_IGNORE
&&
619 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
620 warning(_("%s usually tracks %s, not %s"),
621 ref2
->peer_ref
->name
, ref2
->name
, ref1
->name
);
622 } else if (ref1
->fetch_head_status
== FETCH_HEAD_IGNORE
&&
623 ref2
->fetch_head_status
== FETCH_HEAD_IGNORE
) {
624 die(_("%s tracks both %s and %s"),
625 ref2
->peer_ref
->name
, ref1
->name
, ref2
->name
);
628 * This last possibility doesn't occur because
629 * FETCH_HEAD_IGNORE entries always appear at
630 * the end of the list.
632 BUG("Internal error");
635 free(ref2
->peer_ref
);
639 struct ref
*ref_remove_duplicates(struct ref
*ref_map
)
641 struct string_list refs
= STRING_LIST_INIT_NODUP
;
642 struct ref
*retval
= NULL
;
643 struct ref
**p
= &retval
;
646 struct ref
*ref
= ref_map
;
648 ref_map
= ref_map
->next
;
651 if (!ref
->peer_ref
) {
655 struct string_list_item
*item
=
656 string_list_insert(&refs
, ref
->peer_ref
->name
);
659 /* Entry already existed */
660 handle_duplicate((struct ref
*)item
->util
, ref
);
669 string_list_clear(&refs
, 0);
673 int remote_has_url(struct remote
*remote
, const char *url
)
676 for (i
= 0; i
< remote
->url_nr
; i
++) {
677 if (!strcmp(remote
->url
[i
], url
))
683 static int match_name_with_pattern(const char *key
, const char *name
,
684 const char *value
, char **result
)
686 const char *kstar
= strchr(key
, '*');
692 die(_("key '%s' of pattern had no '*'"), key
);
694 ksuffixlen
= strlen(kstar
+ 1);
695 namelen
= strlen(name
);
696 ret
= !strncmp(name
, key
, klen
) && namelen
>= klen
+ ksuffixlen
&&
697 !memcmp(name
+ namelen
- ksuffixlen
, kstar
+ 1, ksuffixlen
);
699 struct strbuf sb
= STRBUF_INIT
;
700 const char *vstar
= strchr(value
, '*');
702 die(_("value '%s' of pattern has no '*'"), value
);
703 strbuf_add(&sb
, value
, vstar
- value
);
704 strbuf_add(&sb
, name
+ klen
, namelen
- klen
- ksuffixlen
);
705 strbuf_addstr(&sb
, vstar
+ 1);
706 *result
= strbuf_detach(&sb
, NULL
);
711 static int refspec_match(const struct refspec_item
*refspec
,
714 if (refspec
->pattern
)
715 return match_name_with_pattern(refspec
->src
, name
, NULL
, NULL
);
717 return !strcmp(refspec
->src
, name
);
720 static int omit_name_by_refspec(const char *name
, struct refspec
*rs
)
724 for (i
= 0; i
< rs
->nr
; i
++) {
725 if (rs
->items
[i
].negative
&& refspec_match(&rs
->items
[i
], name
))
731 struct ref
*apply_negative_refspecs(struct ref
*ref_map
, struct refspec
*rs
)
735 for (tail
= &ref_map
; *tail
; ) {
736 struct ref
*ref
= *tail
;
738 if (omit_name_by_refspec(ref
->name
, rs
)) {
749 static int query_matches_negative_refspec(struct refspec
*rs
, struct refspec_item
*query
)
751 int i
, matched_negative
= 0;
752 int find_src
= !query
->src
;
753 struct string_list reversed
= STRING_LIST_INIT_NODUP
;
754 const char *needle
= find_src
? query
->dst
: query
->src
;
757 * Check whether the queried ref matches any negative refpsec. If so,
758 * then we should ultimately treat this as not matching the query at
761 * Note that negative refspecs always match the source, but the query
762 * item uses the destination. To handle this, we apply pattern
763 * refspecs in reverse to figure out if the query source matches any
764 * of the negative refspecs.
766 * The first loop finds and expands all positive refspecs
767 * matched by the queried ref.
769 * The second loop checks if any of the results of the first loop
770 * match any negative refspec.
772 for (i
= 0; i
< rs
->nr
; i
++) {
773 struct refspec_item
*refspec
= &rs
->items
[i
];
776 if (refspec
->negative
)
779 /* Note the reversal of src and dst */
780 if (refspec
->pattern
) {
781 const char *key
= refspec
->dst
? refspec
->dst
: refspec
->src
;
782 const char *value
= refspec
->src
;
784 if (match_name_with_pattern(key
, needle
, value
, &expn_name
))
785 string_list_append_nodup(&reversed
, expn_name
);
786 } else if (refspec
->matching
) {
787 /* For the special matching refspec, any query should match */
788 string_list_append(&reversed
, needle
);
789 } else if (!refspec
->src
) {
790 BUG("refspec->src should not be null here");
791 } else if (!strcmp(needle
, refspec
->src
)) {
792 string_list_append(&reversed
, refspec
->src
);
796 for (i
= 0; !matched_negative
&& i
< reversed
.nr
; i
++) {
797 if (omit_name_by_refspec(reversed
.items
[i
].string
, rs
))
798 matched_negative
= 1;
801 string_list_clear(&reversed
, 0);
803 return matched_negative
;
806 static void query_refspecs_multiple(struct refspec
*rs
,
807 struct refspec_item
*query
,
808 struct string_list
*results
)
811 int find_src
= !query
->src
;
813 if (find_src
&& !query
->dst
)
814 BUG("query_refspecs_multiple: need either src or dst");
816 if (query_matches_negative_refspec(rs
, query
))
819 for (i
= 0; i
< rs
->nr
; i
++) {
820 struct refspec_item
*refspec
= &rs
->items
[i
];
821 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
822 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
823 const char *needle
= find_src
? query
->dst
: query
->src
;
824 char **result
= find_src
? &query
->src
: &query
->dst
;
826 if (!refspec
->dst
|| refspec
->negative
)
828 if (refspec
->pattern
) {
829 if (match_name_with_pattern(key
, needle
, value
, result
))
830 string_list_append_nodup(results
, *result
);
831 } else if (!strcmp(needle
, key
)) {
832 string_list_append(results
, value
);
837 int query_refspecs(struct refspec
*rs
, struct refspec_item
*query
)
840 int find_src
= !query
->src
;
841 const char *needle
= find_src
? query
->dst
: query
->src
;
842 char **result
= find_src
? &query
->src
: &query
->dst
;
844 if (find_src
&& !query
->dst
)
845 BUG("query_refspecs: need either src or dst");
847 if (query_matches_negative_refspec(rs
, query
))
850 for (i
= 0; i
< rs
->nr
; i
++) {
851 struct refspec_item
*refspec
= &rs
->items
[i
];
852 const char *key
= find_src
? refspec
->dst
: refspec
->src
;
853 const char *value
= find_src
? refspec
->src
: refspec
->dst
;
855 if (!refspec
->dst
|| refspec
->negative
)
857 if (refspec
->pattern
) {
858 if (match_name_with_pattern(key
, needle
, value
, result
)) {
859 query
->force
= refspec
->force
;
862 } else if (!strcmp(needle
, key
)) {
863 *result
= xstrdup(value
);
864 query
->force
= refspec
->force
;
871 char *apply_refspecs(struct refspec
*rs
, const char *name
)
873 struct refspec_item query
;
875 memset(&query
, 0, sizeof(struct refspec_item
));
876 query
.src
= (char *)name
;
878 if (query_refspecs(rs
, &query
))
884 int remote_find_tracking(struct remote
*remote
, struct refspec_item
*refspec
)
886 return query_refspecs(&remote
->fetch
, refspec
);
889 static struct ref
*alloc_ref_with_prefix(const char *prefix
, size_t prefixlen
,
892 size_t len
= strlen(name
);
893 struct ref
*ref
= xcalloc(1, st_add4(sizeof(*ref
), prefixlen
, len
, 1));
894 memcpy(ref
->name
, prefix
, prefixlen
);
895 memcpy(ref
->name
+ prefixlen
, name
, len
);
899 struct ref
*alloc_ref(const char *name
)
901 return alloc_ref_with_prefix("", 0, name
);
904 struct ref
*copy_ref(const struct ref
*ref
)
910 len
= st_add3(sizeof(struct ref
), strlen(ref
->name
), 1);
912 memcpy(cpy
, ref
, len
);
914 cpy
->symref
= xstrdup_or_null(ref
->symref
);
915 cpy
->remote_status
= xstrdup_or_null(ref
->remote_status
);
916 cpy
->peer_ref
= copy_ref(ref
->peer_ref
);
920 struct ref
*copy_ref_list(const struct ref
*ref
)
922 struct ref
*ret
= NULL
;
923 struct ref
**tail
= &ret
;
925 *tail
= copy_ref(ref
);
927 tail
= &((*tail
)->next
);
932 void free_one_ref(struct ref
*ref
)
936 free_one_ref(ref
->peer_ref
);
937 free(ref
->remote_status
);
942 void free_refs(struct ref
*ref
)
952 int ref_compare_name(const void *va
, const void *vb
)
954 const struct ref
*a
= va
, *b
= vb
;
955 return strcmp(a
->name
, b
->name
);
958 static void *ref_list_get_next(const void *a
)
960 return ((const struct ref
*)a
)->next
;
963 static void ref_list_set_next(void *a
, void *next
)
965 ((struct ref
*)a
)->next
= next
;
968 void sort_ref_list(struct ref
**l
, int (*cmp
)(const void *, const void *))
970 *l
= llist_mergesort(*l
, ref_list_get_next
, ref_list_set_next
, cmp
);
973 int count_refspec_match(const char *pattern
,
975 struct ref
**matched_ref
)
977 int patlen
= strlen(pattern
);
978 struct ref
*matched_weak
= NULL
;
979 struct ref
*matched
= NULL
;
983 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
984 char *name
= refs
->name
;
985 int namelen
= strlen(name
);
987 if (!refname_match(pattern
, name
))
990 /* A match is "weak" if it is with refs outside
991 * heads or tags, and did not specify the pattern
992 * in full (e.g. "refs/remotes/origin/master") or at
993 * least from the toplevel (e.g. "remotes/origin/master");
994 * otherwise "git push $URL master" would result in
995 * ambiguity between remotes/origin/master and heads/master
996 * at the remote site.
998 if (namelen
!= patlen
&&
999 patlen
!= namelen
- 5 &&
1000 !starts_with(name
, "refs/heads/") &&
1001 !starts_with(name
, "refs/tags/")) {
1002 /* We want to catch the case where only weak
1003 * matches are found and there are multiple
1004 * matches, and where more than one strong
1005 * matches are found, as ambiguous. One
1006 * strong match with zero or more weak matches
1007 * are acceptable as a unique match.
1009 matched_weak
= refs
;
1019 *matched_ref
= matched_weak
;
1024 *matched_ref
= matched
;
1029 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
1037 static struct ref
*alloc_delete_ref(void)
1039 struct ref
*ref
= alloc_ref("(delete)");
1040 oidclr(&ref
->new_oid
);
1044 static int try_explicit_object_name(const char *name
,
1047 struct object_id oid
;
1051 *match
= alloc_delete_ref();
1055 if (get_oid(name
, &oid
))
1059 *match
= alloc_ref(name
);
1060 oidcpy(&(*match
)->new_oid
, &oid
);
1065 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
1067 struct ref
*ret
= alloc_ref(name
);
1068 tail_link_ref(ret
, tail
);
1072 static char *guess_ref(const char *name
, struct ref
*peer
)
1074 struct strbuf buf
= STRBUF_INIT
;
1076 const char *r
= resolve_ref_unsafe(peer
->name
, RESOLVE_REF_READING
,
1081 if (starts_with(r
, "refs/heads/")) {
1082 strbuf_addstr(&buf
, "refs/heads/");
1083 } else if (starts_with(r
, "refs/tags/")) {
1084 strbuf_addstr(&buf
, "refs/tags/");
1089 strbuf_addstr(&buf
, name
);
1090 return strbuf_detach(&buf
, NULL
);
1093 static int match_explicit_lhs(struct ref
*src
,
1094 struct refspec_item
*rs
,
1096 int *allocated_match
)
1098 switch (count_refspec_match(rs
->src
, src
, match
)) {
1100 if (allocated_match
)
1101 *allocated_match
= 0;
1104 /* The source could be in the get_sha1() format
1105 * not a reference name. :refs/other is a
1106 * way to delete 'other' ref at the remote end.
1108 if (try_explicit_object_name(rs
->src
, match
) < 0)
1109 return error(_("src refspec %s does not match any"), rs
->src
);
1110 if (allocated_match
)
1111 *allocated_match
= 1;
1114 return error(_("src refspec %s matches more than one"), rs
->src
);
1118 static void show_push_unqualified_ref_name_error(const char *dst_value
,
1119 const char *matched_src_name
)
1121 struct object_id oid
;
1122 enum object_type type
;
1125 * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
1126 * <remote> <src>:<dst>" push, and "being pushed ('%s')" is
1129 error(_("The destination you provided is not a full refname (i.e.,\n"
1130 "starting with \"refs/\"). We tried to guess what you meant by:\n"
1132 "- Looking for a ref that matches '%s' on the remote side.\n"
1133 "- Checking if the <src> being pushed ('%s')\n"
1134 " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n"
1135 " refs/{heads,tags}/ prefix on the remote side.\n"
1137 "Neither worked, so we gave up. You must fully qualify the ref."),
1138 dst_value
, matched_src_name
);
1140 if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME
))
1143 if (get_oid(matched_src_name
, &oid
))
1144 BUG("'%s' is not a valid object, "
1145 "match_explicit_lhs() should catch this!",
1147 type
= oid_object_info(the_repository
, &oid
, NULL
);
1148 if (type
== OBJ_COMMIT
) {
1149 advise(_("The <src> part of the refspec is a commit object.\n"
1150 "Did you mean to create a new branch by pushing to\n"
1151 "'%s:refs/heads/%s'?"),
1152 matched_src_name
, dst_value
);
1153 } else if (type
== OBJ_TAG
) {
1154 advise(_("The <src> part of the refspec is a tag object.\n"
1155 "Did you mean to create a new tag by pushing to\n"
1156 "'%s:refs/tags/%s'?"),
1157 matched_src_name
, dst_value
);
1158 } else if (type
== OBJ_TREE
) {
1159 advise(_("The <src> part of the refspec is a tree object.\n"
1160 "Did you mean to tag a new tree by pushing to\n"
1161 "'%s:refs/tags/%s'?"),
1162 matched_src_name
, dst_value
);
1163 } else if (type
== OBJ_BLOB
) {
1164 advise(_("The <src> part of the refspec is a blob object.\n"
1165 "Did you mean to tag a new blob by pushing to\n"
1166 "'%s:refs/tags/%s'?"),
1167 matched_src_name
, dst_value
);
1169 BUG("'%s' should be commit/tag/tree/blob, is '%d'",
1170 matched_src_name
, type
);
1174 static int match_explicit(struct ref
*src
, struct ref
*dst
,
1175 struct ref
***dst_tail
,
1176 struct refspec_item
*rs
)
1178 struct ref
*matched_src
, *matched_dst
;
1181 const char *dst_value
= rs
->dst
;
1184 if (rs
->pattern
|| rs
->matching
|| rs
->negative
)
1187 matched_src
= matched_dst
= NULL
;
1188 if (match_explicit_lhs(src
, rs
, &matched_src
, &allocated_src
) < 0)
1194 dst_value
= resolve_ref_unsafe(matched_src
->name
,
1195 RESOLVE_REF_READING
,
1198 ((flag
& REF_ISSYMREF
) &&
1199 !starts_with(dst_value
, "refs/heads/")))
1200 die(_("%s cannot be resolved to branch"),
1204 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
1208 if (starts_with(dst_value
, "refs/")) {
1209 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
1210 } else if (is_null_oid(&matched_src
->new_oid
)) {
1211 error(_("unable to delete '%s': remote ref does not exist"),
1213 } else if ((dst_guess
= guess_ref(dst_value
, matched_src
))) {
1214 matched_dst
= make_linked_ref(dst_guess
, dst_tail
);
1217 show_push_unqualified_ref_name_error(dst_value
,
1223 error(_("dst refspec %s matches more than one"),
1229 if (matched_dst
->peer_ref
)
1230 return error(_("dst ref %s receives from more than one src"),
1233 matched_dst
->peer_ref
= allocated_src
?
1235 copy_ref(matched_src
);
1236 matched_dst
->force
= rs
->force
;
1241 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
1242 struct ref
***dst_tail
, struct refspec
*rs
)
1245 for (i
= errs
= 0; i
< rs
->nr
; i
++)
1246 errs
+= match_explicit(src
, dst
, dst_tail
, &rs
->items
[i
]);
1250 static char *get_ref_match(const struct refspec
*rs
, const struct ref
*ref
,
1251 int send_mirror
, int direction
,
1252 const struct refspec_item
**ret_pat
)
1254 const struct refspec_item
*pat
;
1257 int matching_refs
= -1;
1258 for (i
= 0; i
< rs
->nr
; i
++) {
1259 const struct refspec_item
*item
= &rs
->items
[i
];
1264 if (item
->matching
&&
1265 (matching_refs
== -1 || item
->force
)) {
1270 if (item
->pattern
) {
1271 const char *dst_side
= item
->dst
? item
->dst
: item
->src
;
1273 if (direction
== FROM_SRC
)
1274 match
= match_name_with_pattern(item
->src
, ref
->name
, dst_side
, &name
);
1276 match
= match_name_with_pattern(dst_side
, ref
->name
, item
->src
, &name
);
1283 if (matching_refs
== -1)
1286 pat
= &rs
->items
[matching_refs
];
1287 if (pat
->matching
) {
1289 * "matching refs"; traditionally we pushed everything
1290 * including refs outside refs/heads/ hierarchy, but
1291 * that does not make much sense these days.
1293 if (!send_mirror
&& !starts_with(ref
->name
, "refs/heads/"))
1295 name
= xstrdup(ref
->name
);
1302 static struct ref
**tail_ref(struct ref
**head
)
1304 struct ref
**tail
= head
;
1306 tail
= &((*tail
)->next
);
1311 struct commit
**tip
;
1315 static void add_to_tips(struct tips
*tips
, const struct object_id
*oid
)
1317 struct commit
*commit
;
1319 if (is_null_oid(oid
))
1321 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
1322 if (!commit
|| (commit
->object
.flags
& TMP_MARK
))
1324 commit
->object
.flags
|= TMP_MARK
;
1325 ALLOC_GROW(tips
->tip
, tips
->nr
+ 1, tips
->alloc
);
1326 tips
->tip
[tips
->nr
++] = commit
;
1329 static void add_missing_tags(struct ref
*src
, struct ref
**dst
, struct ref
***dst_tail
)
1331 struct string_list dst_tag
= STRING_LIST_INIT_NODUP
;
1332 struct string_list src_tag
= STRING_LIST_INIT_NODUP
;
1333 struct string_list_item
*item
;
1335 struct tips sent_tips
;
1338 * Collect everything we know they would have at the end of
1339 * this push, and collect all tags they have.
1341 memset(&sent_tips
, 0, sizeof(sent_tips
));
1342 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1343 if (ref
->peer_ref
&&
1344 !is_null_oid(&ref
->peer_ref
->new_oid
))
1345 add_to_tips(&sent_tips
, &ref
->peer_ref
->new_oid
);
1347 add_to_tips(&sent_tips
, &ref
->old_oid
);
1348 if (starts_with(ref
->name
, "refs/tags/"))
1349 string_list_append(&dst_tag
, ref
->name
);
1351 clear_commit_marks_many(sent_tips
.nr
, sent_tips
.tip
, TMP_MARK
);
1353 string_list_sort(&dst_tag
);
1355 /* Collect tags they do not have. */
1356 for (ref
= src
; ref
; ref
= ref
->next
) {
1357 if (!starts_with(ref
->name
, "refs/tags/"))
1358 continue; /* not a tag */
1359 if (string_list_has_string(&dst_tag
, ref
->name
))
1360 continue; /* they already have it */
1361 if (oid_object_info(the_repository
, &ref
->new_oid
, NULL
) != OBJ_TAG
)
1362 continue; /* be conservative */
1363 item
= string_list_append(&src_tag
, ref
->name
);
1366 string_list_clear(&dst_tag
, 0);
1369 * At this point, src_tag lists tags that are missing from
1370 * dst, and sent_tips lists the tips we are pushing or those
1371 * that we know they already have. An element in the src_tag
1372 * that is an ancestor of any of the sent_tips needs to be
1373 * sent to the other side.
1376 const int reachable_flag
= 1;
1377 struct commit_list
*found_commits
;
1378 struct commit
**src_commits
;
1379 int nr_src_commits
= 0, alloc_src_commits
= 16;
1380 ALLOC_ARRAY(src_commits
, alloc_src_commits
);
1382 for_each_string_list_item(item
, &src_tag
) {
1383 struct ref
*ref
= item
->util
;
1384 struct commit
*commit
;
1386 if (is_null_oid(&ref
->new_oid
))
1388 commit
= lookup_commit_reference_gently(the_repository
,
1392 /* not pushing a commit, which is not an error */
1395 ALLOC_GROW(src_commits
, nr_src_commits
+ 1, alloc_src_commits
);
1396 src_commits
[nr_src_commits
++] = commit
;
1399 found_commits
= get_reachable_subset(sent_tips
.tip
, sent_tips
.nr
,
1400 src_commits
, nr_src_commits
,
1403 for_each_string_list_item(item
, &src_tag
) {
1404 struct ref
*dst_ref
;
1405 struct ref
*ref
= item
->util
;
1406 struct commit
*commit
;
1408 if (is_null_oid(&ref
->new_oid
))
1410 commit
= lookup_commit_reference_gently(the_repository
,
1414 /* not pushing a commit, which is not an error */
1418 * Is this tag, which they do not have, reachable from
1419 * any of the commits we are sending?
1421 if (!(commit
->object
.flags
& reachable_flag
))
1425 dst_ref
= make_linked_ref(ref
->name
, dst_tail
);
1426 oidcpy(&dst_ref
->new_oid
, &ref
->new_oid
);
1427 dst_ref
->peer_ref
= copy_ref(ref
);
1430 clear_commit_marks_many(nr_src_commits
, src_commits
, reachable_flag
);
1432 free_commit_list(found_commits
);
1435 string_list_clear(&src_tag
, 0);
1436 free(sent_tips
.tip
);
1439 struct ref
*find_ref_by_name(const struct ref
*list
, const char *name
)
1441 for ( ; list
; list
= list
->next
)
1442 if (!strcmp(list
->name
, name
))
1443 return (struct ref
*)list
;
1447 static void prepare_ref_index(struct string_list
*ref_index
, struct ref
*ref
)
1449 for ( ; ref
; ref
= ref
->next
)
1450 string_list_append_nodup(ref_index
, ref
->name
)->util
= ref
;
1452 string_list_sort(ref_index
);
1456 * Given only the set of local refs, sanity-check the set of push
1457 * refspecs. We can't catch all errors that match_push_refs would,
1458 * but we can catch some errors early before even talking to the
1461 int check_push_refs(struct ref
*src
, struct refspec
*rs
)
1466 for (i
= 0; i
< rs
->nr
; i
++) {
1467 struct refspec_item
*item
= &rs
->items
[i
];
1469 if (item
->pattern
|| item
->matching
|| item
->negative
)
1472 ret
|= match_explicit_lhs(src
, item
, NULL
, NULL
);
1479 * Given the set of refs the local repository has, the set of refs the
1480 * remote repository has, and the refspec used for push, determine
1481 * what remote refs we will update and with what value by setting
1482 * peer_ref (which object is being pushed) and force (if the push is
1483 * forced) in elements of "dst". The function may add new elements to
1484 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
1486 int match_push_refs(struct ref
*src
, struct ref
**dst
,
1487 struct refspec
*rs
, int flags
)
1489 int send_all
= flags
& MATCH_REFS_ALL
;
1490 int send_mirror
= flags
& MATCH_REFS_MIRROR
;
1491 int send_prune
= flags
& MATCH_REFS_PRUNE
;
1493 struct ref
*ref
, **dst_tail
= tail_ref(dst
);
1494 struct string_list dst_ref_index
= STRING_LIST_INIT_NODUP
;
1496 /* If no refspec is provided, use the default ":" */
1498 refspec_append(rs
, ":");
1500 errs
= match_explicit_refs(src
, *dst
, &dst_tail
, rs
);
1502 /* pick the remainder */
1503 for (ref
= src
; ref
; ref
= ref
->next
) {
1504 struct string_list_item
*dst_item
;
1505 struct ref
*dst_peer
;
1506 const struct refspec_item
*pat
= NULL
;
1509 dst_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_SRC
, &pat
);
1513 if (!dst_ref_index
.nr
)
1514 prepare_ref_index(&dst_ref_index
, *dst
);
1516 dst_item
= string_list_lookup(&dst_ref_index
, dst_name
);
1517 dst_peer
= dst_item
? dst_item
->util
: NULL
;
1519 if (dst_peer
->peer_ref
)
1520 /* We're already sending something to this ref. */
1523 if (pat
->matching
&& !(send_all
|| send_mirror
))
1525 * Remote doesn't have it, and we have no
1526 * explicit pattern, and we don't have
1527 * --all or --mirror.
1531 /* Create a new one and link it */
1532 dst_peer
= make_linked_ref(dst_name
, &dst_tail
);
1533 oidcpy(&dst_peer
->new_oid
, &ref
->new_oid
);
1534 string_list_insert(&dst_ref_index
,
1535 dst_peer
->name
)->util
= dst_peer
;
1537 dst_peer
->peer_ref
= copy_ref(ref
);
1538 dst_peer
->force
= pat
->force
;
1543 string_list_clear(&dst_ref_index
, 0);
1545 if (flags
& MATCH_REFS_FOLLOW_TAGS
)
1546 add_missing_tags(src
, dst
, &dst_tail
);
1549 struct string_list src_ref_index
= STRING_LIST_INIT_NODUP
;
1550 /* check for missing refs on the remote */
1551 for (ref
= *dst
; ref
; ref
= ref
->next
) {
1555 /* We're already sending something to this ref. */
1558 src_name
= get_ref_match(rs
, ref
, send_mirror
, FROM_DST
, NULL
);
1560 if (!src_ref_index
.nr
)
1561 prepare_ref_index(&src_ref_index
, src
);
1562 if (!string_list_has_string(&src_ref_index
,
1564 ref
->peer_ref
= alloc_delete_ref();
1568 string_list_clear(&src_ref_index
, 0);
1571 *dst
= apply_negative_refspecs(*dst
, rs
);
1578 void set_ref_status_for_push(struct ref
*remote_refs
, int send_mirror
,
1583 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1584 int force_ref_update
= ref
->force
|| force_update
;
1585 int reject_reason
= 0;
1588 oidcpy(&ref
->new_oid
, &ref
->peer_ref
->new_oid
);
1589 else if (!send_mirror
)
1592 ref
->deletion
= is_null_oid(&ref
->new_oid
);
1593 if (!ref
->deletion
&&
1594 oideq(&ref
->old_oid
, &ref
->new_oid
)) {
1595 ref
->status
= REF_STATUS_UPTODATE
;
1600 * If the remote ref has moved and is now different
1601 * from what we expect, reject any push.
1603 * It also is an error if the user told us to check
1604 * with the remote-tracking branch to find the value
1605 * to expect, but we did not have such a tracking
1608 * If the tip of the remote-tracking ref is unreachable
1609 * from any reflog entry of its local ref indicating a
1610 * possible update since checkout; reject the push.
1612 if (ref
->expect_old_sha1
) {
1613 if (!oideq(&ref
->old_oid
, &ref
->old_oid_expect
))
1614 reject_reason
= REF_STATUS_REJECT_STALE
;
1615 else if (ref
->check_reachable
&& ref
->unreachable
)
1617 REF_STATUS_REJECT_REMOTE_UPDATED
;
1620 * If the ref isn't stale, and is reachable
1621 * from one of the reflog entries of
1622 * the local branch, force the update.
1624 force_ref_update
= 1;
1628 * If the update isn't already rejected then check
1629 * the usual "must fast-forward" rules.
1631 * Decide whether an individual refspec A:B can be
1632 * pushed. The push will succeed if any of the
1633 * following are true:
1635 * (1) the remote reference B does not exist
1637 * (2) the remote reference B is being removed (i.e.,
1638 * pushing :B where no source is specified)
1640 * (3) the destination is not under refs/tags/, and
1641 * if the old and new value is a commit, the new
1642 * is a descendant of the old.
1644 * (4) it is forced using the +A:B notation, or by
1645 * passing the --force argument
1648 if (!reject_reason
&& !ref
->deletion
&& !is_null_oid(&ref
->old_oid
)) {
1649 if (starts_with(ref
->name
, "refs/tags/"))
1650 reject_reason
= REF_STATUS_REJECT_ALREADY_EXISTS
;
1651 else if (!has_object_file(&ref
->old_oid
))
1652 reject_reason
= REF_STATUS_REJECT_FETCH_FIRST
;
1653 else if (!lookup_commit_reference_gently(the_repository
, &ref
->old_oid
, 1) ||
1654 !lookup_commit_reference_gently(the_repository
, &ref
->new_oid
, 1))
1655 reject_reason
= REF_STATUS_REJECT_NEEDS_FORCE
;
1656 else if (!ref_newer(&ref
->new_oid
, &ref
->old_oid
))
1657 reject_reason
= REF_STATUS_REJECT_NONFASTFORWARD
;
1661 * "--force" will defeat any rejection implemented
1662 * by the rules above.
1664 if (!force_ref_update
)
1665 ref
->status
= reject_reason
;
1666 else if (reject_reason
)
1667 ref
->forced_update
= 1;
1671 static void set_merge(struct branch
*ret
)
1673 struct remote
*remote
;
1675 struct object_id oid
;
1679 return; /* no branch */
1681 return; /* already run */
1682 if (!ret
->remote_name
|| !ret
->merge_nr
) {
1684 * no merge config; let's make sure we don't confuse callers
1685 * with a non-zero merge_nr but a NULL merge
1691 remote
= remote_get(ret
->remote_name
);
1693 CALLOC_ARRAY(ret
->merge
, ret
->merge_nr
);
1694 for (i
= 0; i
< ret
->merge_nr
; i
++) {
1695 ret
->merge
[i
] = xcalloc(1, sizeof(**ret
->merge
));
1696 ret
->merge
[i
]->src
= xstrdup(ret
->merge_name
[i
]);
1697 if (!remote_find_tracking(remote
, ret
->merge
[i
]) ||
1698 strcmp(ret
->remote_name
, "."))
1700 if (dwim_ref(ret
->merge_name
[i
], strlen(ret
->merge_name
[i
]),
1701 &oid
, &ref
, 0) == 1)
1702 ret
->merge
[i
]->dst
= ref
;
1704 ret
->merge
[i
]->dst
= xstrdup(ret
->merge_name
[i
]);
1708 struct branch
*branch_get(const char *name
)
1713 if (!name
|| !*name
|| !strcmp(name
, "HEAD"))
1714 ret
= the_repository
->remote_state
->current_branch
;
1716 ret
= make_branch(name
, strlen(name
));
1721 int branch_has_merge_config(struct branch
*branch
)
1723 return branch
&& !!branch
->merge
;
1726 int branch_merge_matches(struct branch
*branch
,
1728 const char *refname
)
1730 if (!branch
|| i
< 0 || i
>= branch
->merge_nr
)
1732 return refname_match(branch
->merge
[i
]->src
, refname
);
1735 __attribute__((format (printf
,2,3)))
1736 static const char *error_buf(struct strbuf
*err
, const char *fmt
, ...)
1741 strbuf_vaddf(err
, fmt
, ap
);
1747 const char *branch_get_upstream(struct branch
*branch
, struct strbuf
*err
)
1750 return error_buf(err
, _("HEAD does not point to a branch"));
1752 if (!branch
->merge
|| !branch
->merge
[0]) {
1754 * no merge config; is it because the user didn't define any,
1755 * or because it is not a real branch, and get_branch
1758 if (!ref_exists(branch
->refname
))
1759 return error_buf(err
, _("no such branch: '%s'"),
1761 return error_buf(err
,
1762 _("no upstream configured for branch '%s'"),
1766 if (!branch
->merge
[0]->dst
)
1767 return error_buf(err
,
1768 _("upstream branch '%s' not stored as a remote-tracking branch"),
1769 branch
->merge
[0]->src
);
1771 return branch
->merge
[0]->dst
;
1774 static const char *tracking_for_push_dest(struct remote
*remote
,
1775 const char *refname
,
1780 ret
= apply_refspecs(&remote
->fetch
, refname
);
1782 return error_buf(err
,
1783 _("push destination '%s' on remote '%s' has no local tracking branch"),
1784 refname
, remote
->name
);
1788 static const char *branch_get_push_1(struct branch
*branch
, struct strbuf
*err
)
1790 struct remote
*remote
;
1792 remote
= remote_get(pushremote_for_branch(branch
, NULL
));
1794 return error_buf(err
,
1795 _("branch '%s' has no remote for pushing"),
1798 if (remote
->push
.nr
) {
1802 dst
= apply_refspecs(&remote
->push
, branch
->refname
);
1804 return error_buf(err
,
1805 _("push refspecs for '%s' do not include '%s'"),
1806 remote
->name
, branch
->name
);
1808 ret
= tracking_for_push_dest(remote
, dst
, err
);
1814 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1816 switch (push_default
) {
1817 case PUSH_DEFAULT_NOTHING
:
1818 return error_buf(err
, _("push has no destination (push.default is 'nothing')"));
1820 case PUSH_DEFAULT_MATCHING
:
1821 case PUSH_DEFAULT_CURRENT
:
1822 return tracking_for_push_dest(remote
, branch
->refname
, err
);
1824 case PUSH_DEFAULT_UPSTREAM
:
1825 return branch_get_upstream(branch
, err
);
1827 case PUSH_DEFAULT_UNSPECIFIED
:
1828 case PUSH_DEFAULT_SIMPLE
:
1830 const char *up
, *cur
;
1832 up
= branch_get_upstream(branch
, err
);
1835 cur
= tracking_for_push_dest(remote
, branch
->refname
, err
);
1838 if (strcmp(cur
, up
))
1839 return error_buf(err
,
1840 _("cannot resolve 'simple' push to a single destination"));
1845 BUG("unhandled push situation");
1848 const char *branch_get_push(struct branch
*branch
, struct strbuf
*err
)
1851 return error_buf(err
, _("HEAD does not point to a branch"));
1853 if (!branch
->push_tracking_ref
)
1854 branch
->push_tracking_ref
= branch_get_push_1(branch
, err
);
1855 return branch
->push_tracking_ref
;
1858 static int ignore_symref_update(const char *refname
)
1862 if (!resolve_ref_unsafe(refname
, 0, NULL
, &flag
))
1863 return 0; /* non-existing refs are OK */
1864 return (flag
& REF_ISSYMREF
);
1868 * Create and return a list of (struct ref) consisting of copies of
1869 * each remote_ref that matches refspec. refspec must be a pattern.
1870 * Fill in the copies' peer_ref to describe the local tracking refs to
1871 * which they map. Omit any references that would map to an existing
1872 * local symbolic ref.
1874 static struct ref
*get_expanded_map(const struct ref
*remote_refs
,
1875 const struct refspec_item
*refspec
)
1877 const struct ref
*ref
;
1878 struct ref
*ret
= NULL
;
1879 struct ref
**tail
= &ret
;
1881 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
1882 char *expn_name
= NULL
;
1884 if (strchr(ref
->name
, '^'))
1885 continue; /* a dereference item */
1886 if (match_name_with_pattern(refspec
->src
, ref
->name
,
1887 refspec
->dst
, &expn_name
) &&
1888 !ignore_symref_update(expn_name
)) {
1889 struct ref
*cpy
= copy_ref(ref
);
1891 cpy
->peer_ref
= alloc_ref(expn_name
);
1893 cpy
->peer_ref
->force
= 1;
1903 static const struct ref
*find_ref_by_name_abbrev(const struct ref
*refs
, const char *name
)
1905 const struct ref
*ref
;
1906 const struct ref
*best_match
= NULL
;
1909 for (ref
= refs
; ref
; ref
= ref
->next
) {
1910 int score
= refname_match(name
, ref
->name
);
1912 if (best_score
< score
) {
1920 struct ref
*get_remote_ref(const struct ref
*remote_refs
, const char *name
)
1922 const struct ref
*ref
= find_ref_by_name_abbrev(remote_refs
, name
);
1927 return copy_ref(ref
);
1930 static struct ref
*get_local_ref(const char *name
)
1932 if (!name
|| name
[0] == '\0')
1935 if (starts_with(name
, "refs/"))
1936 return alloc_ref(name
);
1938 if (starts_with(name
, "heads/") ||
1939 starts_with(name
, "tags/") ||
1940 starts_with(name
, "remotes/"))
1941 return alloc_ref_with_prefix("refs/", 5, name
);
1943 return alloc_ref_with_prefix("refs/heads/", 11, name
);
1946 int get_fetch_map(const struct ref
*remote_refs
,
1947 const struct refspec_item
*refspec
,
1951 struct ref
*ref_map
, **rmp
;
1953 if (refspec
->negative
)
1956 if (refspec
->pattern
) {
1957 ref_map
= get_expanded_map(remote_refs
, refspec
);
1959 const char *name
= refspec
->src
[0] ? refspec
->src
: "HEAD";
1961 if (refspec
->exact_sha1
) {
1962 ref_map
= alloc_ref(name
);
1963 get_oid_hex(name
, &ref_map
->old_oid
);
1964 ref_map
->exact_oid
= 1;
1966 ref_map
= get_remote_ref(remote_refs
, name
);
1968 if (!missing_ok
&& !ref_map
)
1969 die(_("couldn't find remote ref %s"), name
);
1971 ref_map
->peer_ref
= get_local_ref(refspec
->dst
);
1972 if (ref_map
->peer_ref
&& refspec
->force
)
1973 ref_map
->peer_ref
->force
= 1;
1977 for (rmp
= &ref_map
; *rmp
; ) {
1978 if ((*rmp
)->peer_ref
) {
1979 if (!starts_with((*rmp
)->peer_ref
->name
, "refs/") ||
1980 check_refname_format((*rmp
)->peer_ref
->name
, 0)) {
1981 struct ref
*ignore
= *rmp
;
1982 error(_("* Ignoring funny ref '%s' locally"),
1983 (*rmp
)->peer_ref
->name
);
1984 *rmp
= (*rmp
)->next
;
1985 free(ignore
->peer_ref
);
1990 rmp
= &((*rmp
)->next
);
1994 tail_link_ref(ref_map
, tail
);
1999 int resolve_remote_symref(struct ref
*ref
, struct ref
*list
)
2003 for (; list
; list
= list
->next
)
2004 if (!strcmp(ref
->symref
, list
->name
)) {
2005 oidcpy(&ref
->old_oid
, &list
->old_oid
);
2012 * Compute the commit ahead/behind values for the pair branch_name, base.
2014 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2015 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2016 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2019 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
2020 * does not exist). Returns 0 if the commits are identical. Returns 1 if
2021 * commits are different.
2024 static int stat_branch_pair(const char *branch_name
, const char *base
,
2025 int *num_ours
, int *num_theirs
,
2026 enum ahead_behind_flags abf
)
2028 struct object_id oid
;
2029 struct commit
*ours
, *theirs
;
2030 struct rev_info revs
;
2031 struct strvec argv
= STRVEC_INIT
;
2033 /* Cannot stat if what we used to build on no longer exists */
2034 if (read_ref(base
, &oid
))
2036 theirs
= lookup_commit_reference(the_repository
, &oid
);
2040 if (read_ref(branch_name
, &oid
))
2042 ours
= lookup_commit_reference(the_repository
, &oid
);
2046 *num_theirs
= *num_ours
= 0;
2048 /* are we the same? */
2051 if (abf
== AHEAD_BEHIND_QUICK
)
2053 if (abf
!= AHEAD_BEHIND_FULL
)
2054 BUG("stat_branch_pair: invalid abf '%d'", abf
);
2056 /* Run "rev-list --left-right ours...theirs" internally... */
2057 strvec_push(&argv
, ""); /* ignored */
2058 strvec_push(&argv
, "--left-right");
2059 strvec_pushf(&argv
, "%s...%s",
2060 oid_to_hex(&ours
->object
.oid
),
2061 oid_to_hex(&theirs
->object
.oid
));
2062 strvec_push(&argv
, "--");
2064 repo_init_revisions(the_repository
, &revs
, NULL
);
2065 setup_revisions(argv
.nr
, argv
.v
, &revs
, NULL
);
2066 if (prepare_revision_walk(&revs
))
2067 die(_("revision walk setup failed"));
2069 /* ... and count the commits on each side. */
2071 struct commit
*c
= get_revision(&revs
);
2074 if (c
->object
.flags
& SYMMETRIC_LEFT
)
2080 /* clear object flags smudged by the above traversal */
2081 clear_commit_marks(ours
, ALL_REV_FLAGS
);
2082 clear_commit_marks(theirs
, ALL_REV_FLAGS
);
2084 strvec_clear(&argv
);
2089 * Lookup the tracking branch for the given branch and if present, optionally
2090 * compute the commit ahead/behind values for the pair.
2092 * If for_push is true, the tracking branch refers to the push branch,
2093 * otherwise it refers to the upstream branch.
2095 * The name of the tracking branch (or NULL if it is not defined) is
2096 * returned via *tracking_name, if it is not itself NULL.
2098 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
2099 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
2100 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
2103 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
2104 * upstream defined, or ref does not exist). Returns 0 if the commits are
2105 * identical. Returns 1 if commits are different.
2107 int stat_tracking_info(struct branch
*branch
, int *num_ours
, int *num_theirs
,
2108 const char **tracking_name
, int for_push
,
2109 enum ahead_behind_flags abf
)
2113 /* Cannot stat unless we are marked to build on top of somebody else. */
2114 base
= for_push
? branch_get_push(branch
, NULL
) :
2115 branch_get_upstream(branch
, NULL
);
2117 *tracking_name
= base
;
2121 return stat_branch_pair(branch
->refname
, base
, num_ours
, num_theirs
, abf
);
2125 * Return true when there is anything to report, otherwise false.
2127 int format_tracking_info(struct branch
*branch
, struct strbuf
*sb
,
2128 enum ahead_behind_flags abf
)
2130 int ours
, theirs
, sti
;
2131 const char *full_base
;
2133 int upstream_is_gone
= 0;
2135 sti
= stat_tracking_info(branch
, &ours
, &theirs
, &full_base
, 0, abf
);
2139 upstream_is_gone
= 1;
2142 base
= shorten_unambiguous_ref(full_base
, 0);
2143 if (upstream_is_gone
) {
2145 _("Your branch is based on '%s', but the upstream is gone.\n"),
2147 if (advice_enabled(ADVICE_STATUS_HINTS
))
2149 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2152 _("Your branch is up to date with '%s'.\n"),
2154 } else if (abf
== AHEAD_BEHIND_QUICK
) {
2156 _("Your branch and '%s' refer to different commits.\n"),
2158 if (advice_enabled(ADVICE_STATUS_HINTS
))
2159 strbuf_addf(sb
, _(" (use \"%s\" for details)\n"),
2160 "git status --ahead-behind");
2161 } else if (!theirs
) {
2163 Q_("Your branch is ahead of '%s' by %d commit.\n",
2164 "Your branch is ahead of '%s' by %d commits.\n",
2167 if (advice_enabled(ADVICE_STATUS_HINTS
))
2169 _(" (use \"git push\" to publish your local commits)\n"));
2172 Q_("Your branch is behind '%s' by %d commit, "
2173 "and can be fast-forwarded.\n",
2174 "Your branch is behind '%s' by %d commits, "
2175 "and can be fast-forwarded.\n",
2178 if (advice_enabled(ADVICE_STATUS_HINTS
))
2180 _(" (use \"git pull\" to update your local branch)\n"));
2183 Q_("Your branch and '%s' have diverged,\n"
2184 "and have %d and %d different commit each, "
2186 "Your branch and '%s' have diverged,\n"
2187 "and have %d and %d different commits each, "
2190 base
, ours
, theirs
);
2191 if (advice_enabled(ADVICE_STATUS_HINTS
))
2193 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
2199 static int one_local_ref(const char *refname
, const struct object_id
*oid
,
2200 int flag
, void *cb_data
)
2202 struct ref
***local_tail
= cb_data
;
2205 /* we already know it starts with refs/ to get here */
2206 if (check_refname_format(refname
+ 5, 0))
2209 ref
= alloc_ref(refname
);
2210 oidcpy(&ref
->new_oid
, oid
);
2212 *local_tail
= &ref
->next
;
2216 struct ref
*get_local_heads(void)
2218 struct ref
*local_refs
= NULL
, **local_tail
= &local_refs
;
2220 for_each_ref(one_local_ref
, &local_tail
);
2224 struct ref
*guess_remote_head(const struct ref
*head
,
2225 const struct ref
*refs
,
2228 const struct ref
*r
;
2229 struct ref
*list
= NULL
;
2230 struct ref
**tail
= &list
;
2236 * Some transports support directly peeking at
2237 * where HEAD points; if that is the case, then
2238 * we don't have to guess.
2241 return copy_ref(find_ref_by_name(refs
, head
->symref
));
2243 /* If a remote branch exists with the default branch name, let's use it. */
2245 char *ref
= xstrfmt("refs/heads/%s",
2246 git_default_branch_name(0));
2248 r
= find_ref_by_name(refs
, ref
);
2250 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2253 /* Fall back to the hard-coded historical default */
2254 r
= find_ref_by_name(refs
, "refs/heads/master");
2255 if (r
&& oideq(&r
->old_oid
, &head
->old_oid
))
2259 /* Look for another ref that points there */
2260 for (r
= refs
; r
; r
= r
->next
) {
2262 starts_with(r
->name
, "refs/heads/") &&
2263 oideq(&r
->old_oid
, &head
->old_oid
)) {
2264 *tail
= copy_ref(r
);
2265 tail
= &((*tail
)->next
);
2274 struct stale_heads_info
{
2275 struct string_list
*ref_names
;
2276 struct ref
**stale_refs_tail
;
2280 static int get_stale_heads_cb(const char *refname
, const struct object_id
*oid
,
2281 int flags
, void *cb_data
)
2283 struct stale_heads_info
*info
= cb_data
;
2284 struct string_list matches
= STRING_LIST_INIT_DUP
;
2285 struct refspec_item query
;
2287 memset(&query
, 0, sizeof(struct refspec_item
));
2288 query
.dst
= (char *)refname
;
2290 query_refspecs_multiple(info
->rs
, &query
, &matches
);
2291 if (matches
.nr
== 0)
2292 goto clean_exit
; /* No matches */
2295 * If we did find a suitable refspec and it's not a symref and
2296 * it's not in the list of refs that currently exist in that
2297 * remote, we consider it to be stale. In order to deal with
2298 * overlapping refspecs, we need to go over all of the
2301 if (flags
& REF_ISSYMREF
)
2304 for (i
= 0; stale
&& i
< matches
.nr
; i
++)
2305 if (string_list_has_string(info
->ref_names
, matches
.items
[i
].string
))
2309 struct ref
*ref
= make_linked_ref(refname
, &info
->stale_refs_tail
);
2310 oidcpy(&ref
->new_oid
, oid
);
2314 string_list_clear(&matches
, 0);
2318 struct ref
*get_stale_heads(struct refspec
*rs
, struct ref
*fetch_map
)
2320 struct ref
*ref
, *stale_refs
= NULL
;
2321 struct string_list ref_names
= STRING_LIST_INIT_NODUP
;
2322 struct stale_heads_info info
;
2324 info
.ref_names
= &ref_names
;
2325 info
.stale_refs_tail
= &stale_refs
;
2327 for (ref
= fetch_map
; ref
; ref
= ref
->next
)
2328 string_list_append(&ref_names
, ref
->name
);
2329 string_list_sort(&ref_names
);
2330 for_each_ref(get_stale_heads_cb
, &info
);
2331 string_list_clear(&ref_names
, 0);
2338 static void clear_cas_option(struct push_cas_option
*cas
)
2342 for (i
= 0; i
< cas
->nr
; i
++)
2343 free(cas
->entry
[i
].refname
);
2345 memset(cas
, 0, sizeof(*cas
));
2348 static struct push_cas
*add_cas_entry(struct push_cas_option
*cas
,
2349 const char *refname
,
2352 struct push_cas
*entry
;
2353 ALLOC_GROW(cas
->entry
, cas
->nr
+ 1, cas
->alloc
);
2354 entry
= &cas
->entry
[cas
->nr
++];
2355 memset(entry
, 0, sizeof(*entry
));
2356 entry
->refname
= xmemdupz(refname
, refnamelen
);
2360 static int parse_push_cas_option(struct push_cas_option
*cas
, const char *arg
, int unset
)
2363 struct push_cas
*entry
;
2366 /* "--no-<option>" */
2367 clear_cas_option(cas
);
2372 /* just "--<option>" */
2373 cas
->use_tracking_for_rest
= 1;
2377 /* "--<option>=refname" or "--<option>=refname:value" */
2378 colon
= strchrnul(arg
, ':');
2379 entry
= add_cas_entry(cas
, arg
, colon
- arg
);
2381 entry
->use_tracking
= 1;
2383 oidclr(&entry
->expect
);
2384 else if (get_oid(colon
+ 1, &entry
->expect
))
2385 return error(_("cannot parse expected object name '%s'"),
2390 int parseopt_push_cas_option(const struct option
*opt
, const char *arg
, int unset
)
2392 return parse_push_cas_option(opt
->value
, arg
, unset
);
2395 int is_empty_cas(const struct push_cas_option
*cas
)
2397 return !cas
->use_tracking_for_rest
&& !cas
->nr
;
2401 * Look at remote.fetch refspec and see if we have a remote
2402 * tracking branch for the refname there. Fill the name of
2403 * the remote-tracking branch in *dst_refname, and the name
2404 * of the commit object at its tip in oid[].
2405 * If we cannot do so, return negative to signal an error.
2407 static int remote_tracking(struct remote
*remote
, const char *refname
,
2408 struct object_id
*oid
, char **dst_refname
)
2412 dst
= apply_refspecs(&remote
->fetch
, refname
);
2414 return -1; /* no tracking ref for refname at remote */
2415 if (read_ref(dst
, oid
))
2416 return -1; /* we know what the tracking ref is but we cannot read it */
2423 * The struct "reflog_commit_array" and related helper functions
2424 * are used for collecting commits into an array during reflog
2425 * traversals in "check_and_collect_until()".
2427 struct reflog_commit_array
{
2428 struct commit
**item
;
2432 #define REFLOG_COMMIT_ARRAY_INIT { 0 }
2434 /* Append a commit to the array. */
2435 static void append_commit(struct reflog_commit_array
*arr
,
2436 struct commit
*commit
)
2438 ALLOC_GROW(arr
->item
, arr
->nr
+ 1, arr
->alloc
);
2439 arr
->item
[arr
->nr
++] = commit
;
2442 /* Free and reset the array. */
2443 static void free_commit_array(struct reflog_commit_array
*arr
)
2445 FREE_AND_NULL(arr
->item
);
2446 arr
->nr
= arr
->alloc
= 0;
2449 struct check_and_collect_until_cb_data
{
2450 struct commit
*remote_commit
;
2451 struct reflog_commit_array
*local_commits
;
2452 timestamp_t remote_reflog_timestamp
;
2455 /* Get the timestamp of the latest entry. */
2456 static int peek_reflog(struct object_id
*o_oid
, struct object_id
*n_oid
,
2457 const char *ident
, timestamp_t timestamp
,
2458 int tz
, const char *message
, void *cb_data
)
2460 timestamp_t
*ts
= cb_data
;
2465 static int check_and_collect_until(struct object_id
*o_oid
,
2466 struct object_id
*n_oid
,
2467 const char *ident
, timestamp_t timestamp
,
2468 int tz
, const char *message
, void *cb_data
)
2470 struct commit
*commit
;
2471 struct check_and_collect_until_cb_data
*cb
= cb_data
;
2473 /* An entry was found. */
2474 if (oideq(n_oid
, &cb
->remote_commit
->object
.oid
))
2477 if ((commit
= lookup_commit_reference(the_repository
, n_oid
)))
2478 append_commit(cb
->local_commits
, commit
);
2481 * If the reflog entry timestamp is older than the remote ref's
2482 * latest reflog entry, there is no need to check or collect
2483 * entries older than this one.
2485 if (timestamp
< cb
->remote_reflog_timestamp
)
2491 #define MERGE_BASES_BATCH_SIZE 8
2494 * Iterate through the reflog of the local ref to check if there is an entry
2495 * for the given remote-tracking ref; runs until the timestamp of an entry is
2496 * older than latest timestamp of remote-tracking ref's reflog. Any commits
2497 * are that seen along the way are collected into an array to check if the
2498 * remote-tracking ref is reachable from any of them.
2500 static int is_reachable_in_reflog(const char *local
, const struct ref
*remote
)
2503 struct commit
*commit
;
2504 struct commit
**chunk
;
2505 struct check_and_collect_until_cb_data cb
;
2506 struct reflog_commit_array arr
= REFLOG_COMMIT_ARRAY_INIT
;
2510 commit
= lookup_commit_reference(the_repository
, &remote
->old_oid
);
2512 goto cleanup_return
;
2515 * Get the timestamp from the latest entry
2516 * of the remote-tracking ref's reflog.
2518 for_each_reflog_ent_reverse(remote
->tracking_ref
, peek_reflog
, &date
);
2520 cb
.remote_commit
= commit
;
2521 cb
.local_commits
= &arr
;
2522 cb
.remote_reflog_timestamp
= date
;
2523 ret
= for_each_reflog_ent_reverse(local
, check_and_collect_until
, &cb
);
2525 /* We found an entry in the reflog. */
2527 goto cleanup_return
;
2530 * Check if the remote commit is reachable from any
2531 * of the commits in the collected array, in batches.
2533 for (chunk
= arr
.item
; chunk
< arr
.item
+ arr
.nr
; chunk
+= size
) {
2534 size
= arr
.item
+ arr
.nr
- chunk
;
2535 if (MERGE_BASES_BATCH_SIZE
< size
)
2536 size
= MERGE_BASES_BATCH_SIZE
;
2538 if ((ret
= in_merge_bases_many(commit
, size
, chunk
)))
2543 free_commit_array(&arr
);
2548 * Check for reachability of a remote-tracking
2549 * ref in the reflog entries of its local ref.
2551 static void check_if_includes_upstream(struct ref
*remote
)
2553 struct ref
*local
= get_local_ref(remote
->name
);
2557 if (is_reachable_in_reflog(local
->name
, remote
) <= 0)
2558 remote
->unreachable
= 1;
2561 static void apply_cas(struct push_cas_option
*cas
,
2562 struct remote
*remote
,
2567 /* Find an explicit --<option>=<name>[:<value>] entry */
2568 for (i
= 0; i
< cas
->nr
; i
++) {
2569 struct push_cas
*entry
= &cas
->entry
[i
];
2570 if (!refname_match(entry
->refname
, ref
->name
))
2572 ref
->expect_old_sha1
= 1;
2573 if (!entry
->use_tracking
)
2574 oidcpy(&ref
->old_oid_expect
, &entry
->expect
);
2575 else if (remote_tracking(remote
, ref
->name
,
2576 &ref
->old_oid_expect
,
2577 &ref
->tracking_ref
))
2578 oidclr(&ref
->old_oid_expect
);
2580 ref
->check_reachable
= cas
->use_force_if_includes
;
2584 /* Are we using "--<option>" to cover all? */
2585 if (!cas
->use_tracking_for_rest
)
2588 ref
->expect_old_sha1
= 1;
2589 if (remote_tracking(remote
, ref
->name
,
2590 &ref
->old_oid_expect
,
2591 &ref
->tracking_ref
))
2592 oidclr(&ref
->old_oid_expect
);
2594 ref
->check_reachable
= cas
->use_force_if_includes
;
2597 void apply_push_cas(struct push_cas_option
*cas
,
2598 struct remote
*remote
,
2599 struct ref
*remote_refs
)
2602 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
2603 apply_cas(cas
, remote
, ref
);
2606 * If "compare-and-swap" is in "use_tracking[_for_rest]"
2607 * mode, and if "--force-if-includes" was specified, run
2610 if (ref
->check_reachable
)
2611 check_if_includes_upstream(ref
);
2615 struct remote_state
*remote_state_new(void)
2617 struct remote_state
*r
= xmalloc(sizeof(*r
));
2619 memset(r
, 0, sizeof(*r
));
2621 hashmap_init(&r
->remotes_hash
, remotes_hash_cmp
, NULL
, 0);
2625 void remote_state_clear(struct remote_state
*remote_state
)
2629 for (i
= 0; i
< remote_state
->remotes_nr
; i
++) {
2630 remote_clear(remote_state
->remotes
[i
]);
2632 FREE_AND_NULL(remote_state
->remotes
);
2633 remote_state
->remotes_alloc
= 0;
2634 remote_state
->remotes_nr
= 0;
2636 hashmap_clear_and_free(&remote_state
->remotes_hash
, struct remote
, ent
);
2638 for (i
= 0; i
< remote_state
->branches_nr
; i
++) {
2639 FREE_AND_NULL(remote_state
->branches
[i
]);
2641 FREE_AND_NULL(remote_state
->branches
);
2642 remote_state
->branches_alloc
= 0;
2643 remote_state
->branches_nr
= 0;