]>
Commit | Line | Data |
---|---|---|
5751f490 DB |
1 | #include "cache.h" |
2 | #include "remote.h" | |
3 | #include "refs.h" | |
4 | ||
5 | static struct remote **remotes; | |
6 | static int allocated_remotes; | |
7 | ||
8 | #define BUF_SIZE (2048) | |
9 | static char buffer[BUF_SIZE]; | |
10 | ||
11 | static void add_push_refspec(struct remote *remote, const char *ref) | |
12 | { | |
13 | int nr = remote->push_refspec_nr + 1; | |
14 | remote->push_refspec = | |
15 | xrealloc(remote->push_refspec, nr * sizeof(char *)); | |
16 | remote->push_refspec[nr-1] = ref; | |
17 | remote->push_refspec_nr = nr; | |
18 | } | |
19 | ||
5d46c9d4 DB |
20 | static void add_fetch_refspec(struct remote *remote, const char *ref) |
21 | { | |
22 | int nr = remote->fetch_refspec_nr + 1; | |
23 | remote->fetch_refspec = | |
24 | xrealloc(remote->fetch_refspec, nr * sizeof(char *)); | |
25 | remote->fetch_refspec[nr-1] = ref; | |
26 | remote->fetch_refspec_nr = nr; | |
27 | } | |
28 | ||
5751f490 DB |
29 | static void add_uri(struct remote *remote, const char *uri) |
30 | { | |
31 | int nr = remote->uri_nr + 1; | |
32 | remote->uri = | |
33 | xrealloc(remote->uri, nr * sizeof(char *)); | |
34 | remote->uri[nr-1] = uri; | |
35 | remote->uri_nr = nr; | |
36 | } | |
37 | ||
38 | static struct remote *make_remote(const char *name, int len) | |
39 | { | |
40 | int i, empty = -1; | |
41 | ||
42 | for (i = 0; i < allocated_remotes; i++) { | |
43 | if (!remotes[i]) { | |
44 | if (empty < 0) | |
45 | empty = i; | |
46 | } else { | |
47 | if (len ? (!strncmp(name, remotes[i]->name, len) && | |
48 | !remotes[i]->name[len]) : | |
49 | !strcmp(name, remotes[i]->name)) | |
50 | return remotes[i]; | |
51 | } | |
52 | } | |
53 | ||
54 | if (empty < 0) { | |
55 | empty = allocated_remotes; | |
56 | allocated_remotes += allocated_remotes ? allocated_remotes : 1; | |
57 | remotes = xrealloc(remotes, | |
58 | sizeof(*remotes) * allocated_remotes); | |
59 | memset(remotes + empty, 0, | |
60 | (allocated_remotes - empty) * sizeof(*remotes)); | |
61 | } | |
62 | remotes[empty] = xcalloc(1, sizeof(struct remote)); | |
63 | if (len) | |
64 | remotes[empty]->name = xstrndup(name, len); | |
65 | else | |
66 | remotes[empty]->name = xstrdup(name); | |
67 | return remotes[empty]; | |
68 | } | |
69 | ||
70 | static void read_remotes_file(struct remote *remote) | |
71 | { | |
72 | FILE *f = fopen(git_path("remotes/%s", remote->name), "r"); | |
73 | ||
74 | if (!f) | |
75 | return; | |
76 | while (fgets(buffer, BUF_SIZE, f)) { | |
77 | int value_list; | |
78 | char *s, *p; | |
79 | ||
80 | if (!prefixcmp(buffer, "URL:")) { | |
81 | value_list = 0; | |
82 | s = buffer + 4; | |
83 | } else if (!prefixcmp(buffer, "Push:")) { | |
84 | value_list = 1; | |
85 | s = buffer + 5; | |
5d46c9d4 DB |
86 | } else if (!prefixcmp(buffer, "Pull:")) { |
87 | value_list = 2; | |
88 | s = buffer + 5; | |
5751f490 DB |
89 | } else |
90 | continue; | |
91 | ||
92 | while (isspace(*s)) | |
93 | s++; | |
94 | if (!*s) | |
95 | continue; | |
96 | ||
97 | p = s + strlen(s); | |
98 | while (isspace(p[-1])) | |
99 | *--p = 0; | |
100 | ||
101 | switch (value_list) { | |
102 | case 0: | |
103 | add_uri(remote, xstrdup(s)); | |
104 | break; | |
105 | case 1: | |
106 | add_push_refspec(remote, xstrdup(s)); | |
107 | break; | |
5d46c9d4 DB |
108 | case 2: |
109 | add_fetch_refspec(remote, xstrdup(s)); | |
110 | break; | |
5751f490 DB |
111 | } |
112 | } | |
113 | fclose(f); | |
114 | } | |
115 | ||
116 | static void read_branches_file(struct remote *remote) | |
117 | { | |
118 | const char *slash = strchr(remote->name, '/'); | |
119 | int n = slash ? slash - remote->name : 1000; | |
120 | FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r"); | |
121 | char *s, *p; | |
122 | int len; | |
123 | ||
124 | if (!f) | |
125 | return; | |
126 | s = fgets(buffer, BUF_SIZE, f); | |
127 | fclose(f); | |
128 | if (!s) | |
129 | return; | |
130 | while (isspace(*s)) | |
131 | s++; | |
132 | if (!*s) | |
133 | return; | |
134 | p = s + strlen(s); | |
135 | while (isspace(p[-1])) | |
136 | *--p = 0; | |
137 | len = p - s; | |
138 | if (slash) | |
139 | len += strlen(slash); | |
140 | p = xmalloc(len + 1); | |
141 | strcpy(p, s); | |
142 | if (slash) | |
143 | strcat(p, slash); | |
144 | add_uri(remote, p); | |
145 | } | |
146 | ||
147 | static char *default_remote_name = NULL; | |
148 | static const char *current_branch = NULL; | |
149 | static int current_branch_len = 0; | |
150 | ||
151 | static int handle_config(const char *key, const char *value) | |
152 | { | |
153 | const char *name; | |
154 | const char *subkey; | |
155 | struct remote *remote; | |
156 | if (!prefixcmp(key, "branch.") && current_branch && | |
157 | !strncmp(key + 7, current_branch, current_branch_len) && | |
158 | !strcmp(key + 7 + current_branch_len, ".remote")) { | |
159 | free(default_remote_name); | |
160 | default_remote_name = xstrdup(value); | |
161 | } | |
162 | if (prefixcmp(key, "remote.")) | |
163 | return 0; | |
164 | name = key + 7; | |
165 | subkey = strrchr(name, '.'); | |
166 | if (!subkey) | |
167 | return error("Config with no key for remote %s", name); | |
168 | if (*subkey == '/') { | |
169 | warning("Config remote shorthand cannot begin with '/': %s", name); | |
170 | return 0; | |
171 | } | |
172 | remote = make_remote(name, subkey - name); | |
173 | if (!value) { | |
174 | /* if we ever have a boolean variable, e.g. "remote.*.disabled" | |
175 | * [remote "frotz"] | |
176 | * disabled | |
177 | * is a valid way to set it to true; we get NULL in value so | |
178 | * we need to handle it here. | |
179 | * | |
180 | * if (!strcmp(subkey, ".disabled")) { | |
181 | * val = git_config_bool(key, value); | |
182 | * return 0; | |
183 | * } else | |
184 | * | |
185 | */ | |
186 | return 0; /* ignore unknown booleans */ | |
187 | } | |
188 | if (!strcmp(subkey, ".url")) { | |
189 | add_uri(remote, xstrdup(value)); | |
190 | } else if (!strcmp(subkey, ".push")) { | |
191 | add_push_refspec(remote, xstrdup(value)); | |
5d46c9d4 DB |
192 | } else if (!strcmp(subkey, ".fetch")) { |
193 | add_fetch_refspec(remote, xstrdup(value)); | |
5751f490 DB |
194 | } else if (!strcmp(subkey, ".receivepack")) { |
195 | if (!remote->receivepack) | |
196 | remote->receivepack = xstrdup(value); | |
197 | else | |
198 | error("more than one receivepack given, using the first"); | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static void read_config(void) | |
204 | { | |
205 | unsigned char sha1[20]; | |
206 | const char *head_ref; | |
207 | int flag; | |
208 | if (default_remote_name) // did this already | |
209 | return; | |
210 | default_remote_name = xstrdup("origin"); | |
211 | current_branch = NULL; | |
212 | head_ref = resolve_ref("HEAD", sha1, 0, &flag); | |
213 | if (head_ref && (flag & REF_ISSYMREF) && | |
214 | !prefixcmp(head_ref, "refs/heads/")) { | |
215 | current_branch = head_ref + strlen("refs/heads/"); | |
216 | current_branch_len = strlen(current_branch); | |
217 | } | |
218 | git_config(handle_config); | |
219 | } | |
220 | ||
6b62816c DB |
221 | static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec) |
222 | { | |
223 | int i; | |
224 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); | |
225 | for (i = 0; i < nr_refspec; i++) { | |
226 | const char *sp, *ep, *gp; | |
227 | sp = refspec[i]; | |
228 | if (*sp == '+') { | |
229 | rs[i].force = 1; | |
230 | sp++; | |
231 | } | |
232 | gp = strchr(sp, '*'); | |
233 | ep = strchr(sp, ':'); | |
234 | if (gp && ep && gp > ep) | |
235 | gp = NULL; | |
236 | if (ep) { | |
237 | if (ep[1]) { | |
238 | const char *glob = strchr(ep + 1, '*'); | |
239 | if (!glob) | |
240 | gp = NULL; | |
241 | if (gp) | |
242 | rs[i].dst = xstrndup(ep + 1, | |
243 | glob - ep - 1); | |
244 | else | |
245 | rs[i].dst = xstrdup(ep + 1); | |
246 | } | |
247 | } else { | |
248 | ep = sp + strlen(sp); | |
249 | } | |
250 | if (gp) { | |
251 | rs[i].pattern = 1; | |
252 | ep = gp; | |
253 | } | |
254 | rs[i].src = xstrndup(sp, ep - sp); | |
255 | } | |
256 | return rs; | |
257 | } | |
258 | ||
5751f490 DB |
259 | struct remote *remote_get(const char *name) |
260 | { | |
261 | struct remote *ret; | |
262 | ||
263 | read_config(); | |
264 | if (!name) | |
265 | name = default_remote_name; | |
266 | ret = make_remote(name, 0); | |
267 | if (name[0] != '/') { | |
268 | if (!ret->uri) | |
269 | read_remotes_file(ret); | |
270 | if (!ret->uri) | |
271 | read_branches_file(ret); | |
272 | } | |
273 | if (!ret->uri) | |
274 | add_uri(ret, name); | |
275 | if (!ret->uri) | |
276 | return NULL; | |
5d46c9d4 | 277 | ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec); |
6b62816c | 278 | ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec); |
5751f490 DB |
279 | return ret; |
280 | } | |
6b62816c | 281 | |
5d46c9d4 DB |
282 | int remote_has_uri(struct remote *remote, const char *uri) |
283 | { | |
284 | int i; | |
285 | for (i = 0; i < remote->uri_nr; i++) { | |
286 | if (!strcmp(remote->uri[i], uri)) | |
287 | return 1; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) | |
293 | { | |
294 | int i; | |
295 | for (i = 0; i < remote->fetch_refspec_nr; i++) { | |
296 | struct refspec *fetch = &remote->fetch[i]; | |
297 | if (!fetch->dst) | |
298 | continue; | |
299 | if (fetch->pattern) { | |
300 | if (!prefixcmp(refspec->src, fetch->src)) { | |
301 | refspec->dst = | |
302 | xmalloc(strlen(fetch->dst) + | |
303 | strlen(refspec->src) - | |
304 | strlen(fetch->src) + 1); | |
305 | strcpy(refspec->dst, fetch->dst); | |
306 | strcpy(refspec->dst + strlen(fetch->dst), | |
307 | refspec->src + strlen(fetch->src)); | |
308 | refspec->force = fetch->force; | |
309 | return 0; | |
310 | } | |
311 | } else { | |
312 | if (!strcmp(refspec->src, fetch->src)) { | |
313 | refspec->dst = xstrdup(fetch->dst); | |
314 | refspec->force = fetch->force; | |
315 | return 0; | |
316 | } | |
317 | } | |
318 | } | |
319 | refspec->dst = NULL; | |
320 | return -1; | |
321 | } | |
322 | ||
6b62816c DB |
323 | static int count_refspec_match(const char *pattern, |
324 | struct ref *refs, | |
325 | struct ref **matched_ref) | |
326 | { | |
327 | int patlen = strlen(pattern); | |
328 | struct ref *matched_weak = NULL; | |
329 | struct ref *matched = NULL; | |
330 | int weak_match = 0; | |
331 | int match = 0; | |
332 | ||
333 | for (weak_match = match = 0; refs; refs = refs->next) { | |
334 | char *name = refs->name; | |
335 | int namelen = strlen(name); | |
6b62816c DB |
336 | |
337 | if (namelen < patlen || | |
338 | memcmp(name + namelen - patlen, pattern, patlen)) | |
339 | continue; | |
340 | if (namelen != patlen && name[namelen - patlen - 1] != '/') | |
341 | continue; | |
342 | ||
343 | /* A match is "weak" if it is with refs outside | |
344 | * heads or tags, and did not specify the pattern | |
345 | * in full (e.g. "refs/remotes/origin/master") or at | |
346 | * least from the toplevel (e.g. "remotes/origin/master"); | |
347 | * otherwise "git push $URL master" would result in | |
348 | * ambiguity between remotes/origin/master and heads/master | |
349 | * at the remote site. | |
350 | */ | |
351 | if (namelen != patlen && | |
352 | patlen != namelen - 5 && | |
353 | prefixcmp(name, "refs/heads/") && | |
354 | prefixcmp(name, "refs/tags/")) { | |
355 | /* We want to catch the case where only weak | |
356 | * matches are found and there are multiple | |
357 | * matches, and where more than one strong | |
358 | * matches are found, as ambiguous. One | |
359 | * strong match with zero or more weak matches | |
360 | * are acceptable as a unique match. | |
361 | */ | |
362 | matched_weak = refs; | |
363 | weak_match++; | |
364 | } | |
365 | else { | |
366 | matched = refs; | |
367 | match++; | |
368 | } | |
369 | } | |
370 | if (!matched) { | |
371 | *matched_ref = matched_weak; | |
372 | return weak_match; | |
373 | } | |
374 | else { | |
375 | *matched_ref = matched; | |
376 | return match; | |
377 | } | |
378 | } | |
379 | ||
380 | static void link_dst_tail(struct ref *ref, struct ref ***tail) | |
381 | { | |
382 | **tail = ref; | |
383 | *tail = &ref->next; | |
384 | **tail = NULL; | |
385 | } | |
386 | ||
387 | static struct ref *try_explicit_object_name(const char *name) | |
388 | { | |
389 | unsigned char sha1[20]; | |
390 | struct ref *ref; | |
391 | int len; | |
392 | ||
393 | if (!*name) { | |
394 | ref = xcalloc(1, sizeof(*ref) + 20); | |
395 | strcpy(ref->name, "(delete)"); | |
396 | hashclr(ref->new_sha1); | |
397 | return ref; | |
398 | } | |
399 | if (get_sha1(name, sha1)) | |
400 | return NULL; | |
401 | len = strlen(name) + 1; | |
402 | ref = xcalloc(1, sizeof(*ref) + len); | |
403 | memcpy(ref->name, name, len); | |
404 | hashcpy(ref->new_sha1, sha1); | |
405 | return ref; | |
406 | } | |
407 | ||
163f0ee5 | 408 | static struct ref *make_dst(const char *name, struct ref ***dst_tail) |
6b62816c | 409 | { |
163f0ee5 JH |
410 | struct ref *dst; |
411 | size_t len; | |
6b62816c | 412 | |
163f0ee5 JH |
413 | len = strlen(name) + 1; |
414 | dst = xcalloc(1, sizeof(*dst) + len); | |
415 | memcpy(dst->name, name, len); | |
416 | link_dst_tail(dst, dst_tail); | |
417 | return dst; | |
418 | } | |
8558fd9e | 419 | |
54a8ad92 JH |
420 | static int match_explicit(struct ref *src, struct ref *dst, |
421 | struct ref ***dst_tail, | |
422 | struct refspec *rs, | |
423 | int errs) | |
6b62816c | 424 | { |
54a8ad92 | 425 | struct ref *matched_src, *matched_dst; |
8558fd9e | 426 | |
54a8ad92 | 427 | const char *dst_value = rs->dst; |
6b62816c | 428 | |
54a8ad92 JH |
429 | if (rs->pattern) |
430 | return errs; | |
8558fd9e | 431 | |
54a8ad92 JH |
432 | matched_src = matched_dst = NULL; |
433 | switch (count_refspec_match(rs->src, src, &matched_src)) { | |
434 | case 1: | |
435 | break; | |
436 | case 0: | |
437 | /* The source could be in the get_sha1() format | |
438 | * not a reference name. :refs/other is a | |
439 | * way to delete 'other' ref at the remote end. | |
440 | */ | |
441 | matched_src = try_explicit_object_name(rs->src); | |
442 | if (matched_src) | |
6b62816c | 443 | break; |
54a8ad92 JH |
444 | error("src refspec %s does not match any.", |
445 | rs->src); | |
446 | break; | |
447 | default: | |
3c8b7df1 | 448 | matched_src = NULL; |
54a8ad92 JH |
449 | error("src refspec %s matches more than one.", |
450 | rs->src); | |
451 | break; | |
452 | } | |
3c8b7df1 JH |
453 | |
454 | if (!matched_src) | |
455 | errs = 1; | |
456 | ||
457 | if (dst_value == NULL) | |
1ed10b88 | 458 | dst_value = matched_src->name; |
3c8b7df1 | 459 | |
54a8ad92 JH |
460 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
461 | case 1: | |
462 | break; | |
463 | case 0: | |
163f0ee5 JH |
464 | if (!memcmp(dst_value, "refs/", 5)) |
465 | matched_dst = make_dst(dst_value, dst_tail); | |
3c8b7df1 | 466 | else |
54a8ad92 JH |
467 | error("dst refspec %s does not match any " |
468 | "existing ref on the remote and does " | |
469 | "not start with refs/.", dst_value); | |
54a8ad92 JH |
470 | break; |
471 | default: | |
3c8b7df1 | 472 | matched_dst = NULL; |
54a8ad92 JH |
473 | error("dst refspec %s matches more than one.", |
474 | dst_value); | |
475 | break; | |
476 | } | |
3c8b7df1 JH |
477 | if (errs || matched_dst == NULL) |
478 | return 1; | |
54a8ad92 JH |
479 | if (matched_dst->peer_ref) { |
480 | errs = 1; | |
481 | error("dst ref %s receives from more than one src.", | |
482 | matched_dst->name); | |
6b62816c | 483 | } |
54a8ad92 JH |
484 | else { |
485 | matched_dst->peer_ref = matched_src; | |
486 | matched_dst->force = rs->force; | |
6b62816c | 487 | } |
54a8ad92 JH |
488 | return errs; |
489 | } | |
490 | ||
491 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
492 | struct ref ***dst_tail, struct refspec *rs, | |
493 | int rs_nr) | |
494 | { | |
495 | int i, errs; | |
496 | for (i = errs = 0; i < rs_nr; i++) | |
497 | errs |= match_explicit(src, dst, dst_tail, &rs[i], errs); | |
6b62816c DB |
498 | return -errs; |
499 | } | |
500 | ||
501 | static struct ref *find_ref_by_name(struct ref *list, const char *name) | |
502 | { | |
503 | for ( ; list; list = list->next) | |
504 | if (!strcmp(list->name, name)) | |
505 | return list; | |
506 | return NULL; | |
507 | } | |
508 | ||
6e66bf3c AR |
509 | static const struct refspec *check_pattern_match(const struct refspec *rs, |
510 | int rs_nr, | |
511 | const struct ref *src) | |
8558fd9e DB |
512 | { |
513 | int i; | |
8558fd9e DB |
514 | for (i = 0; i < rs_nr; i++) { |
515 | if (rs[i].pattern && !prefixcmp(src->name, rs[i].src)) | |
6e66bf3c | 516 | return rs + i; |
8558fd9e | 517 | } |
6e66bf3c | 518 | return NULL; |
8558fd9e DB |
519 | } |
520 | ||
54a8ad92 JH |
521 | /* |
522 | * Note. This is used only by "push"; refspec matching rules for | |
523 | * push and fetch are subtly different, so do not try to reuse it | |
524 | * without thinking. | |
525 | */ | |
6b62816c DB |
526 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
527 | int nr_refspec, char **refspec, int all) | |
528 | { | |
529 | struct refspec *rs = | |
530 | parse_ref_spec(nr_refspec, (const char **) refspec); | |
531 | ||
8558fd9e DB |
532 | if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec)) |
533 | return -1; | |
6b62816c DB |
534 | |
535 | /* pick the remainder */ | |
536 | for ( ; src; src = src->next) { | |
537 | struct ref *dst_peer; | |
6e66bf3c AR |
538 | const struct refspec *pat = NULL; |
539 | char *dst_name; | |
6b62816c DB |
540 | if (src->peer_ref) |
541 | continue; | |
6e66bf3c AR |
542 | if (nr_refspec) { |
543 | pat = check_pattern_match(rs, nr_refspec, src); | |
544 | if (!pat) | |
545 | continue; | |
546 | } | |
8558fd9e | 547 | |
6e66bf3c | 548 | if (pat) { |
efd8f793 DB |
549 | const char *dst_side = pat->dst ? pat->dst : pat->src; |
550 | dst_name = xmalloc(strlen(dst_side) + | |
6e66bf3c AR |
551 | strlen(src->name) - |
552 | strlen(pat->src) + 2); | |
efd8f793 | 553 | strcpy(dst_name, dst_side); |
6e66bf3c AR |
554 | strcat(dst_name, src->name + strlen(pat->src)); |
555 | } else | |
aa32eedc | 556 | dst_name = xstrdup(src->name); |
6e66bf3c | 557 | dst_peer = find_ref_by_name(dst, dst_name); |
8558fd9e DB |
558 | if (dst_peer && dst_peer->peer_ref) |
559 | /* We're already sending something to this ref. */ | |
6e66bf3c | 560 | goto free_name; |
8558fd9e DB |
561 | if (!dst_peer && !nr_refspec && !all) |
562 | /* Remote doesn't have it, and we have no | |
563 | * explicit pattern, and we don't have | |
564 | * --all. */ | |
6e66bf3c | 565 | goto free_name; |
6b62816c DB |
566 | if (!dst_peer) { |
567 | /* Create a new one and link it */ | |
163f0ee5 | 568 | dst_peer = make_dst(dst_name, dst_tail); |
6b62816c | 569 | hashcpy(dst_peer->new_sha1, src->new_sha1); |
6b62816c DB |
570 | } |
571 | dst_peer->peer_ref = src; | |
6e66bf3c AR |
572 | free_name: |
573 | free(dst_name); | |
6b62816c DB |
574 | } |
575 | return 0; | |
576 | } |