Commit | Line | Data |
---|---|---|
5751f490 DB |
1 | #include "cache.h" |
2 | #include "remote.h" | |
3 | #include "refs.h" | |
4 | ||
844112ca JH |
5 | struct counted_string { |
6 | size_t len; | |
7 | const char *s; | |
8 | }; | |
55029ae4 DB |
9 | struct rewrite { |
10 | const char *base; | |
844112ca JH |
11 | size_t baselen; |
12 | struct counted_string *instead_of; | |
55029ae4 DB |
13 | int instead_of_nr; |
14 | int instead_of_alloc; | |
15 | }; | |
16 | ||
5751f490 | 17 | static struct remote **remotes; |
2d31347b DB |
18 | static int remotes_alloc; |
19 | static int remotes_nr; | |
5751f490 | 20 | |
cf818348 | 21 | static struct branch **branches; |
2d31347b DB |
22 | static int branches_alloc; |
23 | static int branches_nr; | |
cf818348 DB |
24 | |
25 | static struct branch *current_branch; | |
26 | static const char *default_remote_name; | |
27 | ||
55029ae4 DB |
28 | static struct rewrite **rewrite; |
29 | static int rewrite_alloc; | |
30 | static int rewrite_nr; | |
31 | ||
5751f490 DB |
32 | #define BUF_SIZE (2048) |
33 | static char buffer[BUF_SIZE]; | |
34 | ||
55029ae4 DB |
35 | static const char *alias_url(const char *url) |
36 | { | |
37 | int i, j; | |
844112ca JH |
38 | char *ret; |
39 | struct counted_string *longest; | |
40 | int longest_i; | |
41 | ||
42 | longest = NULL; | |
43 | longest_i = -1; | |
55029ae4 DB |
44 | for (i = 0; i < rewrite_nr; i++) { |
45 | if (!rewrite[i]) | |
46 | continue; | |
47 | for (j = 0; j < rewrite[i]->instead_of_nr; j++) { | |
844112ca JH |
48 | if (!prefixcmp(url, rewrite[i]->instead_of[j].s) && |
49 | (!longest || | |
50 | longest->len < rewrite[i]->instead_of[j].len)) { | |
51 | longest = &(rewrite[i]->instead_of[j]); | |
52 | longest_i = i; | |
55029ae4 DB |
53 | } |
54 | } | |
55 | } | |
844112ca JH |
56 | if (!longest) |
57 | return url; | |
58 | ||
59 | ret = malloc(rewrite[longest_i]->baselen + | |
60 | (strlen(url) - longest->len) + 1); | |
61 | strcpy(ret, rewrite[longest_i]->base); | |
62 | strcpy(ret + rewrite[longest_i]->baselen, url + longest->len); | |
63 | return ret; | |
55029ae4 DB |
64 | } |
65 | ||
5751f490 DB |
66 | static void add_push_refspec(struct remote *remote, const char *ref) |
67 | { | |
2d31347b DB |
68 | ALLOC_GROW(remote->push_refspec, |
69 | remote->push_refspec_nr + 1, | |
70 | remote->push_refspec_alloc); | |
71 | remote->push_refspec[remote->push_refspec_nr++] = ref; | |
5751f490 DB |
72 | } |
73 | ||
5d46c9d4 DB |
74 | static void add_fetch_refspec(struct remote *remote, const char *ref) |
75 | { | |
2d31347b DB |
76 | ALLOC_GROW(remote->fetch_refspec, |
77 | remote->fetch_refspec_nr + 1, | |
78 | remote->fetch_refspec_alloc); | |
79 | remote->fetch_refspec[remote->fetch_refspec_nr++] = ref; | |
5d46c9d4 DB |
80 | } |
81 | ||
28b91f8a | 82 | static void add_url(struct remote *remote, const char *url) |
5751f490 | 83 | { |
2d31347b DB |
84 | ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc); |
85 | remote->url[remote->url_nr++] = url; | |
5751f490 DB |
86 | } |
87 | ||
55029ae4 DB |
88 | static void add_url_alias(struct remote *remote, const char *url) |
89 | { | |
90 | add_url(remote, alias_url(url)); | |
91 | } | |
92 | ||
5751f490 DB |
93 | static struct remote *make_remote(const char *name, int len) |
94 | { | |
2d31347b DB |
95 | struct remote *ret; |
96 | int i; | |
5751f490 | 97 | |
2d31347b DB |
98 | for (i = 0; i < remotes_nr; i++) { |
99 | if (len ? (!strncmp(name, remotes[i]->name, len) && | |
100 | !remotes[i]->name[len]) : | |
101 | !strcmp(name, remotes[i]->name)) | |
102 | return remotes[i]; | |
5751f490 DB |
103 | } |
104 | ||
2d31347b DB |
105 | ret = xcalloc(1, sizeof(struct remote)); |
106 | ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc); | |
107 | remotes[remotes_nr++] = ret; | |
5751f490 | 108 | if (len) |
2d31347b | 109 | ret->name = xstrndup(name, len); |
5751f490 | 110 | else |
2d31347b DB |
111 | ret->name = xstrdup(name); |
112 | return ret; | |
5751f490 DB |
113 | } |
114 | ||
cf818348 DB |
115 | static void add_merge(struct branch *branch, const char *name) |
116 | { | |
2d31347b DB |
117 | ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, |
118 | branch->merge_alloc); | |
119 | branch->merge_name[branch->merge_nr++] = name; | |
cf818348 DB |
120 | } |
121 | ||
122 | static struct branch *make_branch(const char *name, int len) | |
123 | { | |
2d31347b DB |
124 | struct branch *ret; |
125 | int i; | |
cf818348 DB |
126 | char *refname; |
127 | ||
2d31347b DB |
128 | for (i = 0; i < branches_nr; i++) { |
129 | if (len ? (!strncmp(name, branches[i]->name, len) && | |
130 | !branches[i]->name[len]) : | |
131 | !strcmp(name, branches[i]->name)) | |
132 | return branches[i]; | |
cf818348 DB |
133 | } |
134 | ||
2d31347b DB |
135 | ALLOC_GROW(branches, branches_nr + 1, branches_alloc); |
136 | ret = xcalloc(1, sizeof(struct branch)); | |
137 | branches[branches_nr++] = ret; | |
cf818348 | 138 | if (len) |
2d31347b | 139 | ret->name = xstrndup(name, len); |
cf818348 | 140 | else |
2d31347b | 141 | ret->name = xstrdup(name); |
cf818348 DB |
142 | refname = malloc(strlen(name) + strlen("refs/heads/") + 1); |
143 | strcpy(refname, "refs/heads/"); | |
2d31347b DB |
144 | strcpy(refname + strlen("refs/heads/"), ret->name); |
145 | ret->refname = refname; | |
cf818348 | 146 | |
2d31347b | 147 | return ret; |
cf818348 DB |
148 | } |
149 | ||
55029ae4 DB |
150 | static struct rewrite *make_rewrite(const char *base, int len) |
151 | { | |
152 | struct rewrite *ret; | |
153 | int i; | |
154 | ||
155 | for (i = 0; i < rewrite_nr; i++) { | |
844112ca JH |
156 | if (len |
157 | ? (len == rewrite[i]->baselen && | |
158 | !strncmp(base, rewrite[i]->base, len)) | |
159 | : !strcmp(base, rewrite[i]->base)) | |
55029ae4 DB |
160 | return rewrite[i]; |
161 | } | |
162 | ||
163 | ALLOC_GROW(rewrite, rewrite_nr + 1, rewrite_alloc); | |
164 | ret = xcalloc(1, sizeof(struct rewrite)); | |
165 | rewrite[rewrite_nr++] = ret; | |
844112ca | 166 | if (len) { |
55029ae4 | 167 | ret->base = xstrndup(base, len); |
844112ca JH |
168 | ret->baselen = len; |
169 | } | |
170 | else { | |
55029ae4 | 171 | ret->base = xstrdup(base); |
844112ca JH |
172 | ret->baselen = strlen(base); |
173 | } | |
55029ae4 DB |
174 | return ret; |
175 | } | |
176 | ||
177 | static void add_instead_of(struct rewrite *rewrite, const char *instead_of) | |
178 | { | |
179 | ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc); | |
844112ca JH |
180 | rewrite->instead_of[rewrite->instead_of_nr].s = instead_of; |
181 | rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of); | |
182 | rewrite->instead_of_nr++; | |
55029ae4 DB |
183 | } |
184 | ||
5751f490 DB |
185 | static void read_remotes_file(struct remote *remote) |
186 | { | |
187 | FILE *f = fopen(git_path("remotes/%s", remote->name), "r"); | |
188 | ||
189 | if (!f) | |
190 | return; | |
191 | while (fgets(buffer, BUF_SIZE, f)) { | |
192 | int value_list; | |
193 | char *s, *p; | |
194 | ||
195 | if (!prefixcmp(buffer, "URL:")) { | |
196 | value_list = 0; | |
197 | s = buffer + 4; | |
198 | } else if (!prefixcmp(buffer, "Push:")) { | |
199 | value_list = 1; | |
200 | s = buffer + 5; | |
5d46c9d4 DB |
201 | } else if (!prefixcmp(buffer, "Pull:")) { |
202 | value_list = 2; | |
203 | s = buffer + 5; | |
5751f490 DB |
204 | } else |
205 | continue; | |
206 | ||
207 | while (isspace(*s)) | |
208 | s++; | |
209 | if (!*s) | |
210 | continue; | |
211 | ||
212 | p = s + strlen(s); | |
213 | while (isspace(p[-1])) | |
214 | *--p = 0; | |
215 | ||
216 | switch (value_list) { | |
217 | case 0: | |
55029ae4 | 218 | add_url_alias(remote, xstrdup(s)); |
5751f490 DB |
219 | break; |
220 | case 1: | |
221 | add_push_refspec(remote, xstrdup(s)); | |
222 | break; | |
5d46c9d4 DB |
223 | case 2: |
224 | add_fetch_refspec(remote, xstrdup(s)); | |
225 | break; | |
5751f490 DB |
226 | } |
227 | } | |
228 | fclose(f); | |
229 | } | |
230 | ||
231 | static void read_branches_file(struct remote *remote) | |
232 | { | |
233 | const char *slash = strchr(remote->name, '/'); | |
cf818348 DB |
234 | char *frag; |
235 | char *branch; | |
5751f490 DB |
236 | int n = slash ? slash - remote->name : 1000; |
237 | FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r"); | |
238 | char *s, *p; | |
239 | int len; | |
240 | ||
241 | if (!f) | |
242 | return; | |
243 | s = fgets(buffer, BUF_SIZE, f); | |
244 | fclose(f); | |
245 | if (!s) | |
246 | return; | |
247 | while (isspace(*s)) | |
248 | s++; | |
249 | if (!*s) | |
250 | return; | |
251 | p = s + strlen(s); | |
252 | while (isspace(p[-1])) | |
253 | *--p = 0; | |
254 | len = p - s; | |
255 | if (slash) | |
256 | len += strlen(slash); | |
257 | p = xmalloc(len + 1); | |
258 | strcpy(p, s); | |
259 | if (slash) | |
260 | strcat(p, slash); | |
cf818348 DB |
261 | frag = strchr(p, '#'); |
262 | if (frag) { | |
263 | *(frag++) = '\0'; | |
264 | branch = xmalloc(strlen(frag) + 12); | |
265 | strcpy(branch, "refs/heads/"); | |
266 | strcat(branch, frag); | |
267 | } else { | |
268 | branch = "refs/heads/master"; | |
269 | } | |
55029ae4 | 270 | add_url_alias(remote, p); |
cf818348 | 271 | add_fetch_refspec(remote, branch); |
d71ab174 | 272 | remote->fetch_tags = 1; /* always auto-follow */ |
5751f490 DB |
273 | } |
274 | ||
5751f490 DB |
275 | static int handle_config(const char *key, const char *value) |
276 | { | |
277 | const char *name; | |
278 | const char *subkey; | |
279 | struct remote *remote; | |
cf818348 DB |
280 | struct branch *branch; |
281 | if (!prefixcmp(key, "branch.")) { | |
282 | name = key + 7; | |
283 | subkey = strrchr(name, '.'); | |
cf818348 DB |
284 | if (!subkey) |
285 | return 0; | |
896c0535 | 286 | branch = make_branch(name, subkey - name); |
cf818348 | 287 | if (!strcmp(subkey, ".remote")) { |
d2370cc2 JH |
288 | if (!value) |
289 | return config_error_nonbool(key); | |
cf818348 DB |
290 | branch->remote_name = xstrdup(value); |
291 | if (branch == current_branch) | |
292 | default_remote_name = branch->remote_name; | |
d2370cc2 JH |
293 | } else if (!strcmp(subkey, ".merge")) { |
294 | if (!value) | |
295 | return config_error_nonbool(key); | |
cf818348 | 296 | add_merge(branch, xstrdup(value)); |
d2370cc2 | 297 | } |
cf818348 | 298 | return 0; |
5751f490 | 299 | } |
55029ae4 DB |
300 | if (!prefixcmp(key, "url.")) { |
301 | struct rewrite *rewrite; | |
302 | name = key + 5; | |
303 | subkey = strrchr(name, '.'); | |
304 | if (!subkey) | |
305 | return 0; | |
306 | rewrite = make_rewrite(name, subkey - name); | |
307 | if (!strcmp(subkey, ".insteadof")) { | |
308 | if (!value) | |
309 | return config_error_nonbool(key); | |
310 | add_instead_of(rewrite, xstrdup(value)); | |
311 | } | |
312 | } | |
5751f490 DB |
313 | if (prefixcmp(key, "remote.")) |
314 | return 0; | |
315 | name = key + 7; | |
316 | subkey = strrchr(name, '.'); | |
317 | if (!subkey) | |
318 | return error("Config with no key for remote %s", name); | |
319 | if (*subkey == '/') { | |
320 | warning("Config remote shorthand cannot begin with '/': %s", name); | |
321 | return 0; | |
322 | } | |
323 | remote = make_remote(name, subkey - name); | |
324 | if (!value) { | |
325 | /* if we ever have a boolean variable, e.g. "remote.*.disabled" | |
326 | * [remote "frotz"] | |
327 | * disabled | |
328 | * is a valid way to set it to true; we get NULL in value so | |
329 | * we need to handle it here. | |
330 | * | |
331 | * if (!strcmp(subkey, ".disabled")) { | |
332 | * val = git_config_bool(key, value); | |
333 | * return 0; | |
334 | * } else | |
335 | * | |
336 | */ | |
337 | return 0; /* ignore unknown booleans */ | |
338 | } | |
339 | if (!strcmp(subkey, ".url")) { | |
28b91f8a | 340 | add_url(remote, xstrdup(value)); |
5751f490 DB |
341 | } else if (!strcmp(subkey, ".push")) { |
342 | add_push_refspec(remote, xstrdup(value)); | |
5d46c9d4 DB |
343 | } else if (!strcmp(subkey, ".fetch")) { |
344 | add_fetch_refspec(remote, xstrdup(value)); | |
5751f490 DB |
345 | } else if (!strcmp(subkey, ".receivepack")) { |
346 | if (!remote->receivepack) | |
347 | remote->receivepack = xstrdup(value); | |
348 | else | |
349 | error("more than one receivepack given, using the first"); | |
0012ba21 DB |
350 | } else if (!strcmp(subkey, ".uploadpack")) { |
351 | if (!remote->uploadpack) | |
352 | remote->uploadpack = xstrdup(value); | |
353 | else | |
354 | error("more than one uploadpack given, using the first"); | |
d71ab174 DB |
355 | } else if (!strcmp(subkey, ".tagopt")) { |
356 | if (!strcmp(value, "--no-tags")) | |
357 | remote->fetch_tags = -1; | |
14c98218 SV |
358 | } else if (!strcmp(subkey, ".proxy")) { |
359 | remote->http_proxy = xstrdup(value); | |
211c8968 JS |
360 | } else if (!strcmp(subkey, ".skipdefaultupdate")) |
361 | remote->skip_default_update = 1; | |
5751f490 DB |
362 | return 0; |
363 | } | |
364 | ||
55029ae4 DB |
365 | static void alias_all_urls(void) |
366 | { | |
367 | int i, j; | |
368 | for (i = 0; i < remotes_nr; i++) { | |
369 | if (!remotes[i]) | |
370 | continue; | |
371 | for (j = 0; j < remotes[i]->url_nr; j++) { | |
372 | remotes[i]->url[j] = alias_url(remotes[i]->url[j]); | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
5751f490 DB |
377 | static void read_config(void) |
378 | { | |
379 | unsigned char sha1[20]; | |
380 | const char *head_ref; | |
381 | int flag; | |
382 | if (default_remote_name) // did this already | |
383 | return; | |
384 | default_remote_name = xstrdup("origin"); | |
385 | current_branch = NULL; | |
386 | head_ref = resolve_ref("HEAD", sha1, 0, &flag); | |
387 | if (head_ref && (flag & REF_ISSYMREF) && | |
388 | !prefixcmp(head_ref, "refs/heads/")) { | |
cf818348 DB |
389 | current_branch = |
390 | make_branch(head_ref + strlen("refs/heads/"), 0); | |
5751f490 DB |
391 | } |
392 | git_config(handle_config); | |
55029ae4 | 393 | alias_all_urls(); |
5751f490 DB |
394 | } |
395 | ||
46220ca1 | 396 | static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch) |
6b62816c DB |
397 | { |
398 | int i; | |
ef00d150 | 399 | int st; |
6b62816c | 400 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); |
46220ca1 | 401 | |
6b62816c | 402 | for (i = 0; i < nr_refspec; i++) { |
46220ca1 JH |
403 | size_t llen, rlen; |
404 | int is_glob; | |
405 | const char *lhs, *rhs; | |
406 | ||
407 | llen = rlen = is_glob = 0; | |
408 | ||
409 | lhs = refspec[i]; | |
410 | if (*lhs == '+') { | |
6b62816c | 411 | rs[i].force = 1; |
46220ca1 | 412 | lhs++; |
6b62816c | 413 | } |
46220ca1 JH |
414 | |
415 | rhs = strrchr(lhs, ':'); | |
416 | if (rhs) { | |
417 | rhs++; | |
418 | rlen = strlen(rhs); | |
419 | is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*")); | |
420 | rs[i].dst = xstrndup(rhs, rlen - is_glob * 2); | |
6b62816c | 421 | } |
ef00d150 | 422 | |
46220ca1 JH |
423 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); |
424 | if (is_glob != (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2))) | |
425 | goto invalid; | |
426 | ||
427 | if (is_glob) { | |
428 | llen -= 2; | |
429 | rlen -= 2; | |
ef00d150 | 430 | } |
46220ca1 JH |
431 | rs[i].pattern = is_glob; |
432 | rs[i].src = xstrndup(lhs, llen); | |
433 | ||
434 | if (fetch) { | |
435 | /* | |
436 | * LHS | |
437 | * - empty is allowed; it means HEAD. | |
438 | * - otherwise it must be a valid looking ref. | |
439 | */ | |
440 | if (!*rs[i].src) | |
441 | ; /* empty is ok */ | |
442 | else { | |
443 | st = check_ref_format(rs[i].src); | |
444 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
445 | goto invalid; | |
446 | } | |
447 | /* | |
448 | * RHS | |
449 | * - missing is allowed. | |
450 | * - empty is ok; it means not to store. | |
451 | * - otherwise it must be a valid looking ref. | |
452 | */ | |
453 | if (!rs[i].dst) { | |
454 | ; /* ok */ | |
455 | } else if (!*rs[i].dst) { | |
456 | ; /* ok */ | |
457 | } else { | |
458 | st = check_ref_format(rs[i].dst); | |
459 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
460 | goto invalid; | |
461 | } | |
462 | } else { | |
463 | /* | |
464 | * LHS | |
465 | * - empty is allowed; it means delete. | |
466 | * - when wildcarded, it must be a valid looking ref. | |
467 | * - otherwise, it must be an extended SHA-1, but | |
468 | * there is no existing way to validate this. | |
469 | */ | |
470 | if (!*rs[i].src) | |
471 | ; /* empty is ok */ | |
472 | else if (is_glob) { | |
473 | st = check_ref_format(rs[i].src); | |
474 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
475 | goto invalid; | |
476 | } | |
477 | else | |
478 | ; /* anything goes, for now */ | |
479 | /* | |
480 | * RHS | |
481 | * - missing is allowed, but LHS then must be a | |
482 | * valid looking ref. | |
483 | * - empty is not allowed. | |
484 | * - otherwise it must be a valid looking ref. | |
485 | */ | |
486 | if (!rs[i].dst) { | |
487 | st = check_ref_format(rs[i].src); | |
488 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
489 | goto invalid; | |
490 | } else if (!*rs[i].dst) { | |
491 | goto invalid; | |
492 | } else { | |
493 | st = check_ref_format(rs[i].dst); | |
494 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
495 | goto invalid; | |
496 | } | |
ef00d150 | 497 | } |
6b62816c DB |
498 | } |
499 | return rs; | |
46220ca1 JH |
500 | |
501 | invalid: | |
502 | die("Invalid refspec '%s'", refspec[i]); | |
503 | } | |
504 | ||
505 | struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec) | |
506 | { | |
507 | return parse_refspec_internal(nr_refspec, refspec, 1); | |
508 | } | |
509 | ||
510 | struct refspec *parse_push_refspec(int nr_refspec, const char **refspec) | |
511 | { | |
512 | return parse_refspec_internal(nr_refspec, refspec, 0); | |
6b62816c DB |
513 | } |
514 | ||
df93e33c DB |
515 | static int valid_remote_nick(const char *name) |
516 | { | |
517 | if (!name[0] || /* not empty */ | |
518 | (name[0] == '.' && /* not "." */ | |
519 | (!name[1] || /* not ".." */ | |
520 | (name[1] == '.' && !name[2])))) | |
521 | return 0; | |
522 | return !strchr(name, '/'); /* no slash */ | |
523 | } | |
524 | ||
5751f490 DB |
525 | struct remote *remote_get(const char *name) |
526 | { | |
527 | struct remote *ret; | |
528 | ||
529 | read_config(); | |
530 | if (!name) | |
531 | name = default_remote_name; | |
532 | ret = make_remote(name, 0); | |
df93e33c | 533 | if (valid_remote_nick(name)) { |
28b91f8a | 534 | if (!ret->url) |
5751f490 | 535 | read_remotes_file(ret); |
28b91f8a | 536 | if (!ret->url) |
5751f490 DB |
537 | read_branches_file(ret); |
538 | } | |
28b91f8a | 539 | if (!ret->url) |
55029ae4 | 540 | add_url_alias(ret, name); |
28b91f8a | 541 | if (!ret->url) |
5751f490 | 542 | return NULL; |
46220ca1 JH |
543 | ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); |
544 | ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec); | |
5751f490 DB |
545 | return ret; |
546 | } | |
6b62816c | 547 | |
b42f6927 JS |
548 | int for_each_remote(each_remote_fn fn, void *priv) |
549 | { | |
550 | int i, result = 0; | |
551 | read_config(); | |
2d31347b | 552 | for (i = 0; i < remotes_nr && !result; i++) { |
b42f6927 JS |
553 | struct remote *r = remotes[i]; |
554 | if (!r) | |
555 | continue; | |
556 | if (!r->fetch) | |
46220ca1 JH |
557 | r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, |
558 | r->fetch_refspec); | |
b42f6927 | 559 | if (!r->push) |
46220ca1 JH |
560 | r->push = parse_push_refspec(r->push_refspec_nr, |
561 | r->push_refspec); | |
b42f6927 JS |
562 | result = fn(r, priv); |
563 | } | |
564 | return result; | |
565 | } | |
566 | ||
2467a4fa DB |
567 | void ref_remove_duplicates(struct ref *ref_map) |
568 | { | |
569 | struct ref **posn; | |
570 | struct ref *next; | |
571 | for (; ref_map; ref_map = ref_map->next) { | |
572 | if (!ref_map->peer_ref) | |
573 | continue; | |
574 | posn = &ref_map->next; | |
575 | while (*posn) { | |
576 | if ((*posn)->peer_ref && | |
577 | !strcmp((*posn)->peer_ref->name, | |
578 | ref_map->peer_ref->name)) { | |
579 | if (strcmp((*posn)->name, ref_map->name)) | |
580 | die("%s tracks both %s and %s", | |
581 | ref_map->peer_ref->name, | |
582 | (*posn)->name, ref_map->name); | |
583 | next = (*posn)->next; | |
584 | free((*posn)->peer_ref); | |
585 | free(*posn); | |
586 | *posn = next; | |
587 | } else { | |
588 | posn = &(*posn)->next; | |
589 | } | |
590 | } | |
591 | } | |
592 | } | |
593 | ||
28b91f8a | 594 | int remote_has_url(struct remote *remote, const char *url) |
5d46c9d4 DB |
595 | { |
596 | int i; | |
28b91f8a SP |
597 | for (i = 0; i < remote->url_nr; i++) { |
598 | if (!strcmp(remote->url[i], url)) | |
5d46c9d4 DB |
599 | return 1; |
600 | } | |
601 | return 0; | |
602 | } | |
603 | ||
604 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) | |
605 | { | |
b42f6927 JS |
606 | int find_src = refspec->src == NULL; |
607 | char *needle, **result; | |
5d46c9d4 | 608 | int i; |
b42f6927 JS |
609 | |
610 | if (find_src) { | |
009c5bcd | 611 | if (!refspec->dst) |
b42f6927 JS |
612 | return error("find_tracking: need either src or dst"); |
613 | needle = refspec->dst; | |
614 | result = &refspec->src; | |
615 | } else { | |
616 | needle = refspec->src; | |
617 | result = &refspec->dst; | |
618 | } | |
619 | ||
5d46c9d4 DB |
620 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
621 | struct refspec *fetch = &remote->fetch[i]; | |
b42f6927 JS |
622 | const char *key = find_src ? fetch->dst : fetch->src; |
623 | const char *value = find_src ? fetch->src : fetch->dst; | |
5d46c9d4 DB |
624 | if (!fetch->dst) |
625 | continue; | |
626 | if (fetch->pattern) { | |
ef00d150 DB |
627 | if (!prefixcmp(needle, key) && |
628 | needle[strlen(key)] == '/') { | |
b42f6927 JS |
629 | *result = xmalloc(strlen(value) + |
630 | strlen(needle) - | |
631 | strlen(key) + 1); | |
632 | strcpy(*result, value); | |
633 | strcpy(*result + strlen(value), | |
634 | needle + strlen(key)); | |
5d46c9d4 DB |
635 | refspec->force = fetch->force; |
636 | return 0; | |
637 | } | |
b42f6927 JS |
638 | } else if (!strcmp(needle, key)) { |
639 | *result = xstrdup(value); | |
640 | refspec->force = fetch->force; | |
641 | return 0; | |
5d46c9d4 DB |
642 | } |
643 | } | |
5d46c9d4 DB |
644 | return -1; |
645 | } | |
646 | ||
dfd255dd DB |
647 | struct ref *alloc_ref(unsigned namelen) |
648 | { | |
649 | struct ref *ret = xmalloc(sizeof(struct ref) + namelen); | |
650 | memset(ret, 0, sizeof(struct ref) + namelen); | |
651 | return ret; | |
652 | } | |
653 | ||
4577370e | 654 | static struct ref *copy_ref(const struct ref *ref) |
d71ab174 DB |
655 | { |
656 | struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1); | |
657 | memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1); | |
658 | ret->next = NULL; | |
659 | return ret; | |
660 | } | |
661 | ||
4577370e DB |
662 | struct ref *copy_ref_list(const struct ref *ref) |
663 | { | |
664 | struct ref *ret = NULL; | |
665 | struct ref **tail = &ret; | |
666 | while (ref) { | |
667 | *tail = copy_ref(ref); | |
668 | ref = ref->next; | |
669 | tail = &((*tail)->next); | |
670 | } | |
671 | return ret; | |
672 | } | |
673 | ||
dfd255dd DB |
674 | void free_refs(struct ref *ref) |
675 | { | |
676 | struct ref *next; | |
677 | while (ref) { | |
678 | next = ref->next; | |
8e0f7003 | 679 | free(ref->peer_ref); |
dfd255dd DB |
680 | free(ref); |
681 | ref = next; | |
682 | } | |
683 | } | |
684 | ||
6b62816c DB |
685 | static int count_refspec_match(const char *pattern, |
686 | struct ref *refs, | |
687 | struct ref **matched_ref) | |
688 | { | |
689 | int patlen = strlen(pattern); | |
690 | struct ref *matched_weak = NULL; | |
691 | struct ref *matched = NULL; | |
692 | int weak_match = 0; | |
693 | int match = 0; | |
694 | ||
695 | for (weak_match = match = 0; refs; refs = refs->next) { | |
696 | char *name = refs->name; | |
697 | int namelen = strlen(name); | |
6b62816c | 698 | |
ae36bdcf | 699 | if (!refname_match(pattern, name, ref_rev_parse_rules)) |
6b62816c DB |
700 | continue; |
701 | ||
702 | /* A match is "weak" if it is with refs outside | |
703 | * heads or tags, and did not specify the pattern | |
704 | * in full (e.g. "refs/remotes/origin/master") or at | |
705 | * least from the toplevel (e.g. "remotes/origin/master"); | |
706 | * otherwise "git push $URL master" would result in | |
707 | * ambiguity between remotes/origin/master and heads/master | |
708 | * at the remote site. | |
709 | */ | |
710 | if (namelen != patlen && | |
711 | patlen != namelen - 5 && | |
712 | prefixcmp(name, "refs/heads/") && | |
713 | prefixcmp(name, "refs/tags/")) { | |
714 | /* We want to catch the case where only weak | |
715 | * matches are found and there are multiple | |
716 | * matches, and where more than one strong | |
717 | * matches are found, as ambiguous. One | |
718 | * strong match with zero or more weak matches | |
719 | * are acceptable as a unique match. | |
720 | */ | |
721 | matched_weak = refs; | |
722 | weak_match++; | |
723 | } | |
724 | else { | |
725 | matched = refs; | |
726 | match++; | |
727 | } | |
728 | } | |
729 | if (!matched) { | |
730 | *matched_ref = matched_weak; | |
731 | return weak_match; | |
732 | } | |
733 | else { | |
734 | *matched_ref = matched; | |
735 | return match; | |
736 | } | |
737 | } | |
738 | ||
1d735267 | 739 | static void tail_link_ref(struct ref *ref, struct ref ***tail) |
6b62816c DB |
740 | { |
741 | **tail = ref; | |
1d735267 DB |
742 | while (ref->next) |
743 | ref = ref->next; | |
6b62816c | 744 | *tail = &ref->next; |
6b62816c DB |
745 | } |
746 | ||
747 | static struct ref *try_explicit_object_name(const char *name) | |
748 | { | |
749 | unsigned char sha1[20]; | |
750 | struct ref *ref; | |
751 | int len; | |
752 | ||
753 | if (!*name) { | |
dfd255dd | 754 | ref = alloc_ref(20); |
6b62816c DB |
755 | strcpy(ref->name, "(delete)"); |
756 | hashclr(ref->new_sha1); | |
757 | return ref; | |
758 | } | |
759 | if (get_sha1(name, sha1)) | |
760 | return NULL; | |
761 | len = strlen(name) + 1; | |
dfd255dd | 762 | ref = alloc_ref(len); |
6b62816c DB |
763 | memcpy(ref->name, name, len); |
764 | hashcpy(ref->new_sha1, sha1); | |
765 | return ref; | |
766 | } | |
767 | ||
1d735267 | 768 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
6b62816c | 769 | { |
1d735267 | 770 | struct ref *ret; |
163f0ee5 | 771 | size_t len; |
6b62816c | 772 | |
163f0ee5 | 773 | len = strlen(name) + 1; |
1d735267 DB |
774 | ret = alloc_ref(len); |
775 | memcpy(ret->name, name, len); | |
776 | tail_link_ref(ret, tail); | |
777 | return ret; | |
163f0ee5 | 778 | } |
8558fd9e | 779 | |
54a8ad92 JH |
780 | static int match_explicit(struct ref *src, struct ref *dst, |
781 | struct ref ***dst_tail, | |
782 | struct refspec *rs, | |
783 | int errs) | |
6b62816c | 784 | { |
54a8ad92 | 785 | struct ref *matched_src, *matched_dst; |
8558fd9e | 786 | |
54a8ad92 | 787 | const char *dst_value = rs->dst; |
6b62816c | 788 | |
54a8ad92 JH |
789 | if (rs->pattern) |
790 | return errs; | |
8558fd9e | 791 | |
54a8ad92 JH |
792 | matched_src = matched_dst = NULL; |
793 | switch (count_refspec_match(rs->src, src, &matched_src)) { | |
794 | case 1: | |
795 | break; | |
796 | case 0: | |
797 | /* The source could be in the get_sha1() format | |
798 | * not a reference name. :refs/other is a | |
799 | * way to delete 'other' ref at the remote end. | |
800 | */ | |
801 | matched_src = try_explicit_object_name(rs->src); | |
7dfee372 SP |
802 | if (!matched_src) |
803 | error("src refspec %s does not match any.", rs->src); | |
54a8ad92 JH |
804 | break; |
805 | default: | |
3c8b7df1 | 806 | matched_src = NULL; |
7dfee372 | 807 | error("src refspec %s matches more than one.", rs->src); |
54a8ad92 JH |
808 | break; |
809 | } | |
3c8b7df1 JH |
810 | |
811 | if (!matched_src) | |
812 | errs = 1; | |
813 | ||
4491e62a | 814 | if (!dst_value) { |
9f0ea7e8 DB |
815 | unsigned char sha1[20]; |
816 | int flag; | |
817 | ||
4491e62a SP |
818 | if (!matched_src) |
819 | return errs; | |
9f0ea7e8 DB |
820 | dst_value = resolve_ref(matched_src->name, sha1, 1, &flag); |
821 | if (!dst_value || | |
822 | ((flag & REF_ISSYMREF) && | |
823 | prefixcmp(dst_value, "refs/heads/"))) | |
824 | die("%s cannot be resolved to branch.", | |
825 | matched_src->name); | |
4491e62a | 826 | } |
3c8b7df1 | 827 | |
54a8ad92 JH |
828 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
829 | case 1: | |
830 | break; | |
831 | case 0: | |
163f0ee5 | 832 | if (!memcmp(dst_value, "refs/", 5)) |
1d735267 | 833 | matched_dst = make_linked_ref(dst_value, dst_tail); |
3c8b7df1 | 834 | else |
54a8ad92 JH |
835 | error("dst refspec %s does not match any " |
836 | "existing ref on the remote and does " | |
837 | "not start with refs/.", dst_value); | |
54a8ad92 JH |
838 | break; |
839 | default: | |
3c8b7df1 | 840 | matched_dst = NULL; |
54a8ad92 JH |
841 | error("dst refspec %s matches more than one.", |
842 | dst_value); | |
843 | break; | |
844 | } | |
009c5bcd | 845 | if (errs || !matched_dst) |
3c8b7df1 | 846 | return 1; |
54a8ad92 JH |
847 | if (matched_dst->peer_ref) { |
848 | errs = 1; | |
849 | error("dst ref %s receives from more than one src.", | |
850 | matched_dst->name); | |
6b62816c | 851 | } |
54a8ad92 JH |
852 | else { |
853 | matched_dst->peer_ref = matched_src; | |
854 | matched_dst->force = rs->force; | |
6b62816c | 855 | } |
54a8ad92 JH |
856 | return errs; |
857 | } | |
858 | ||
859 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
860 | struct ref ***dst_tail, struct refspec *rs, | |
861 | int rs_nr) | |
862 | { | |
863 | int i, errs; | |
864 | for (i = errs = 0; i < rs_nr; i++) | |
865 | errs |= match_explicit(src, dst, dst_tail, &rs[i], errs); | |
6b62816c DB |
866 | return -errs; |
867 | } | |
868 | ||
6e66bf3c AR |
869 | static const struct refspec *check_pattern_match(const struct refspec *rs, |
870 | int rs_nr, | |
871 | const struct ref *src) | |
8558fd9e DB |
872 | { |
873 | int i; | |
8558fd9e | 874 | for (i = 0; i < rs_nr; i++) { |
ef00d150 DB |
875 | if (rs[i].pattern && |
876 | !prefixcmp(src->name, rs[i].src) && | |
877 | src->name[strlen(rs[i].src)] == '/') | |
6e66bf3c | 878 | return rs + i; |
8558fd9e | 879 | } |
6e66bf3c | 880 | return NULL; |
8558fd9e DB |
881 | } |
882 | ||
54a8ad92 JH |
883 | /* |
884 | * Note. This is used only by "push"; refspec matching rules for | |
885 | * push and fetch are subtly different, so do not try to reuse it | |
886 | * without thinking. | |
887 | */ | |
6b62816c | 888 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
28b9d6e5 | 889 | int nr_refspec, const char **refspec, int flags) |
6b62816c DB |
890 | { |
891 | struct refspec *rs = | |
46220ca1 | 892 | parse_push_refspec(nr_refspec, (const char **) refspec); |
28b9d6e5 AW |
893 | int send_all = flags & MATCH_REFS_ALL; |
894 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
6b62816c | 895 | |
8558fd9e DB |
896 | if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec)) |
897 | return -1; | |
6b62816c DB |
898 | |
899 | /* pick the remainder */ | |
900 | for ( ; src; src = src->next) { | |
901 | struct ref *dst_peer; | |
6e66bf3c AR |
902 | const struct refspec *pat = NULL; |
903 | char *dst_name; | |
6b62816c DB |
904 | if (src->peer_ref) |
905 | continue; | |
6e66bf3c AR |
906 | if (nr_refspec) { |
907 | pat = check_pattern_match(rs, nr_refspec, src); | |
908 | if (!pat) | |
909 | continue; | |
910 | } | |
28b9d6e5 | 911 | else if (!send_mirror && prefixcmp(src->name, "refs/heads/")) |
098e711e JH |
912 | /* |
913 | * "matching refs"; traditionally we pushed everything | |
914 | * including refs outside refs/heads/ hierarchy, but | |
915 | * that does not make much sense these days. | |
916 | */ | |
917 | continue; | |
8558fd9e | 918 | |
6e66bf3c | 919 | if (pat) { |
efd8f793 DB |
920 | const char *dst_side = pat->dst ? pat->dst : pat->src; |
921 | dst_name = xmalloc(strlen(dst_side) + | |
6e66bf3c AR |
922 | strlen(src->name) - |
923 | strlen(pat->src) + 2); | |
efd8f793 | 924 | strcpy(dst_name, dst_side); |
6e66bf3c AR |
925 | strcat(dst_name, src->name + strlen(pat->src)); |
926 | } else | |
aa32eedc | 927 | dst_name = xstrdup(src->name); |
6e66bf3c | 928 | dst_peer = find_ref_by_name(dst, dst_name); |
8558fd9e DB |
929 | if (dst_peer && dst_peer->peer_ref) |
930 | /* We're already sending something to this ref. */ | |
6e66bf3c | 931 | goto free_name; |
28b9d6e5 AW |
932 | |
933 | if (!dst_peer && !nr_refspec && !(send_all || send_mirror)) | |
934 | /* | |
935 | * Remote doesn't have it, and we have no | |
8558fd9e | 936 | * explicit pattern, and we don't have |
28b9d6e5 AW |
937 | * --all nor --mirror. |
938 | */ | |
6e66bf3c | 939 | goto free_name; |
6b62816c DB |
940 | if (!dst_peer) { |
941 | /* Create a new one and link it */ | |
1d735267 | 942 | dst_peer = make_linked_ref(dst_name, dst_tail); |
6b62816c | 943 | hashcpy(dst_peer->new_sha1, src->new_sha1); |
6b62816c DB |
944 | } |
945 | dst_peer->peer_ref = src; | |
5eb73581 JK |
946 | if (pat) |
947 | dst_peer->force = pat->force; | |
6e66bf3c AR |
948 | free_name: |
949 | free(dst_name); | |
6b62816c DB |
950 | } |
951 | return 0; | |
952 | } | |
cf818348 DB |
953 | |
954 | struct branch *branch_get(const char *name) | |
955 | { | |
956 | struct branch *ret; | |
957 | ||
958 | read_config(); | |
959 | if (!name || !*name || !strcmp(name, "HEAD")) | |
960 | ret = current_branch; | |
961 | else | |
962 | ret = make_branch(name, 0); | |
963 | if (ret && ret->remote_name) { | |
964 | ret->remote = remote_get(ret->remote_name); | |
965 | if (ret->merge_nr) { | |
966 | int i; | |
967 | ret->merge = xcalloc(sizeof(*ret->merge), | |
968 | ret->merge_nr); | |
969 | for (i = 0; i < ret->merge_nr; i++) { | |
970 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); | |
971 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); | |
972 | remote_find_tracking(ret->remote, | |
973 | ret->merge[i]); | |
974 | } | |
975 | } | |
976 | } | |
977 | return ret; | |
978 | } | |
979 | ||
980 | int branch_has_merge_config(struct branch *branch) | |
981 | { | |
982 | return branch && !!branch->merge; | |
983 | } | |
984 | ||
85682c19 SP |
985 | int branch_merge_matches(struct branch *branch, |
986 | int i, | |
987 | const char *refname) | |
cf818348 | 988 | { |
85682c19 | 989 | if (!branch || i < 0 || i >= branch->merge_nr) |
cf818348 | 990 | return 0; |
605b4978 | 991 | return refname_match(branch->merge[i]->src, refname, ref_fetch_rules); |
cf818348 | 992 | } |
d71ab174 | 993 | |
4577370e | 994 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
d71ab174 DB |
995 | const struct refspec *refspec) |
996 | { | |
4577370e | 997 | const struct ref *ref; |
d71ab174 DB |
998 | struct ref *ret = NULL; |
999 | struct ref **tail = &ret; | |
1000 | ||
1001 | int remote_prefix_len = strlen(refspec->src); | |
1002 | int local_prefix_len = strlen(refspec->dst); | |
1003 | ||
1004 | for (ref = remote_refs; ref; ref = ref->next) { | |
1005 | if (strchr(ref->name, '^')) | |
1006 | continue; /* a dereference item */ | |
1007 | if (!prefixcmp(ref->name, refspec->src)) { | |
4577370e | 1008 | const char *match; |
d71ab174 DB |
1009 | struct ref *cpy = copy_ref(ref); |
1010 | match = ref->name + remote_prefix_len; | |
1011 | ||
1012 | cpy->peer_ref = alloc_ref(local_prefix_len + | |
1013 | strlen(match) + 1); | |
1014 | sprintf(cpy->peer_ref->name, "%s%s", | |
1015 | refspec->dst, match); | |
1016 | if (refspec->force) | |
1017 | cpy->peer_ref->force = 1; | |
1018 | *tail = cpy; | |
1019 | tail = &cpy->next; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | return ret; | |
1024 | } | |
1025 | ||
4577370e | 1026 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
d71ab174 | 1027 | { |
4577370e | 1028 | const struct ref *ref; |
d71ab174 | 1029 | for (ref = refs; ref; ref = ref->next) { |
605b4978 | 1030 | if (refname_match(name, ref->name, ref_fetch_rules)) |
d71ab174 DB |
1031 | return ref; |
1032 | } | |
1033 | return NULL; | |
1034 | } | |
1035 | ||
4577370e | 1036 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
d71ab174 | 1037 | { |
4577370e | 1038 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
d71ab174 DB |
1039 | |
1040 | if (!ref) | |
9ad7c5ae | 1041 | return NULL; |
d71ab174 DB |
1042 | |
1043 | return copy_ref(ref); | |
1044 | } | |
1045 | ||
1046 | static struct ref *get_local_ref(const char *name) | |
1047 | { | |
1048 | struct ref *ret; | |
1049 | if (!name) | |
1050 | return NULL; | |
1051 | ||
1052 | if (!prefixcmp(name, "refs/")) { | |
1053 | ret = alloc_ref(strlen(name) + 1); | |
1054 | strcpy(ret->name, name); | |
1055 | return ret; | |
1056 | } | |
1057 | ||
1058 | if (!prefixcmp(name, "heads/") || | |
1059 | !prefixcmp(name, "tags/") || | |
1060 | !prefixcmp(name, "remotes/")) { | |
1061 | ret = alloc_ref(strlen(name) + 6); | |
1062 | sprintf(ret->name, "refs/%s", name); | |
1063 | return ret; | |
1064 | } | |
1065 | ||
1066 | ret = alloc_ref(strlen(name) + 12); | |
1067 | sprintf(ret->name, "refs/heads/%s", name); | |
1068 | return ret; | |
1069 | } | |
1070 | ||
4577370e | 1071 | int get_fetch_map(const struct ref *remote_refs, |
d71ab174 | 1072 | const struct refspec *refspec, |
9ad7c5ae JH |
1073 | struct ref ***tail, |
1074 | int missing_ok) | |
d71ab174 | 1075 | { |
ef00d150 | 1076 | struct ref *ref_map, **rmp; |
d71ab174 DB |
1077 | |
1078 | if (refspec->pattern) { | |
1079 | ref_map = get_expanded_map(remote_refs, refspec); | |
1080 | } else { | |
9ad7c5ae JH |
1081 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
1082 | ||
1083 | ref_map = get_remote_ref(remote_refs, name); | |
1084 | if (!missing_ok && !ref_map) | |
1085 | die("Couldn't find remote ref %s", name); | |
1086 | if (ref_map) { | |
1087 | ref_map->peer_ref = get_local_ref(refspec->dst); | |
1088 | if (ref_map->peer_ref && refspec->force) | |
1089 | ref_map->peer_ref->force = 1; | |
1090 | } | |
d71ab174 DB |
1091 | } |
1092 | ||
ef00d150 DB |
1093 | for (rmp = &ref_map; *rmp; ) { |
1094 | if ((*rmp)->peer_ref) { | |
1095 | int st = check_ref_format((*rmp)->peer_ref->name + 5); | |
1096 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) { | |
1097 | struct ref *ignore = *rmp; | |
1098 | error("* Ignoring funny ref '%s' locally", | |
1099 | (*rmp)->peer_ref->name); | |
1100 | *rmp = (*rmp)->next; | |
1101 | free(ignore->peer_ref); | |
1102 | free(ignore); | |
1103 | continue; | |
1104 | } | |
1105 | } | |
1106 | rmp = &((*rmp)->next); | |
d71ab174 DB |
1107 | } |
1108 | ||
8f70a765 AR |
1109 | if (ref_map) |
1110 | tail_link_ref(ref_map, tail); | |
d71ab174 DB |
1111 | |
1112 | return 0; | |
1113 | } |