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