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