]>
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 | ||
c091b3d4 | 308 | static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch) |
6b62816c DB |
309 | { |
310 | int i; | |
c091b3d4 | 311 | int st; |
6b62816c | 312 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); |
c091b3d4 | 313 | |
6b62816c | 314 | for (i = 0; i < nr_refspec; i++) { |
c091b3d4 DB |
315 | size_t llen, rlen; |
316 | int is_glob; | |
317 | const char *lhs, *rhs; | |
318 | ||
319 | llen = rlen = is_glob = 0; | |
320 | ||
321 | lhs = refspec[i]; | |
322 | if (*lhs == '+') { | |
6b62816c | 323 | rs[i].force = 1; |
c091b3d4 DB |
324 | lhs++; |
325 | } | |
326 | ||
327 | rhs = strrchr(lhs, ':'); | |
328 | if (rhs) { | |
329 | rhs++; | |
330 | rlen = strlen(rhs); | |
331 | is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*")); | |
332 | if (is_glob) | |
333 | rlen -= 2; | |
334 | rs[i].dst = xstrndup(rhs, rlen); | |
6b62816c | 335 | } |
c091b3d4 DB |
336 | |
337 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); | |
338 | if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) { | |
339 | if ((rhs && !is_glob) || (!rhs && fetch)) | |
340 | goto invalid; | |
341 | is_glob = 1; | |
342 | llen -= 2; | |
343 | } else if (rhs && is_glob) { | |
344 | goto invalid; | |
345 | } | |
346 | ||
347 | rs[i].pattern = is_glob; | |
348 | rs[i].src = xstrndup(lhs, llen); | |
349 | ||
350 | if (fetch) { | |
351 | /* | |
352 | * LHS | |
353 | * - empty is allowed; it means HEAD. | |
354 | * - otherwise it must be a valid looking ref. | |
355 | */ | |
356 | if (!*rs[i].src) | |
357 | ; /* empty is ok */ | |
358 | else { | |
359 | st = check_ref_format(rs[i].src); | |
360 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
361 | goto invalid; | |
362 | } | |
363 | /* | |
364 | * RHS | |
365 | * - missing is ok, and is same as empty. | |
366 | * - empty is ok; it means not to store. | |
367 | * - otherwise it must be a valid looking ref. | |
368 | */ | |
369 | if (!rs[i].dst) { | |
370 | ; /* ok */ | |
371 | } else if (!*rs[i].dst) { | |
372 | ; /* ok */ | |
373 | } else { | |
374 | st = check_ref_format(rs[i].dst); | |
375 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
376 | goto invalid; | |
6b62816c DB |
377 | } |
378 | } else { | |
c091b3d4 DB |
379 | /* |
380 | * LHS | |
381 | * - empty is allowed; it means delete. | |
382 | * - when wildcarded, it must be a valid looking ref. | |
383 | * - otherwise, it must be an extended SHA-1, but | |
384 | * there is no existing way to validate this. | |
385 | */ | |
386 | if (!*rs[i].src) | |
387 | ; /* empty is ok */ | |
388 | else if (is_glob) { | |
389 | st = check_ref_format(rs[i].src); | |
390 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
391 | goto invalid; | |
392 | } | |
393 | else | |
394 | ; /* anything goes, for now */ | |
395 | /* | |
396 | * RHS | |
397 | * - missing is allowed, but LHS then must be a | |
398 | * valid looking ref. | |
399 | * - empty is not allowed. | |
400 | * - otherwise it must be a valid looking ref. | |
401 | */ | |
402 | if (!rs[i].dst) { | |
403 | st = check_ref_format(rs[i].src); | |
404 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
405 | goto invalid; | |
406 | } else if (!*rs[i].dst) { | |
407 | goto invalid; | |
408 | } else { | |
409 | st = check_ref_format(rs[i].dst); | |
410 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) | |
411 | goto invalid; | |
412 | } | |
6b62816c | 413 | } |
6b62816c DB |
414 | } |
415 | return rs; | |
c091b3d4 DB |
416 | |
417 | invalid: | |
418 | die("Invalid refspec '%s'", refspec[i]); | |
419 | } | |
420 | ||
421 | struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec) | |
422 | { | |
423 | return parse_refspec_internal(nr_refspec, refspec, 1); | |
424 | } | |
425 | ||
426 | struct refspec *parse_push_refspec(int nr_refspec, const char **refspec) | |
427 | { | |
428 | return parse_refspec_internal(nr_refspec, refspec, 0); | |
6b62816c DB |
429 | } |
430 | ||
df93e33c DB |
431 | static int valid_remote_nick(const char *name) |
432 | { | |
433 | if (!name[0] || /* not empty */ | |
434 | (name[0] == '.' && /* not "." */ | |
435 | (!name[1] || /* not ".." */ | |
436 | (name[1] == '.' && !name[2])))) | |
437 | return 0; | |
438 | return !strchr(name, '/'); /* no slash */ | |
439 | } | |
440 | ||
5751f490 DB |
441 | struct remote *remote_get(const char *name) |
442 | { | |
443 | struct remote *ret; | |
444 | ||
445 | read_config(); | |
446 | if (!name) | |
447 | name = default_remote_name; | |
448 | ret = make_remote(name, 0); | |
df93e33c | 449 | if (valid_remote_nick(name)) { |
28b91f8a | 450 | if (!ret->url) |
5751f490 | 451 | read_remotes_file(ret); |
28b91f8a | 452 | if (!ret->url) |
5751f490 DB |
453 | read_branches_file(ret); |
454 | } | |
28b91f8a SP |
455 | if (!ret->url) |
456 | add_url(ret, name); | |
457 | if (!ret->url) | |
5751f490 | 458 | return NULL; |
c091b3d4 DB |
459 | ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); |
460 | ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec); | |
5751f490 DB |
461 | return ret; |
462 | } | |
6b62816c | 463 | |
b42f6927 JS |
464 | int for_each_remote(each_remote_fn fn, void *priv) |
465 | { | |
466 | int i, result = 0; | |
467 | read_config(); | |
468 | for (i = 0; i < allocated_remotes && !result; i++) { | |
469 | struct remote *r = remotes[i]; | |
470 | if (!r) | |
471 | continue; | |
472 | if (!r->fetch) | |
c091b3d4 DB |
473 | r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, |
474 | r->fetch_refspec); | |
b42f6927 | 475 | if (!r->push) |
c091b3d4 DB |
476 | r->push = parse_push_refspec(r->push_refspec_nr, |
477 | r->push_refspec); | |
b42f6927 JS |
478 | result = fn(r, priv); |
479 | } | |
480 | return result; | |
481 | } | |
482 | ||
2467a4fa DB |
483 | void ref_remove_duplicates(struct ref *ref_map) |
484 | { | |
485 | struct ref **posn; | |
486 | struct ref *next; | |
487 | for (; ref_map; ref_map = ref_map->next) { | |
488 | if (!ref_map->peer_ref) | |
489 | continue; | |
490 | posn = &ref_map->next; | |
491 | while (*posn) { | |
492 | if ((*posn)->peer_ref && | |
493 | !strcmp((*posn)->peer_ref->name, | |
494 | ref_map->peer_ref->name)) { | |
495 | if (strcmp((*posn)->name, ref_map->name)) | |
496 | die("%s tracks both %s and %s", | |
497 | ref_map->peer_ref->name, | |
498 | (*posn)->name, ref_map->name); | |
499 | next = (*posn)->next; | |
500 | free((*posn)->peer_ref); | |
501 | free(*posn); | |
502 | *posn = next; | |
503 | } else { | |
504 | posn = &(*posn)->next; | |
505 | } | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
28b91f8a | 510 | int remote_has_url(struct remote *remote, const char *url) |
5d46c9d4 DB |
511 | { |
512 | int i; | |
28b91f8a SP |
513 | for (i = 0; i < remote->url_nr; i++) { |
514 | if (!strcmp(remote->url[i], url)) | |
5d46c9d4 DB |
515 | return 1; |
516 | } | |
517 | return 0; | |
518 | } | |
519 | ||
520 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) | |
521 | { | |
b42f6927 JS |
522 | int find_src = refspec->src == NULL; |
523 | char *needle, **result; | |
5d46c9d4 | 524 | int i; |
b42f6927 JS |
525 | |
526 | if (find_src) { | |
009c5bcd | 527 | if (!refspec->dst) |
b42f6927 JS |
528 | return error("find_tracking: need either src or dst"); |
529 | needle = refspec->dst; | |
530 | result = &refspec->src; | |
531 | } else { | |
532 | needle = refspec->src; | |
533 | result = &refspec->dst; | |
534 | } | |
535 | ||
5d46c9d4 DB |
536 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
537 | struct refspec *fetch = &remote->fetch[i]; | |
b42f6927 JS |
538 | const char *key = find_src ? fetch->dst : fetch->src; |
539 | const char *value = find_src ? fetch->src : fetch->dst; | |
5d46c9d4 DB |
540 | if (!fetch->dst) |
541 | continue; | |
542 | if (fetch->pattern) { | |
c091b3d4 DB |
543 | if (!prefixcmp(needle, key) && |
544 | needle[strlen(key)] == '/') { | |
b42f6927 JS |
545 | *result = xmalloc(strlen(value) + |
546 | strlen(needle) - | |
547 | strlen(key) + 1); | |
548 | strcpy(*result, value); | |
549 | strcpy(*result + strlen(value), | |
550 | needle + strlen(key)); | |
5d46c9d4 DB |
551 | refspec->force = fetch->force; |
552 | return 0; | |
553 | } | |
b42f6927 JS |
554 | } else if (!strcmp(needle, key)) { |
555 | *result = xstrdup(value); | |
556 | refspec->force = fetch->force; | |
557 | return 0; | |
5d46c9d4 DB |
558 | } |
559 | } | |
5d46c9d4 DB |
560 | return -1; |
561 | } | |
562 | ||
dfd255dd DB |
563 | struct ref *alloc_ref(unsigned namelen) |
564 | { | |
565 | struct ref *ret = xmalloc(sizeof(struct ref) + namelen); | |
566 | memset(ret, 0, sizeof(struct ref) + namelen); | |
567 | return ret; | |
568 | } | |
569 | ||
4577370e | 570 | static struct ref *copy_ref(const struct ref *ref) |
d71ab174 DB |
571 | { |
572 | struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1); | |
573 | memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1); | |
574 | ret->next = NULL; | |
575 | return ret; | |
576 | } | |
577 | ||
4577370e DB |
578 | struct ref *copy_ref_list(const struct ref *ref) |
579 | { | |
580 | struct ref *ret = NULL; | |
581 | struct ref **tail = &ret; | |
582 | while (ref) { | |
583 | *tail = copy_ref(ref); | |
584 | ref = ref->next; | |
585 | tail = &((*tail)->next); | |
586 | } | |
587 | return ret; | |
588 | } | |
589 | ||
dfd255dd DB |
590 | void free_refs(struct ref *ref) |
591 | { | |
592 | struct ref *next; | |
593 | while (ref) { | |
594 | next = ref->next; | |
595 | if (ref->peer_ref) | |
596 | free(ref->peer_ref); | |
597 | free(ref); | |
598 | ref = next; | |
599 | } | |
600 | } | |
601 | ||
6b62816c DB |
602 | static int count_refspec_match(const char *pattern, |
603 | struct ref *refs, | |
604 | struct ref **matched_ref) | |
605 | { | |
606 | int patlen = strlen(pattern); | |
607 | struct ref *matched_weak = NULL; | |
608 | struct ref *matched = NULL; | |
609 | int weak_match = 0; | |
610 | int match = 0; | |
611 | ||
612 | for (weak_match = match = 0; refs; refs = refs->next) { | |
613 | char *name = refs->name; | |
614 | int namelen = strlen(name); | |
6b62816c | 615 | |
ae36bdcf | 616 | if (!refname_match(pattern, name, ref_rev_parse_rules)) |
6b62816c DB |
617 | continue; |
618 | ||
619 | /* A match is "weak" if it is with refs outside | |
620 | * heads or tags, and did not specify the pattern | |
621 | * in full (e.g. "refs/remotes/origin/master") or at | |
622 | * least from the toplevel (e.g. "remotes/origin/master"); | |
623 | * otherwise "git push $URL master" would result in | |
624 | * ambiguity between remotes/origin/master and heads/master | |
625 | * at the remote site. | |
626 | */ | |
627 | if (namelen != patlen && | |
628 | patlen != namelen - 5 && | |
629 | prefixcmp(name, "refs/heads/") && | |
630 | prefixcmp(name, "refs/tags/")) { | |
631 | /* We want to catch the case where only weak | |
632 | * matches are found and there are multiple | |
633 | * matches, and where more than one strong | |
634 | * matches are found, as ambiguous. One | |
635 | * strong match with zero or more weak matches | |
636 | * are acceptable as a unique match. | |
637 | */ | |
638 | matched_weak = refs; | |
639 | weak_match++; | |
640 | } | |
641 | else { | |
642 | matched = refs; | |
643 | match++; | |
644 | } | |
645 | } | |
646 | if (!matched) { | |
647 | *matched_ref = matched_weak; | |
648 | return weak_match; | |
649 | } | |
650 | else { | |
651 | *matched_ref = matched; | |
652 | return match; | |
653 | } | |
654 | } | |
655 | ||
1d735267 | 656 | static void tail_link_ref(struct ref *ref, struct ref ***tail) |
6b62816c DB |
657 | { |
658 | **tail = ref; | |
1d735267 DB |
659 | while (ref->next) |
660 | ref = ref->next; | |
6b62816c | 661 | *tail = &ref->next; |
6b62816c DB |
662 | } |
663 | ||
664 | static struct ref *try_explicit_object_name(const char *name) | |
665 | { | |
666 | unsigned char sha1[20]; | |
667 | struct ref *ref; | |
668 | int len; | |
669 | ||
670 | if (!*name) { | |
dfd255dd | 671 | ref = alloc_ref(20); |
6b62816c DB |
672 | strcpy(ref->name, "(delete)"); |
673 | hashclr(ref->new_sha1); | |
674 | return ref; | |
675 | } | |
676 | if (get_sha1(name, sha1)) | |
677 | return NULL; | |
678 | len = strlen(name) + 1; | |
dfd255dd | 679 | ref = alloc_ref(len); |
6b62816c DB |
680 | memcpy(ref->name, name, len); |
681 | hashcpy(ref->new_sha1, sha1); | |
682 | return ref; | |
683 | } | |
684 | ||
1d735267 | 685 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
6b62816c | 686 | { |
1d735267 | 687 | struct ref *ret; |
163f0ee5 | 688 | size_t len; |
6b62816c | 689 | |
163f0ee5 | 690 | len = strlen(name) + 1; |
1d735267 DB |
691 | ret = alloc_ref(len); |
692 | memcpy(ret->name, name, len); | |
693 | tail_link_ref(ret, tail); | |
694 | return ret; | |
163f0ee5 | 695 | } |
8558fd9e | 696 | |
54a8ad92 JH |
697 | static int match_explicit(struct ref *src, struct ref *dst, |
698 | struct ref ***dst_tail, | |
699 | struct refspec *rs, | |
700 | int errs) | |
6b62816c | 701 | { |
54a8ad92 | 702 | struct ref *matched_src, *matched_dst; |
8558fd9e | 703 | |
54a8ad92 | 704 | const char *dst_value = rs->dst; |
6b62816c | 705 | |
54a8ad92 JH |
706 | if (rs->pattern) |
707 | return errs; | |
8558fd9e | 708 | |
54a8ad92 JH |
709 | matched_src = matched_dst = NULL; |
710 | switch (count_refspec_match(rs->src, src, &matched_src)) { | |
711 | case 1: | |
712 | break; | |
713 | case 0: | |
714 | /* The source could be in the get_sha1() format | |
715 | * not a reference name. :refs/other is a | |
716 | * way to delete 'other' ref at the remote end. | |
717 | */ | |
718 | matched_src = try_explicit_object_name(rs->src); | |
7dfee372 SP |
719 | if (!matched_src) |
720 | error("src refspec %s does not match any.", rs->src); | |
54a8ad92 JH |
721 | break; |
722 | default: | |
3c8b7df1 | 723 | matched_src = NULL; |
7dfee372 | 724 | error("src refspec %s matches more than one.", rs->src); |
54a8ad92 JH |
725 | break; |
726 | } | |
3c8b7df1 JH |
727 | |
728 | if (!matched_src) | |
729 | errs = 1; | |
730 | ||
4491e62a SP |
731 | if (!dst_value) { |
732 | if (!matched_src) | |
733 | return errs; | |
1ed10b88 | 734 | dst_value = matched_src->name; |
4491e62a | 735 | } |
3c8b7df1 | 736 | |
54a8ad92 JH |
737 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
738 | case 1: | |
739 | break; | |
740 | case 0: | |
163f0ee5 | 741 | if (!memcmp(dst_value, "refs/", 5)) |
1d735267 | 742 | matched_dst = make_linked_ref(dst_value, dst_tail); |
3c8b7df1 | 743 | else |
54a8ad92 JH |
744 | error("dst refspec %s does not match any " |
745 | "existing ref on the remote and does " | |
746 | "not start with refs/.", dst_value); | |
54a8ad92 JH |
747 | break; |
748 | default: | |
3c8b7df1 | 749 | matched_dst = NULL; |
54a8ad92 JH |
750 | error("dst refspec %s matches more than one.", |
751 | dst_value); | |
752 | break; | |
753 | } | |
009c5bcd | 754 | if (errs || !matched_dst) |
3c8b7df1 | 755 | return 1; |
54a8ad92 JH |
756 | if (matched_dst->peer_ref) { |
757 | errs = 1; | |
758 | error("dst ref %s receives from more than one src.", | |
759 | matched_dst->name); | |
6b62816c | 760 | } |
54a8ad92 JH |
761 | else { |
762 | matched_dst->peer_ref = matched_src; | |
763 | matched_dst->force = rs->force; | |
6b62816c | 764 | } |
54a8ad92 JH |
765 | return errs; |
766 | } | |
767 | ||
768 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
769 | struct ref ***dst_tail, struct refspec *rs, | |
770 | int rs_nr) | |
771 | { | |
772 | int i, errs; | |
773 | for (i = errs = 0; i < rs_nr; i++) | |
774 | errs |= match_explicit(src, dst, dst_tail, &rs[i], errs); | |
6b62816c DB |
775 | return -errs; |
776 | } | |
777 | ||
6e66bf3c AR |
778 | static const struct refspec *check_pattern_match(const struct refspec *rs, |
779 | int rs_nr, | |
780 | const struct ref *src) | |
8558fd9e DB |
781 | { |
782 | int i; | |
8558fd9e | 783 | for (i = 0; i < rs_nr; i++) { |
c091b3d4 DB |
784 | if (rs[i].pattern && |
785 | !prefixcmp(src->name, rs[i].src) && | |
786 | src->name[strlen(rs[i].src)] == '/') | |
6e66bf3c | 787 | return rs + i; |
8558fd9e | 788 | } |
6e66bf3c | 789 | return NULL; |
8558fd9e DB |
790 | } |
791 | ||
54a8ad92 JH |
792 | /* |
793 | * Note. This is used only by "push"; refspec matching rules for | |
794 | * push and fetch are subtly different, so do not try to reuse it | |
795 | * without thinking. | |
796 | */ | |
6b62816c | 797 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
28b9d6e5 | 798 | int nr_refspec, const char **refspec, int flags) |
6b62816c DB |
799 | { |
800 | struct refspec *rs = | |
c091b3d4 | 801 | parse_push_refspec(nr_refspec, (const char **) refspec); |
28b9d6e5 AW |
802 | int send_all = flags & MATCH_REFS_ALL; |
803 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
6b62816c | 804 | |
8558fd9e DB |
805 | if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec)) |
806 | return -1; | |
6b62816c DB |
807 | |
808 | /* pick the remainder */ | |
809 | for ( ; src; src = src->next) { | |
810 | struct ref *dst_peer; | |
6e66bf3c AR |
811 | const struct refspec *pat = NULL; |
812 | char *dst_name; | |
6b62816c DB |
813 | if (src->peer_ref) |
814 | continue; | |
6e66bf3c AR |
815 | if (nr_refspec) { |
816 | pat = check_pattern_match(rs, nr_refspec, src); | |
817 | if (!pat) | |
818 | continue; | |
819 | } | |
28b9d6e5 | 820 | else if (!send_mirror && prefixcmp(src->name, "refs/heads/")) |
098e711e JH |
821 | /* |
822 | * "matching refs"; traditionally we pushed everything | |
823 | * including refs outside refs/heads/ hierarchy, but | |
824 | * that does not make much sense these days. | |
825 | */ | |
826 | continue; | |
8558fd9e | 827 | |
6e66bf3c | 828 | if (pat) { |
efd8f793 DB |
829 | const char *dst_side = pat->dst ? pat->dst : pat->src; |
830 | dst_name = xmalloc(strlen(dst_side) + | |
6e66bf3c AR |
831 | strlen(src->name) - |
832 | strlen(pat->src) + 2); | |
efd8f793 | 833 | strcpy(dst_name, dst_side); |
6e66bf3c AR |
834 | strcat(dst_name, src->name + strlen(pat->src)); |
835 | } else | |
aa32eedc | 836 | dst_name = xstrdup(src->name); |
6e66bf3c | 837 | dst_peer = find_ref_by_name(dst, dst_name); |
8558fd9e DB |
838 | if (dst_peer && dst_peer->peer_ref) |
839 | /* We're already sending something to this ref. */ | |
6e66bf3c | 840 | goto free_name; |
28b9d6e5 AW |
841 | |
842 | if (!dst_peer && !nr_refspec && !(send_all || send_mirror)) | |
843 | /* | |
844 | * Remote doesn't have it, and we have no | |
8558fd9e | 845 | * explicit pattern, and we don't have |
28b9d6e5 AW |
846 | * --all nor --mirror. |
847 | */ | |
6e66bf3c | 848 | goto free_name; |
6b62816c DB |
849 | if (!dst_peer) { |
850 | /* Create a new one and link it */ | |
1d735267 | 851 | dst_peer = make_linked_ref(dst_name, dst_tail); |
6b62816c | 852 | hashcpy(dst_peer->new_sha1, src->new_sha1); |
6b62816c DB |
853 | } |
854 | dst_peer->peer_ref = src; | |
5eb73581 JK |
855 | if (pat) |
856 | dst_peer->force = pat->force; | |
6e66bf3c AR |
857 | free_name: |
858 | free(dst_name); | |
6b62816c DB |
859 | } |
860 | return 0; | |
861 | } | |
cf818348 DB |
862 | |
863 | struct branch *branch_get(const char *name) | |
864 | { | |
865 | struct branch *ret; | |
866 | ||
867 | read_config(); | |
868 | if (!name || !*name || !strcmp(name, "HEAD")) | |
869 | ret = current_branch; | |
870 | else | |
871 | ret = make_branch(name, 0); | |
872 | if (ret && ret->remote_name) { | |
873 | ret->remote = remote_get(ret->remote_name); | |
874 | if (ret->merge_nr) { | |
875 | int i; | |
876 | ret->merge = xcalloc(sizeof(*ret->merge), | |
877 | ret->merge_nr); | |
878 | for (i = 0; i < ret->merge_nr; i++) { | |
879 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); | |
880 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); | |
881 | remote_find_tracking(ret->remote, | |
882 | ret->merge[i]); | |
883 | } | |
884 | } | |
885 | } | |
886 | return ret; | |
887 | } | |
888 | ||
889 | int branch_has_merge_config(struct branch *branch) | |
890 | { | |
891 | return branch && !!branch->merge; | |
892 | } | |
893 | ||
85682c19 SP |
894 | int branch_merge_matches(struct branch *branch, |
895 | int i, | |
896 | const char *refname) | |
cf818348 | 897 | { |
85682c19 | 898 | if (!branch || i < 0 || i >= branch->merge_nr) |
cf818348 | 899 | return 0; |
605b4978 | 900 | return refname_match(branch->merge[i]->src, refname, ref_fetch_rules); |
cf818348 | 901 | } |
d71ab174 | 902 | |
4577370e | 903 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
d71ab174 DB |
904 | const struct refspec *refspec) |
905 | { | |
4577370e | 906 | const struct ref *ref; |
d71ab174 DB |
907 | struct ref *ret = NULL; |
908 | struct ref **tail = &ret; | |
909 | ||
910 | int remote_prefix_len = strlen(refspec->src); | |
911 | int local_prefix_len = strlen(refspec->dst); | |
912 | ||
913 | for (ref = remote_refs; ref; ref = ref->next) { | |
914 | if (strchr(ref->name, '^')) | |
915 | continue; /* a dereference item */ | |
916 | if (!prefixcmp(ref->name, refspec->src)) { | |
4577370e | 917 | const char *match; |
d71ab174 DB |
918 | struct ref *cpy = copy_ref(ref); |
919 | match = ref->name + remote_prefix_len; | |
920 | ||
921 | cpy->peer_ref = alloc_ref(local_prefix_len + | |
922 | strlen(match) + 1); | |
923 | sprintf(cpy->peer_ref->name, "%s%s", | |
924 | refspec->dst, match); | |
925 | if (refspec->force) | |
926 | cpy->peer_ref->force = 1; | |
927 | *tail = cpy; | |
928 | tail = &cpy->next; | |
929 | } | |
930 | } | |
931 | ||
932 | return ret; | |
933 | } | |
934 | ||
4577370e | 935 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
d71ab174 | 936 | { |
4577370e | 937 | const struct ref *ref; |
d71ab174 | 938 | for (ref = refs; ref; ref = ref->next) { |
605b4978 | 939 | if (refname_match(name, ref->name, ref_fetch_rules)) |
d71ab174 DB |
940 | return ref; |
941 | } | |
942 | return NULL; | |
943 | } | |
944 | ||
4577370e | 945 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
d71ab174 | 946 | { |
4577370e | 947 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
d71ab174 DB |
948 | |
949 | if (!ref) | |
9ad7c5ae | 950 | return NULL; |
d71ab174 DB |
951 | |
952 | return copy_ref(ref); | |
953 | } | |
954 | ||
955 | static struct ref *get_local_ref(const char *name) | |
956 | { | |
957 | struct ref *ret; | |
958 | if (!name) | |
959 | return NULL; | |
960 | ||
961 | if (!prefixcmp(name, "refs/")) { | |
962 | ret = alloc_ref(strlen(name) + 1); | |
963 | strcpy(ret->name, name); | |
964 | return ret; | |
965 | } | |
966 | ||
967 | if (!prefixcmp(name, "heads/") || | |
968 | !prefixcmp(name, "tags/") || | |
969 | !prefixcmp(name, "remotes/")) { | |
970 | ret = alloc_ref(strlen(name) + 6); | |
971 | sprintf(ret->name, "refs/%s", name); | |
972 | return ret; | |
973 | } | |
974 | ||
975 | ret = alloc_ref(strlen(name) + 12); | |
976 | sprintf(ret->name, "refs/heads/%s", name); | |
977 | return ret; | |
978 | } | |
979 | ||
4577370e | 980 | int get_fetch_map(const struct ref *remote_refs, |
d71ab174 | 981 | const struct refspec *refspec, |
9ad7c5ae JH |
982 | struct ref ***tail, |
983 | int missing_ok) | |
d71ab174 | 984 | { |
c091b3d4 | 985 | struct ref *ref_map, **rmp; |
d71ab174 DB |
986 | |
987 | if (refspec->pattern) { | |
988 | ref_map = get_expanded_map(remote_refs, refspec); | |
989 | } else { | |
9ad7c5ae JH |
990 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
991 | ||
992 | ref_map = get_remote_ref(remote_refs, name); | |
993 | if (!missing_ok && !ref_map) | |
994 | die("Couldn't find remote ref %s", name); | |
995 | if (ref_map) { | |
996 | ref_map->peer_ref = get_local_ref(refspec->dst); | |
997 | if (ref_map->peer_ref && refspec->force) | |
998 | ref_map->peer_ref->force = 1; | |
999 | } | |
d71ab174 DB |
1000 | } |
1001 | ||
c091b3d4 DB |
1002 | for (rmp = &ref_map; *rmp; ) { |
1003 | if ((*rmp)->peer_ref) { | |
1004 | int st = check_ref_format((*rmp)->peer_ref->name + 5); | |
1005 | if (st && st != CHECK_REF_FORMAT_ONELEVEL) { | |
1006 | struct ref *ignore = *rmp; | |
1007 | error("* Ignoring funny ref '%s' locally", | |
1008 | (*rmp)->peer_ref->name); | |
1009 | *rmp = (*rmp)->next; | |
1010 | free(ignore->peer_ref); | |
1011 | free(ignore); | |
1012 | continue; | |
1013 | } | |
1014 | } | |
1015 | rmp = &((*rmp)->next); | |
d71ab174 DB |
1016 | } |
1017 | ||
8f70a765 AR |
1018 | if (ref_map) |
1019 | tail_link_ref(ref_map, tail); | |
d71ab174 DB |
1020 | |
1021 | return 0; | |
1022 | } |