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