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