]>
Commit | Line | Data |
---|---|---|
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 |
11 | static struct refspec s_tag_refspec = { |
12 | 0, | |
13 | 1, | |
b84c343c | 14 | 0, |
08fbdb30 DB |
15 | "refs/tags/*", |
16 | "refs/tags/*" | |
e0aaa29f DB |
17 | }; |
18 | ||
19 | const struct refspec *tag_refspec = &s_tag_refspec; | |
20 | ||
844112ca JH |
21 | struct counted_string { |
22 | size_t len; | |
23 | const char *s; | |
24 | }; | |
55029ae4 DB |
25 | struct 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 |
32 | struct rewrites { |
33 | struct rewrite **rewrite; | |
34 | int rewrite_alloc; | |
35 | int rewrite_nr; | |
36 | }; | |
55029ae4 | 37 | |
5751f490 | 38 | static struct remote **remotes; |
2d31347b DB |
39 | static int remotes_alloc; |
40 | static int remotes_nr; | |
5751f490 | 41 | |
cf818348 | 42 | static struct branch **branches; |
2d31347b DB |
43 | static int branches_alloc; |
44 | static int branches_nr; | |
cf818348 DB |
45 | |
46 | static struct branch *current_branch; | |
47 | static const char *default_remote_name; | |
fa685bdf | 48 | static int explicit_default_remote_name; |
cf818348 | 49 | |
d071d942 | 50 | static struct rewrites rewrites; |
1c2eafb8 | 51 | static struct rewrites rewrites_push; |
55029ae4 | 52 | |
5751f490 DB |
53 | #define BUF_SIZE (2048) |
54 | static char buffer[BUF_SIZE]; | |
55 | ||
0a4da29d DB |
56 | static int valid_remote(const struct remote *remote) |
57 | { | |
c578f51d | 58 | return (!!remote->url) || (!!remote->foreign_vcs); |
0a4da29d DB |
59 | } |
60 | ||
d071d942 | 61 | static 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 |
92 | static 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 |
100 | static 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 | 108 | static 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 |
114 | static 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 |
120 | static 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 | ||
127 | static 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 |
133 | static 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 |
155 | static 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 | ||
162 | static 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 | 190 | static 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 | ||
217 | static 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 |
225 | static 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 | ||
272 | static 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 | 343 | static 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; | |
14c98218 | 446 | } else if (!strcmp(subkey, ".proxy")) { |
84bb2dfd PB |
447 | return git_config_string((const char **)&remote->http_proxy, |
448 | key, value); | |
c578f51d DB |
449 | } else if (!strcmp(subkey, ".vcs")) { |
450 | return git_config_string(&remote->foreign_vcs, key, value); | |
84bb2dfd | 451 | } |
5751f490 DB |
452 | return 0; |
453 | } | |
454 | ||
55029ae4 DB |
455 | static void alias_all_urls(void) |
456 | { | |
457 | int i, j; | |
458 | for (i = 0; i < remotes_nr; i++) { | |
1c2eafb8 | 459 | int add_pushurl_aliases; |
55029ae4 DB |
460 | if (!remotes[i]) |
461 | continue; | |
20346234 | 462 | for (j = 0; j < remotes[i]->pushurl_nr; j++) { |
d071d942 | 463 | remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites); |
20346234 | 464 | } |
1c2eafb8 JT |
465 | add_pushurl_aliases = remotes[i]->pushurl_nr == 0; |
466 | for (j = 0; j < remotes[i]->url_nr; j++) { | |
467 | if (add_pushurl_aliases) | |
468 | add_pushurl_alias(remotes[i], remotes[i]->url[j]); | |
469 | remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites); | |
470 | } | |
55029ae4 DB |
471 | } |
472 | } | |
473 | ||
5751f490 DB |
474 | static void read_config(void) |
475 | { | |
476 | unsigned char sha1[20]; | |
477 | const char *head_ref; | |
478 | int flag; | |
479 | if (default_remote_name) // did this already | |
480 | return; | |
481 | default_remote_name = xstrdup("origin"); | |
482 | current_branch = NULL; | |
483 | head_ref = resolve_ref("HEAD", sha1, 0, &flag); | |
484 | if (head_ref && (flag & REF_ISSYMREF) && | |
485 | !prefixcmp(head_ref, "refs/heads/")) { | |
cf818348 DB |
486 | current_branch = |
487 | make_branch(head_ref + strlen("refs/heads/"), 0); | |
5751f490 | 488 | } |
ef90d6d4 | 489 | git_config(handle_config, NULL); |
55029ae4 | 490 | alias_all_urls(); |
5751f490 DB |
491 | } |
492 | ||
47c6ef1c JH |
493 | /* |
494 | * We need to make sure the tracking branches are well formed, but a | |
495 | * wildcard refspec in "struct refspec" must have a trailing slash. We | |
496 | * temporarily drop the trailing '/' while calling check_ref_format(), | |
497 | * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL | |
498 | * error return is Ok for a wildcard refspec. | |
499 | */ | |
500 | static int verify_refname(char *name, int is_glob) | |
501 | { | |
08fbdb30 | 502 | int result; |
47c6ef1c | 503 | |
47c6ef1c | 504 | result = check_ref_format(name); |
08fbdb30 DB |
505 | if (is_glob && result == CHECK_REF_FORMAT_WILDCARD) |
506 | result = CHECK_REF_FORMAT_OK; | |
47c6ef1c JH |
507 | return result; |
508 | } | |
509 | ||
2cb1f36d BC |
510 | /* |
511 | * This function frees a refspec array. | |
512 | * Warning: code paths should be checked to ensure that the src | |
513 | * and dst pointers are always freeable pointers as well | |
514 | * as the refspec pointer itself. | |
515 | */ | |
697d7f5d | 516 | static void free_refspecs(struct refspec *refspec, int nr_refspec) |
2cb1f36d BC |
517 | { |
518 | int i; | |
519 | ||
520 | if (!refspec) | |
521 | return; | |
522 | ||
523 | for (i = 0; i < nr_refspec; i++) { | |
524 | free(refspec[i].src); | |
525 | free(refspec[i].dst); | |
526 | } | |
527 | free(refspec); | |
528 | } | |
529 | ||
24b6177e | 530 | static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify) |
6b62816c DB |
531 | { |
532 | int i; | |
ef00d150 | 533 | int st; |
6b62816c | 534 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); |
46220ca1 | 535 | |
6b62816c | 536 | for (i = 0; i < nr_refspec; i++) { |
47c6ef1c | 537 | size_t llen; |
46220ca1 JH |
538 | int is_glob; |
539 | const char *lhs, *rhs; | |
540 | ||
8e76bf3f | 541 | is_glob = 0; |
46220ca1 JH |
542 | |
543 | lhs = refspec[i]; | |
544 | if (*lhs == '+') { | |
6b62816c | 545 | rs[i].force = 1; |
46220ca1 | 546 | lhs++; |
6b62816c | 547 | } |
46220ca1 JH |
548 | |
549 | rhs = strrchr(lhs, ':'); | |
a83619d6 PB |
550 | |
551 | /* | |
552 | * Before going on, special case ":" (or "+:") as a refspec | |
553 | * for matching refs. | |
554 | */ | |
555 | if (!fetch && rhs == lhs && rhs[1] == '\0') { | |
556 | rs[i].matching = 1; | |
557 | continue; | |
558 | } | |
559 | ||
46220ca1 | 560 | if (rhs) { |
47c6ef1c | 561 | size_t rlen = strlen(++rhs); |
abd2bde7 | 562 | is_glob = (1 <= rlen && strchr(rhs, '*')); |
08fbdb30 | 563 | rs[i].dst = xstrndup(rhs, rlen); |
6b62816c | 564 | } |
ef00d150 | 565 | |
46220ca1 | 566 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); |
abd2bde7 | 567 | if (1 <= llen && memchr(lhs, '*', llen)) { |
7d19da46 JH |
568 | if ((rhs && !is_glob) || (!rhs && fetch)) |
569 | goto invalid; | |
570 | is_glob = 1; | |
7d19da46 JH |
571 | } else if (rhs && is_glob) { |
572 | goto invalid; | |
ef00d150 | 573 | } |
7d19da46 | 574 | |
46220ca1 JH |
575 | rs[i].pattern = is_glob; |
576 | rs[i].src = xstrndup(lhs, llen); | |
577 | ||
578 | if (fetch) { | |
579 | /* | |
580 | * LHS | |
581 | * - empty is allowed; it means HEAD. | |
582 | * - otherwise it must be a valid looking ref. | |
583 | */ | |
584 | if (!*rs[i].src) | |
585 | ; /* empty is ok */ | |
586 | else { | |
47c6ef1c | 587 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
588 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
589 | goto invalid; | |
590 | } | |
591 | /* | |
592 | * RHS | |
7d19da46 | 593 | * - missing is ok, and is same as empty. |
46220ca1 JH |
594 | * - empty is ok; it means not to store. |
595 | * - otherwise it must be a valid looking ref. | |
596 | */ | |
597 | if (!rs[i].dst) { | |
598 | ; /* ok */ | |
599 | } else if (!*rs[i].dst) { | |
600 | ; /* ok */ | |
601 | } else { | |
47c6ef1c | 602 | st = verify_refname(rs[i].dst, is_glob); |
46220ca1 JH |
603 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
604 | goto invalid; | |
605 | } | |
606 | } else { | |
607 | /* | |
608 | * LHS | |
609 | * - empty is allowed; it means delete. | |
610 | * - when wildcarded, it must be a valid looking ref. | |
611 | * - otherwise, it must be an extended SHA-1, but | |
612 | * there is no existing way to validate this. | |
613 | */ | |
614 | if (!*rs[i].src) | |
615 | ; /* empty is ok */ | |
616 | else if (is_glob) { | |
47c6ef1c | 617 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
618 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
619 | goto invalid; | |
620 | } | |
621 | else | |
622 | ; /* anything goes, for now */ | |
623 | /* | |
624 | * RHS | |
625 | * - missing is allowed, but LHS then must be a | |
626 | * valid looking ref. | |
627 | * - empty is not allowed. | |
628 | * - otherwise it must be a valid looking ref. | |
629 | */ | |
630 | if (!rs[i].dst) { | |
47c6ef1c | 631 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
632 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
633 | goto invalid; | |
634 | } else if (!*rs[i].dst) { | |
635 | goto invalid; | |
636 | } else { | |
47c6ef1c | 637 | st = verify_refname(rs[i].dst, is_glob); |
46220ca1 JH |
638 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
639 | goto invalid; | |
640 | } | |
ef00d150 | 641 | } |
6b62816c DB |
642 | } |
643 | return rs; | |
46220ca1 JH |
644 | |
645 | invalid: | |
24b6177e | 646 | if (verify) { |
2cb1f36d BC |
647 | /* |
648 | * nr_refspec must be greater than zero and i must be valid | |
649 | * since it is only possible to reach this point from within | |
650 | * the for loop above. | |
651 | */ | |
652 | free_refspecs(rs, i+1); | |
24b6177e JF |
653 | return NULL; |
654 | } | |
46220ca1 JH |
655 | die("Invalid refspec '%s'", refspec[i]); |
656 | } | |
657 | ||
24b6177e JF |
658 | int valid_fetch_refspec(const char *fetch_refspec_str) |
659 | { | |
660 | const char *fetch_refspec[] = { fetch_refspec_str }; | |
661 | struct refspec *refspec; | |
662 | ||
663 | refspec = parse_refspec_internal(1, fetch_refspec, 1, 1); | |
2cb1f36d | 664 | free_refspecs(refspec, 1); |
24b6177e JF |
665 | return !!refspec; |
666 | } | |
667 | ||
46220ca1 JH |
668 | struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec) |
669 | { | |
24b6177e | 670 | return parse_refspec_internal(nr_refspec, refspec, 1, 0); |
46220ca1 JH |
671 | } |
672 | ||
697d7f5d | 673 | static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec) |
46220ca1 | 674 | { |
24b6177e | 675 | return parse_refspec_internal(nr_refspec, refspec, 0, 0); |
6b62816c DB |
676 | } |
677 | ||
72ff8943 DB |
678 | void free_refspec(int nr_refspec, struct refspec *refspec) |
679 | { | |
680 | int i; | |
681 | for (i = 0; i < nr_refspec; i++) { | |
682 | free(refspec[i].src); | |
683 | free(refspec[i].dst); | |
684 | } | |
685 | free(refspec); | |
686 | } | |
687 | ||
df93e33c DB |
688 | static int valid_remote_nick(const char *name) |
689 | { | |
8ca12c0d | 690 | if (!name[0] || is_dot_or_dotdot(name)) |
df93e33c DB |
691 | return 0; |
692 | return !strchr(name, '/'); /* no slash */ | |
693 | } | |
694 | ||
5751f490 DB |
695 | struct remote *remote_get(const char *name) |
696 | { | |
697 | struct remote *ret; | |
fa685bdf | 698 | int name_given = 0; |
5751f490 DB |
699 | |
700 | read_config(); | |
fa685bdf DB |
701 | if (name) |
702 | name_given = 1; | |
703 | else { | |
5751f490 | 704 | name = default_remote_name; |
fa685bdf DB |
705 | name_given = explicit_default_remote_name; |
706 | } | |
9326d494 | 707 | |
5751f490 | 708 | ret = make_remote(name, 0); |
df93e33c | 709 | if (valid_remote_nick(name)) { |
0a4da29d | 710 | if (!valid_remote(ret)) |
5751f490 | 711 | read_remotes_file(ret); |
0a4da29d | 712 | if (!valid_remote(ret)) |
5751f490 DB |
713 | read_branches_file(ret); |
714 | } | |
0a4da29d | 715 | if (name_given && !valid_remote(ret)) |
55029ae4 | 716 | add_url_alias(ret, name); |
0a4da29d | 717 | if (!valid_remote(ret)) |
5751f490 | 718 | return NULL; |
46220ca1 JH |
719 | ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); |
720 | ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec); | |
5751f490 DB |
721 | return ret; |
722 | } | |
6b62816c | 723 | |
9a23ba33 FAG |
724 | int remote_is_configured(const char *name) |
725 | { | |
726 | int i; | |
727 | read_config(); | |
728 | ||
729 | for (i = 0; i < remotes_nr; i++) | |
730 | if (!strcmp(name, remotes[i]->name)) | |
731 | return 1; | |
732 | return 0; | |
733 | } | |
734 | ||
b42f6927 JS |
735 | int for_each_remote(each_remote_fn fn, void *priv) |
736 | { | |
737 | int i, result = 0; | |
738 | read_config(); | |
2d31347b | 739 | for (i = 0; i < remotes_nr && !result; i++) { |
b42f6927 JS |
740 | struct remote *r = remotes[i]; |
741 | if (!r) | |
742 | continue; | |
743 | if (!r->fetch) | |
46220ca1 JH |
744 | r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, |
745 | r->fetch_refspec); | |
b42f6927 | 746 | if (!r->push) |
46220ca1 JH |
747 | r->push = parse_push_refspec(r->push_refspec_nr, |
748 | r->push_refspec); | |
b42f6927 JS |
749 | result = fn(r, priv); |
750 | } | |
751 | return result; | |
752 | } | |
753 | ||
2467a4fa DB |
754 | void ref_remove_duplicates(struct ref *ref_map) |
755 | { | |
73cf0822 JP |
756 | struct string_list refs = { NULL, 0, 0, 0 }; |
757 | struct string_list_item *item = NULL; | |
758 | struct ref *prev = NULL, *next = NULL; | |
759 | for (; ref_map; prev = ref_map, ref_map = next) { | |
760 | next = ref_map->next; | |
2467a4fa DB |
761 | if (!ref_map->peer_ref) |
762 | continue; | |
73cf0822 JP |
763 | |
764 | item = string_list_lookup(ref_map->peer_ref->name, &refs); | |
765 | if (item) { | |
766 | if (strcmp(((struct ref *)item->util)->name, | |
767 | ref_map->name)) | |
768 | die("%s tracks both %s and %s", | |
769 | ref_map->peer_ref->name, | |
770 | ((struct ref *)item->util)->name, | |
771 | ref_map->name); | |
772 | prev->next = ref_map->next; | |
773 | free(ref_map->peer_ref); | |
774 | free(ref_map); | |
95c96d48 JP |
775 | ref_map = prev; /* skip this; we freed it */ |
776 | continue; | |
2467a4fa | 777 | } |
73cf0822 JP |
778 | |
779 | item = string_list_insert(ref_map->peer_ref->name, &refs); | |
780 | item->util = ref_map; | |
2467a4fa | 781 | } |
73cf0822 | 782 | string_list_clear(&refs, 0); |
2467a4fa DB |
783 | } |
784 | ||
28b91f8a | 785 | int remote_has_url(struct remote *remote, const char *url) |
5d46c9d4 DB |
786 | { |
787 | int i; | |
28b91f8a SP |
788 | for (i = 0; i < remote->url_nr; i++) { |
789 | if (!strcmp(remote->url[i], url)) | |
5d46c9d4 DB |
790 | return 1; |
791 | } | |
792 | return 0; | |
793 | } | |
794 | ||
e928213f DB |
795 | static int match_name_with_pattern(const char *key, const char *name, |
796 | const char *value, char **result) | |
a3c84239 | 797 | { |
08fbdb30 DB |
798 | const char *kstar = strchr(key, '*'); |
799 | size_t klen; | |
abd2bde7 DB |
800 | size_t ksuffixlen; |
801 | size_t namelen; | |
08fbdb30 DB |
802 | int ret; |
803 | if (!kstar) | |
804 | die("Key '%s' of pattern had no '*'", key); | |
805 | klen = kstar - key; | |
abd2bde7 DB |
806 | ksuffixlen = strlen(kstar + 1); |
807 | namelen = strlen(name); | |
808 | ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && | |
809 | !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); | |
e928213f | 810 | if (ret && value) { |
08fbdb30 DB |
811 | const char *vstar = strchr(value, '*'); |
812 | size_t vlen; | |
abd2bde7 | 813 | size_t vsuffixlen; |
08fbdb30 DB |
814 | if (!vstar) |
815 | die("Value '%s' of pattern has no '*'", value); | |
816 | vlen = vstar - value; | |
abd2bde7 DB |
817 | vsuffixlen = strlen(vstar + 1); |
818 | *result = xmalloc(vlen + vsuffixlen + | |
e928213f | 819 | strlen(name) - |
abd2bde7 DB |
820 | klen - ksuffixlen + 1); |
821 | strncpy(*result, value, vlen); | |
822 | strncpy(*result + vlen, | |
823 | name + klen, namelen - klen - ksuffixlen); | |
824 | strcpy(*result + vlen + namelen - klen - ksuffixlen, | |
825 | vstar + 1); | |
e928213f | 826 | } |
a3c84239 DB |
827 | return ret; |
828 | } | |
829 | ||
72ff8943 DB |
830 | char *apply_refspecs(struct refspec *refspecs, int nr_refspec, |
831 | const char *name) | |
832 | { | |
833 | int i; | |
834 | char *ret = NULL; | |
835 | for (i = 0; i < nr_refspec; i++) { | |
836 | struct refspec *refspec = refspecs + i; | |
837 | if (refspec->pattern) { | |
838 | if (match_name_with_pattern(refspec->src, name, | |
839 | refspec->dst, &ret)) | |
840 | return ret; | |
841 | } else if (!strcmp(refspec->src, name)) | |
842 | return strdup(refspec->dst); | |
843 | } | |
844 | return NULL; | |
845 | } | |
846 | ||
5d46c9d4 DB |
847 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) |
848 | { | |
b42f6927 JS |
849 | int find_src = refspec->src == NULL; |
850 | char *needle, **result; | |
5d46c9d4 | 851 | int i; |
b42f6927 JS |
852 | |
853 | if (find_src) { | |
009c5bcd | 854 | if (!refspec->dst) |
b42f6927 JS |
855 | return error("find_tracking: need either src or dst"); |
856 | needle = refspec->dst; | |
857 | result = &refspec->src; | |
858 | } else { | |
859 | needle = refspec->src; | |
860 | result = &refspec->dst; | |
861 | } | |
862 | ||
5d46c9d4 DB |
863 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
864 | struct refspec *fetch = &remote->fetch[i]; | |
b42f6927 JS |
865 | const char *key = find_src ? fetch->dst : fetch->src; |
866 | const char *value = find_src ? fetch->src : fetch->dst; | |
5d46c9d4 DB |
867 | if (!fetch->dst) |
868 | continue; | |
869 | if (fetch->pattern) { | |
e928213f | 870 | if (match_name_with_pattern(key, needle, value, result)) { |
5d46c9d4 DB |
871 | refspec->force = fetch->force; |
872 | return 0; | |
873 | } | |
b42f6927 JS |
874 | } else if (!strcmp(needle, key)) { |
875 | *result = xstrdup(value); | |
876 | refspec->force = fetch->force; | |
877 | return 0; | |
5d46c9d4 DB |
878 | } |
879 | } | |
5d46c9d4 DB |
880 | return -1; |
881 | } | |
882 | ||
8009768e RS |
883 | static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, |
884 | const char *name) | |
885 | { | |
886 | size_t len = strlen(name); | |
887 | struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1); | |
888 | memcpy(ref->name, prefix, prefixlen); | |
889 | memcpy(ref->name + prefixlen, name, len); | |
890 | return ref; | |
891 | } | |
892 | ||
59c69c0c | 893 | struct ref *alloc_ref(const char *name) |
dfd255dd | 894 | { |
59c69c0c | 895 | return alloc_ref_with_prefix("", 0, name); |
737922aa KK |
896 | } |
897 | ||
4577370e | 898 | static struct ref *copy_ref(const struct ref *ref) |
d71ab174 | 899 | { |
7b3db095 JS |
900 | struct ref *cpy; |
901 | size_t len; | |
902 | if (!ref) | |
903 | return NULL; | |
904 | len = strlen(ref->name); | |
905 | cpy = xmalloc(sizeof(struct ref) + len + 1); | |
906 | memcpy(cpy, ref, sizeof(struct ref) + len + 1); | |
907 | cpy->next = NULL; | |
908 | cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL; | |
909 | cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL; | |
910 | cpy->peer_ref = copy_ref(ref->peer_ref); | |
911 | return cpy; | |
d71ab174 DB |
912 | } |
913 | ||
4577370e DB |
914 | struct ref *copy_ref_list(const struct ref *ref) |
915 | { | |
916 | struct ref *ret = NULL; | |
917 | struct ref **tail = &ret; | |
918 | while (ref) { | |
919 | *tail = copy_ref(ref); | |
920 | ref = ref->next; | |
921 | tail = &((*tail)->next); | |
922 | } | |
923 | return ret; | |
924 | } | |
925 | ||
697d7f5d | 926 | static void free_ref(struct ref *ref) |
be885d96 DB |
927 | { |
928 | if (!ref) | |
929 | return; | |
7b3db095 | 930 | free_ref(ref->peer_ref); |
be885d96 DB |
931 | free(ref->remote_status); |
932 | free(ref->symref); | |
933 | free(ref); | |
934 | } | |
935 | ||
dfd255dd DB |
936 | void free_refs(struct ref *ref) |
937 | { | |
938 | struct ref *next; | |
939 | while (ref) { | |
940 | next = ref->next; | |
be885d96 | 941 | free_ref(ref); |
dfd255dd DB |
942 | ref = next; |
943 | } | |
944 | } | |
945 | ||
6b62816c DB |
946 | static int count_refspec_match(const char *pattern, |
947 | struct ref *refs, | |
948 | struct ref **matched_ref) | |
949 | { | |
950 | int patlen = strlen(pattern); | |
951 | struct ref *matched_weak = NULL; | |
952 | struct ref *matched = NULL; | |
953 | int weak_match = 0; | |
954 | int match = 0; | |
955 | ||
956 | for (weak_match = match = 0; refs; refs = refs->next) { | |
957 | char *name = refs->name; | |
958 | int namelen = strlen(name); | |
6b62816c | 959 | |
ae36bdcf | 960 | if (!refname_match(pattern, name, ref_rev_parse_rules)) |
6b62816c DB |
961 | continue; |
962 | ||
963 | /* A match is "weak" if it is with refs outside | |
964 | * heads or tags, and did not specify the pattern | |
965 | * in full (e.g. "refs/remotes/origin/master") or at | |
966 | * least from the toplevel (e.g. "remotes/origin/master"); | |
967 | * otherwise "git push $URL master" would result in | |
968 | * ambiguity between remotes/origin/master and heads/master | |
969 | * at the remote site. | |
970 | */ | |
971 | if (namelen != patlen && | |
972 | patlen != namelen - 5 && | |
973 | prefixcmp(name, "refs/heads/") && | |
974 | prefixcmp(name, "refs/tags/")) { | |
975 | /* We want to catch the case where only weak | |
976 | * matches are found and there are multiple | |
977 | * matches, and where more than one strong | |
978 | * matches are found, as ambiguous. One | |
979 | * strong match with zero or more weak matches | |
980 | * are acceptable as a unique match. | |
981 | */ | |
982 | matched_weak = refs; | |
983 | weak_match++; | |
984 | } | |
985 | else { | |
986 | matched = refs; | |
987 | match++; | |
988 | } | |
989 | } | |
990 | if (!matched) { | |
991 | *matched_ref = matched_weak; | |
992 | return weak_match; | |
993 | } | |
994 | else { | |
995 | *matched_ref = matched; | |
996 | return match; | |
997 | } | |
998 | } | |
999 | ||
1d735267 | 1000 | static void tail_link_ref(struct ref *ref, struct ref ***tail) |
6b62816c DB |
1001 | { |
1002 | **tail = ref; | |
1d735267 DB |
1003 | while (ref->next) |
1004 | ref = ref->next; | |
6b62816c | 1005 | *tail = &ref->next; |
6b62816c DB |
1006 | } |
1007 | ||
1008 | static struct ref *try_explicit_object_name(const char *name) | |
1009 | { | |
1010 | unsigned char sha1[20]; | |
1011 | struct ref *ref; | |
6b62816c DB |
1012 | |
1013 | if (!*name) { | |
59c69c0c | 1014 | ref = alloc_ref("(delete)"); |
6b62816c DB |
1015 | hashclr(ref->new_sha1); |
1016 | return ref; | |
1017 | } | |
1018 | if (get_sha1(name, sha1)) | |
1019 | return NULL; | |
59c69c0c | 1020 | ref = alloc_ref(name); |
6b62816c DB |
1021 | hashcpy(ref->new_sha1, sha1); |
1022 | return ref; | |
1023 | } | |
1024 | ||
1d735267 | 1025 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
6b62816c | 1026 | { |
59c69c0c | 1027 | struct ref *ret = alloc_ref(name); |
1d735267 DB |
1028 | tail_link_ref(ret, tail); |
1029 | return ret; | |
163f0ee5 | 1030 | } |
8558fd9e | 1031 | |
f8aae120 JK |
1032 | static char *guess_ref(const char *name, struct ref *peer) |
1033 | { | |
1034 | struct strbuf buf = STRBUF_INIT; | |
1035 | unsigned char sha1[20]; | |
1036 | ||
1037 | const char *r = resolve_ref(peer->name, sha1, 1, NULL); | |
1038 | if (!r) | |
1039 | return NULL; | |
1040 | ||
1041 | if (!prefixcmp(r, "refs/heads/")) | |
1042 | strbuf_addstr(&buf, "refs/heads/"); | |
1043 | else if (!prefixcmp(r, "refs/tags/")) | |
1044 | strbuf_addstr(&buf, "refs/tags/"); | |
1045 | else | |
1046 | return NULL; | |
1047 | ||
1048 | strbuf_addstr(&buf, name); | |
1049 | return strbuf_detach(&buf, NULL); | |
1050 | } | |
1051 | ||
54a8ad92 JH |
1052 | static int match_explicit(struct ref *src, struct ref *dst, |
1053 | struct ref ***dst_tail, | |
9a7bbd1d | 1054 | struct refspec *rs) |
6b62816c | 1055 | { |
54a8ad92 | 1056 | struct ref *matched_src, *matched_dst; |
cdf690e5 | 1057 | int copy_src; |
8558fd9e | 1058 | |
54a8ad92 | 1059 | const char *dst_value = rs->dst; |
f8aae120 | 1060 | char *dst_guess; |
6b62816c | 1061 | |
a83619d6 | 1062 | if (rs->pattern || rs->matching) |
9a7bbd1d | 1063 | return 0; |
8558fd9e | 1064 | |
54a8ad92 JH |
1065 | matched_src = matched_dst = NULL; |
1066 | switch (count_refspec_match(rs->src, src, &matched_src)) { | |
1067 | case 1: | |
cdf690e5 | 1068 | copy_src = 1; |
54a8ad92 JH |
1069 | break; |
1070 | case 0: | |
1071 | /* The source could be in the get_sha1() format | |
1072 | * not a reference name. :refs/other is a | |
1073 | * way to delete 'other' ref at the remote end. | |
1074 | */ | |
1075 | matched_src = try_explicit_object_name(rs->src); | |
7dfee372 | 1076 | if (!matched_src) |
9a7bbd1d | 1077 | return error("src refspec %s does not match any.", rs->src); |
cdf690e5 | 1078 | copy_src = 0; |
54a8ad92 JH |
1079 | break; |
1080 | default: | |
9a7bbd1d | 1081 | return error("src refspec %s matches more than one.", rs->src); |
54a8ad92 | 1082 | } |
3c8b7df1 | 1083 | |
4491e62a | 1084 | if (!dst_value) { |
9f0ea7e8 DB |
1085 | unsigned char sha1[20]; |
1086 | int flag; | |
1087 | ||
9f0ea7e8 DB |
1088 | dst_value = resolve_ref(matched_src->name, sha1, 1, &flag); |
1089 | if (!dst_value || | |
1090 | ((flag & REF_ISSYMREF) && | |
1091 | prefixcmp(dst_value, "refs/heads/"))) | |
1092 | die("%s cannot be resolved to branch.", | |
1093 | matched_src->name); | |
4491e62a | 1094 | } |
3c8b7df1 | 1095 | |
54a8ad92 JH |
1096 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
1097 | case 1: | |
1098 | break; | |
1099 | case 0: | |
163f0ee5 | 1100 | if (!memcmp(dst_value, "refs/", 5)) |
1d735267 | 1101 | matched_dst = make_linked_ref(dst_value, dst_tail); |
eeefa7c9 | 1102 | else if ((dst_guess = guess_ref(dst_value, matched_src))) |
f8aae120 | 1103 | matched_dst = make_linked_ref(dst_guess, dst_tail); |
3c8b7df1 | 1104 | else |
f8aae120 JK |
1105 | error("unable to push to unqualified destination: %s\n" |
1106 | "The destination refspec neither matches an " | |
1107 | "existing ref on the remote nor\n" | |
1108 | "begins with refs/, and we are unable to " | |
1109 | "guess a prefix based on the source ref.", | |
1110 | dst_value); | |
54a8ad92 JH |
1111 | break; |
1112 | default: | |
3c8b7df1 | 1113 | matched_dst = NULL; |
54a8ad92 JH |
1114 | error("dst refspec %s matches more than one.", |
1115 | dst_value); | |
1116 | break; | |
1117 | } | |
9a7bbd1d JK |
1118 | if (!matched_dst) |
1119 | return -1; | |
1120 | if (matched_dst->peer_ref) | |
1121 | return error("dst ref %s receives from more than one src.", | |
54a8ad92 | 1122 | matched_dst->name); |
54a8ad92 | 1123 | else { |
cdf690e5 | 1124 | matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src; |
54a8ad92 | 1125 | matched_dst->force = rs->force; |
6b62816c | 1126 | } |
9a7bbd1d | 1127 | return 0; |
54a8ad92 JH |
1128 | } |
1129 | ||
1130 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
1131 | struct ref ***dst_tail, struct refspec *rs, | |
1132 | int rs_nr) | |
1133 | { | |
1134 | int i, errs; | |
1135 | for (i = errs = 0; i < rs_nr; i++) | |
9a7bbd1d JK |
1136 | errs += match_explicit(src, dst, dst_tail, &rs[i]); |
1137 | return errs; | |
6b62816c DB |
1138 | } |
1139 | ||
6e66bf3c AR |
1140 | static const struct refspec *check_pattern_match(const struct refspec *rs, |
1141 | int rs_nr, | |
1142 | const struct ref *src) | |
8558fd9e DB |
1143 | { |
1144 | int i; | |
a83619d6 | 1145 | int matching_refs = -1; |
8558fd9e | 1146 | for (i = 0; i < rs_nr; i++) { |
a83619d6 PB |
1147 | if (rs[i].matching && |
1148 | (matching_refs == -1 || rs[i].force)) { | |
1149 | matching_refs = i; | |
1150 | continue; | |
1151 | } | |
1152 | ||
e928213f DB |
1153 | if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name, |
1154 | NULL, NULL)) | |
6e66bf3c | 1155 | return rs + i; |
8558fd9e | 1156 | } |
a83619d6 PB |
1157 | if (matching_refs != -1) |
1158 | return rs + matching_refs; | |
1159 | else | |
1160 | return NULL; | |
8558fd9e DB |
1161 | } |
1162 | ||
6d2bf96e CB |
1163 | static struct ref **tail_ref(struct ref **head) |
1164 | { | |
1165 | struct ref **tail = head; | |
1166 | while (*tail) | |
1167 | tail = &((*tail)->next); | |
1168 | return tail; | |
1169 | } | |
1170 | ||
54a8ad92 JH |
1171 | /* |
1172 | * Note. This is used only by "push"; refspec matching rules for | |
1173 | * push and fetch are subtly different, so do not try to reuse it | |
1174 | * without thinking. | |
1175 | */ | |
6d2bf96e | 1176 | int match_refs(struct ref *src, struct ref **dst, |
28b9d6e5 | 1177 | int nr_refspec, const char **refspec, int flags) |
6b62816c | 1178 | { |
a83619d6 | 1179 | struct refspec *rs; |
28b9d6e5 AW |
1180 | int send_all = flags & MATCH_REFS_ALL; |
1181 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
5f48cb95 | 1182 | int errs; |
2af202be | 1183 | static const char *default_refspec[] = { ":", NULL }; |
6d2bf96e | 1184 | struct ref **dst_tail = tail_ref(dst); |
6b62816c | 1185 | |
a83619d6 PB |
1186 | if (!nr_refspec) { |
1187 | nr_refspec = 1; | |
1188 | refspec = default_refspec; | |
1189 | } | |
1190 | rs = parse_push_refspec(nr_refspec, (const char **) refspec); | |
6d2bf96e | 1191 | errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec); |
6b62816c DB |
1192 | |
1193 | /* pick the remainder */ | |
1194 | for ( ; src; src = src->next) { | |
1195 | struct ref *dst_peer; | |
6e66bf3c AR |
1196 | const struct refspec *pat = NULL; |
1197 | char *dst_name; | |
6b62816c DB |
1198 | if (src->peer_ref) |
1199 | continue; | |
a83619d6 PB |
1200 | |
1201 | pat = check_pattern_match(rs, nr_refspec, src); | |
1202 | if (!pat) | |
1203 | continue; | |
1204 | ||
1205 | if (pat->matching) { | |
098e711e JH |
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 | */ | |
a83619d6 PB |
1211 | if (!send_mirror && prefixcmp(src->name, "refs/heads/")) |
1212 | continue; | |
1213 | dst_name = xstrdup(src->name); | |
8558fd9e | 1214 | |
a83619d6 | 1215 | } else { |
efd8f793 | 1216 | const char *dst_side = pat->dst ? pat->dst : pat->src; |
e928213f DB |
1217 | if (!match_name_with_pattern(pat->src, src->name, |
1218 | dst_side, &dst_name)) | |
1219 | die("Didn't think it matches any more"); | |
a83619d6 | 1220 | } |
6d2bf96e | 1221 | dst_peer = find_ref_by_name(*dst, dst_name); |
a83619d6 PB |
1222 | if (dst_peer) { |
1223 | if (dst_peer->peer_ref) | |
1224 | /* We're already sending something to this ref. */ | |
1225 | goto free_name; | |
1226 | ||
1227 | } else { | |
1228 | if (pat->matching && !(send_all || send_mirror)) | |
1229 | /* | |
1230 | * Remote doesn't have it, and we have no | |
1231 | * explicit pattern, and we don't have | |
1232 | * --all nor --mirror. | |
1233 | */ | |
1234 | goto free_name; | |
28b9d6e5 | 1235 | |
6b62816c | 1236 | /* Create a new one and link it */ |
6d2bf96e | 1237 | dst_peer = make_linked_ref(dst_name, &dst_tail); |
6b62816c | 1238 | hashcpy(dst_peer->new_sha1, src->new_sha1); |
6b62816c | 1239 | } |
cdf690e5 | 1240 | dst_peer->peer_ref = copy_ref(src); |
a83619d6 | 1241 | dst_peer->force = pat->force; |
6e66bf3c AR |
1242 | free_name: |
1243 | free(dst_name); | |
6b62816c | 1244 | } |
5f48cb95 JS |
1245 | if (errs) |
1246 | return -1; | |
6b62816c DB |
1247 | return 0; |
1248 | } | |
cf818348 | 1249 | |
20e8b465 TRC |
1250 | void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, |
1251 | int force_update) | |
1252 | { | |
1253 | struct ref *ref; | |
1254 | ||
1255 | for (ref = remote_refs; ref; ref = ref->next) { | |
1256 | if (ref->peer_ref) | |
1257 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); | |
1258 | else if (!send_mirror) | |
1259 | continue; | |
1260 | ||
1261 | ref->deletion = is_null_sha1(ref->new_sha1); | |
1262 | if (!ref->deletion && | |
1263 | !hashcmp(ref->old_sha1, ref->new_sha1)) { | |
1264 | ref->status = REF_STATUS_UPTODATE; | |
1265 | continue; | |
1266 | } | |
1267 | ||
1268 | /* This part determines what can overwrite what. | |
1269 | * The rules are: | |
1270 | * | |
1271 | * (0) you can always use --force or +A:B notation to | |
1272 | * selectively force individual ref pairs. | |
1273 | * | |
1274 | * (1) if the old thing does not exist, it is OK. | |
1275 | * | |
1276 | * (2) if you do not have the old thing, you are not allowed | |
1277 | * to overwrite it; you would not know what you are losing | |
1278 | * otherwise. | |
1279 | * | |
1280 | * (3) if both new and old are commit-ish, and new is a | |
1281 | * descendant of old, it is OK. | |
1282 | * | |
1283 | * (4) regardless of all of the above, removing :B is | |
1284 | * always allowed. | |
1285 | */ | |
1286 | ||
1287 | ref->nonfastforward = | |
1288 | !ref->deletion && | |
1289 | !is_null_sha1(ref->old_sha1) && | |
1290 | (!has_sha1_file(ref->old_sha1) | |
1291 | || !ref_newer(ref->new_sha1, ref->old_sha1)); | |
1292 | ||
1293 | if (ref->nonfastforward && !ref->force && !force_update) { | |
1294 | ref->status = REF_STATUS_REJECT_NONFASTFORWARD; | |
1295 | continue; | |
1296 | } | |
1297 | } | |
1298 | } | |
1299 | ||
cf818348 DB |
1300 | struct branch *branch_get(const char *name) |
1301 | { | |
1302 | struct branch *ret; | |
1303 | ||
1304 | read_config(); | |
1305 | if (!name || !*name || !strcmp(name, "HEAD")) | |
1306 | ret = current_branch; | |
1307 | else | |
1308 | ret = make_branch(name, 0); | |
1309 | if (ret && ret->remote_name) { | |
1310 | ret->remote = remote_get(ret->remote_name); | |
1311 | if (ret->merge_nr) { | |
1312 | int i; | |
1313 | ret->merge = xcalloc(sizeof(*ret->merge), | |
1314 | ret->merge_nr); | |
1315 | for (i = 0; i < ret->merge_nr; i++) { | |
1316 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); | |
1317 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); | |
5e6e2b48 MG |
1318 | if (remote_find_tracking(ret->remote, ret->merge[i]) |
1319 | && !strcmp(ret->remote_name, ".")) | |
1320 | ret->merge[i]->dst = xstrdup(ret->merge_name[i]); | |
cf818348 DB |
1321 | } |
1322 | } | |
1323 | } | |
1324 | return ret; | |
1325 | } | |
1326 | ||
1327 | int branch_has_merge_config(struct branch *branch) | |
1328 | { | |
1329 | return branch && !!branch->merge; | |
1330 | } | |
1331 | ||
85682c19 SP |
1332 | int branch_merge_matches(struct branch *branch, |
1333 | int i, | |
1334 | const char *refname) | |
cf818348 | 1335 | { |
85682c19 | 1336 | if (!branch || i < 0 || i >= branch->merge_nr) |
cf818348 | 1337 | return 0; |
605b4978 | 1338 | return refname_match(branch->merge[i]->src, refname, ref_fetch_rules); |
cf818348 | 1339 | } |
d71ab174 | 1340 | |
4577370e | 1341 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
d71ab174 DB |
1342 | const struct refspec *refspec) |
1343 | { | |
4577370e | 1344 | const struct ref *ref; |
d71ab174 DB |
1345 | struct ref *ret = NULL; |
1346 | struct ref **tail = &ret; | |
1347 | ||
e928213f | 1348 | char *expn_name; |
d71ab174 DB |
1349 | |
1350 | for (ref = remote_refs; ref; ref = ref->next) { | |
1351 | if (strchr(ref->name, '^')) | |
1352 | continue; /* a dereference item */ | |
e928213f DB |
1353 | if (match_name_with_pattern(refspec->src, ref->name, |
1354 | refspec->dst, &expn_name)) { | |
d71ab174 | 1355 | struct ref *cpy = copy_ref(ref); |
d71ab174 | 1356 | |
e928213f DB |
1357 | cpy->peer_ref = alloc_ref(expn_name); |
1358 | free(expn_name); | |
d71ab174 DB |
1359 | if (refspec->force) |
1360 | cpy->peer_ref->force = 1; | |
1361 | *tail = cpy; | |
1362 | tail = &cpy->next; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | return ret; | |
1367 | } | |
1368 | ||
4577370e | 1369 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
d71ab174 | 1370 | { |
4577370e | 1371 | const struct ref *ref; |
d71ab174 | 1372 | for (ref = refs; ref; ref = ref->next) { |
605b4978 | 1373 | if (refname_match(name, ref->name, ref_fetch_rules)) |
d71ab174 DB |
1374 | return ref; |
1375 | } | |
1376 | return NULL; | |
1377 | } | |
1378 | ||
4577370e | 1379 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
d71ab174 | 1380 | { |
4577370e | 1381 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
d71ab174 DB |
1382 | |
1383 | if (!ref) | |
9ad7c5ae | 1384 | return NULL; |
d71ab174 DB |
1385 | |
1386 | return copy_ref(ref); | |
1387 | } | |
1388 | ||
1389 | static struct ref *get_local_ref(const char *name) | |
1390 | { | |
3eb96997 | 1391 | if (!name || name[0] == '\0') |
d71ab174 DB |
1392 | return NULL; |
1393 | ||
59c69c0c RS |
1394 | if (!prefixcmp(name, "refs/")) |
1395 | return alloc_ref(name); | |
d71ab174 DB |
1396 | |
1397 | if (!prefixcmp(name, "heads/") || | |
1398 | !prefixcmp(name, "tags/") || | |
8009768e RS |
1399 | !prefixcmp(name, "remotes/")) |
1400 | return alloc_ref_with_prefix("refs/", 5, name); | |
d71ab174 | 1401 | |
8009768e | 1402 | return alloc_ref_with_prefix("refs/heads/", 11, name); |
d71ab174 DB |
1403 | } |
1404 | ||
4577370e | 1405 | int get_fetch_map(const struct ref *remote_refs, |
d71ab174 | 1406 | const struct refspec *refspec, |
9ad7c5ae JH |
1407 | struct ref ***tail, |
1408 | int missing_ok) | |
d71ab174 | 1409 | { |
ef00d150 | 1410 | struct ref *ref_map, **rmp; |
d71ab174 DB |
1411 | |
1412 | if (refspec->pattern) { | |
1413 | ref_map = get_expanded_map(remote_refs, refspec); | |
1414 | } else { | |
9ad7c5ae JH |
1415 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
1416 | ||
1417 | ref_map = get_remote_ref(remote_refs, name); | |
1418 | if (!missing_ok && !ref_map) | |
1419 | die("Couldn't find remote ref %s", name); | |
1420 | if (ref_map) { | |
1421 | ref_map->peer_ref = get_local_ref(refspec->dst); | |
1422 | if (ref_map->peer_ref && refspec->force) | |
1423 | ref_map->peer_ref->force = 1; | |
1424 | } | |
d71ab174 DB |
1425 | } |
1426 | ||
ef00d150 DB |
1427 | for (rmp = &ref_map; *rmp; ) { |
1428 | if ((*rmp)->peer_ref) { | |
1429 | int st = check_ref_format((*rmp)->peer_ref->name + 5); | |
1430 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) { | |
1431 | struct ref *ignore = *rmp; | |
1432 | error("* Ignoring funny ref '%s' locally", | |
1433 | (*rmp)->peer_ref->name); | |
1434 | *rmp = (*rmp)->next; | |
1435 | free(ignore->peer_ref); | |
1436 | free(ignore); | |
1437 | continue; | |
1438 | } | |
1439 | } | |
1440 | rmp = &((*rmp)->next); | |
d71ab174 DB |
1441 | } |
1442 | ||
8f70a765 AR |
1443 | if (ref_map) |
1444 | tail_link_ref(ref_map, tail); | |
d71ab174 DB |
1445 | |
1446 | return 0; | |
1447 | } | |
be885d96 DB |
1448 | |
1449 | int resolve_remote_symref(struct ref *ref, struct ref *list) | |
1450 | { | |
1451 | if (!ref->symref) | |
1452 | return 0; | |
1453 | for (; list; list = list->next) | |
1454 | if (!strcmp(ref->symref, list->name)) { | |
1455 | hashcpy(ref->old_sha1, list->old_sha1); | |
1456 | return 0; | |
1457 | } | |
1458 | return 1; | |
1459 | } | |
6d21bf96 | 1460 | |
ec8452d5 JS |
1461 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
1462 | { | |
1463 | while (list) { | |
1464 | struct commit_list *temp = list; | |
1465 | temp->item->object.flags &= ~mark; | |
1466 | list = temp->next; | |
1467 | free(temp); | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) | |
1472 | { | |
1473 | struct object *o; | |
1474 | struct commit *old, *new; | |
1475 | struct commit_list *list, *used; | |
1476 | int found = 0; | |
1477 | ||
1478 | /* Both new and old must be commit-ish and new is descendant of | |
1479 | * old. Otherwise we require --force. | |
1480 | */ | |
1481 | o = deref_tag(parse_object(old_sha1), NULL, 0); | |
1482 | if (!o || o->type != OBJ_COMMIT) | |
1483 | return 0; | |
1484 | old = (struct commit *) o; | |
1485 | ||
1486 | o = deref_tag(parse_object(new_sha1), NULL, 0); | |
1487 | if (!o || o->type != OBJ_COMMIT) | |
1488 | return 0; | |
1489 | new = (struct commit *) o; | |
1490 | ||
1491 | if (parse_commit(new) < 0) | |
1492 | return 0; | |
1493 | ||
1494 | used = list = NULL; | |
1495 | commit_list_insert(new, &list); | |
1496 | while (list) { | |
1497 | new = pop_most_recent_commit(&list, TMP_MARK); | |
1498 | commit_list_insert(new, &used); | |
1499 | if (new == old) { | |
1500 | found = 1; | |
1501 | break; | |
1502 | } | |
1503 | } | |
1504 | unmark_and_free(list, TMP_MARK); | |
1505 | unmark_and_free(used, TMP_MARK); | |
1506 | return found; | |
1507 | } | |
1508 | ||
6d21bf96 JH |
1509 | /* |
1510 | * Return true if there is anything to report, otherwise false. | |
1511 | */ | |
1512 | int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs) | |
1513 | { | |
1514 | unsigned char sha1[20]; | |
1515 | struct commit *ours, *theirs; | |
1516 | char symmetric[84]; | |
1517 | struct rev_info revs; | |
1518 | const char *rev_argv[10], *base; | |
1519 | int rev_argc; | |
1520 | ||
1521 | /* | |
1522 | * Nothing to report unless we are marked to build on top of | |
1523 | * somebody else. | |
1524 | */ | |
1525 | if (!branch || | |
1526 | !branch->merge || !branch->merge[0] || !branch->merge[0]->dst) | |
1527 | return 0; | |
1528 | ||
1529 | /* | |
1530 | * If what we used to build on no longer exists, there is | |
1531 | * nothing to report. | |
1532 | */ | |
1533 | base = branch->merge[0]->dst; | |
1534 | if (!resolve_ref(base, sha1, 1, NULL)) | |
1535 | return 0; | |
57ffc5f8 | 1536 | theirs = lookup_commit_reference(sha1); |
6d21bf96 JH |
1537 | if (!theirs) |
1538 | return 0; | |
1539 | ||
1540 | if (!resolve_ref(branch->refname, sha1, 1, NULL)) | |
1541 | return 0; | |
57ffc5f8 | 1542 | ours = lookup_commit_reference(sha1); |
6d21bf96 JH |
1543 | if (!ours) |
1544 | return 0; | |
1545 | ||
1546 | /* are we the same? */ | |
1547 | if (theirs == ours) | |
1548 | return 0; | |
1549 | ||
8fbf879e | 1550 | /* Run "rev-list --left-right ours...theirs" internally... */ |
6d21bf96 JH |
1551 | rev_argc = 0; |
1552 | rev_argv[rev_argc++] = NULL; | |
1553 | rev_argv[rev_argc++] = "--left-right"; | |
1554 | rev_argv[rev_argc++] = symmetric; | |
1555 | rev_argv[rev_argc++] = "--"; | |
1556 | rev_argv[rev_argc] = NULL; | |
1557 | ||
1558 | strcpy(symmetric, sha1_to_hex(ours->object.sha1)); | |
1559 | strcpy(symmetric + 40, "..."); | |
1560 | strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1)); | |
1561 | ||
1562 | init_revisions(&revs, NULL); | |
1563 | setup_revisions(rev_argc, rev_argv, &revs, NULL); | |
1564 | prepare_revision_walk(&revs); | |
1565 | ||
1566 | /* ... and count the commits on each side. */ | |
1567 | *num_ours = 0; | |
1568 | *num_theirs = 0; | |
1569 | while (1) { | |
1570 | struct commit *c = get_revision(&revs); | |
1571 | if (!c) | |
1572 | break; | |
1573 | if (c->object.flags & SYMMETRIC_LEFT) | |
1574 | (*num_ours)++; | |
1575 | else | |
1576 | (*num_theirs)++; | |
1577 | } | |
c0234b2e JH |
1578 | |
1579 | /* clear object flags smudged by the above traversal */ | |
1580 | clear_commit_marks(ours, ALL_REV_FLAGS); | |
1581 | clear_commit_marks(theirs, ALL_REV_FLAGS); | |
6d21bf96 JH |
1582 | return 1; |
1583 | } | |
1584 | ||
1585 | /* | |
1586 | * Return true when there is anything to report, otherwise false. | |
1587 | */ | |
1588 | int format_tracking_info(struct branch *branch, struct strbuf *sb) | |
1589 | { | |
1590 | int num_ours, num_theirs; | |
4de53ce0 | 1591 | const char *base; |
6d21bf96 JH |
1592 | |
1593 | if (!stat_tracking_info(branch, &num_ours, &num_theirs)) | |
1594 | return 0; | |
1595 | ||
1596 | base = branch->merge[0]->dst; | |
45972ffb | 1597 | base = shorten_unambiguous_ref(base, 0); |
6d21bf96 | 1598 | if (!num_theirs) |
4de53ce0 | 1599 | strbuf_addf(sb, "Your branch is ahead of '%s' " |
6d21bf96 | 1600 | "by %d commit%s.\n", |
4de53ce0 | 1601 | base, num_ours, (num_ours == 1) ? "" : "s"); |
6d21bf96 | 1602 | else if (!num_ours) |
4de53ce0 AP |
1603 | strbuf_addf(sb, "Your branch is behind '%s' " |
1604 | "by %d commit%s, " | |
6d21bf96 | 1605 | "and can be fast-forwarded.\n", |
4de53ce0 | 1606 | base, num_theirs, (num_theirs == 1) ? "" : "s"); |
6d21bf96 | 1607 | else |
4de53ce0 AP |
1608 | strbuf_addf(sb, "Your branch and '%s' have diverged,\n" |
1609 | "and have %d and %d different commit(s) each, " | |
1610 | "respectively.\n", | |
1611 | base, num_ours, num_theirs); | |
6d21bf96 JH |
1612 | return 1; |
1613 | } | |
454e2025 JS |
1614 | |
1615 | static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) | |
1616 | { | |
1617 | struct ref ***local_tail = cb_data; | |
1618 | struct ref *ref; | |
1619 | int len; | |
1620 | ||
1621 | /* we already know it starts with refs/ to get here */ | |
1622 | if (check_ref_format(refname + 5)) | |
1623 | return 0; | |
1624 | ||
1625 | len = strlen(refname) + 1; | |
1626 | ref = xcalloc(1, sizeof(*ref) + len); | |
1627 | hashcpy(ref->new_sha1, sha1); | |
1628 | memcpy(ref->name, refname, len); | |
1629 | **local_tail = ref; | |
1630 | *local_tail = &ref->next; | |
1631 | return 0; | |
1632 | } | |
1633 | ||
1634 | struct ref *get_local_heads(void) | |
1635 | { | |
55f0566f | 1636 | struct ref *local_refs = NULL, **local_tail = &local_refs; |
454e2025 JS |
1637 | for_each_ref(one_local_ref, &local_tail); |
1638 | return local_refs; | |
1639 | } | |
8ef51733 | 1640 | |
4229f1fa JS |
1641 | struct ref *guess_remote_head(const struct ref *head, |
1642 | const struct ref *refs, | |
1643 | int all) | |
8ef51733 | 1644 | { |
8ef51733 | 1645 | const struct ref *r; |
4229f1fa JS |
1646 | struct ref *list = NULL; |
1647 | struct ref **tail = &list; | |
8ef51733 | 1648 | |
6cb4e6cc | 1649 | if (!head) |
8ef51733 JS |
1650 | return NULL; |
1651 | ||
fbb074c2 JK |
1652 | /* |
1653 | * Some transports support directly peeking at | |
1654 | * where HEAD points; if that is the case, then | |
1655 | * we don't have to guess. | |
1656 | */ | |
1657 | if (head->symref) | |
1658 | return copy_ref(find_ref_by_name(refs, head->symref)); | |
1659 | ||
8ef51733 | 1660 | /* If refs/heads/master could be right, it is. */ |
4229f1fa JS |
1661 | if (!all) { |
1662 | r = find_ref_by_name(refs, "refs/heads/master"); | |
1663 | if (r && !hashcmp(r->old_sha1, head->old_sha1)) | |
1664 | return copy_ref(r); | |
1665 | } | |
8ef51733 JS |
1666 | |
1667 | /* Look for another ref that points there */ | |
4229f1fa JS |
1668 | for (r = refs; r; r = r->next) { |
1669 | if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) { | |
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 | |
1680 | struct stale_heads_info { | |
1681 | struct remote *remote; | |
1682 | struct string_list *ref_names; | |
1683 | struct ref **stale_refs_tail; | |
1684 | }; | |
1685 | ||
1686 | static 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 | ||
1703 | struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map) | |
1704 | { | |
1705 | struct ref *ref, *stale_refs = NULL; | |
1706 | struct string_list ref_names = { NULL, 0, 0, 0 }; | |
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) | |
1712 | string_list_append(ref->name, &ref_names); | |
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 | } |