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