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