]>
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" | |
5751f490 | 7 | |
e0aaa29f DB |
8 | static struct refspec s_tag_refspec = { |
9 | 0, | |
10 | 1, | |
b84c343c | 11 | 0, |
e0aaa29f DB |
12 | "refs/tags/", |
13 | "refs/tags/" | |
14 | }; | |
15 | ||
16 | const struct refspec *tag_refspec = &s_tag_refspec; | |
17 | ||
844112ca JH |
18 | struct counted_string { |
19 | size_t len; | |
20 | const char *s; | |
21 | }; | |
55029ae4 DB |
22 | struct rewrite { |
23 | const char *base; | |
844112ca JH |
24 | size_t baselen; |
25 | struct counted_string *instead_of; | |
55029ae4 DB |
26 | int instead_of_nr; |
27 | int instead_of_alloc; | |
28 | }; | |
29 | ||
5751f490 | 30 | static struct remote **remotes; |
2d31347b DB |
31 | static int remotes_alloc; |
32 | static int remotes_nr; | |
5751f490 | 33 | |
cf818348 | 34 | static struct branch **branches; |
2d31347b DB |
35 | static int branches_alloc; |
36 | static int branches_nr; | |
cf818348 DB |
37 | |
38 | static struct branch *current_branch; | |
39 | static const char *default_remote_name; | |
40 | ||
55029ae4 DB |
41 | static struct rewrite **rewrite; |
42 | static int rewrite_alloc; | |
43 | static int rewrite_nr; | |
44 | ||
5751f490 DB |
45 | #define BUF_SIZE (2048) |
46 | static char buffer[BUF_SIZE]; | |
47 | ||
55029ae4 DB |
48 | static const char *alias_url(const char *url) |
49 | { | |
50 | int i, j; | |
844112ca JH |
51 | char *ret; |
52 | struct counted_string *longest; | |
53 | int longest_i; | |
54 | ||
55 | longest = NULL; | |
56 | longest_i = -1; | |
55029ae4 DB |
57 | for (i = 0; i < rewrite_nr; i++) { |
58 | if (!rewrite[i]) | |
59 | continue; | |
60 | for (j = 0; j < rewrite[i]->instead_of_nr; j++) { | |
844112ca JH |
61 | if (!prefixcmp(url, rewrite[i]->instead_of[j].s) && |
62 | (!longest || | |
63 | longest->len < rewrite[i]->instead_of[j].len)) { | |
64 | longest = &(rewrite[i]->instead_of[j]); | |
65 | longest_i = i; | |
55029ae4 DB |
66 | } |
67 | } | |
68 | } | |
844112ca JH |
69 | if (!longest) |
70 | return url; | |
71 | ||
72 | ret = malloc(rewrite[longest_i]->baselen + | |
73 | (strlen(url) - longest->len) + 1); | |
74 | strcpy(ret, rewrite[longest_i]->base); | |
75 | strcpy(ret + rewrite[longest_i]->baselen, url + longest->len); | |
76 | return ret; | |
55029ae4 DB |
77 | } |
78 | ||
5751f490 DB |
79 | static void add_push_refspec(struct remote *remote, const char *ref) |
80 | { | |
2d31347b DB |
81 | ALLOC_GROW(remote->push_refspec, |
82 | remote->push_refspec_nr + 1, | |
83 | remote->push_refspec_alloc); | |
84 | remote->push_refspec[remote->push_refspec_nr++] = ref; | |
5751f490 DB |
85 | } |
86 | ||
5d46c9d4 DB |
87 | static void add_fetch_refspec(struct remote *remote, const char *ref) |
88 | { | |
2d31347b DB |
89 | ALLOC_GROW(remote->fetch_refspec, |
90 | remote->fetch_refspec_nr + 1, | |
91 | remote->fetch_refspec_alloc); | |
92 | remote->fetch_refspec[remote->fetch_refspec_nr++] = ref; | |
5d46c9d4 DB |
93 | } |
94 | ||
28b91f8a | 95 | static void add_url(struct remote *remote, const char *url) |
5751f490 | 96 | { |
2d31347b DB |
97 | ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc); |
98 | remote->url[remote->url_nr++] = url; | |
5751f490 DB |
99 | } |
100 | ||
55029ae4 DB |
101 | static void add_url_alias(struct remote *remote, const char *url) |
102 | { | |
103 | add_url(remote, alias_url(url)); | |
104 | } | |
105 | ||
5751f490 DB |
106 | static struct remote *make_remote(const char *name, int len) |
107 | { | |
2d31347b DB |
108 | struct remote *ret; |
109 | int i; | |
5751f490 | 110 | |
2d31347b DB |
111 | for (i = 0; i < remotes_nr; i++) { |
112 | if (len ? (!strncmp(name, remotes[i]->name, len) && | |
113 | !remotes[i]->name[len]) : | |
114 | !strcmp(name, remotes[i]->name)) | |
115 | return remotes[i]; | |
5751f490 DB |
116 | } |
117 | ||
2d31347b DB |
118 | ret = xcalloc(1, sizeof(struct remote)); |
119 | ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc); | |
120 | remotes[remotes_nr++] = ret; | |
5751f490 | 121 | if (len) |
2d31347b | 122 | ret->name = xstrndup(name, len); |
5751f490 | 123 | else |
2d31347b DB |
124 | ret->name = xstrdup(name); |
125 | return ret; | |
5751f490 DB |
126 | } |
127 | ||
cf818348 DB |
128 | static void add_merge(struct branch *branch, const char *name) |
129 | { | |
2d31347b DB |
130 | ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, |
131 | branch->merge_alloc); | |
132 | branch->merge_name[branch->merge_nr++] = name; | |
cf818348 DB |
133 | } |
134 | ||
135 | static struct branch *make_branch(const char *name, int len) | |
136 | { | |
2d31347b DB |
137 | struct branch *ret; |
138 | int i; | |
cf818348 DB |
139 | char *refname; |
140 | ||
2d31347b DB |
141 | for (i = 0; i < branches_nr; i++) { |
142 | if (len ? (!strncmp(name, branches[i]->name, len) && | |
143 | !branches[i]->name[len]) : | |
144 | !strcmp(name, branches[i]->name)) | |
145 | return branches[i]; | |
cf818348 DB |
146 | } |
147 | ||
2d31347b DB |
148 | ALLOC_GROW(branches, branches_nr + 1, branches_alloc); |
149 | ret = xcalloc(1, sizeof(struct branch)); | |
150 | branches[branches_nr++] = ret; | |
cf818348 | 151 | if (len) |
2d31347b | 152 | ret->name = xstrndup(name, len); |
cf818348 | 153 | else |
2d31347b | 154 | ret->name = xstrdup(name); |
cf818348 DB |
155 | refname = malloc(strlen(name) + strlen("refs/heads/") + 1); |
156 | strcpy(refname, "refs/heads/"); | |
2d31347b DB |
157 | strcpy(refname + strlen("refs/heads/"), ret->name); |
158 | ret->refname = refname; | |
cf818348 | 159 | |
2d31347b | 160 | return ret; |
cf818348 DB |
161 | } |
162 | ||
55029ae4 DB |
163 | static struct rewrite *make_rewrite(const char *base, int len) |
164 | { | |
165 | struct rewrite *ret; | |
166 | int i; | |
167 | ||
168 | for (i = 0; i < rewrite_nr; i++) { | |
844112ca JH |
169 | if (len |
170 | ? (len == rewrite[i]->baselen && | |
171 | !strncmp(base, rewrite[i]->base, len)) | |
172 | : !strcmp(base, rewrite[i]->base)) | |
55029ae4 DB |
173 | return rewrite[i]; |
174 | } | |
175 | ||
176 | ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc); | |
177 | ret = xcalloc(1, sizeof(struct rewrite)); | |
178 | rewrite[rewrite_nr++] = ret; | |
844112ca | 179 | if (len) { |
55029ae4 | 180 | ret->base = xstrndup(base, len); |
844112ca JH |
181 | ret->baselen = len; |
182 | } | |
183 | else { | |
55029ae4 | 184 | ret->base = xstrdup(base); |
844112ca JH |
185 | ret->baselen = strlen(base); |
186 | } | |
55029ae4 DB |
187 | return ret; |
188 | } | |
189 | ||
190 | static void add_instead_of(struct rewrite *rewrite, const char *instead_of) | |
191 | { | |
192 | ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc); | |
844112ca JH |
193 | rewrite->instead_of[rewrite->instead_of_nr].s = instead_of; |
194 | rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of); | |
195 | rewrite->instead_of_nr++; | |
55029ae4 DB |
196 | } |
197 | ||
5751f490 DB |
198 | static void read_remotes_file(struct remote *remote) |
199 | { | |
200 | FILE *f = fopen(git_path("remotes/%s", remote->name), "r"); | |
201 | ||
202 | if (!f) | |
203 | return; | |
204 | while (fgets(buffer, BUF_SIZE, f)) { | |
205 | int value_list; | |
206 | char *s, *p; | |
207 | ||
208 | if (!prefixcmp(buffer, "URL:")) { | |
209 | value_list = 0; | |
210 | s = buffer + 4; | |
211 | } else if (!prefixcmp(buffer, "Push:")) { | |
212 | value_list = 1; | |
213 | s = buffer + 5; | |
5d46c9d4 DB |
214 | } else if (!prefixcmp(buffer, "Pull:")) { |
215 | value_list = 2; | |
216 | s = buffer + 5; | |
5751f490 DB |
217 | } else |
218 | continue; | |
219 | ||
220 | while (isspace(*s)) | |
221 | s++; | |
222 | if (!*s) | |
223 | continue; | |
224 | ||
225 | p = s + strlen(s); | |
226 | while (isspace(p[-1])) | |
227 | *--p = 0; | |
228 | ||
229 | switch (value_list) { | |
230 | case 0: | |
55029ae4 | 231 | add_url_alias(remote, xstrdup(s)); |
5751f490 DB |
232 | break; |
233 | case 1: | |
234 | add_push_refspec(remote, xstrdup(s)); | |
235 | break; | |
5d46c9d4 DB |
236 | case 2: |
237 | add_fetch_refspec(remote, xstrdup(s)); | |
238 | break; | |
5751f490 DB |
239 | } |
240 | } | |
241 | fclose(f); | |
242 | } | |
243 | ||
244 | static void read_branches_file(struct remote *remote) | |
245 | { | |
246 | const char *slash = strchr(remote->name, '/'); | |
cf818348 | 247 | char *frag; |
472fa4cd | 248 | struct strbuf branch; |
5751f490 DB |
249 | int n = slash ? slash - remote->name : 1000; |
250 | FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r"); | |
251 | char *s, *p; | |
252 | int len; | |
253 | ||
254 | if (!f) | |
255 | return; | |
256 | s = fgets(buffer, BUF_SIZE, f); | |
257 | fclose(f); | |
258 | if (!s) | |
259 | return; | |
260 | while (isspace(*s)) | |
261 | s++; | |
262 | if (!*s) | |
263 | return; | |
264 | p = s + strlen(s); | |
265 | while (isspace(p[-1])) | |
266 | *--p = 0; | |
267 | len = p - s; | |
268 | if (slash) | |
269 | len += strlen(slash); | |
270 | p = xmalloc(len + 1); | |
271 | strcpy(p, s); | |
272 | if (slash) | |
273 | strcat(p, slash); | |
472fa4cd DB |
274 | |
275 | /* | |
276 | * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when | |
277 | * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from | |
278 | * the partial URL obtained from the branches file plus | |
279 | * "/netdev-2.6" and does not store it in any tracking ref. | |
280 | * #branch specifier in the file is ignored. | |
281 | * | |
282 | * Otherwise, the branches file would have URL and optionally | |
283 | * #branch specified. The "master" (or specified) branch is | |
284 | * fetched and stored in the local branch of the same name. | |
285 | */ | |
286 | strbuf_init(&branch, 0); | |
cf818348 DB |
287 | frag = strchr(p, '#'); |
288 | if (frag) { | |
289 | *(frag++) = '\0'; | |
472fa4cd DB |
290 | strbuf_addf(&branch, "refs/heads/%s", frag); |
291 | } else | |
292 | strbuf_addstr(&branch, "refs/heads/master"); | |
293 | if (!slash) { | |
294 | strbuf_addf(&branch, ":refs/heads/%s", remote->name); | |
cf818348 | 295 | } else { |
472fa4cd DB |
296 | strbuf_reset(&branch); |
297 | strbuf_addstr(&branch, "HEAD:"); | |
cf818348 | 298 | } |
55029ae4 | 299 | add_url_alias(remote, p); |
472fa4cd | 300 | add_fetch_refspec(remote, strbuf_detach(&branch, 0)); |
18afe101 MK |
301 | /* |
302 | * Cogito compatible push: push current HEAD to remote #branch | |
303 | * (master if missing) | |
304 | */ | |
305 | strbuf_init(&branch, 0); | |
306 | strbuf_addstr(&branch, "HEAD"); | |
307 | if (frag) | |
308 | strbuf_addf(&branch, ":refs/heads/%s", frag); | |
309 | else | |
310 | strbuf_addstr(&branch, ":refs/heads/master"); | |
311 | add_push_refspec(remote, strbuf_detach(&branch, 0)); | |
d71ab174 | 312 | remote->fetch_tags = 1; /* always auto-follow */ |
5751f490 DB |
313 | } |
314 | ||
ef90d6d4 | 315 | static int handle_config(const char *key, const char *value, void *cb) |
5751f490 DB |
316 | { |
317 | const char *name; | |
318 | const char *subkey; | |
319 | struct remote *remote; | |
cf818348 DB |
320 | struct branch *branch; |
321 | if (!prefixcmp(key, "branch.")) { | |
322 | name = key + 7; | |
323 | subkey = strrchr(name, '.'); | |
cf818348 DB |
324 | if (!subkey) |
325 | return 0; | |
896c0535 | 326 | branch = make_branch(name, subkey - name); |
cf818348 | 327 | if (!strcmp(subkey, ".remote")) { |
d2370cc2 JH |
328 | if (!value) |
329 | return config_error_nonbool(key); | |
cf818348 DB |
330 | branch->remote_name = xstrdup(value); |
331 | if (branch == current_branch) | |
332 | default_remote_name = branch->remote_name; | |
d2370cc2 JH |
333 | } else if (!strcmp(subkey, ".merge")) { |
334 | if (!value) | |
335 | return config_error_nonbool(key); | |
cf818348 | 336 | add_merge(branch, xstrdup(value)); |
d2370cc2 | 337 | } |
cf818348 | 338 | return 0; |
5751f490 | 339 | } |
55029ae4 DB |
340 | if (!prefixcmp(key, "url.")) { |
341 | struct rewrite *rewrite; | |
60e3aba9 | 342 | name = key + 4; |
55029ae4 DB |
343 | subkey = strrchr(name, '.'); |
344 | if (!subkey) | |
345 | return 0; | |
346 | rewrite = make_rewrite(name, subkey - name); | |
347 | if (!strcmp(subkey, ".insteadof")) { | |
348 | if (!value) | |
349 | return config_error_nonbool(key); | |
350 | add_instead_of(rewrite, xstrdup(value)); | |
351 | } | |
352 | } | |
5751f490 DB |
353 | if (prefixcmp(key, "remote.")) |
354 | return 0; | |
355 | name = key + 7; | |
c82efafc BC |
356 | if (*name == '/') { |
357 | warning("Config remote shorthand cannot begin with '/': %s", | |
358 | name); | |
359 | return 0; | |
360 | } | |
5751f490 DB |
361 | subkey = strrchr(name, '.'); |
362 | if (!subkey) | |
363 | return error("Config with no key for remote %s", name); | |
5751f490 | 364 | remote = make_remote(name, subkey - name); |
84bb2dfd PB |
365 | if (!strcmp(subkey, ".mirror")) |
366 | remote->mirror = git_config_bool(key, value); | |
367 | else if (!strcmp(subkey, ".skipdefaultupdate")) | |
368 | remote->skip_default_update = git_config_bool(key, value); | |
369 | ||
370 | else if (!strcmp(subkey, ".url")) { | |
371 | const char *v; | |
372 | if (git_config_string(&v, key, value)) | |
373 | return -1; | |
374 | add_url(remote, v); | |
5751f490 | 375 | } else if (!strcmp(subkey, ".push")) { |
84bb2dfd PB |
376 | const char *v; |
377 | if (git_config_string(&v, key, value)) | |
378 | return -1; | |
379 | add_push_refspec(remote, v); | |
5d46c9d4 | 380 | } else if (!strcmp(subkey, ".fetch")) { |
84bb2dfd PB |
381 | const char *v; |
382 | if (git_config_string(&v, key, value)) | |
383 | return -1; | |
384 | add_fetch_refspec(remote, v); | |
5751f490 | 385 | } else if (!strcmp(subkey, ".receivepack")) { |
84bb2dfd PB |
386 | const char *v; |
387 | if (git_config_string(&v, key, value)) | |
388 | return -1; | |
5751f490 | 389 | if (!remote->receivepack) |
84bb2dfd | 390 | remote->receivepack = v; |
5751f490 DB |
391 | else |
392 | error("more than one receivepack given, using the first"); | |
0012ba21 | 393 | } else if (!strcmp(subkey, ".uploadpack")) { |
84bb2dfd PB |
394 | const char *v; |
395 | if (git_config_string(&v, key, value)) | |
396 | return -1; | |
0012ba21 | 397 | if (!remote->uploadpack) |
84bb2dfd | 398 | remote->uploadpack = v; |
0012ba21 DB |
399 | else |
400 | error("more than one uploadpack given, using the first"); | |
d71ab174 DB |
401 | } else if (!strcmp(subkey, ".tagopt")) { |
402 | if (!strcmp(value, "--no-tags")) | |
403 | remote->fetch_tags = -1; | |
14c98218 | 404 | } else if (!strcmp(subkey, ".proxy")) { |
84bb2dfd PB |
405 | return git_config_string((const char **)&remote->http_proxy, |
406 | key, value); | |
407 | } | |
5751f490 DB |
408 | return 0; |
409 | } | |
410 | ||
55029ae4 DB |
411 | static void alias_all_urls(void) |
412 | { | |
413 | int i, j; | |
414 | for (i = 0; i < remotes_nr; i++) { | |
415 | if (!remotes[i]) | |
416 | continue; | |
417 | for (j = 0; j < remotes[i]->url_nr; j++) { | |
418 | remotes[i]->url[j] = alias_url(remotes[i]->url[j]); | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
5751f490 DB |
423 | static void read_config(void) |
424 | { | |
425 | unsigned char sha1[20]; | |
426 | const char *head_ref; | |
427 | int flag; | |
428 | if (default_remote_name) // did this already | |
429 | return; | |
430 | default_remote_name = xstrdup("origin"); | |
431 | current_branch = NULL; | |
432 | head_ref = resolve_ref("HEAD", sha1, 0, &flag); | |
433 | if (head_ref && (flag & REF_ISSYMREF) && | |
434 | !prefixcmp(head_ref, "refs/heads/")) { | |
cf818348 DB |
435 | current_branch = |
436 | make_branch(head_ref + strlen("refs/heads/"), 0); | |
5751f490 | 437 | } |
ef90d6d4 | 438 | git_config(handle_config, NULL); |
55029ae4 | 439 | alias_all_urls(); |
5751f490 DB |
440 | } |
441 | ||
47c6ef1c JH |
442 | /* |
443 | * We need to make sure the tracking branches are well formed, but a | |
444 | * wildcard refspec in "struct refspec" must have a trailing slash. We | |
445 | * temporarily drop the trailing '/' while calling check_ref_format(), | |
446 | * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL | |
447 | * error return is Ok for a wildcard refspec. | |
448 | */ | |
449 | static int verify_refname(char *name, int is_glob) | |
450 | { | |
451 | int result, len = -1; | |
452 | ||
453 | if (is_glob) { | |
454 | len = strlen(name); | |
455 | assert(name[len - 1] == '/'); | |
456 | name[len - 1] = '\0'; | |
457 | } | |
458 | result = check_ref_format(name); | |
459 | if (is_glob) | |
460 | name[len - 1] = '/'; | |
461 | return result; | |
462 | } | |
463 | ||
24b6177e | 464 | static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify) |
6b62816c DB |
465 | { |
466 | int i; | |
ef00d150 | 467 | int st; |
6b62816c | 468 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); |
46220ca1 | 469 | |
6b62816c | 470 | for (i = 0; i < nr_refspec; i++) { |
47c6ef1c | 471 | size_t llen; |
46220ca1 JH |
472 | int is_glob; |
473 | const char *lhs, *rhs; | |
474 | ||
47c6ef1c | 475 | llen = is_glob = 0; |
46220ca1 JH |
476 | |
477 | lhs = refspec[i]; | |
478 | if (*lhs == '+') { | |
6b62816c | 479 | rs[i].force = 1; |
46220ca1 | 480 | lhs++; |
6b62816c | 481 | } |
46220ca1 JH |
482 | |
483 | rhs = strrchr(lhs, ':'); | |
a83619d6 PB |
484 | |
485 | /* | |
486 | * Before going on, special case ":" (or "+:") as a refspec | |
487 | * for matching refs. | |
488 | */ | |
489 | if (!fetch && rhs == lhs && rhs[1] == '\0') { | |
490 | rs[i].matching = 1; | |
491 | continue; | |
492 | } | |
493 | ||
46220ca1 | 494 | if (rhs) { |
47c6ef1c | 495 | size_t rlen = strlen(++rhs); |
46220ca1 | 496 | is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*")); |
47c6ef1c | 497 | rs[i].dst = xstrndup(rhs, rlen - is_glob); |
6b62816c | 498 | } |
ef00d150 | 499 | |
46220ca1 | 500 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); |
7d19da46 JH |
501 | if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) { |
502 | if ((rhs && !is_glob) || (!rhs && fetch)) | |
503 | goto invalid; | |
504 | is_glob = 1; | |
47c6ef1c | 505 | llen--; |
7d19da46 JH |
506 | } else if (rhs && is_glob) { |
507 | goto invalid; | |
ef00d150 | 508 | } |
7d19da46 | 509 | |
46220ca1 JH |
510 | rs[i].pattern = is_glob; |
511 | rs[i].src = xstrndup(lhs, llen); | |
512 | ||
513 | if (fetch) { | |
514 | /* | |
515 | * LHS | |
516 | * - empty is allowed; it means HEAD. | |
517 | * - otherwise it must be a valid looking ref. | |
518 | */ | |
519 | if (!*rs[i].src) | |
520 | ; /* empty is ok */ | |
521 | else { | |
47c6ef1c | 522 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
523 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
524 | goto invalid; | |
525 | } | |
526 | /* | |
527 | * RHS | |
7d19da46 | 528 | * - missing is ok, and is same as empty. |
46220ca1 JH |
529 | * - empty is ok; it means not to store. |
530 | * - otherwise it must be a valid looking ref. | |
531 | */ | |
532 | if (!rs[i].dst) { | |
533 | ; /* ok */ | |
534 | } else if (!*rs[i].dst) { | |
535 | ; /* ok */ | |
536 | } else { | |
47c6ef1c | 537 | st = verify_refname(rs[i].dst, is_glob); |
46220ca1 JH |
538 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
539 | goto invalid; | |
540 | } | |
541 | } else { | |
542 | /* | |
543 | * LHS | |
544 | * - empty is allowed; it means delete. | |
545 | * - when wildcarded, it must be a valid looking ref. | |
546 | * - otherwise, it must be an extended SHA-1, but | |
547 | * there is no existing way to validate this. | |
548 | */ | |
549 | if (!*rs[i].src) | |
550 | ; /* empty is ok */ | |
551 | else if (is_glob) { | |
47c6ef1c | 552 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
553 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
554 | goto invalid; | |
555 | } | |
556 | else | |
557 | ; /* anything goes, for now */ | |
558 | /* | |
559 | * RHS | |
560 | * - missing is allowed, but LHS then must be a | |
561 | * valid looking ref. | |
562 | * - empty is not allowed. | |
563 | * - otherwise it must be a valid looking ref. | |
564 | */ | |
565 | if (!rs[i].dst) { | |
47c6ef1c | 566 | st = verify_refname(rs[i].src, is_glob); |
46220ca1 JH |
567 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
568 | goto invalid; | |
569 | } else if (!*rs[i].dst) { | |
570 | goto invalid; | |
571 | } else { | |
47c6ef1c | 572 | st = verify_refname(rs[i].dst, is_glob); |
46220ca1 JH |
573 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) |
574 | goto invalid; | |
575 | } | |
ef00d150 | 576 | } |
6b62816c DB |
577 | } |
578 | return rs; | |
46220ca1 JH |
579 | |
580 | invalid: | |
24b6177e JF |
581 | if (verify) { |
582 | free(rs); | |
583 | return NULL; | |
584 | } | |
46220ca1 JH |
585 | die("Invalid refspec '%s'", refspec[i]); |
586 | } | |
587 | ||
24b6177e JF |
588 | int valid_fetch_refspec(const char *fetch_refspec_str) |
589 | { | |
590 | const char *fetch_refspec[] = { fetch_refspec_str }; | |
591 | struct refspec *refspec; | |
592 | ||
593 | refspec = parse_refspec_internal(1, fetch_refspec, 1, 1); | |
c71e9179 | 594 | free(refspec); |
24b6177e JF |
595 | return !!refspec; |
596 | } | |
597 | ||
46220ca1 JH |
598 | struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec) |
599 | { | |
24b6177e | 600 | return parse_refspec_internal(nr_refspec, refspec, 1, 0); |
46220ca1 JH |
601 | } |
602 | ||
603 | struct refspec *parse_push_refspec(int nr_refspec, const char **refspec) | |
604 | { | |
24b6177e | 605 | return parse_refspec_internal(nr_refspec, refspec, 0, 0); |
6b62816c DB |
606 | } |
607 | ||
df93e33c DB |
608 | static int valid_remote_nick(const char *name) |
609 | { | |
610 | if (!name[0] || /* not empty */ | |
611 | (name[0] == '.' && /* not "." */ | |
612 | (!name[1] || /* not ".." */ | |
613 | (name[1] == '.' && !name[2])))) | |
614 | return 0; | |
615 | return !strchr(name, '/'); /* no slash */ | |
616 | } | |
617 | ||
5751f490 DB |
618 | struct remote *remote_get(const char *name) |
619 | { | |
620 | struct remote *ret; | |
621 | ||
622 | read_config(); | |
623 | if (!name) | |
624 | name = default_remote_name; | |
625 | ret = make_remote(name, 0); | |
df93e33c | 626 | if (valid_remote_nick(name)) { |
28b91f8a | 627 | if (!ret->url) |
5751f490 | 628 | read_remotes_file(ret); |
28b91f8a | 629 | if (!ret->url) |
5751f490 DB |
630 | read_branches_file(ret); |
631 | } | |
28b91f8a | 632 | if (!ret->url) |
55029ae4 | 633 | add_url_alias(ret, name); |
28b91f8a | 634 | if (!ret->url) |
5751f490 | 635 | return NULL; |
46220ca1 JH |
636 | ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); |
637 | ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec); | |
5751f490 DB |
638 | return ret; |
639 | } | |
6b62816c | 640 | |
b42f6927 JS |
641 | int for_each_remote(each_remote_fn fn, void *priv) |
642 | { | |
643 | int i, result = 0; | |
644 | read_config(); | |
2d31347b | 645 | for (i = 0; i < remotes_nr && !result; i++) { |
b42f6927 JS |
646 | struct remote *r = remotes[i]; |
647 | if (!r) | |
648 | continue; | |
649 | if (!r->fetch) | |
46220ca1 JH |
650 | r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, |
651 | r->fetch_refspec); | |
b42f6927 | 652 | if (!r->push) |
46220ca1 JH |
653 | r->push = parse_push_refspec(r->push_refspec_nr, |
654 | r->push_refspec); | |
b42f6927 JS |
655 | result = fn(r, priv); |
656 | } | |
657 | return result; | |
658 | } | |
659 | ||
2467a4fa DB |
660 | void ref_remove_duplicates(struct ref *ref_map) |
661 | { | |
662 | struct ref **posn; | |
663 | struct ref *next; | |
664 | for (; ref_map; ref_map = ref_map->next) { | |
665 | if (!ref_map->peer_ref) | |
666 | continue; | |
667 | posn = &ref_map->next; | |
668 | while (*posn) { | |
669 | if ((*posn)->peer_ref && | |
670 | !strcmp((*posn)->peer_ref->name, | |
671 | ref_map->peer_ref->name)) { | |
672 | if (strcmp((*posn)->name, ref_map->name)) | |
673 | die("%s tracks both %s and %s", | |
674 | ref_map->peer_ref->name, | |
675 | (*posn)->name, ref_map->name); | |
676 | next = (*posn)->next; | |
677 | free((*posn)->peer_ref); | |
678 | free(*posn); | |
679 | *posn = next; | |
680 | } else { | |
681 | posn = &(*posn)->next; | |
682 | } | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
28b91f8a | 687 | int remote_has_url(struct remote *remote, const char *url) |
5d46c9d4 DB |
688 | { |
689 | int i; | |
28b91f8a SP |
690 | for (i = 0; i < remote->url_nr; i++) { |
691 | if (!strcmp(remote->url[i], url)) | |
5d46c9d4 DB |
692 | return 1; |
693 | } | |
694 | return 0; | |
695 | } | |
696 | ||
697 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) | |
698 | { | |
b42f6927 JS |
699 | int find_src = refspec->src == NULL; |
700 | char *needle, **result; | |
5d46c9d4 | 701 | int i; |
b42f6927 JS |
702 | |
703 | if (find_src) { | |
009c5bcd | 704 | if (!refspec->dst) |
b42f6927 JS |
705 | return error("find_tracking: need either src or dst"); |
706 | needle = refspec->dst; | |
707 | result = &refspec->src; | |
708 | } else { | |
709 | needle = refspec->src; | |
710 | result = &refspec->dst; | |
711 | } | |
712 | ||
5d46c9d4 DB |
713 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
714 | struct refspec *fetch = &remote->fetch[i]; | |
b42f6927 JS |
715 | const char *key = find_src ? fetch->dst : fetch->src; |
716 | const char *value = find_src ? fetch->src : fetch->dst; | |
5d46c9d4 DB |
717 | if (!fetch->dst) |
718 | continue; | |
719 | if (fetch->pattern) { | |
47c6ef1c | 720 | if (!prefixcmp(needle, key)) { |
b42f6927 JS |
721 | *result = xmalloc(strlen(value) + |
722 | strlen(needle) - | |
723 | strlen(key) + 1); | |
724 | strcpy(*result, value); | |
725 | strcpy(*result + strlen(value), | |
726 | needle + strlen(key)); | |
5d46c9d4 DB |
727 | refspec->force = fetch->force; |
728 | return 0; | |
729 | } | |
b42f6927 JS |
730 | } else if (!strcmp(needle, key)) { |
731 | *result = xstrdup(value); | |
732 | refspec->force = fetch->force; | |
733 | return 0; | |
5d46c9d4 DB |
734 | } |
735 | } | |
5d46c9d4 DB |
736 | return -1; |
737 | } | |
738 | ||
dfd255dd DB |
739 | struct ref *alloc_ref(unsigned namelen) |
740 | { | |
741 | struct ref *ret = xmalloc(sizeof(struct ref) + namelen); | |
742 | memset(ret, 0, sizeof(struct ref) + namelen); | |
743 | return ret; | |
744 | } | |
745 | ||
737922aa KK |
746 | struct ref *alloc_ref_from_str(const char* str) |
747 | { | |
748 | struct ref *ret = alloc_ref(strlen(str) + 1); | |
749 | strcpy(ret->name, str); | |
750 | return ret; | |
751 | } | |
752 | ||
4577370e | 753 | static struct ref *copy_ref(const struct ref *ref) |
d71ab174 DB |
754 | { |
755 | struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1); | |
756 | memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1); | |
757 | ret->next = NULL; | |
758 | return ret; | |
759 | } | |
760 | ||
4577370e DB |
761 | struct ref *copy_ref_list(const struct ref *ref) |
762 | { | |
763 | struct ref *ret = NULL; | |
764 | struct ref **tail = &ret; | |
765 | while (ref) { | |
766 | *tail = copy_ref(ref); | |
767 | ref = ref->next; | |
768 | tail = &((*tail)->next); | |
769 | } | |
770 | return ret; | |
771 | } | |
772 | ||
be885d96 DB |
773 | void free_ref(struct ref *ref) |
774 | { | |
775 | if (!ref) | |
776 | return; | |
777 | free(ref->remote_status); | |
778 | free(ref->symref); | |
779 | free(ref); | |
780 | } | |
781 | ||
dfd255dd DB |
782 | void free_refs(struct ref *ref) |
783 | { | |
784 | struct ref *next; | |
785 | while (ref) { | |
786 | next = ref->next; | |
8e0f7003 | 787 | free(ref->peer_ref); |
be885d96 | 788 | free_ref(ref); |
dfd255dd DB |
789 | ref = next; |
790 | } | |
791 | } | |
792 | ||
6b62816c DB |
793 | static int count_refspec_match(const char *pattern, |
794 | struct ref *refs, | |
795 | struct ref **matched_ref) | |
796 | { | |
797 | int patlen = strlen(pattern); | |
798 | struct ref *matched_weak = NULL; | |
799 | struct ref *matched = NULL; | |
800 | int weak_match = 0; | |
801 | int match = 0; | |
802 | ||
803 | for (weak_match = match = 0; refs; refs = refs->next) { | |
804 | char *name = refs->name; | |
805 | int namelen = strlen(name); | |
6b62816c | 806 | |
ae36bdcf | 807 | if (!refname_match(pattern, name, ref_rev_parse_rules)) |
6b62816c DB |
808 | continue; |
809 | ||
810 | /* A match is "weak" if it is with refs outside | |
811 | * heads or tags, and did not specify the pattern | |
812 | * in full (e.g. "refs/remotes/origin/master") or at | |
813 | * least from the toplevel (e.g. "remotes/origin/master"); | |
814 | * otherwise "git push $URL master" would result in | |
815 | * ambiguity between remotes/origin/master and heads/master | |
816 | * at the remote site. | |
817 | */ | |
818 | if (namelen != patlen && | |
819 | patlen != namelen - 5 && | |
820 | prefixcmp(name, "refs/heads/") && | |
821 | prefixcmp(name, "refs/tags/")) { | |
822 | /* We want to catch the case where only weak | |
823 | * matches are found and there are multiple | |
824 | * matches, and where more than one strong | |
825 | * matches are found, as ambiguous. One | |
826 | * strong match with zero or more weak matches | |
827 | * are acceptable as a unique match. | |
828 | */ | |
829 | matched_weak = refs; | |
830 | weak_match++; | |
831 | } | |
832 | else { | |
833 | matched = refs; | |
834 | match++; | |
835 | } | |
836 | } | |
837 | if (!matched) { | |
838 | *matched_ref = matched_weak; | |
839 | return weak_match; | |
840 | } | |
841 | else { | |
842 | *matched_ref = matched; | |
843 | return match; | |
844 | } | |
845 | } | |
846 | ||
1d735267 | 847 | static void tail_link_ref(struct ref *ref, struct ref ***tail) |
6b62816c DB |
848 | { |
849 | **tail = ref; | |
1d735267 DB |
850 | while (ref->next) |
851 | ref = ref->next; | |
6b62816c | 852 | *tail = &ref->next; |
6b62816c DB |
853 | } |
854 | ||
855 | static struct ref *try_explicit_object_name(const char *name) | |
856 | { | |
857 | unsigned char sha1[20]; | |
858 | struct ref *ref; | |
6b62816c DB |
859 | |
860 | if (!*name) { | |
dfd255dd | 861 | ref = alloc_ref(20); |
6b62816c DB |
862 | strcpy(ref->name, "(delete)"); |
863 | hashclr(ref->new_sha1); | |
864 | return ref; | |
865 | } | |
866 | if (get_sha1(name, sha1)) | |
867 | return NULL; | |
737922aa | 868 | ref = alloc_ref_from_str(name); |
6b62816c DB |
869 | hashcpy(ref->new_sha1, sha1); |
870 | return ref; | |
871 | } | |
872 | ||
1d735267 | 873 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
6b62816c | 874 | { |
737922aa | 875 | struct ref *ret = alloc_ref_from_str(name); |
1d735267 DB |
876 | tail_link_ref(ret, tail); |
877 | return ret; | |
163f0ee5 | 878 | } |
8558fd9e | 879 | |
f8aae120 JK |
880 | static char *guess_ref(const char *name, struct ref *peer) |
881 | { | |
882 | struct strbuf buf = STRBUF_INIT; | |
883 | unsigned char sha1[20]; | |
884 | ||
885 | const char *r = resolve_ref(peer->name, sha1, 1, NULL); | |
886 | if (!r) | |
887 | return NULL; | |
888 | ||
889 | if (!prefixcmp(r, "refs/heads/")) | |
890 | strbuf_addstr(&buf, "refs/heads/"); | |
891 | else if (!prefixcmp(r, "refs/tags/")) | |
892 | strbuf_addstr(&buf, "refs/tags/"); | |
893 | else | |
894 | return NULL; | |
895 | ||
896 | strbuf_addstr(&buf, name); | |
897 | return strbuf_detach(&buf, NULL); | |
898 | } | |
899 | ||
54a8ad92 JH |
900 | static int match_explicit(struct ref *src, struct ref *dst, |
901 | struct ref ***dst_tail, | |
9a7bbd1d | 902 | struct refspec *rs) |
6b62816c | 903 | { |
54a8ad92 | 904 | struct ref *matched_src, *matched_dst; |
8558fd9e | 905 | |
54a8ad92 | 906 | const char *dst_value = rs->dst; |
f8aae120 | 907 | char *dst_guess; |
6b62816c | 908 | |
a83619d6 | 909 | if (rs->pattern || rs->matching) |
9a7bbd1d | 910 | return 0; |
8558fd9e | 911 | |
54a8ad92 JH |
912 | matched_src = matched_dst = NULL; |
913 | switch (count_refspec_match(rs->src, src, &matched_src)) { | |
914 | case 1: | |
915 | break; | |
916 | case 0: | |
917 | /* The source could be in the get_sha1() format | |
918 | * not a reference name. :refs/other is a | |
919 | * way to delete 'other' ref at the remote end. | |
920 | */ | |
921 | matched_src = try_explicit_object_name(rs->src); | |
7dfee372 | 922 | if (!matched_src) |
9a7bbd1d | 923 | return error("src refspec %s does not match any.", rs->src); |
54a8ad92 JH |
924 | break; |
925 | default: | |
9a7bbd1d | 926 | return error("src refspec %s matches more than one.", rs->src); |
54a8ad92 | 927 | } |
3c8b7df1 | 928 | |
4491e62a | 929 | if (!dst_value) { |
9f0ea7e8 DB |
930 | unsigned char sha1[20]; |
931 | int flag; | |
932 | ||
9f0ea7e8 DB |
933 | dst_value = resolve_ref(matched_src->name, sha1, 1, &flag); |
934 | if (!dst_value || | |
935 | ((flag & REF_ISSYMREF) && | |
936 | prefixcmp(dst_value, "refs/heads/"))) | |
937 | die("%s cannot be resolved to branch.", | |
938 | matched_src->name); | |
4491e62a | 939 | } |
3c8b7df1 | 940 | |
54a8ad92 JH |
941 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
942 | case 1: | |
943 | break; | |
944 | case 0: | |
163f0ee5 | 945 | if (!memcmp(dst_value, "refs/", 5)) |
1d735267 | 946 | matched_dst = make_linked_ref(dst_value, dst_tail); |
f8aae120 JK |
947 | else if((dst_guess = guess_ref(dst_value, matched_src))) |
948 | matched_dst = make_linked_ref(dst_guess, dst_tail); | |
3c8b7df1 | 949 | else |
f8aae120 JK |
950 | error("unable to push to unqualified destination: %s\n" |
951 | "The destination refspec neither matches an " | |
952 | "existing ref on the remote nor\n" | |
953 | "begins with refs/, and we are unable to " | |
954 | "guess a prefix based on the source ref.", | |
955 | dst_value); | |
54a8ad92 JH |
956 | break; |
957 | default: | |
3c8b7df1 | 958 | matched_dst = NULL; |
54a8ad92 JH |
959 | error("dst refspec %s matches more than one.", |
960 | dst_value); | |
961 | break; | |
962 | } | |
9a7bbd1d JK |
963 | if (!matched_dst) |
964 | return -1; | |
965 | if (matched_dst->peer_ref) | |
966 | return error("dst ref %s receives from more than one src.", | |
54a8ad92 | 967 | matched_dst->name); |
54a8ad92 JH |
968 | else { |
969 | matched_dst->peer_ref = matched_src; | |
970 | matched_dst->force = rs->force; | |
6b62816c | 971 | } |
9a7bbd1d | 972 | return 0; |
54a8ad92 JH |
973 | } |
974 | ||
975 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
976 | struct ref ***dst_tail, struct refspec *rs, | |
977 | int rs_nr) | |
978 | { | |
979 | int i, errs; | |
980 | for (i = errs = 0; i < rs_nr; i++) | |
9a7bbd1d JK |
981 | errs += match_explicit(src, dst, dst_tail, &rs[i]); |
982 | return errs; | |
6b62816c DB |
983 | } |
984 | ||
6e66bf3c AR |
985 | static const struct refspec *check_pattern_match(const struct refspec *rs, |
986 | int rs_nr, | |
987 | const struct ref *src) | |
8558fd9e DB |
988 | { |
989 | int i; | |
a83619d6 | 990 | int matching_refs = -1; |
8558fd9e | 991 | for (i = 0; i < rs_nr; i++) { |
a83619d6 PB |
992 | if (rs[i].matching && |
993 | (matching_refs == -1 || rs[i].force)) { | |
994 | matching_refs = i; | |
995 | continue; | |
996 | } | |
997 | ||
47c6ef1c | 998 | if (rs[i].pattern && !prefixcmp(src->name, rs[i].src)) |
6e66bf3c | 999 | return rs + i; |
8558fd9e | 1000 | } |
a83619d6 PB |
1001 | if (matching_refs != -1) |
1002 | return rs + matching_refs; | |
1003 | else | |
1004 | return NULL; | |
8558fd9e DB |
1005 | } |
1006 | ||
54a8ad92 JH |
1007 | /* |
1008 | * Note. This is used only by "push"; refspec matching rules for | |
1009 | * push and fetch are subtly different, so do not try to reuse it | |
1010 | * without thinking. | |
1011 | */ | |
6b62816c | 1012 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
28b9d6e5 | 1013 | int nr_refspec, const char **refspec, int flags) |
6b62816c | 1014 | { |
a83619d6 | 1015 | struct refspec *rs; |
28b9d6e5 AW |
1016 | int send_all = flags & MATCH_REFS_ALL; |
1017 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
a83619d6 | 1018 | static const char *default_refspec[] = { ":", 0 }; |
6b62816c | 1019 | |
a83619d6 PB |
1020 | if (!nr_refspec) { |
1021 | nr_refspec = 1; | |
1022 | refspec = default_refspec; | |
1023 | } | |
1024 | rs = parse_push_refspec(nr_refspec, (const char **) refspec); | |
8558fd9e DB |
1025 | if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec)) |
1026 | return -1; | |
6b62816c DB |
1027 | |
1028 | /* pick the remainder */ | |
1029 | for ( ; src; src = src->next) { | |
1030 | struct ref *dst_peer; | |
6e66bf3c AR |
1031 | const struct refspec *pat = NULL; |
1032 | char *dst_name; | |
6b62816c DB |
1033 | if (src->peer_ref) |
1034 | continue; | |
a83619d6 PB |
1035 | |
1036 | pat = check_pattern_match(rs, nr_refspec, src); | |
1037 | if (!pat) | |
1038 | continue; | |
1039 | ||
1040 | if (pat->matching) { | |
098e711e JH |
1041 | /* |
1042 | * "matching refs"; traditionally we pushed everything | |
1043 | * including refs outside refs/heads/ hierarchy, but | |
1044 | * that does not make much sense these days. | |
1045 | */ | |
a83619d6 PB |
1046 | if (!send_mirror && prefixcmp(src->name, "refs/heads/")) |
1047 | continue; | |
1048 | dst_name = xstrdup(src->name); | |
8558fd9e | 1049 | |
a83619d6 | 1050 | } else { |
efd8f793 DB |
1051 | const char *dst_side = pat->dst ? pat->dst : pat->src; |
1052 | dst_name = xmalloc(strlen(dst_side) + | |
6e66bf3c AR |
1053 | strlen(src->name) - |
1054 | strlen(pat->src) + 2); | |
efd8f793 | 1055 | strcpy(dst_name, dst_side); |
6e66bf3c | 1056 | strcat(dst_name, src->name + strlen(pat->src)); |
a83619d6 | 1057 | } |
6e66bf3c | 1058 | dst_peer = find_ref_by_name(dst, dst_name); |
a83619d6 PB |
1059 | if (dst_peer) { |
1060 | if (dst_peer->peer_ref) | |
1061 | /* We're already sending something to this ref. */ | |
1062 | goto free_name; | |
1063 | ||
1064 | } else { | |
1065 | if (pat->matching && !(send_all || send_mirror)) | |
1066 | /* | |
1067 | * Remote doesn't have it, and we have no | |
1068 | * explicit pattern, and we don't have | |
1069 | * --all nor --mirror. | |
1070 | */ | |
1071 | goto free_name; | |
28b9d6e5 | 1072 | |
6b62816c | 1073 | /* Create a new one and link it */ |
1d735267 | 1074 | dst_peer = make_linked_ref(dst_name, dst_tail); |
6b62816c | 1075 | hashcpy(dst_peer->new_sha1, src->new_sha1); |
6b62816c DB |
1076 | } |
1077 | dst_peer->peer_ref = src; | |
a83619d6 | 1078 | dst_peer->force = pat->force; |
6e66bf3c AR |
1079 | free_name: |
1080 | free(dst_name); | |
6b62816c DB |
1081 | } |
1082 | return 0; | |
1083 | } | |
cf818348 DB |
1084 | |
1085 | struct branch *branch_get(const char *name) | |
1086 | { | |
1087 | struct branch *ret; | |
1088 | ||
1089 | read_config(); | |
1090 | if (!name || !*name || !strcmp(name, "HEAD")) | |
1091 | ret = current_branch; | |
1092 | else | |
1093 | ret = make_branch(name, 0); | |
1094 | if (ret && ret->remote_name) { | |
1095 | ret->remote = remote_get(ret->remote_name); | |
1096 | if (ret->merge_nr) { | |
1097 | int i; | |
1098 | ret->merge = xcalloc(sizeof(*ret->merge), | |
1099 | ret->merge_nr); | |
1100 | for (i = 0; i < ret->merge_nr; i++) { | |
1101 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); | |
1102 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); | |
1103 | remote_find_tracking(ret->remote, | |
1104 | ret->merge[i]); | |
1105 | } | |
1106 | } | |
1107 | } | |
1108 | return ret; | |
1109 | } | |
1110 | ||
1111 | int branch_has_merge_config(struct branch *branch) | |
1112 | { | |
1113 | return branch && !!branch->merge; | |
1114 | } | |
1115 | ||
85682c19 SP |
1116 | int branch_merge_matches(struct branch *branch, |
1117 | int i, | |
1118 | const char *refname) | |
cf818348 | 1119 | { |
85682c19 | 1120 | if (!branch || i < 0 || i >= branch->merge_nr) |
cf818348 | 1121 | return 0; |
605b4978 | 1122 | return refname_match(branch->merge[i]->src, refname, ref_fetch_rules); |
cf818348 | 1123 | } |
d71ab174 | 1124 | |
4577370e | 1125 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
d71ab174 DB |
1126 | const struct refspec *refspec) |
1127 | { | |
4577370e | 1128 | const struct ref *ref; |
d71ab174 DB |
1129 | struct ref *ret = NULL; |
1130 | struct ref **tail = &ret; | |
1131 | ||
1132 | int remote_prefix_len = strlen(refspec->src); | |
1133 | int local_prefix_len = strlen(refspec->dst); | |
1134 | ||
1135 | for (ref = remote_refs; ref; ref = ref->next) { | |
1136 | if (strchr(ref->name, '^')) | |
1137 | continue; /* a dereference item */ | |
1138 | if (!prefixcmp(ref->name, refspec->src)) { | |
4577370e | 1139 | const char *match; |
d71ab174 DB |
1140 | struct ref *cpy = copy_ref(ref); |
1141 | match = ref->name + remote_prefix_len; | |
1142 | ||
1143 | cpy->peer_ref = alloc_ref(local_prefix_len + | |
1144 | strlen(match) + 1); | |
1145 | sprintf(cpy->peer_ref->name, "%s%s", | |
1146 | refspec->dst, match); | |
1147 | if (refspec->force) | |
1148 | cpy->peer_ref->force = 1; | |
1149 | *tail = cpy; | |
1150 | tail = &cpy->next; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | return ret; | |
1155 | } | |
1156 | ||
4577370e | 1157 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
d71ab174 | 1158 | { |
4577370e | 1159 | const struct ref *ref; |
d71ab174 | 1160 | for (ref = refs; ref; ref = ref->next) { |
605b4978 | 1161 | if (refname_match(name, ref->name, ref_fetch_rules)) |
d71ab174 DB |
1162 | return ref; |
1163 | } | |
1164 | return NULL; | |
1165 | } | |
1166 | ||
4577370e | 1167 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
d71ab174 | 1168 | { |
4577370e | 1169 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
d71ab174 DB |
1170 | |
1171 | if (!ref) | |
9ad7c5ae | 1172 | return NULL; |
d71ab174 DB |
1173 | |
1174 | return copy_ref(ref); | |
1175 | } | |
1176 | ||
1177 | static struct ref *get_local_ref(const char *name) | |
1178 | { | |
1179 | struct ref *ret; | |
1180 | if (!name) | |
1181 | return NULL; | |
1182 | ||
1183 | if (!prefixcmp(name, "refs/")) { | |
737922aa | 1184 | return alloc_ref_from_str(name); |
d71ab174 DB |
1185 | } |
1186 | ||
1187 | if (!prefixcmp(name, "heads/") || | |
1188 | !prefixcmp(name, "tags/") || | |
1189 | !prefixcmp(name, "remotes/")) { | |
1190 | ret = alloc_ref(strlen(name) + 6); | |
1191 | sprintf(ret->name, "refs/%s", name); | |
1192 | return ret; | |
1193 | } | |
1194 | ||
1195 | ret = alloc_ref(strlen(name) + 12); | |
1196 | sprintf(ret->name, "refs/heads/%s", name); | |
1197 | return ret; | |
1198 | } | |
1199 | ||
4577370e | 1200 | int get_fetch_map(const struct ref *remote_refs, |
d71ab174 | 1201 | const struct refspec *refspec, |
9ad7c5ae JH |
1202 | struct ref ***tail, |
1203 | int missing_ok) | |
d71ab174 | 1204 | { |
ef00d150 | 1205 | struct ref *ref_map, **rmp; |
d71ab174 DB |
1206 | |
1207 | if (refspec->pattern) { | |
1208 | ref_map = get_expanded_map(remote_refs, refspec); | |
1209 | } else { | |
9ad7c5ae JH |
1210 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
1211 | ||
1212 | ref_map = get_remote_ref(remote_refs, name); | |
1213 | if (!missing_ok && !ref_map) | |
1214 | die("Couldn't find remote ref %s", name); | |
1215 | if (ref_map) { | |
1216 | ref_map->peer_ref = get_local_ref(refspec->dst); | |
1217 | if (ref_map->peer_ref && refspec->force) | |
1218 | ref_map->peer_ref->force = 1; | |
1219 | } | |
d71ab174 DB |
1220 | } |
1221 | ||
ef00d150 DB |
1222 | for (rmp = &ref_map; *rmp; ) { |
1223 | if ((*rmp)->peer_ref) { | |
1224 | int st = check_ref_format((*rmp)->peer_ref->name + 5); | |
1225 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) { | |
1226 | struct ref *ignore = *rmp; | |
1227 | error("* Ignoring funny ref '%s' locally", | |
1228 | (*rmp)->peer_ref->name); | |
1229 | *rmp = (*rmp)->next; | |
1230 | free(ignore->peer_ref); | |
1231 | free(ignore); | |
1232 | continue; | |
1233 | } | |
1234 | } | |
1235 | rmp = &((*rmp)->next); | |
d71ab174 DB |
1236 | } |
1237 | ||
8f70a765 AR |
1238 | if (ref_map) |
1239 | tail_link_ref(ref_map, tail); | |
d71ab174 DB |
1240 | |
1241 | return 0; | |
1242 | } | |
be885d96 DB |
1243 | |
1244 | int resolve_remote_symref(struct ref *ref, struct ref *list) | |
1245 | { | |
1246 | if (!ref->symref) | |
1247 | return 0; | |
1248 | for (; list; list = list->next) | |
1249 | if (!strcmp(ref->symref, list->name)) { | |
1250 | hashcpy(ref->old_sha1, list->old_sha1); | |
1251 | return 0; | |
1252 | } | |
1253 | return 1; | |
1254 | } | |
6d21bf96 JH |
1255 | |
1256 | /* | |
1257 | * Return true if there is anything to report, otherwise false. | |
1258 | */ | |
1259 | int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs) | |
1260 | { | |
1261 | unsigned char sha1[20]; | |
1262 | struct commit *ours, *theirs; | |
1263 | char symmetric[84]; | |
1264 | struct rev_info revs; | |
1265 | const char *rev_argv[10], *base; | |
1266 | int rev_argc; | |
1267 | ||
1268 | /* | |
1269 | * Nothing to report unless we are marked to build on top of | |
1270 | * somebody else. | |
1271 | */ | |
1272 | if (!branch || | |
1273 | !branch->merge || !branch->merge[0] || !branch->merge[0]->dst) | |
1274 | return 0; | |
1275 | ||
1276 | /* | |
1277 | * If what we used to build on no longer exists, there is | |
1278 | * nothing to report. | |
1279 | */ | |
1280 | base = branch->merge[0]->dst; | |
1281 | if (!resolve_ref(base, sha1, 1, NULL)) | |
1282 | return 0; | |
1283 | theirs = lookup_commit(sha1); | |
1284 | if (!theirs) | |
1285 | return 0; | |
1286 | ||
1287 | if (!resolve_ref(branch->refname, sha1, 1, NULL)) | |
1288 | return 0; | |
1289 | ours = lookup_commit(sha1); | |
1290 | if (!ours) | |
1291 | return 0; | |
1292 | ||
1293 | /* are we the same? */ | |
1294 | if (theirs == ours) | |
1295 | return 0; | |
1296 | ||
1297 | /* Run "rev-list --left-right ours...theirs" internally... */ | |
1298 | rev_argc = 0; | |
1299 | rev_argv[rev_argc++] = NULL; | |
1300 | rev_argv[rev_argc++] = "--left-right"; | |
1301 | rev_argv[rev_argc++] = symmetric; | |
1302 | rev_argv[rev_argc++] = "--"; | |
1303 | rev_argv[rev_argc] = NULL; | |
1304 | ||
1305 | strcpy(symmetric, sha1_to_hex(ours->object.sha1)); | |
1306 | strcpy(symmetric + 40, "..."); | |
1307 | strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1)); | |
1308 | ||
1309 | init_revisions(&revs, NULL); | |
1310 | setup_revisions(rev_argc, rev_argv, &revs, NULL); | |
1311 | prepare_revision_walk(&revs); | |
1312 | ||
1313 | /* ... and count the commits on each side. */ | |
1314 | *num_ours = 0; | |
1315 | *num_theirs = 0; | |
1316 | while (1) { | |
1317 | struct commit *c = get_revision(&revs); | |
1318 | if (!c) | |
1319 | break; | |
1320 | if (c->object.flags & SYMMETRIC_LEFT) | |
1321 | (*num_ours)++; | |
1322 | else | |
1323 | (*num_theirs)++; | |
1324 | } | |
c0234b2e JH |
1325 | |
1326 | /* clear object flags smudged by the above traversal */ | |
1327 | clear_commit_marks(ours, ALL_REV_FLAGS); | |
1328 | clear_commit_marks(theirs, ALL_REV_FLAGS); | |
6d21bf96 JH |
1329 | return 1; |
1330 | } | |
1331 | ||
1332 | /* | |
1333 | * Return true when there is anything to report, otherwise false. | |
1334 | */ | |
1335 | int format_tracking_info(struct branch *branch, struct strbuf *sb) | |
1336 | { | |
1337 | int num_ours, num_theirs; | |
4de53ce0 | 1338 | const char *base; |
6d21bf96 JH |
1339 | |
1340 | if (!stat_tracking_info(branch, &num_ours, &num_theirs)) | |
1341 | return 0; | |
1342 | ||
1343 | base = branch->merge[0]->dst; | |
1344 | if (!prefixcmp(base, "refs/remotes/")) { | |
6d21bf96 | 1345 | base += strlen("refs/remotes/"); |
6d21bf96 JH |
1346 | } |
1347 | if (!num_theirs) | |
4de53ce0 | 1348 | strbuf_addf(sb, "Your branch is ahead of '%s' " |
6d21bf96 | 1349 | "by %d commit%s.\n", |
4de53ce0 | 1350 | base, num_ours, (num_ours == 1) ? "" : "s"); |
6d21bf96 | 1351 | else if (!num_ours) |
4de53ce0 AP |
1352 | strbuf_addf(sb, "Your branch is behind '%s' " |
1353 | "by %d commit%s, " | |
6d21bf96 | 1354 | "and can be fast-forwarded.\n", |
4de53ce0 | 1355 | base, num_theirs, (num_theirs == 1) ? "" : "s"); |
6d21bf96 | 1356 | else |
4de53ce0 AP |
1357 | strbuf_addf(sb, "Your branch and '%s' have diverged,\n" |
1358 | "and have %d and %d different commit(s) each, " | |
1359 | "respectively.\n", | |
1360 | base, num_ours, num_theirs); | |
6d21bf96 JH |
1361 | return 1; |
1362 | } |