refspec: rename struct refspec to struct refspec_item
[git/git.git] / remote.c
CommitLineData
5751f490 1#include "cache.h"
b2141fc1 2#include "config.h"
5751f490
DB
3#include "remote.h"
4#include "refs.h"
ec0cb496 5#include "refspec.h"
6d21bf96
JH
6#include "commit.h"
7#include "diff.h"
8#include "revision.h"
8ca12c0d 9#include "dir.h"
ec8452d5 10#include "tag.h"
73cf0822 11#include "string-list.h"
ed81c76b 12#include "mergesort.h"
0b282cc4 13#include "argv-array.h"
5751f490 14
6ddba5e2
FC
15enum map_direction { FROM_SRC, FROM_DST };
16
844112ca
JH
17struct counted_string {
18 size_t len;
19 const char *s;
20};
55029ae4
DB
21struct rewrite {
22 const char *base;
844112ca
JH
23 size_t baselen;
24 struct counted_string *instead_of;
55029ae4
DB
25 int instead_of_nr;
26 int instead_of_alloc;
27};
d071d942
JT
28struct rewrites {
29 struct rewrite **rewrite;
30 int rewrite_alloc;
31 int rewrite_nr;
32};
55029ae4 33
5751f490 34static struct remote **remotes;
2d31347b
DB
35static int remotes_alloc;
36static int remotes_nr;
d0da003d 37static struct hashmap remotes_hash;
5751f490 38
cf818348 39static struct branch **branches;
2d31347b
DB
40static int branches_alloc;
41static int branches_nr;
cf818348
DB
42
43static struct branch *current_branch;
f24f715e 44static const char *pushremote_name;
cf818348 45
d071d942 46static struct rewrites rewrites;
1c2eafb8 47static struct rewrites rewrites_push;
55029ae4 48
0a4da29d
DB
49static int valid_remote(const struct remote *remote)
50{
c578f51d 51 return (!!remote->url) || (!!remote->foreign_vcs);
0a4da29d
DB
52}
53
d071d942 54static const char *alias_url(const char *url, struct rewrites *r)
55029ae4
DB
55{
56 int i, j;
844112ca
JH
57 struct counted_string *longest;
58 int longest_i;
59
60 longest = NULL;
61 longest_i = -1;
d071d942
JT
62 for (i = 0; i < r->rewrite_nr; i++) {
63 if (!r->rewrite[i])
55029ae4 64 continue;
d071d942 65 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
59556548 66 if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
844112ca 67 (!longest ||
d071d942
JT
68 longest->len < r->rewrite[i]->instead_of[j].len)) {
69 longest = &(r->rewrite[i]->instead_of[j]);
844112ca 70 longest_i = i;
55029ae4
DB
71 }
72 }
73 }
844112ca
JH
74 if (!longest)
75 return url;
76
75faa45a 77 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
55029ae4
DB
78}
79
5751f490
DB
80static void add_push_refspec(struct remote *remote, const char *ref)
81{
2d31347b
DB
82 ALLOC_GROW(remote->push_refspec,
83 remote->push_refspec_nr + 1,
84 remote->push_refspec_alloc);
85 remote->push_refspec[remote->push_refspec_nr++] = ref;
5751f490
DB
86}
87
5d46c9d4
DB
88static void add_fetch_refspec(struct remote *remote, const char *ref)
89{
2d31347b
DB
90 ALLOC_GROW(remote->fetch_refspec,
91 remote->fetch_refspec_nr + 1,
92 remote->fetch_refspec_alloc);
93 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
5d46c9d4
DB
94}
95
97716d21
ÆAB
96void add_prune_tags_to_fetch_refspec(struct remote *remote)
97{
98 int nr = remote->fetch_refspec_nr;
99 int bufsize = nr + 1;
0ad4a5ff 100 int size = sizeof(struct refspec_item);
97716d21
ÆAB
101
102 remote->fetch = xrealloc(remote->fetch, size * bufsize);
103 memcpy(&remote->fetch[nr], tag_refspec, size);
104 add_fetch_refspec(remote, xstrdup(TAG_REFSPEC));
105}
106
28b91f8a 107static void add_url(struct remote *remote, const char *url)
5751f490 108{
2d31347b
DB
109 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
110 remote->url[remote->url_nr++] = url;
5751f490
DB
111}
112
20346234
MG
113static void add_pushurl(struct remote *remote, const char *pushurl)
114{
115 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
116 remote->pushurl[remote->pushurl_nr++] = pushurl;
117}
118
1c2eafb8
JT
119static void add_pushurl_alias(struct remote *remote, const char *url)
120{
121 const char *pushurl = alias_url(url, &rewrites_push);
122 if (pushurl != url)
123 add_pushurl(remote, pushurl);
124}
125
126static void add_url_alias(struct remote *remote, const char *url)
127{
128 add_url(remote, alias_url(url, &rewrites));
129 add_pushurl_alias(remote, url);
130}
131
d0da003d
PR
132struct remotes_hash_key {
133 const char *str;
134 int len;
135};
136
7663cdc8 137static int remotes_hash_cmp(const void *unused_cmp_data,
45dcb35f
SB
138 const void *entry,
139 const void *entry_or_key,
140 const void *keydata)
d0da003d 141{
45dcb35f
SB
142 const struct remote *a = entry;
143 const struct remote *b = entry_or_key;
144 const struct remotes_hash_key *key = keydata;
145
d0da003d
PR
146 if (key)
147 return strncmp(a->name, key->str, key->len) || a->name[key->len];
148 else
149 return strcmp(a->name, b->name);
150}
151
152static inline void init_remotes_hash(void)
153{
154 if (!remotes_hash.cmpfn)
45dcb35f 155 hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
d0da003d
PR
156}
157
5751f490
DB
158static struct remote *make_remote(const char *name, int len)
159{
d0da003d
PR
160 struct remote *ret, *replaced;
161 struct remotes_hash_key lookup;
162 struct hashmap_entry lookup_entry;
5751f490 163
d0da003d
PR
164 if (!len)
165 len = strlen(name);
166
167 init_remotes_hash();
168 lookup.str = name;
169 lookup.len = len;
170 hashmap_entry_init(&lookup_entry, memhash(name, len));
171
172 if ((ret = hashmap_get(&remotes_hash, &lookup_entry, &lookup)) != NULL)
173 return ret;
5751f490 174
2d31347b 175 ret = xcalloc(1, sizeof(struct remote));
737c5a9c 176 ret->prune = -1; /* unspecified */
97716d21 177 ret->prune_tags = -1; /* unspecified */
2d31347b
DB
178 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
179 remotes[remotes_nr++] = ret;
d0da003d
PR
180 ret->name = xstrndup(name, len);
181
182 hashmap_entry_init(ret, lookup_entry.hash);
183 replaced = hashmap_put(&remotes_hash, ret);
184 assert(replaced == NULL); /* no previous entry overwritten */
2d31347b 185 return ret;
5751f490
DB
186}
187
cf818348
DB
188static void add_merge(struct branch *branch, const char *name)
189{
2d31347b
DB
190 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
191 branch->merge_alloc);
192 branch->merge_name[branch->merge_nr++] = name;
cf818348
DB
193}
194
195static struct branch *make_branch(const char *name, int len)
196{
2d31347b
DB
197 struct branch *ret;
198 int i;
cf818348 199
2d31347b
DB
200 for (i = 0; i < branches_nr; i++) {
201 if (len ? (!strncmp(name, branches[i]->name, len) &&
202 !branches[i]->name[len]) :
203 !strcmp(name, branches[i]->name))
204 return branches[i];
cf818348
DB
205 }
206
2d31347b
DB
207 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
208 ret = xcalloc(1, sizeof(struct branch));
209 branches[branches_nr++] = ret;
cf818348 210 if (len)
2d31347b 211 ret->name = xstrndup(name, len);
cf818348 212 else
2d31347b 213 ret->name = xstrdup(name);
fa3f60b7 214 ret->refname = xstrfmt("refs/heads/%s", ret->name);
cf818348 215
2d31347b 216 return ret;
cf818348
DB
217}
218
d071d942 219static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
55029ae4
DB
220{
221 struct rewrite *ret;
222 int i;
223
d071d942 224 for (i = 0; i < r->rewrite_nr; i++) {
844112ca 225 if (len
d071d942
JT
226 ? (len == r->rewrite[i]->baselen &&
227 !strncmp(base, r->rewrite[i]->base, len))
228 : !strcmp(base, r->rewrite[i]->base))
229 return r->rewrite[i];
55029ae4
DB
230 }
231
d071d942 232 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
55029ae4 233 ret = xcalloc(1, sizeof(struct rewrite));
d071d942 234 r->rewrite[r->rewrite_nr++] = ret;
844112ca 235 if (len) {
55029ae4 236 ret->base = xstrndup(base, len);
844112ca
JH
237 ret->baselen = len;
238 }
239 else {
55029ae4 240 ret->base = xstrdup(base);
844112ca
JH
241 ret->baselen = strlen(base);
242 }
55029ae4
DB
243 return ret;
244}
245
246static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
247{
248 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
844112ca
JH
249 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
250 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
251 rewrite->instead_of_nr++;
55029ae4
DB
252}
253
0e265a92
JK
254static const char *skip_spaces(const char *s)
255{
256 while (isspace(*s))
257 s++;
258 return s;
259}
260
5751f490
DB
261static void read_remotes_file(struct remote *remote)
262{
0e265a92 263 struct strbuf buf = STRBUF_INIT;
e9d983f1 264 FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
5751f490
DB
265
266 if (!f)
267 return;
e459b073 268 remote->configured_in_repo = 1;
89cf4c70 269 remote->origin = REMOTE_REMOTES;
18814d0e 270 while (strbuf_getline(&buf, f) != EOF) {
0e265a92 271 const char *v;
5751f490 272
0e265a92 273 strbuf_rtrim(&buf);
5751f490 274
0e265a92
JK
275 if (skip_prefix(buf.buf, "URL:", &v))
276 add_url_alias(remote, xstrdup(skip_spaces(v)));
277 else if (skip_prefix(buf.buf, "Push:", &v))
278 add_push_refspec(remote, xstrdup(skip_spaces(v)));
279 else if (skip_prefix(buf.buf, "Pull:", &v))
280 add_fetch_refspec(remote, xstrdup(skip_spaces(v)));
5751f490 281 }
0e265a92 282 strbuf_release(&buf);
5751f490
DB
283 fclose(f);
284}
285
286static void read_branches_file(struct remote *remote)
287{
cf818348 288 char *frag;
f28e3ab2 289 struct strbuf buf = STRBUF_INIT;
e9d983f1 290 FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
5751f490
DB
291
292 if (!f)
293 return;
f28e3ab2 294
8f309aeb 295 strbuf_getline_lf(&buf, f);
0fb19906 296 fclose(f);
f28e3ab2
JK
297 strbuf_trim(&buf);
298 if (!buf.len) {
299 strbuf_release(&buf);
5751f490 300 return;
f28e3ab2
JK
301 }
302
e459b073 303 remote->configured_in_repo = 1;
89cf4c70 304 remote->origin = REMOTE_BRANCHES;
472fa4cd
DB
305
306 /*
55cfde25 307 * The branches file would have URL and optionally
472fa4cd 308 * #branch specified. The "master" (or specified) branch is
f28e3ab2
JK
309 * fetched and stored in the local branch matching the
310 * remote name.
472fa4cd 311 */
f28e3ab2
JK
312 frag = strchr(buf.buf, '#');
313 if (frag)
cf818348 314 *(frag++) = '\0';
f28e3ab2
JK
315 else
316 frag = "master";
317
318 add_url_alias(remote, strbuf_detach(&buf, NULL));
319 add_fetch_refspec(remote, xstrfmt("refs/heads/%s:refs/heads/%s",
320 frag, remote->name));
55cfde25 321
18afe101
MK
322 /*
323 * Cogito compatible push: push current HEAD to remote #branch
324 * (master if missing)
325 */
f28e3ab2 326 add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag));
d71ab174 327 remote->fetch_tags = 1; /* always auto-follow */
5751f490
DB
328}
329
ef90d6d4 330static int handle_config(const char *key, const char *value, void *cb)
5751f490
DB
331{
332 const char *name;
bc60f8a7 333 int namelen;
5751f490
DB
334 const char *subkey;
335 struct remote *remote;
cf818348 336 struct branch *branch;
bc60f8a7
TG
337 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
338 if (!name)
cf818348 339 return 0;
bc60f8a7
TG
340 branch = make_branch(name, namelen);
341 if (!strcmp(subkey, "remote")) {
e41bf352 342 return git_config_string(&branch->remote_name, key, value);
bc60f8a7 343 } else if (!strcmp(subkey, "pushremote")) {
da66b274 344 return git_config_string(&branch->pushremote_name, key, value);
bc60f8a7 345 } else if (!strcmp(subkey, "merge")) {
d2370cc2
JH
346 if (!value)
347 return config_error_nonbool(key);
cf818348 348 add_merge(branch, xstrdup(value));
d2370cc2 349 }
cf818348 350 return 0;
5751f490 351 }
bc60f8a7 352 if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
55029ae4 353 struct rewrite *rewrite;
bc60f8a7 354 if (!name)
55029ae4 355 return 0;
bc60f8a7
TG
356 if (!strcmp(subkey, "insteadof")) {
357 rewrite = make_rewrite(&rewrites, name, namelen);
1c2eafb8
JT
358 if (!value)
359 return config_error_nonbool(key);
360 add_instead_of(rewrite, xstrdup(value));
bc60f8a7
TG
361 } else if (!strcmp(subkey, "pushinsteadof")) {
362 rewrite = make_rewrite(&rewrites_push, name, namelen);
55029ae4
DB
363 if (!value)
364 return config_error_nonbool(key);
365 add_instead_of(rewrite, xstrdup(value));
366 }
367 }
224c2171 368
bc60f8a7 369 if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
5751f490 370 return 0;
224c2171
RR
371
372 /* Handle remote.* variables */
bc60f8a7 373 if (!name && !strcmp(subkey, "pushdefault"))
224c2171
RR
374 return git_config_string(&pushremote_name, key, value);
375
bc60f8a7
TG
376 if (!name)
377 return 0;
224c2171 378 /* Handle remote.<name>.* variables */
c82efafc
BC
379 if (*name == '/') {
380 warning("Config remote shorthand cannot begin with '/': %s",
381 name);
382 return 0;
383 }
bc60f8a7 384 remote = make_remote(name, namelen);
89cf4c70 385 remote->origin = REMOTE_CONFIG;
e459b073
JS
386 if (current_config_scope() == CONFIG_SCOPE_REPO)
387 remote->configured_in_repo = 1;
bc60f8a7 388 if (!strcmp(subkey, "mirror"))
84bb2dfd 389 remote->mirror = git_config_bool(key, value);
bc60f8a7 390 else if (!strcmp(subkey, "skipdefaultupdate"))
84bb2dfd 391 remote->skip_default_update = git_config_bool(key, value);
bc60f8a7 392 else if (!strcmp(subkey, "skipfetchall"))
7cc91a2f 393 remote->skip_default_update = git_config_bool(key, value);
bc60f8a7 394 else if (!strcmp(subkey, "prune"))
737c5a9c 395 remote->prune = git_config_bool(key, value);
97716d21
ÆAB
396 else if (!strcmp(subkey, "prunetags"))
397 remote->prune_tags = git_config_bool(key, value);
bc60f8a7 398 else if (!strcmp(subkey, "url")) {
84bb2dfd
PB
399 const char *v;
400 if (git_config_string(&v, key, value))
401 return -1;
402 add_url(remote, v);
bc60f8a7 403 } else if (!strcmp(subkey, "pushurl")) {
20346234
MG
404 const char *v;
405 if (git_config_string(&v, key, value))
406 return -1;
407 add_pushurl(remote, v);
bc60f8a7 408 } else if (!strcmp(subkey, "push")) {
84bb2dfd
PB
409 const char *v;
410 if (git_config_string(&v, key, value))
411 return -1;
412 add_push_refspec(remote, v);
bc60f8a7 413 } else if (!strcmp(subkey, "fetch")) {
84bb2dfd
PB
414 const char *v;
415 if (git_config_string(&v, key, value))
416 return -1;
417 add_fetch_refspec(remote, v);
bc60f8a7 418 } else if (!strcmp(subkey, "receivepack")) {
84bb2dfd
PB
419 const char *v;
420 if (git_config_string(&v, key, value))
421 return -1;
5751f490 422 if (!remote->receivepack)
84bb2dfd 423 remote->receivepack = v;
5751f490
DB
424 else
425 error("more than one receivepack given, using the first");
bc60f8a7 426 } else if (!strcmp(subkey, "uploadpack")) {
84bb2dfd
PB
427 const char *v;
428 if (git_config_string(&v, key, value))
429 return -1;
0012ba21 430 if (!remote->uploadpack)
84bb2dfd 431 remote->uploadpack = v;
0012ba21
DB
432 else
433 error("more than one uploadpack given, using the first");
bc60f8a7 434 } else if (!strcmp(subkey, "tagopt")) {
d71ab174
DB
435 if (!strcmp(value, "--no-tags"))
436 remote->fetch_tags = -1;
944163a4
ST
437 else if (!strcmp(value, "--tags"))
438 remote->fetch_tags = 2;
bc60f8a7 439 } else if (!strcmp(subkey, "proxy")) {
84bb2dfd
PB
440 return git_config_string((const char **)&remote->http_proxy,
441 key, value);
bc60f8a7 442 } else if (!strcmp(subkey, "proxyauthmethod")) {
ef976395
KF
443 return git_config_string((const char **)&remote->http_proxy_authmethod,
444 key, value);
bc60f8a7 445 } else if (!strcmp(subkey, "vcs")) {
c578f51d 446 return git_config_string(&remote->foreign_vcs, key, value);
84bb2dfd 447 }
5751f490
DB
448 return 0;
449}
450
55029ae4
DB
451static void alias_all_urls(void)
452{
453 int i, j;
454 for (i = 0; i < remotes_nr; i++) {
1c2eafb8 455 int add_pushurl_aliases;
55029ae4
DB
456 if (!remotes[i])
457 continue;
20346234 458 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
d071d942 459 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
20346234 460 }
1c2eafb8
JT
461 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
462 for (j = 0; j < remotes[i]->url_nr; j++) {
463 if (add_pushurl_aliases)
464 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
465 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
466 }
55029ae4
DB
467 }
468}
469
5751f490
DB
470static void read_config(void)
471{
e41bf352 472 static int loaded;
5751f490 473 int flag;
e41bf352
JK
474
475 if (loaded)
5751f490 476 return;
e41bf352
JK
477 loaded = 1;
478
5751f490 479 current_branch = NULL;
f2f12d16 480 if (startup_info->have_repository) {
744c040b 481 const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
f2f12d16
JK
482 if (head_ref && (flag & REF_ISSYMREF) &&
483 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
484 current_branch = make_branch(head_ref, 0);
485 }
5751f490 486 }
ef90d6d4 487 git_config(handle_config, NULL);
55029ae4 488 alias_all_urls();
5751f490
DB
489}
490
df93e33c
DB
491static int valid_remote_nick(const char *name)
492{
8ca12c0d 493 if (!name[0] || is_dot_or_dotdot(name))
df93e33c 494 return 0;
d9244ecf
JS
495
496 /* remote nicknames cannot contain slashes */
497 while (*name)
498 if (is_dir_sep(*name++))
499 return 0;
500 return 1;
df93e33c
DB
501}
502
f052154d
JK
503const char *remote_for_branch(struct branch *branch, int *explicit)
504{
505 if (branch && branch->remote_name) {
506 if (explicit)
507 *explicit = 1;
508 return branch->remote_name;
509 }
510 if (explicit)
511 *explicit = 0;
512 return "origin";
513}
514
da66b274
JK
515const char *pushremote_for_branch(struct branch *branch, int *explicit)
516{
517 if (branch && branch->pushremote_name) {
518 if (explicit)
519 *explicit = 1;
520 return branch->pushremote_name;
521 }
522 if (pushremote_name) {
523 if (explicit)
524 *explicit = 1;
525 return pushremote_name;
526 }
527 return remote_for_branch(branch, explicit);
528}
529
9700fae5
W
530const char *remote_ref_for_branch(struct branch *branch, int for_push,
531 int *explicit)
532{
533 if (branch) {
534 if (!for_push) {
535 if (branch->merge_nr) {
536 if (explicit)
537 *explicit = 1;
538 return branch->merge_name[0];
539 }
540 } else {
541 const char *dst, *remote_name =
542 pushremote_for_branch(branch, NULL);
543 struct remote *remote = remote_get(remote_name);
544
545 if (remote && remote->push_refspec_nr &&
546 (dst = apply_refspecs(remote->push,
547 remote->push_refspec_nr,
548 branch->refname))) {
549 if (explicit)
550 *explicit = 1;
551 return dst;
552 }
553 }
554 }
555 if (explicit)
556 *explicit = 0;
557 return "";
558}
559
da66b274
JK
560static struct remote *remote_get_1(const char *name,
561 const char *(*get_default)(struct branch *, int *))
5751f490
DB
562{
563 struct remote *ret;
fa685bdf 564 int name_given = 0;
5751f490 565
8770e6fb
JK
566 read_config();
567
fa685bdf
DB
568 if (name)
569 name_given = 1;
da66b274
JK
570 else
571 name = get_default(current_branch, &name_given);
9326d494 572
5751f490 573 ret = make_remote(name, 0);
4539c218 574 if (valid_remote_nick(name) && have_git_dir()) {
0a4da29d 575 if (!valid_remote(ret))
5751f490 576 read_remotes_file(ret);
0a4da29d 577 if (!valid_remote(ret))
5751f490
DB
578 read_branches_file(ret);
579 }
0a4da29d 580 if (name_given && !valid_remote(ret))
55029ae4 581 add_url_alias(ret, name);
0a4da29d 582 if (!valid_remote(ret))
5751f490 583 return NULL;
46220ca1
JH
584 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
585 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
5751f490
DB
586 return ret;
587}
6b62816c 588
f24f715e
RR
589struct remote *remote_get(const char *name)
590{
da66b274 591 return remote_get_1(name, remote_for_branch);
f24f715e
RR
592}
593
594struct remote *pushremote_get(const char *name)
595{
da66b274 596 return remote_get_1(name, pushremote_for_branch);
f24f715e
RR
597}
598
e459b073 599int remote_is_configured(struct remote *remote, int in_repo)
9a23ba33 600{
e459b073
JS
601 if (!remote)
602 return 0;
603 if (in_repo)
604 return remote->configured_in_repo;
605 return !!remote->origin;
9a23ba33
FAG
606}
607
b42f6927
JS
608int for_each_remote(each_remote_fn fn, void *priv)
609{
610 int i, result = 0;
611 read_config();
2d31347b 612 for (i = 0; i < remotes_nr && !result; i++) {
b42f6927
JS
613 struct remote *r = remotes[i];
614 if (!r)
615 continue;
616 if (!r->fetch)
46220ca1
JH
617 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
618 r->fetch_refspec);
b42f6927 619 if (!r->push)
46220ca1
JH
620 r->push = parse_push_refspec(r->push_refspec_nr,
621 r->push_refspec);
b42f6927
JS
622 result = fn(r, priv);
623 }
624 return result;
625}
626
df02ebda
MH
627static void handle_duplicate(struct ref *ref1, struct ref *ref2)
628{
f096e6e8
MH
629 if (strcmp(ref1->name, ref2->name)) {
630 if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
631 ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
632 die(_("Cannot fetch both %s and %s to %s"),
633 ref1->name, ref2->name, ref2->peer_ref->name);
634 } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
635 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
636 warning(_("%s usually tracks %s, not %s"),
637 ref2->peer_ref->name, ref2->name, ref1->name);
638 } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
639 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
640 die(_("%s tracks both %s and %s"),
641 ref2->peer_ref->name, ref1->name, ref2->name);
642 } else {
643 /*
644 * This last possibility doesn't occur because
645 * FETCH_HEAD_IGNORE entries always appear at
646 * the end of the list.
647 */
648 die(_("Internal error"));
649 }
650 }
df02ebda
MH
651 free(ref2->peer_ref);
652 free(ref2);
653}
654
b9afe665 655struct ref *ref_remove_duplicates(struct ref *ref_map)
2467a4fa 656{
183113a5 657 struct string_list refs = STRING_LIST_INIT_NODUP;
b9afe665
MH
658 struct ref *retval = NULL;
659 struct ref **p = &retval;
73cf0822 660
b9afe665
MH
661 while (ref_map) {
662 struct ref *ref = ref_map;
663
664 ref_map = ref_map->next;
665 ref->next = NULL;
73cf0822 666
b9afe665
MH
667 if (!ref->peer_ref) {
668 *p = ref;
669 p = &ref->next;
09ea1f8e 670 } else {
b9afe665
MH
671 struct string_list_item *item =
672 string_list_insert(&refs, ref->peer_ref->name);
673
674 if (item->util) {
675 /* Entry already existed */
df02ebda 676 handle_duplicate((struct ref *)item->util, ref);
b9afe665
MH
677 } else {
678 *p = ref;
679 p = &ref->next;
680 item->util = ref;
681 }
2467a4fa
DB
682 }
683 }
b9afe665 684
73cf0822 685 string_list_clear(&refs, 0);
b9afe665 686 return retval;
2467a4fa
DB
687}
688
28b91f8a 689int remote_has_url(struct remote *remote, const char *url)
5d46c9d4
DB
690{
691 int i;
28b91f8a
SP
692 for (i = 0; i < remote->url_nr; i++) {
693 if (!strcmp(remote->url[i], url))
5d46c9d4
DB
694 return 1;
695 }
696 return 0;
697}
698
e928213f
DB
699static int match_name_with_pattern(const char *key, const char *name,
700 const char *value, char **result)
a3c84239 701{
08fbdb30
DB
702 const char *kstar = strchr(key, '*');
703 size_t klen;
abd2bde7
DB
704 size_t ksuffixlen;
705 size_t namelen;
08fbdb30
DB
706 int ret;
707 if (!kstar)
708 die("Key '%s' of pattern had no '*'", key);
709 klen = kstar - key;
abd2bde7
DB
710 ksuffixlen = strlen(kstar + 1);
711 namelen = strlen(name);
712 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
713 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
e928213f 714 if (ret && value) {
07bfa575 715 struct strbuf sb = STRBUF_INIT;
08fbdb30 716 const char *vstar = strchr(value, '*');
08fbdb30
DB
717 if (!vstar)
718 die("Value '%s' of pattern has no '*'", value);
07bfa575
RS
719 strbuf_add(&sb, value, vstar - value);
720 strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
721 strbuf_addstr(&sb, vstar + 1);
722 *result = strbuf_detach(&sb, NULL);
e928213f 723 }
a3c84239
DB
724 return ret;
725}
726
0ad4a5ff 727static void query_refspecs_multiple(struct refspec_item *refs, int ref_count, struct refspec_item *query, struct string_list *results)
e6f63712
CMN
728{
729 int i;
730 int find_src = !query->src;
731
732 if (find_src && !query->dst)
733 error("query_refspecs_multiple: need either src or dst");
734
735 for (i = 0; i < ref_count; i++) {
0ad4a5ff 736 struct refspec_item *refspec = &refs[i];
e6f63712
CMN
737 const char *key = find_src ? refspec->dst : refspec->src;
738 const char *value = find_src ? refspec->src : refspec->dst;
739 const char *needle = find_src ? query->dst : query->src;
740 char **result = find_src ? &query->src : &query->dst;
741
742 if (!refspec->dst)
743 continue;
744 if (refspec->pattern) {
745 if (match_name_with_pattern(key, needle, value, result))
746 string_list_append_nodup(results, *result);
747 } else if (!strcmp(needle, key)) {
748 string_list_append(results, value);
749 }
750 }
751}
752
0ad4a5ff 753int query_refspecs(struct refspec_item *refs, int ref_count, struct refspec_item *query)
72ff8943
DB
754{
755 int i;
c500352e 756 int find_src = !query->src;
049bff8f
MH
757 const char *needle = find_src ? query->dst : query->src;
758 char **result = find_src ? &query->src : &query->dst;
72ff8943 759
c500352e
CMN
760 if (find_src && !query->dst)
761 return error("query_refspecs: need either src or dst");
b42f6927 762
c500352e 763 for (i = 0; i < ref_count; i++) {
0ad4a5ff 764 struct refspec_item *refspec = &refs[i];
c500352e
CMN
765 const char *key = find_src ? refspec->dst : refspec->src;
766 const char *value = find_src ? refspec->src : refspec->dst;
b42f6927 767
c500352e 768 if (!refspec->dst)
5d46c9d4 769 continue;
c500352e 770 if (refspec->pattern) {
e928213f 771 if (match_name_with_pattern(key, needle, value, result)) {
c500352e 772 query->force = refspec->force;
5d46c9d4
DB
773 return 0;
774 }
b42f6927
JS
775 } else if (!strcmp(needle, key)) {
776 *result = xstrdup(value);
c500352e 777 query->force = refspec->force;
b42f6927 778 return 0;
5d46c9d4
DB
779 }
780 }
5d46c9d4
DB
781 return -1;
782}
783
0ad4a5ff 784char *apply_refspecs(struct refspec_item *refspecs, int nr_refspec,
c500352e
CMN
785 const char *name)
786{
0ad4a5ff 787 struct refspec_item query;
c500352e 788
0ad4a5ff 789 memset(&query, 0, sizeof(struct refspec_item));
c500352e
CMN
790 query.src = (char *)name;
791
792 if (query_refspecs(refspecs, nr_refspec, &query))
793 return NULL;
794
795 return query.dst;
796}
797
0ad4a5ff 798int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
c500352e
CMN
799{
800 return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
801}
802
8009768e
RS
803static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
804 const char *name)
805{
806 size_t len = strlen(name);
50a6c8ef 807 struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
8009768e
RS
808 memcpy(ref->name, prefix, prefixlen);
809 memcpy(ref->name + prefixlen, name, len);
810 return ref;
811}
812
59c69c0c 813struct ref *alloc_ref(const char *name)
dfd255dd 814{
59c69c0c 815 return alloc_ref_with_prefix("", 0, name);
737922aa
KK
816}
817
59a57757 818struct ref *copy_ref(const struct ref *ref)
d71ab174 819{
7b3db095
JS
820 struct ref *cpy;
821 size_t len;
822 if (!ref)
823 return NULL;
50a6c8ef
JK
824 len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
825 cpy = xmalloc(len);
826 memcpy(cpy, ref, len);
7b3db095 827 cpy->next = NULL;
8c53f071
JK
828 cpy->symref = xstrdup_or_null(ref->symref);
829 cpy->remote_status = xstrdup_or_null(ref->remote_status);
7b3db095
JS
830 cpy->peer_ref = copy_ref(ref->peer_ref);
831 return cpy;
d71ab174
DB
832}
833
4577370e
DB
834struct ref *copy_ref_list(const struct ref *ref)
835{
836 struct ref *ret = NULL;
837 struct ref **tail = &ret;
838 while (ref) {
839 *tail = copy_ref(ref);
840 ref = ref->next;
841 tail = &((*tail)->next);
842 }
843 return ret;
844}
845
697d7f5d 846static void free_ref(struct ref *ref)
be885d96
DB
847{
848 if (!ref)
849 return;
7b3db095 850 free_ref(ref->peer_ref);
be885d96
DB
851 free(ref->remote_status);
852 free(ref->symref);
853 free(ref);
854}
855
dfd255dd
DB
856void free_refs(struct ref *ref)
857{
858 struct ref *next;
859 while (ref) {
860 next = ref->next;
be885d96 861 free_ref(ref);
dfd255dd
DB
862 ref = next;
863 }
864}
865
ed81c76b
JK
866int ref_compare_name(const void *va, const void *vb)
867{
868 const struct ref *a = va, *b = vb;
869 return strcmp(a->name, b->name);
870}
871
872static void *ref_list_get_next(const void *a)
873{
874 return ((const struct ref *)a)->next;
875}
876
877static void ref_list_set_next(void *a, void *next)
878{
879 ((struct ref *)a)->next = next;
880}
881
882void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
883{
884 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
885}
886
ca02465b
JH
887int count_refspec_match(const char *pattern,
888 struct ref *refs,
889 struct ref **matched_ref)
6b62816c
DB
890{
891 int patlen = strlen(pattern);
892 struct ref *matched_weak = NULL;
893 struct ref *matched = NULL;
894 int weak_match = 0;
895 int match = 0;
896
897 for (weak_match = match = 0; refs; refs = refs->next) {
898 char *name = refs->name;
899 int namelen = strlen(name);
6b62816c 900
54457fe5 901 if (!refname_match(pattern, name))
6b62816c
DB
902 continue;
903
904 /* A match is "weak" if it is with refs outside
905 * heads or tags, and did not specify the pattern
906 * in full (e.g. "refs/remotes/origin/master") or at
907 * least from the toplevel (e.g. "remotes/origin/master");
908 * otherwise "git push $URL master" would result in
909 * ambiguity between remotes/origin/master and heads/master
910 * at the remote site.
911 */
912 if (namelen != patlen &&
913 patlen != namelen - 5 &&
59556548
CC
914 !starts_with(name, "refs/heads/") &&
915 !starts_with(name, "refs/tags/")) {
6b62816c
DB
916 /* We want to catch the case where only weak
917 * matches are found and there are multiple
918 * matches, and where more than one strong
919 * matches are found, as ambiguous. One
920 * strong match with zero or more weak matches
921 * are acceptable as a unique match.
922 */
923 matched_weak = refs;
924 weak_match++;
925 }
926 else {
927 matched = refs;
928 match++;
929 }
930 }
931 if (!matched) {
471fd3fe
JK
932 if (matched_ref)
933 *matched_ref = matched_weak;
6b62816c
DB
934 return weak_match;
935 }
936 else {
471fd3fe
JK
937 if (matched_ref)
938 *matched_ref = matched;
6b62816c
DB
939 return match;
940 }
941}
942
1d735267 943static void tail_link_ref(struct ref *ref, struct ref ***tail)
6b62816c
DB
944{
945 **tail = ref;
1d735267
DB
946 while (ref->next)
947 ref = ref->next;
6b62816c 948 *tail = &ref->next;
6b62816c
DB
949}
950
67655246
FC
951static struct ref *alloc_delete_ref(void)
952{
953 struct ref *ref = alloc_ref("(delete)");
f4e54d02 954 oidclr(&ref->new_oid);
67655246
FC
955 return ref;
956}
957
471fd3fe
JK
958static int try_explicit_object_name(const char *name,
959 struct ref **match)
6b62816c 960{
fcd30b13 961 struct object_id oid;
6b62816c 962
471fd3fe
JK
963 if (!*name) {
964 if (match)
965 *match = alloc_delete_ref();
966 return 0;
967 }
968
e82caf38 969 if (get_oid(name, &oid))
471fd3fe
JK
970 return -1;
971
972 if (match) {
973 *match = alloc_ref(name);
fcd30b13 974 oidcpy(&(*match)->new_oid, &oid);
471fd3fe
JK
975 }
976 return 0;
6b62816c
DB
977}
978
1d735267 979static struct ref *make_linked_ref(const char *name, struct ref ***tail)
6b62816c 980{
59c69c0c 981 struct ref *ret = alloc_ref(name);
1d735267
DB
982 tail_link_ref(ret, tail);
983 return ret;
163f0ee5 984}
8558fd9e 985
f8aae120
JK
986static char *guess_ref(const char *name, struct ref *peer)
987{
988 struct strbuf buf = STRBUF_INIT;
f8aae120 989
7695d118 990 const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
744c040b 991 NULL, NULL);
f8aae120
JK
992 if (!r)
993 return NULL;
994
59556548 995 if (starts_with(r, "refs/heads/"))
f8aae120 996 strbuf_addstr(&buf, "refs/heads/");
59556548 997 else if (starts_with(r, "refs/tags/"))
f8aae120
JK
998 strbuf_addstr(&buf, "refs/tags/");
999 else
1000 return NULL;
1001
1002 strbuf_addstr(&buf, name);
1003 return strbuf_detach(&buf, NULL);
1004}
1005
f7ade3d3 1006static int match_explicit_lhs(struct ref *src,
0ad4a5ff 1007 struct refspec_item *rs,
f7ade3d3
JK
1008 struct ref **match,
1009 int *allocated_match)
1010{
1011 switch (count_refspec_match(rs->src, src, match)) {
1012 case 1:
471fd3fe
JK
1013 if (allocated_match)
1014 *allocated_match = 0;
f7ade3d3
JK
1015 return 0;
1016 case 0:
1017 /* The source could be in the get_sha1() format
1018 * not a reference name. :refs/other is a
1019 * way to delete 'other' ref at the remote end.
1020 */
471fd3fe 1021 if (try_explicit_object_name(rs->src, match) < 0)
f7ade3d3 1022 return error("src refspec %s does not match any.", rs->src);
471fd3fe
JK
1023 if (allocated_match)
1024 *allocated_match = 1;
f7ade3d3
JK
1025 return 0;
1026 default:
1027 return error("src refspec %s matches more than one.", rs->src);
1028 }
1029}
1030
54a8ad92
JH
1031static int match_explicit(struct ref *src, struct ref *dst,
1032 struct ref ***dst_tail,
0ad4a5ff 1033 struct refspec_item *rs)
6b62816c 1034{
54a8ad92 1035 struct ref *matched_src, *matched_dst;
f7ade3d3 1036 int allocated_src;
8558fd9e 1037
54a8ad92 1038 const char *dst_value = rs->dst;
f8aae120 1039 char *dst_guess;
6b62816c 1040
a83619d6 1041 if (rs->pattern || rs->matching)
9a7bbd1d 1042 return 0;
8558fd9e 1043
54a8ad92 1044 matched_src = matched_dst = NULL;
f7ade3d3
JK
1045 if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
1046 return -1;
3c8b7df1 1047
4491e62a 1048 if (!dst_value) {
9f0ea7e8
DB
1049 int flag;
1050
7695d118
RS
1051 dst_value = resolve_ref_unsafe(matched_src->name,
1052 RESOLVE_REF_READING,
744c040b 1053 NULL, &flag);
9f0ea7e8
DB
1054 if (!dst_value ||
1055 ((flag & REF_ISSYMREF) &&
59556548 1056 !starts_with(dst_value, "refs/heads/")))
9f0ea7e8
DB
1057 die("%s cannot be resolved to branch.",
1058 matched_src->name);
4491e62a 1059 }
3c8b7df1 1060
54a8ad92
JH
1061 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1062 case 1:
1063 break;
1064 case 0:
50e19a83 1065 if (starts_with(dst_value, "refs/"))
1d735267 1066 matched_dst = make_linked_ref(dst_value, dst_tail);
f4e54d02 1067 else if (is_null_oid(&matched_src->new_oid))
5742c82b
JK
1068 error("unable to delete '%s': remote ref does not exist",
1069 dst_value);
3dc7ea91 1070 else if ((dst_guess = guess_ref(dst_value, matched_src))) {
f8aae120 1071 matched_dst = make_linked_ref(dst_guess, dst_tail);
3dc7ea91
JS
1072 free(dst_guess);
1073 } else
f8aae120
JK
1074 error("unable to push to unqualified destination: %s\n"
1075 "The destination refspec neither matches an "
1076 "existing ref on the remote nor\n"
1077 "begins with refs/, and we are unable to "
1078 "guess a prefix based on the source ref.",
1079 dst_value);
54a8ad92
JH
1080 break;
1081 default:
3c8b7df1 1082 matched_dst = NULL;
54a8ad92
JH
1083 error("dst refspec %s matches more than one.",
1084 dst_value);
1085 break;
1086 }
9a7bbd1d
JK
1087 if (!matched_dst)
1088 return -1;
1089 if (matched_dst->peer_ref)
1090 return error("dst ref %s receives from more than one src.",
54a8ad92 1091 matched_dst->name);
54a8ad92 1092 else {
f7ade3d3
JK
1093 matched_dst->peer_ref = allocated_src ?
1094 matched_src :
1095 copy_ref(matched_src);
54a8ad92 1096 matched_dst->force = rs->force;
6b62816c 1097 }
9a7bbd1d 1098 return 0;
54a8ad92
JH
1099}
1100
1101static int match_explicit_refs(struct ref *src, struct ref *dst,
0ad4a5ff 1102 struct ref ***dst_tail, struct refspec_item *rs,
54a8ad92
JH
1103 int rs_nr)
1104{
1105 int i, errs;
1106 for (i = errs = 0; i < rs_nr; i++)
9a7bbd1d
JK
1107 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1108 return errs;
6b62816c
DB
1109}
1110
0ad4a5ff
BW
1111static char *get_ref_match(const struct refspec_item *rs, int rs_nr, const struct ref *ref,
1112 int send_mirror, int direction, const struct refspec_item **ret_pat)
8558fd9e 1113{
0ad4a5ff 1114 const struct refspec_item *pat;
db70a04c 1115 char *name;
8558fd9e 1116 int i;
a83619d6 1117 int matching_refs = -1;
8558fd9e 1118 for (i = 0; i < rs_nr; i++) {
a83619d6
PB
1119 if (rs[i].matching &&
1120 (matching_refs == -1 || rs[i].force)) {
1121 matching_refs = i;
1122 continue;
1123 }
1124
db70a04c
FC
1125 if (rs[i].pattern) {
1126 const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src;
6ddba5e2
FC
1127 int match;
1128 if (direction == FROM_SRC)
1129 match = match_name_with_pattern(rs[i].src, ref->name, dst_side, &name);
1130 else
1131 match = match_name_with_pattern(dst_side, ref->name, rs[i].src, &name);
1132 if (match) {
db70a04c
FC
1133 matching_refs = i;
1134 break;
1135 }
1136 }
8558fd9e 1137 }
db70a04c 1138 if (matching_refs == -1)
a83619d6 1139 return NULL;
db70a04c
FC
1140
1141 pat = rs + matching_refs;
1142 if (pat->matching) {
1143 /*
1144 * "matching refs"; traditionally we pushed everything
1145 * including refs outside refs/heads/ hierarchy, but
1146 * that does not make much sense these days.
1147 */
59556548 1148 if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
db70a04c
FC
1149 return NULL;
1150 name = xstrdup(ref->name);
1151 }
1152 if (ret_pat)
1153 *ret_pat = pat;
1154 return name;
8558fd9e
DB
1155}
1156
6d2bf96e
CB
1157static struct ref **tail_ref(struct ref **head)
1158{
1159 struct ref **tail = head;
1160 while (*tail)
1161 tail = &((*tail)->next);
1162 return tail;
1163}
1164
c2aba155
JH
1165struct tips {
1166 struct commit **tip;
1167 int nr, alloc;
1168};
1169
fcd30b13 1170static void add_to_tips(struct tips *tips, const struct object_id *oid)
c2aba155
JH
1171{
1172 struct commit *commit;
1173
fcd30b13 1174 if (is_null_oid(oid))
c2aba155 1175 return;
bc83266a 1176 commit = lookup_commit_reference_gently(oid, 1);
c2aba155
JH
1177 if (!commit || (commit->object.flags & TMP_MARK))
1178 return;
1179 commit->object.flags |= TMP_MARK;
1180 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1181 tips->tip[tips->nr++] = commit;
1182}
1183
1184static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1185{
1186 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1187 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1188 struct string_list_item *item;
1189 struct ref *ref;
1190 struct tips sent_tips;
1191
1192 /*
1193 * Collect everything we know they would have at the end of
1194 * this push, and collect all tags they have.
1195 */
1196 memset(&sent_tips, 0, sizeof(sent_tips));
1197 for (ref = *dst; ref; ref = ref->next) {
1198 if (ref->peer_ref &&
f4e54d02 1199 !is_null_oid(&ref->peer_ref->new_oid))
fcd30b13 1200 add_to_tips(&sent_tips, &ref->peer_ref->new_oid);
c2aba155 1201 else
fcd30b13 1202 add_to_tips(&sent_tips, &ref->old_oid);
59556548 1203 if (starts_with(ref->name, "refs/tags/"))
c2aba155
JH
1204 string_list_append(&dst_tag, ref->name);
1205 }
1206 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1207
3383e199 1208 string_list_sort(&dst_tag);
c2aba155
JH
1209
1210 /* Collect tags they do not have. */
1211 for (ref = src; ref; ref = ref->next) {
59556548 1212 if (!starts_with(ref->name, "refs/tags/"))
c2aba155
JH
1213 continue; /* not a tag */
1214 if (string_list_has_string(&dst_tag, ref->name))
1215 continue; /* they already have it */
abef9020 1216 if (oid_object_info(&ref->new_oid, NULL) != OBJ_TAG)
c2aba155
JH
1217 continue; /* be conservative */
1218 item = string_list_append(&src_tag, ref->name);
1219 item->util = ref;
1220 }
1221 string_list_clear(&dst_tag, 0);
1222
1223 /*
1224 * At this point, src_tag lists tags that are missing from
1225 * dst, and sent_tips lists the tips we are pushing or those
1226 * that we know they already have. An element in the src_tag
1227 * that is an ancestor of any of the sent_tips needs to be
1228 * sent to the other side.
1229 */
1230 if (sent_tips.nr) {
1231 for_each_string_list_item(item, &src_tag) {
1232 struct ref *ref = item->util;
1233 struct ref *dst_ref;
1234 struct commit *commit;
1235
f4e54d02 1236 if (is_null_oid(&ref->new_oid))
c2aba155 1237 continue;
bc83266a 1238 commit = lookup_commit_reference_gently(&ref->new_oid,
1239 1);
c2aba155
JH
1240 if (!commit)
1241 /* not pushing a commit, which is not an error */
1242 continue;
1243
1244 /*
1245 * Is this tag, which they do not have, reachable from
1246 * any of the commits we are sending?
1247 */
1248 if (!in_merge_bases_many(commit, sent_tips.nr, sent_tips.tip))
1249 continue;
1250
1251 /* Add it in */
1252 dst_ref = make_linked_ref(ref->name, dst_tail);
f4e54d02 1253 oidcpy(&dst_ref->new_oid, &ref->new_oid);
c2aba155
JH
1254 dst_ref->peer_ref = copy_ref(ref);
1255 }
1256 }
1257 string_list_clear(&src_tag, 0);
1258 free(sent_tips.tip);
1259}
1260
47a59185
JH
1261struct ref *find_ref_by_name(const struct ref *list, const char *name)
1262{
1263 for ( ; list; list = list->next)
1264 if (!strcmp(list->name, name))
1265 return (struct ref *)list;
1266 return NULL;
1267}
1268
f1bd15ab
BC
1269static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
1270{
1271 for ( ; ref; ref = ref->next)
1272 string_list_append_nodup(ref_index, ref->name)->util = ref;
1273
3383e199 1274 string_list_sort(ref_index);
f1bd15ab
BC
1275}
1276
ba928c13
JK
1277/*
1278 * Given only the set of local refs, sanity-check the set of push
1279 * refspecs. We can't catch all errors that match_push_refs would,
1280 * but we can catch some errors early before even talking to the
1281 * remote side.
1282 */
1283int check_push_refs(struct ref *src, int nr_refspec, const char **refspec_names)
1284{
0ad4a5ff 1285 struct refspec_item *refspec = parse_push_refspec(nr_refspec, refspec_names);
ba928c13
JK
1286 int ret = 0;
1287 int i;
1288
1289 for (i = 0; i < nr_refspec; i++) {
0ad4a5ff 1290 struct refspec_item *rs = refspec + i;
ba928c13
JK
1291
1292 if (rs->pattern || rs->matching)
1293 continue;
1294
1295 ret |= match_explicit_lhs(src, rs, NULL, NULL);
1296 }
1297
1298 free_refspec(nr_refspec, refspec);
1299 return ret;
1300}
1301
54a8ad92 1302/*
29753cdd
JH
1303 * Given the set of refs the local repository has, the set of refs the
1304 * remote repository has, and the refspec used for push, determine
1305 * what remote refs we will update and with what value by setting
1306 * peer_ref (which object is being pushed) and force (if the push is
1307 * forced) in elements of "dst". The function may add new elements to
1308 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
54a8ad92 1309 */
29753cdd
JH
1310int match_push_refs(struct ref *src, struct ref **dst,
1311 int nr_refspec, const char **refspec, int flags)
6b62816c 1312{
0ad4a5ff 1313 struct refspec_item *rs;
28b9d6e5
AW
1314 int send_all = flags & MATCH_REFS_ALL;
1315 int send_mirror = flags & MATCH_REFS_MIRROR;
6ddba5e2 1316 int send_prune = flags & MATCH_REFS_PRUNE;
5f48cb95 1317 int errs;
2af202be 1318 static const char *default_refspec[] = { ":", NULL };
b1d8b1f3 1319 struct ref *ref, **dst_tail = tail_ref(dst);
f1bd15ab 1320 struct string_list dst_ref_index = STRING_LIST_INIT_NODUP;
6b62816c 1321
a83619d6
PB
1322 if (!nr_refspec) {
1323 nr_refspec = 1;
1324 refspec = default_refspec;
1325 }
1326 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
6d2bf96e 1327 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
6b62816c
DB
1328
1329 /* pick the remainder */
b1d8b1f3 1330 for (ref = src; ref; ref = ref->next) {
f1bd15ab 1331 struct string_list_item *dst_item;
6b62816c 1332 struct ref *dst_peer;
0ad4a5ff 1333 const struct refspec_item *pat = NULL;
6e66bf3c 1334 char *dst_name;
db70a04c 1335
6ddba5e2 1336 dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_SRC, &pat);
db70a04c 1337 if (!dst_name)
a83619d6
PB
1338 continue;
1339
f1bd15ab
BC
1340 if (!dst_ref_index.nr)
1341 prepare_ref_index(&dst_ref_index, *dst);
1342
1343 dst_item = string_list_lookup(&dst_ref_index, dst_name);
1344 dst_peer = dst_item ? dst_item->util : NULL;
a83619d6
PB
1345 if (dst_peer) {
1346 if (dst_peer->peer_ref)
1347 /* We're already sending something to this ref. */
1348 goto free_name;
a83619d6
PB
1349 } else {
1350 if (pat->matching && !(send_all || send_mirror))
1351 /*
1352 * Remote doesn't have it, and we have no
1353 * explicit pattern, and we don't have
01689909 1354 * --all or --mirror.
a83619d6
PB
1355 */
1356 goto free_name;
28b9d6e5 1357
6b62816c 1358 /* Create a new one and link it */
6d2bf96e 1359 dst_peer = make_linked_ref(dst_name, &dst_tail);
f4e54d02 1360 oidcpy(&dst_peer->new_oid, &ref->new_oid);
f1bd15ab
BC
1361 string_list_insert(&dst_ref_index,
1362 dst_peer->name)->util = dst_peer;
6b62816c 1363 }
b1d8b1f3 1364 dst_peer->peer_ref = copy_ref(ref);
a83619d6 1365 dst_peer->force = pat->force;
6e66bf3c
AR
1366 free_name:
1367 free(dst_name);
6b62816c 1368 }
c2aba155 1369
f1bd15ab
BC
1370 string_list_clear(&dst_ref_index, 0);
1371
c2aba155
JH
1372 if (flags & MATCH_REFS_FOLLOW_TAGS)
1373 add_missing_tags(src, dst, &dst_tail);
1374
6ddba5e2 1375 if (send_prune) {
f1bd15ab 1376 struct string_list src_ref_index = STRING_LIST_INIT_NODUP;
6ddba5e2
FC
1377 /* check for missing refs on the remote */
1378 for (ref = *dst; ref; ref = ref->next) {
1379 char *src_name;
1380
1381 if (ref->peer_ref)
1382 /* We're already sending something to this ref. */
1383 continue;
1384
1385 src_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_DST, NULL);
1386 if (src_name) {
f1bd15ab
BC
1387 if (!src_ref_index.nr)
1388 prepare_ref_index(&src_ref_index, src);
1389 if (!string_list_has_string(&src_ref_index,
1390 src_name))
6ddba5e2
FC
1391 ref->peer_ref = alloc_delete_ref();
1392 free(src_name);
1393 }
1394 }
f1bd15ab 1395 string_list_clear(&src_ref_index, 0);
6ddba5e2 1396 }
5f48cb95
JS
1397 if (errs)
1398 return -1;
6b62816c
DB
1399 return 0;
1400}
cf818348 1401
20e8b465 1402void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
631b5ef2 1403 int force_update)
20e8b465
TRC
1404{
1405 struct ref *ref;
1406
1407 for (ref = remote_refs; ref; ref = ref->next) {
8c5f6f71 1408 int force_ref_update = ref->force || force_update;
631b5ef2 1409 int reject_reason = 0;
8c5f6f71 1410
20e8b465 1411 if (ref->peer_ref)
f4e54d02 1412 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
20e8b465
TRC
1413 else if (!send_mirror)
1414 continue;
1415
f4e54d02 1416 ref->deletion = is_null_oid(&ref->new_oid);
20e8b465 1417 if (!ref->deletion &&
f4e54d02 1418 !oidcmp(&ref->old_oid, &ref->new_oid)) {
20e8b465
TRC
1419 ref->status = REF_STATUS_UPTODATE;
1420 continue;
1421 }
1422
a272b289 1423 /*
b2e93f88
AW
1424 * If the remote ref has moved and is now different
1425 * from what we expect, reject any push.
631b5ef2
JH
1426 *
1427 * It also is an error if the user told us to check
1428 * with the remote-tracking branch to find the value
1429 * to expect, but we did not have such a tracking
1430 * branch.
1431 */
1432 if (ref->expect_old_sha1) {
64ac39af 1433 if (oidcmp(&ref->old_oid, &ref->old_oid_expect))
631b5ef2 1434 reject_reason = REF_STATUS_REJECT_STALE;
b2e93f88
AW
1435 else
1436 /* If the ref isn't stale then force the update. */
1437 force_ref_update = 1;
631b5ef2
JH
1438 }
1439
1440 /*
b2e93f88
AW
1441 * If the update isn't already rejected then check
1442 * the usual "must fast-forward" rules.
631b5ef2 1443 *
256b9d70
JH
1444 * Decide whether an individual refspec A:B can be
1445 * pushed. The push will succeed if any of the
1446 * following are true:
20e8b465 1447 *
a272b289 1448 * (1) the remote reference B does not exist
20e8b465 1449 *
a272b289
CR
1450 * (2) the remote reference B is being removed (i.e.,
1451 * pushing :B where no source is specified)
20e8b465 1452 *
256b9d70
JH
1453 * (3) the destination is not under refs/tags/, and
1454 * if the old and new value is a commit, the new
1455 * is a descendant of the old.
20e8b465 1456 *
a272b289
CR
1457 * (4) it is forced using the +A:B notation, or by
1458 * passing the --force argument
20e8b465
TRC
1459 */
1460
b2e93f88 1461 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
59556548 1462 if (starts_with(ref->name, "refs/tags/"))
631b5ef2 1463 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
f4e54d02 1464 else if (!has_object_file(&ref->old_oid))
631b5ef2 1465 reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
bc83266a 1466 else if (!lookup_commit_reference_gently(&ref->old_oid, 1) ||
1467 !lookup_commit_reference_gently(&ref->new_oid, 1))
631b5ef2 1468 reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
6f3d57b6 1469 else if (!ref_newer(&ref->new_oid, &ref->old_oid))
631b5ef2 1470 reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
20e8b465 1471 }
631b5ef2
JH
1472
1473 /*
1474 * "--force" will defeat any rejection implemented
1475 * by the rules above.
1476 */
1477 if (!force_ref_update)
1478 ref->status = reject_reason;
1479 else if (reject_reason)
1480 ref->forced_update = 1;
20e8b465
TRC
1481 }
1482}
1483
05e73682
JH
1484static void set_merge(struct branch *ret)
1485{
9e3751d4 1486 struct remote *remote;
05e73682 1487 char *ref;
fcd30b13 1488 struct object_id oid;
05e73682
JH
1489 int i;
1490
ee2499fe
JK
1491 if (!ret)
1492 return; /* no branch */
1493 if (ret->merge)
1494 return; /* already run */
1495 if (!ret->remote_name || !ret->merge_nr) {
1496 /*
1497 * no merge config; let's make sure we don't confuse callers
1498 * with a non-zero merge_nr but a NULL merge
1499 */
1500 ret->merge_nr = 0;
1501 return;
1502 }
1503
9e3751d4
JK
1504 remote = remote_get(ret->remote_name);
1505
05e73682
JH
1506 ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
1507 for (i = 0; i < ret->merge_nr; i++) {
1508 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1509 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
9e3751d4 1510 if (!remote_find_tracking(remote, ret->merge[i]) ||
05e73682
JH
1511 strcmp(ret->remote_name, "."))
1512 continue;
1513 if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
cca5fa64 1514 &oid, &ref) == 1)
05e73682
JH
1515 ret->merge[i]->dst = ref;
1516 else
1517 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1518 }
1519}
1520
cf818348
DB
1521struct branch *branch_get(const char *name)
1522{
1523 struct branch *ret;
1524
1525 read_config();
1526 if (!name || !*name || !strcmp(name, "HEAD"))
1527 ret = current_branch;
1528 else
1529 ret = make_branch(name, 0);
ee2499fe 1530 set_merge(ret);
cf818348
DB
1531 return ret;
1532}
1533
1534int branch_has_merge_config(struct branch *branch)
1535{
1536 return branch && !!branch->merge;
1537}
1538
85682c19
SP
1539int branch_merge_matches(struct branch *branch,
1540 int i,
1541 const char *refname)
cf818348 1542{
85682c19 1543 if (!branch || i < 0 || i >= branch->merge_nr)
cf818348 1544 return 0;
54457fe5 1545 return refname_match(branch->merge[i]->src, refname);
cf818348 1546}
d71ab174 1547
060e7766 1548__attribute__((format (printf,2,3)))
3a429d0a
JK
1549static const char *error_buf(struct strbuf *err, const char *fmt, ...)
1550{
1551 if (err) {
1552 va_list ap;
1553 va_start(ap, fmt);
1554 strbuf_vaddf(err, fmt, ap);
1555 va_end(ap);
1556 }
1557 return NULL;
1558}
1559
1560const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
1561{
1562 if (!branch)
1563 return error_buf(err, _("HEAD does not point to a branch"));
1ca41a19
JK
1564
1565 if (!branch->merge || !branch->merge[0]) {
1566 /*
1567 * no merge config; is it because the user didn't define any,
1568 * or because it is not a real branch, and get_branch
1569 * auto-vivified it?
1570 */
3a429d0a
JK
1571 if (!ref_exists(branch->refname))
1572 return error_buf(err, _("no such branch: '%s'"),
1573 branch->name);
1ca41a19
JK
1574 return error_buf(err,
1575 _("no upstream configured for branch '%s'"),
1576 branch->name);
1577 }
1578
1579 if (!branch->merge[0]->dst)
3a429d0a
JK
1580 return error_buf(err,
1581 _("upstream branch '%s' not stored as a remote-tracking branch"),
1582 branch->merge[0]->src);
3a429d0a 1583
a9f9f8cc
JK
1584 return branch->merge[0]->dst;
1585}
1586
e291c75a
JK
1587static const char *tracking_for_push_dest(struct remote *remote,
1588 const char *refname,
1589 struct strbuf *err)
1590{
1591 char *ret;
1592
1593 ret = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
1594 if (!ret)
1595 return error_buf(err,
1596 _("push destination '%s' on remote '%s' has no local tracking branch"),
1597 refname, remote->name);
1598 return ret;
1599}
1600
1601static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1602{
1603 struct remote *remote;
1604
e291c75a
JK
1605 remote = remote_get(pushremote_for_branch(branch, NULL));
1606 if (!remote)
1607 return error_buf(err,
1608 _("branch '%s' has no remote for pushing"),
1609 branch->name);
1610
1611 if (remote->push_refspec_nr) {
1612 char *dst;
1613 const char *ret;
1614
1615 dst = apply_refspecs(remote->push, remote->push_refspec_nr,
1616 branch->refname);
1617 if (!dst)
1618 return error_buf(err,
1619 _("push refspecs for '%s' do not include '%s'"),
1620 remote->name, branch->name);
1621
1622 ret = tracking_for_push_dest(remote, dst, err);
1623 free(dst);
1624 return ret;
1625 }
1626
1627 if (remote->mirror)
1628 return tracking_for_push_dest(remote, branch->refname, err);
1629
1630 switch (push_default) {
1631 case PUSH_DEFAULT_NOTHING:
1632 return error_buf(err, _("push has no destination (push.default is 'nothing')"));
1633
1634 case PUSH_DEFAULT_MATCHING:
1635 case PUSH_DEFAULT_CURRENT:
1636 return tracking_for_push_dest(remote, branch->refname, err);
1637
1638 case PUSH_DEFAULT_UPSTREAM:
1639 return branch_get_upstream(branch, err);
1640
1641 case PUSH_DEFAULT_UNSPECIFIED:
1642 case PUSH_DEFAULT_SIMPLE:
1643 {
1644 const char *up, *cur;
1645
1646 up = branch_get_upstream(branch, err);
1647 if (!up)
1648 return NULL;
1649 cur = tracking_for_push_dest(remote, branch->refname, err);
1650 if (!cur)
1651 return NULL;
1652 if (strcmp(cur, up))
1653 return error_buf(err,
1654 _("cannot resolve 'simple' push to a single destination"));
1655 return cur;
1656 }
1657 }
1658
1659 die("BUG: unhandled push situation");
1660}
1661
1662const char *branch_get_push(struct branch *branch, struct strbuf *err)
1663{
b10731f4
KM
1664 if (!branch)
1665 return error_buf(err, _("HEAD does not point to a branch"));
1666
e291c75a
JK
1667 if (!branch->push_tracking_ref)
1668 branch->push_tracking_ref = branch_get_push_1(branch, err);
1669 return branch->push_tracking_ref;
1670}
1671
f8fb971e
JH
1672static int ignore_symref_update(const char *refname)
1673{
f8fb971e
JH
1674 int flag;
1675
744c040b 1676 if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
f8fb971e
JH
1677 return 0; /* non-existing refs are OK */
1678 return (flag & REF_ISSYMREF);
1679}
1680
f166db26
MH
1681/*
1682 * Create and return a list of (struct ref) consisting of copies of
1683 * each remote_ref that matches refspec. refspec must be a pattern.
1684 * Fill in the copies' peer_ref to describe the local tracking refs to
1685 * which they map. Omit any references that would map to an existing
1686 * local symbolic ref.
1687 */
4577370e 1688static struct ref *get_expanded_map(const struct ref *remote_refs,
0ad4a5ff 1689 const struct refspec_item *refspec)
d71ab174 1690{
4577370e 1691 const struct ref *ref;
d71ab174
DB
1692 struct ref *ret = NULL;
1693 struct ref **tail = &ret;
1694
d71ab174 1695 for (ref = remote_refs; ref; ref = ref->next) {
e31a17f7
MH
1696 char *expn_name = NULL;
1697
d71ab174
DB
1698 if (strchr(ref->name, '^'))
1699 continue; /* a dereference item */
e928213f 1700 if (match_name_with_pattern(refspec->src, ref->name,
f8fb971e
JH
1701 refspec->dst, &expn_name) &&
1702 !ignore_symref_update(expn_name)) {
d71ab174 1703 struct ref *cpy = copy_ref(ref);
d71ab174 1704
e928213f 1705 cpy->peer_ref = alloc_ref(expn_name);
d71ab174
DB
1706 if (refspec->force)
1707 cpy->peer_ref->force = 1;
1708 *tail = cpy;
1709 tail = &cpy->next;
1710 }
e31a17f7 1711 free(expn_name);
d71ab174
DB
1712 }
1713
1714 return ret;
1715}
1716
4577370e 1717static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
d71ab174 1718{
4577370e 1719 const struct ref *ref;
d71ab174 1720 for (ref = refs; ref; ref = ref->next) {
54457fe5 1721 if (refname_match(name, ref->name))
d71ab174
DB
1722 return ref;
1723 }
1724 return NULL;
1725}
1726
4577370e 1727struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
d71ab174 1728{
4577370e 1729 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
d71ab174
DB
1730
1731 if (!ref)
9ad7c5ae 1732 return NULL;
d71ab174
DB
1733
1734 return copy_ref(ref);
1735}
1736
1737static struct ref *get_local_ref(const char *name)
1738{
3eb96997 1739 if (!name || name[0] == '\0')
d71ab174
DB
1740 return NULL;
1741
59556548 1742 if (starts_with(name, "refs/"))
59c69c0c 1743 return alloc_ref(name);
d71ab174 1744
59556548
CC
1745 if (starts_with(name, "heads/") ||
1746 starts_with(name, "tags/") ||
1747 starts_with(name, "remotes/"))
8009768e 1748 return alloc_ref_with_prefix("refs/", 5, name);
d71ab174 1749
8009768e 1750 return alloc_ref_with_prefix("refs/heads/", 11, name);
d71ab174
DB
1751}
1752
4577370e 1753int get_fetch_map(const struct ref *remote_refs,
0ad4a5ff 1754 const struct refspec_item *refspec,
9ad7c5ae
JH
1755 struct ref ***tail,
1756 int missing_ok)
d71ab174 1757{
ef00d150 1758 struct ref *ref_map, **rmp;
d71ab174
DB
1759
1760 if (refspec->pattern) {
1761 ref_map = get_expanded_map(remote_refs, refspec);
1762 } else {
9ad7c5ae
JH
1763 const char *name = refspec->src[0] ? refspec->src : "HEAD";
1764
6e7b66ee
JH
1765 if (refspec->exact_sha1) {
1766 ref_map = alloc_ref(name);
f4e54d02 1767 get_oid_hex(name, &ref_map->old_oid);
6e7b66ee
JH
1768 } else {
1769 ref_map = get_remote_ref(remote_refs, name);
1770 }
9ad7c5ae
JH
1771 if (!missing_ok && !ref_map)
1772 die("Couldn't find remote ref %s", name);
1773 if (ref_map) {
1774 ref_map->peer_ref = get_local_ref(refspec->dst);
1775 if (ref_map->peer_ref && refspec->force)
1776 ref_map->peer_ref->force = 1;
1777 }
d71ab174
DB
1778 }
1779
ef00d150
DB
1780 for (rmp = &ref_map; *rmp; ) {
1781 if ((*rmp)->peer_ref) {
59556548 1782 if (!starts_with((*rmp)->peer_ref->name, "refs/") ||
5c08c1f2 1783 check_refname_format((*rmp)->peer_ref->name, 0)) {
ef00d150
DB
1784 struct ref *ignore = *rmp;
1785 error("* Ignoring funny ref '%s' locally",
1786 (*rmp)->peer_ref->name);
1787 *rmp = (*rmp)->next;
1788 free(ignore->peer_ref);
1789 free(ignore);
1790 continue;
1791 }
1792 }
1793 rmp = &((*rmp)->next);
d71ab174
DB
1794 }
1795
8f70a765
AR
1796 if (ref_map)
1797 tail_link_ref(ref_map, tail);
d71ab174
DB
1798
1799 return 0;
1800}
be885d96
DB
1801
1802int resolve_remote_symref(struct ref *ref, struct ref *list)
1803{
1804 if (!ref->symref)
1805 return 0;
1806 for (; list; list = list->next)
1807 if (!strcmp(ref->symref, list->name)) {
f4e54d02 1808 oidcpy(&ref->old_oid, &list->old_oid);
be885d96
DB
1809 return 0;
1810 }
1811 return 1;
1812}
6d21bf96 1813
ec8452d5
JS
1814static void unmark_and_free(struct commit_list *list, unsigned int mark)
1815{
1816 while (list) {
e510ab89
RS
1817 struct commit *commit = pop_commit(&list);
1818 commit->object.flags &= ~mark;
ec8452d5
JS
1819 }
1820}
1821
6f3d57b6 1822int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
ec8452d5
JS
1823{
1824 struct object *o;
f3bbe630 1825 struct commit *old_commit, *new_commit;
ec8452d5
JS
1826 struct commit_list *list, *used;
1827 int found = 0;
1828
75e5c0dc 1829 /*
f3bbe630
BW
1830 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
1831 * old_commit. Otherwise we require --force.
ec8452d5 1832 */
c251c83d 1833 o = deref_tag(parse_object(old_oid), NULL, 0);
ec8452d5
JS
1834 if (!o || o->type != OBJ_COMMIT)
1835 return 0;
f3bbe630 1836 old_commit = (struct commit *) o;
ec8452d5 1837
c251c83d 1838 o = deref_tag(parse_object(new_oid), NULL, 0);
ec8452d5
JS
1839 if (!o || o->type != OBJ_COMMIT)
1840 return 0;
f3bbe630 1841 new_commit = (struct commit *) o;
ec8452d5 1842
f3bbe630 1843 if (parse_commit(new_commit) < 0)
ec8452d5
JS
1844 return 0;
1845
1846 used = list = NULL;
f3bbe630 1847 commit_list_insert(new_commit, &list);
ec8452d5 1848 while (list) {
f3bbe630
BW
1849 new_commit = pop_most_recent_commit(&list, TMP_MARK);
1850 commit_list_insert(new_commit, &used);
1851 if (new_commit == old_commit) {
ec8452d5
JS
1852 found = 1;
1853 break;
1854 }
1855 }
1856 unmark_and_free(list, TMP_MARK);
1857 unmark_and_free(used, TMP_MARK);
1858 return found;
1859}
1860
6d21bf96 1861/*
d7d1b496
JH
1862 * Lookup the upstream branch for the given branch and if present, optionally
1863 * compute the commit ahead/behind values for the pair.
1864 *
1865 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
1866 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
1867 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
1868 * set to zero).
1869 *
1870 * The name of the upstream branch (or NULL if no upstream is defined) is
1871 * returned via *upstream_name, if it is not itself NULL.
f2e08739 1872 *
979cb245 1873 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
d7d1b496
JH
1874 * upstream defined, or ref does not exist). Returns 0 if the commits are
1875 * identical. Returns 1 if commits are different.
6d21bf96 1876 */
979cb245 1877int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
d7d1b496 1878 const char **upstream_name, enum ahead_behind_flags abf)
6d21bf96 1879{
fcd30b13 1880 struct object_id oid;
6d21bf96 1881 struct commit *ours, *theirs;
6d21bf96 1882 struct rev_info revs;
0b282cc4
JK
1883 const char *base;
1884 struct argv_array argv = ARGV_ARRAY_INIT;
6d21bf96 1885
f2e08739 1886 /* Cannot stat unless we are marked to build on top of somebody else. */
3a429d0a 1887 base = branch_get_upstream(branch, NULL);
979cb245
JK
1888 if (upstream_name)
1889 *upstream_name = base;
a9f9f8cc 1890 if (!base)
979cb245 1891 return -1;
6d21bf96 1892
f2e08739 1893 /* Cannot stat if what we used to build on no longer exists */
34c290a6 1894 if (read_ref(base, &oid))
f2e08739 1895 return -1;
bc83266a 1896 theirs = lookup_commit_reference(&oid);
6d21bf96 1897 if (!theirs)
f2e08739 1898 return -1;
6d21bf96 1899
34c290a6 1900 if (read_ref(branch->refname, &oid))
f2e08739 1901 return -1;
bc83266a 1902 ours = lookup_commit_reference(&oid);
6d21bf96 1903 if (!ours)
f2e08739 1904 return -1;
6d21bf96 1905
d7d1b496
JH
1906 *num_theirs = *num_ours = 0;
1907
6d21bf96 1908 /* are we the same? */
d7d1b496 1909 if (theirs == ours)
979cb245 1910 return 0;
d7d1b496
JH
1911 if (abf == AHEAD_BEHIND_QUICK)
1912 return 1;
fd9b544a
JH
1913 if (abf != AHEAD_BEHIND_FULL)
1914 BUG("stat_tracking_info: invalid abf '%d'", abf);
6d21bf96 1915
8fbf879e 1916 /* Run "rev-list --left-right ours...theirs" internally... */
0b282cc4
JK
1917 argv_array_push(&argv, ""); /* ignored */
1918 argv_array_push(&argv, "--left-right");
1919 argv_array_pushf(&argv, "%s...%s",
f2fd0760 1920 oid_to_hex(&ours->object.oid),
1921 oid_to_hex(&theirs->object.oid));
0b282cc4 1922 argv_array_push(&argv, "--");
6d21bf96
JH
1923
1924 init_revisions(&revs, NULL);
0b282cc4 1925 setup_revisions(argv.argc, argv.argv, &revs, NULL);
81c3ce3c
SB
1926 if (prepare_revision_walk(&revs))
1927 die("revision walk setup failed");
6d21bf96
JH
1928
1929 /* ... and count the commits on each side. */
6d21bf96
JH
1930 while (1) {
1931 struct commit *c = get_revision(&revs);
1932 if (!c)
1933 break;
1934 if (c->object.flags & SYMMETRIC_LEFT)
1935 (*num_ours)++;
1936 else
1937 (*num_theirs)++;
1938 }
c0234b2e
JH
1939
1940 /* clear object flags smudged by the above traversal */
1941 clear_commit_marks(ours, ALL_REV_FLAGS);
1942 clear_commit_marks(theirs, ALL_REV_FLAGS);
0b282cc4
JK
1943
1944 argv_array_clear(&argv);
d7d1b496 1945 return 1;
6d21bf96
JH
1946}
1947
1948/*
1949 * Return true when there is anything to report, otherwise false.
1950 */
f39a757d
JH
1951int format_tracking_info(struct branch *branch, struct strbuf *sb,
1952 enum ahead_behind_flags abf)
6d21bf96 1953{
f39a757d 1954 int ours, theirs, sti;
979cb245 1955 const char *full_base;
2f50babe 1956 char *base;
f2e08739 1957 int upstream_is_gone = 0;
6d21bf96 1958
f39a757d
JH
1959 sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
1960 if (sti < 0) {
979cb245
JK
1961 if (!full_base)
1962 return 0;
f2e08739 1963 upstream_is_gone = 1;
f2e08739 1964 }
6d21bf96 1965
979cb245 1966 base = shorten_unambiguous_ref(full_base, 0);
f2e08739
JX
1967 if (upstream_is_gone) {
1968 strbuf_addf(sb,
1969 _("Your branch is based on '%s', but the upstream is gone.\n"),
1970 base);
1971 if (advice_status_hints)
a22ae753 1972 strbuf_addstr(sb,
f2e08739 1973 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
f39a757d 1974 } else if (!sti) {
f223459b 1975 strbuf_addf(sb,
7560f547 1976 _("Your branch is up to date with '%s'.\n"),
f223459b 1977 base);
f39a757d
JH
1978 } else if (abf == AHEAD_BEHIND_QUICK) {
1979 strbuf_addf(sb,
1980 _("Your branch and '%s' refer to different commits.\n"),
1981 base);
1982 if (advice_status_hints)
1983 strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
1984 "git status --ahead-behind");
f2e08739 1985 } else if (!theirs) {
8a5b7494
JX
1986 strbuf_addf(sb,
1987 Q_("Your branch is ahead of '%s' by %d commit.\n",
1988 "Your branch is ahead of '%s' by %d commits.\n",
f2e08739
JX
1989 ours),
1990 base, ours);
491e3075 1991 if (advice_status_hints)
a22ae753 1992 strbuf_addstr(sb,
491e3075 1993 _(" (use \"git push\" to publish your local commits)\n"));
f2e08739 1994 } else if (!ours) {
8a5b7494
JX
1995 strbuf_addf(sb,
1996 Q_("Your branch is behind '%s' by %d commit, "
1997 "and can be fast-forwarded.\n",
1998 "Your branch is behind '%s' by %d commits, "
1999 "and can be fast-forwarded.\n",
f2e08739
JX
2000 theirs),
2001 base, theirs);
491e3075 2002 if (advice_status_hints)
a22ae753 2003 strbuf_addstr(sb,
491e3075 2004 _(" (use \"git pull\" to update your local branch)\n"));
c190ced6 2005 } else {
8a5b7494
JX
2006 strbuf_addf(sb,
2007 Q_("Your branch and '%s' have diverged,\n"
2008 "and have %d and %d different commit each, "
2009 "respectively.\n",
2010 "Your branch and '%s' have diverged,\n"
2011 "and have %d and %d different commits each, "
2012 "respectively.\n",
f54bea44 2013 ours + theirs),
f2e08739 2014 base, ours, theirs);
491e3075 2015 if (advice_status_hints)
a22ae753 2016 strbuf_addstr(sb,
491e3075 2017 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
c190ced6 2018 }
2f50babe 2019 free(base);
6d21bf96
JH
2020 return 1;
2021}
454e2025 2022
455ade65
MH
2023static int one_local_ref(const char *refname, const struct object_id *oid,
2024 int flag, void *cb_data)
454e2025
JS
2025{
2026 struct ref ***local_tail = cb_data;
2027 struct ref *ref;
454e2025
JS
2028
2029 /* we already know it starts with refs/ to get here */
8d9c5010 2030 if (check_refname_format(refname + 5, 0))
454e2025
JS
2031 return 0;
2032
96ffc06f 2033 ref = alloc_ref(refname);
f4e54d02 2034 oidcpy(&ref->new_oid, oid);
454e2025
JS
2035 **local_tail = ref;
2036 *local_tail = &ref->next;
2037 return 0;
2038}
2039
2040struct ref *get_local_heads(void)
2041{
55f0566f 2042 struct ref *local_refs = NULL, **local_tail = &local_refs;
2b2a5be3 2043
454e2025
JS
2044 for_each_ref(one_local_ref, &local_tail);
2045 return local_refs;
2046}
8ef51733 2047
4229f1fa
JS
2048struct ref *guess_remote_head(const struct ref *head,
2049 const struct ref *refs,
2050 int all)
8ef51733 2051{
8ef51733 2052 const struct ref *r;
4229f1fa
JS
2053 struct ref *list = NULL;
2054 struct ref **tail = &list;
8ef51733 2055
6cb4e6cc 2056 if (!head)
8ef51733
JS
2057 return NULL;
2058
fbb074c2
JK
2059 /*
2060 * Some transports support directly peeking at
2061 * where HEAD points; if that is the case, then
2062 * we don't have to guess.
2063 */
2064 if (head->symref)
2065 return copy_ref(find_ref_by_name(refs, head->symref));
2066
8ef51733 2067 /* If refs/heads/master could be right, it is. */
4229f1fa
JS
2068 if (!all) {
2069 r = find_ref_by_name(refs, "refs/heads/master");
f4e54d02 2070 if (r && !oidcmp(&r->old_oid, &head->old_oid))
4229f1fa
JS
2071 return copy_ref(r);
2072 }
8ef51733
JS
2073
2074 /* Look for another ref that points there */
4229f1fa 2075 for (r = refs; r; r = r->next) {
61adfd30 2076 if (r != head &&
59556548 2077 starts_with(r->name, "refs/heads/") &&
f4e54d02 2078 !oidcmp(&r->old_oid, &head->old_oid)) {
4229f1fa
JS
2079 *tail = copy_ref(r);
2080 tail = &((*tail)->next);
2081 if (!all)
2082 break;
2083 }
2084 }
8ef51733 2085
4229f1fa 2086 return list;
8ef51733 2087}
f2ef6075
JS
2088
2089struct stale_heads_info {
f2ef6075
JS
2090 struct string_list *ref_names;
2091 struct ref **stale_refs_tail;
0ad4a5ff 2092 struct refspec_item *refs;
ed43de6e 2093 int ref_count;
f2ef6075
JS
2094};
2095
455ade65
MH
2096static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
2097 int flags, void *cb_data)
f2ef6075
JS
2098{
2099 struct stale_heads_info *info = cb_data;
e6f63712 2100 struct string_list matches = STRING_LIST_INIT_DUP;
0ad4a5ff 2101 struct refspec_item query;
e6f63712 2102 int i, stale = 1;
0ad4a5ff 2103 memset(&query, 0, sizeof(struct refspec_item));
ed43de6e
CMN
2104 query.dst = (char *)refname;
2105
e6f63712
CMN
2106 query_refspecs_multiple(info->refs, info->ref_count, &query, &matches);
2107 if (matches.nr == 0)
2108 goto clean_exit; /* No matches */
ed43de6e
CMN
2109
2110 /*
2111 * If we did find a suitable refspec and it's not a symref and
2112 * it's not in the list of refs that currently exist in that
e6f63712
CMN
2113 * remote, we consider it to be stale. In order to deal with
2114 * overlapping refspecs, we need to go over all of the
2115 * matching refs.
ed43de6e 2116 */
e6f63712
CMN
2117 if (flags & REF_ISSYMREF)
2118 goto clean_exit;
2119
2120 for (i = 0; stale && i < matches.nr; i++)
2121 if (string_list_has_string(info->ref_names, matches.items[i].string))
2122 stale = 0;
2123
2124 if (stale) {
ed43de6e 2125 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
f4e54d02 2126 oidcpy(&ref->new_oid, oid);
f2ef6075 2127 }
ed43de6e 2128
e6f63712
CMN
2129clean_exit:
2130 string_list_clear(&matches, 0);
f2ef6075
JS
2131 return 0;
2132}
2133
0ad4a5ff 2134struct ref *get_stale_heads(struct refspec_item *refs, int ref_count, struct ref *fetch_map)
f2ef6075
JS
2135{
2136 struct ref *ref, *stale_refs = NULL;
183113a5 2137 struct string_list ref_names = STRING_LIST_INIT_NODUP;
f2ef6075 2138 struct stale_heads_info info;
2b2a5be3 2139
f2ef6075
JS
2140 info.ref_names = &ref_names;
2141 info.stale_refs_tail = &stale_refs;
ed43de6e
CMN
2142 info.refs = refs;
2143 info.ref_count = ref_count;
f2ef6075 2144 for (ref = fetch_map; ref; ref = ref->next)
1d2f80fa 2145 string_list_append(&ref_names, ref->name);
3383e199 2146 string_list_sort(&ref_names);
f2ef6075
JS
2147 for_each_ref(get_stale_heads_cb, &info);
2148 string_list_clear(&ref_names, 0);
2149 return stale_refs;
2150}
28f5d176
JH
2151
2152/*
2153 * Compare-and-swap
2154 */
a355b11d 2155static void clear_cas_option(struct push_cas_option *cas)
28f5d176
JH
2156{
2157 int i;
2158
2159 for (i = 0; i < cas->nr; i++)
2160 free(cas->entry[i].refname);
2161 free(cas->entry);
2162 memset(cas, 0, sizeof(*cas));
2163}
2164
2165static struct push_cas *add_cas_entry(struct push_cas_option *cas,
2166 const char *refname,
2167 size_t refnamelen)
2168{
2169 struct push_cas *entry;
2170 ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
2171 entry = &cas->entry[cas->nr++];
2172 memset(entry, 0, sizeof(*entry));
2173 entry->refname = xmemdupz(refname, refnamelen);
2174 return entry;
2175}
2176
8668976b 2177static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
28f5d176
JH
2178{
2179 const char *colon;
2180 struct push_cas *entry;
2181
2182 if (unset) {
2183 /* "--no-<option>" */
2184 clear_cas_option(cas);
2185 return 0;
2186 }
2187
2188 if (!arg) {
2189 /* just "--<option>" */
2190 cas->use_tracking_for_rest = 1;
2191 return 0;
2192 }
2193
2194 /* "--<option>=refname" or "--<option>=refname:value" */
2195 colon = strchrnul(arg, ':');
2196 entry = add_cas_entry(cas, arg, colon - arg);
2197 if (!*colon)
2198 entry->use_tracking = 1;
eee98e74 2199 else if (!colon[1])
b8566f8f 2200 oidclr(&entry->expect);
2201 else if (get_oid(colon + 1, &entry->expect))
28f5d176
JH
2202 return error("cannot parse expected object name '%s'", colon + 1);
2203 return 0;
2204}
2205
2206int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
2207{
2208 return parse_push_cas_option(opt->value, arg, unset);
2209}
91048a95
JH
2210
2211int is_empty_cas(const struct push_cas_option *cas)
2212{
2213 return !cas->use_tracking_for_rest && !cas->nr;
2214}
2215
2216/*
2217 * Look at remote.fetch refspec and see if we have a remote
2218 * tracking branch for the refname there. Fill its current
2219 * value in sha1[].
2220 * If we cannot do so, return negative to signal an error.
2221 */
2222static int remote_tracking(struct remote *remote, const char *refname,
fcd30b13 2223 struct object_id *oid)
91048a95
JH
2224{
2225 char *dst;
2226
2227 dst = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
2228 if (!dst)
2229 return -1; /* no tracking ref for refname at remote */
34c290a6 2230 if (read_ref(dst, oid))
91048a95
JH
2231 return -1; /* we know what the tracking ref is but we cannot read it */
2232 return 0;
2233}
2234
2235static void apply_cas(struct push_cas_option *cas,
2236 struct remote *remote,
2237 struct ref *ref)
2238{
2239 int i;
2240
2241 /* Find an explicit --<option>=<name>[:<value>] entry */
2242 for (i = 0; i < cas->nr; i++) {
2243 struct push_cas *entry = &cas->entry[i];
54457fe5 2244 if (!refname_match(entry->refname, ref->name))
91048a95
JH
2245 continue;
2246 ref->expect_old_sha1 = 1;
2247 if (!entry->use_tracking)
b8566f8f 2248 oidcpy(&ref->old_oid_expect, &entry->expect);
fcd30b13 2249 else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
64ac39af 2250 oidclr(&ref->old_oid_expect);
91048a95
JH
2251 return;
2252 }
2253
2254 /* Are we using "--<option>" to cover all? */
2255 if (!cas->use_tracking_for_rest)
2256 return;
2257
2258 ref->expect_old_sha1 = 1;
fcd30b13 2259 if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
64ac39af 2260 oidclr(&ref->old_oid_expect);
91048a95
JH
2261}
2262
2263void apply_push_cas(struct push_cas_option *cas,
2264 struct remote *remote,
2265 struct ref *remote_refs)
2266{
2267 struct ref *ref;
2268 for (ref = remote_refs; ref; ref = ref->next)
2269 apply_cas(cas, remote, ref);
2270}