]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
3 | ||
4 | #include "git-compat-util.h" | |
5 | #include "abspath.h" | |
6 | #include "config.h" | |
7 | #include "environment.h" | |
8 | #include "gettext.h" | |
9 | #include "hex.h" | |
10 | #include "remote.h" | |
11 | #include "urlmatch.h" | |
12 | #include "refs.h" | |
13 | #include "refspec.h" | |
14 | #include "object-name.h" | |
15 | #include "object-store.h" | |
16 | #include "path.h" | |
17 | #include "commit.h" | |
18 | #include "diff.h" | |
19 | #include "revision.h" | |
20 | #include "dir.h" | |
21 | #include "setup.h" | |
22 | #include "string-list.h" | |
23 | #include "strvec.h" | |
24 | #include "commit-reach.h" | |
25 | #include "advice.h" | |
26 | #include "connect.h" | |
27 | #include "parse-options.h" | |
28 | #include "transport.h" | |
29 | ||
30 | enum map_direction { FROM_SRC, FROM_DST }; | |
31 | ||
32 | struct counted_string { | |
33 | size_t len; | |
34 | const char *s; | |
35 | }; | |
36 | ||
37 | static int valid_remote(const struct remote *remote) | |
38 | { | |
39 | return !!remote->url.nr; | |
40 | } | |
41 | ||
42 | static char *alias_url(const char *url, struct rewrites *r) | |
43 | { | |
44 | int i, j; | |
45 | struct counted_string *longest; | |
46 | int longest_i; | |
47 | ||
48 | longest = NULL; | |
49 | longest_i = -1; | |
50 | for (i = 0; i < r->rewrite_nr; i++) { | |
51 | if (!r->rewrite[i]) | |
52 | continue; | |
53 | for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) { | |
54 | if (starts_with(url, r->rewrite[i]->instead_of[j].s) && | |
55 | (!longest || | |
56 | longest->len < r->rewrite[i]->instead_of[j].len)) { | |
57 | longest = &(r->rewrite[i]->instead_of[j]); | |
58 | longest_i = i; | |
59 | } | |
60 | } | |
61 | } | |
62 | if (!longest) | |
63 | return NULL; | |
64 | ||
65 | return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len); | |
66 | } | |
67 | ||
68 | static void add_url(struct remote *remote, const char *url) | |
69 | { | |
70 | if (*url) | |
71 | strvec_push(&remote->url, url); | |
72 | else | |
73 | strvec_clear(&remote->url); | |
74 | } | |
75 | ||
76 | static void add_pushurl(struct remote *remote, const char *pushurl) | |
77 | { | |
78 | if (*pushurl) | |
79 | strvec_push(&remote->pushurl, pushurl); | |
80 | else | |
81 | strvec_clear(&remote->pushurl); | |
82 | } | |
83 | ||
84 | static void add_pushurl_alias(struct remote_state *remote_state, | |
85 | struct remote *remote, const char *url) | |
86 | { | |
87 | char *alias = alias_url(url, &remote_state->rewrites_push); | |
88 | if (alias) | |
89 | add_pushurl(remote, alias); | |
90 | free(alias); | |
91 | } | |
92 | ||
93 | static void add_url_alias(struct remote_state *remote_state, | |
94 | struct remote *remote, const char *url) | |
95 | { | |
96 | char *alias = alias_url(url, &remote_state->rewrites); | |
97 | add_url(remote, alias ? alias : url); | |
98 | add_pushurl_alias(remote_state, remote, url); | |
99 | free(alias); | |
100 | } | |
101 | ||
102 | struct remotes_hash_key { | |
103 | const char *str; | |
104 | int len; | |
105 | }; | |
106 | ||
107 | static int remotes_hash_cmp(const void *cmp_data UNUSED, | |
108 | const struct hashmap_entry *eptr, | |
109 | const struct hashmap_entry *entry_or_key, | |
110 | const void *keydata) | |
111 | { | |
112 | const struct remote *a, *b; | |
113 | const struct remotes_hash_key *key = keydata; | |
114 | ||
115 | a = container_of(eptr, const struct remote, ent); | |
116 | b = container_of(entry_or_key, const struct remote, ent); | |
117 | ||
118 | if (key) | |
119 | return !!xstrncmpz(a->name, key->str, key->len); | |
120 | else | |
121 | return strcmp(a->name, b->name); | |
122 | } | |
123 | ||
124 | static struct remote *make_remote(struct remote_state *remote_state, | |
125 | const char *name, int len) | |
126 | { | |
127 | struct remote *ret; | |
128 | struct remotes_hash_key lookup; | |
129 | struct hashmap_entry lookup_entry, *e; | |
130 | ||
131 | if (!len) | |
132 | len = strlen(name); | |
133 | ||
134 | lookup.str = name; | |
135 | lookup.len = len; | |
136 | hashmap_entry_init(&lookup_entry, memhash(name, len)); | |
137 | ||
138 | e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup); | |
139 | if (e) | |
140 | return container_of(e, struct remote, ent); | |
141 | ||
142 | CALLOC_ARRAY(ret, 1); | |
143 | ret->prune = -1; /* unspecified */ | |
144 | ret->prune_tags = -1; /* unspecified */ | |
145 | ret->name = xstrndup(name, len); | |
146 | refspec_init_push(&ret->push); | |
147 | refspec_init_fetch(&ret->fetch); | |
148 | string_list_init_dup(&ret->server_options); | |
149 | ||
150 | ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1, | |
151 | remote_state->remotes_alloc); | |
152 | remote_state->remotes[remote_state->remotes_nr++] = ret; | |
153 | ||
154 | hashmap_entry_init(&ret->ent, lookup_entry.hash); | |
155 | if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent)) | |
156 | BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); | |
157 | return ret; | |
158 | } | |
159 | ||
160 | static void remote_clear(struct remote *remote) | |
161 | { | |
162 | free((char *)remote->name); | |
163 | free((char *)remote->foreign_vcs); | |
164 | ||
165 | strvec_clear(&remote->url); | |
166 | strvec_clear(&remote->pushurl); | |
167 | ||
168 | free((char *)remote->receivepack); | |
169 | free((char *)remote->uploadpack); | |
170 | FREE_AND_NULL(remote->http_proxy); | |
171 | FREE_AND_NULL(remote->http_proxy_authmethod); | |
172 | string_list_clear(&remote->server_options, 0); | |
173 | } | |
174 | ||
175 | static void add_merge(struct branch *branch, const char *name) | |
176 | { | |
177 | ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, | |
178 | branch->merge_alloc); | |
179 | branch->merge_name[branch->merge_nr++] = name; | |
180 | } | |
181 | ||
182 | struct branches_hash_key { | |
183 | const char *str; | |
184 | int len; | |
185 | }; | |
186 | ||
187 | static int branches_hash_cmp(const void *cmp_data UNUSED, | |
188 | const struct hashmap_entry *eptr, | |
189 | const struct hashmap_entry *entry_or_key, | |
190 | const void *keydata) | |
191 | { | |
192 | const struct branch *a, *b; | |
193 | const struct branches_hash_key *key = keydata; | |
194 | ||
195 | a = container_of(eptr, const struct branch, ent); | |
196 | b = container_of(entry_or_key, const struct branch, ent); | |
197 | ||
198 | if (key) | |
199 | return !!xstrncmpz(a->name, key->str, key->len); | |
200 | else | |
201 | return strcmp(a->name, b->name); | |
202 | } | |
203 | ||
204 | static struct branch *find_branch(struct remote_state *remote_state, | |
205 | const char *name, size_t len) | |
206 | { | |
207 | struct branches_hash_key lookup; | |
208 | struct hashmap_entry lookup_entry, *e; | |
209 | ||
210 | lookup.str = name; | |
211 | lookup.len = len; | |
212 | hashmap_entry_init(&lookup_entry, memhash(name, len)); | |
213 | ||
214 | e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup); | |
215 | if (e) | |
216 | return container_of(e, struct branch, ent); | |
217 | ||
218 | return NULL; | |
219 | } | |
220 | ||
221 | static void die_on_missing_branch(struct repository *repo, | |
222 | struct branch *branch) | |
223 | { | |
224 | /* branch == NULL is always valid because it represents detached HEAD. */ | |
225 | if (branch && | |
226 | branch != find_branch(repo->remote_state, branch->name, | |
227 | strlen(branch->name))) | |
228 | die("branch %s was not found in the repository", branch->name); | |
229 | } | |
230 | ||
231 | static struct branch *make_branch(struct remote_state *remote_state, | |
232 | const char *name, size_t len) | |
233 | { | |
234 | struct branch *ret; | |
235 | ||
236 | ret = find_branch(remote_state, name, len); | |
237 | if (ret) | |
238 | return ret; | |
239 | ||
240 | CALLOC_ARRAY(ret, 1); | |
241 | ret->name = xstrndup(name, len); | |
242 | ret->refname = xstrfmt("refs/heads/%s", ret->name); | |
243 | ||
244 | hashmap_entry_init(&ret->ent, memhash(name, len)); | |
245 | if (hashmap_put_entry(&remote_state->branches_hash, ret, ent)) | |
246 | BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); | |
247 | return ret; | |
248 | } | |
249 | ||
250 | static void branch_release(struct branch *branch) | |
251 | { | |
252 | free((char *)branch->name); | |
253 | free((char *)branch->refname); | |
254 | free(branch->remote_name); | |
255 | free(branch->pushremote_name); | |
256 | for (int i = 0; i < branch->merge_nr; i++) | |
257 | refspec_item_clear(branch->merge[i]); | |
258 | free(branch->merge); | |
259 | } | |
260 | ||
261 | static struct rewrite *make_rewrite(struct rewrites *r, | |
262 | const char *base, size_t len) | |
263 | { | |
264 | struct rewrite *ret; | |
265 | int i; | |
266 | ||
267 | for (i = 0; i < r->rewrite_nr; i++) { | |
268 | if (len == r->rewrite[i]->baselen && | |
269 | !strncmp(base, r->rewrite[i]->base, len)) | |
270 | return r->rewrite[i]; | |
271 | } | |
272 | ||
273 | ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc); | |
274 | CALLOC_ARRAY(ret, 1); | |
275 | r->rewrite[r->rewrite_nr++] = ret; | |
276 | ret->base = xstrndup(base, len); | |
277 | ret->baselen = len; | |
278 | return ret; | |
279 | } | |
280 | ||
281 | static void rewrites_release(struct rewrites *r) | |
282 | { | |
283 | for (int i = 0; i < r->rewrite_nr; i++) | |
284 | free((char *)r->rewrite[i]->base); | |
285 | free(r->rewrite); | |
286 | memset(r, 0, sizeof(*r)); | |
287 | } | |
288 | ||
289 | static void add_instead_of(struct rewrite *rewrite, const char *instead_of) | |
290 | { | |
291 | ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc); | |
292 | rewrite->instead_of[rewrite->instead_of_nr].s = instead_of; | |
293 | rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of); | |
294 | rewrite->instead_of_nr++; | |
295 | } | |
296 | ||
297 | #ifndef WITH_BREAKING_CHANGES | |
298 | static const char *skip_spaces(const char *s) | |
299 | { | |
300 | while (isspace(*s)) | |
301 | s++; | |
302 | return s; | |
303 | } | |
304 | ||
305 | static void warn_about_deprecated_remote_type(const char *type, | |
306 | const struct remote *remote) | |
307 | { | |
308 | warning(_("reading remote from \"%s/%s\", which is nominated for removal.\n" | |
309 | "\n" | |
310 | "If you still use the \"remotes/\" directory it is recommended to\n" | |
311 | "migrate to config-based remotes:\n" | |
312 | "\n" | |
313 | "\tgit remote rename %s %s\n" | |
314 | "\n" | |
315 | "If you cannot, please let us know why you still need to use it by\n" | |
316 | "sending an e-mail to <git@vger.kernel.org>."), | |
317 | type, remote->name, remote->name, remote->name); | |
318 | } | |
319 | ||
320 | static void read_remotes_file(struct remote_state *remote_state, | |
321 | struct remote *remote) | |
322 | { | |
323 | struct strbuf buf = STRBUF_INIT; | |
324 | FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf, | |
325 | "remotes/%s", remote->name), "r"); | |
326 | ||
327 | if (!f) | |
328 | goto out; | |
329 | ||
330 | warn_about_deprecated_remote_type("remotes", remote); | |
331 | ||
332 | remote->configured_in_repo = 1; | |
333 | remote->origin = REMOTE_REMOTES; | |
334 | while (strbuf_getline(&buf, f) != EOF) { | |
335 | const char *v; | |
336 | ||
337 | strbuf_rtrim(&buf); | |
338 | ||
339 | if (skip_prefix(buf.buf, "URL:", &v)) | |
340 | add_url_alias(remote_state, remote, | |
341 | skip_spaces(v)); | |
342 | else if (skip_prefix(buf.buf, "Push:", &v)) | |
343 | refspec_append(&remote->push, skip_spaces(v)); | |
344 | else if (skip_prefix(buf.buf, "Pull:", &v)) | |
345 | refspec_append(&remote->fetch, skip_spaces(v)); | |
346 | } | |
347 | fclose(f); | |
348 | ||
349 | out: | |
350 | strbuf_release(&buf); | |
351 | } | |
352 | ||
353 | static void read_branches_file(struct remote_state *remote_state, | |
354 | struct remote *remote) | |
355 | { | |
356 | char *frag, *to_free = NULL; | |
357 | struct strbuf buf = STRBUF_INIT; | |
358 | FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf, | |
359 | "branches/%s", remote->name), "r"); | |
360 | ||
361 | if (!f) | |
362 | goto out; | |
363 | ||
364 | warn_about_deprecated_remote_type("branches", remote); | |
365 | ||
366 | strbuf_getline_lf(&buf, f); | |
367 | fclose(f); | |
368 | strbuf_trim(&buf); | |
369 | if (!buf.len) | |
370 | goto out; | |
371 | ||
372 | remote->configured_in_repo = 1; | |
373 | remote->origin = REMOTE_BRANCHES; | |
374 | ||
375 | /* | |
376 | * The branches file would have URL and optionally | |
377 | * #branch specified. The default (or specified) branch is | |
378 | * fetched and stored in the local branch matching the | |
379 | * remote name. | |
380 | */ | |
381 | frag = strchr(buf.buf, '#'); | |
382 | if (frag) | |
383 | *(frag++) = '\0'; | |
384 | else | |
385 | frag = to_free = repo_default_branch_name(the_repository, 0); | |
386 | ||
387 | add_url_alias(remote_state, remote, buf.buf); | |
388 | refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", | |
389 | frag, remote->name); | |
390 | ||
391 | /* | |
392 | * Cogito compatible push: push current HEAD to remote #branch | |
393 | * (master if missing) | |
394 | */ | |
395 | refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag); | |
396 | remote->fetch_tags = 1; /* always auto-follow */ | |
397 | ||
398 | out: | |
399 | strbuf_release(&buf); | |
400 | free(to_free); | |
401 | } | |
402 | #endif /* WITH_BREAKING_CHANGES */ | |
403 | ||
404 | static int handle_config(const char *key, const char *value, | |
405 | const struct config_context *ctx, void *cb) | |
406 | { | |
407 | const char *name; | |
408 | size_t namelen; | |
409 | const char *subkey; | |
410 | struct remote *remote; | |
411 | struct branch *branch; | |
412 | struct remote_state *remote_state = cb; | |
413 | const struct key_value_info *kvi = ctx->kvi; | |
414 | ||
415 | if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) { | |
416 | /* There is no subsection. */ | |
417 | if (!name) | |
418 | return 0; | |
419 | /* There is a subsection, but it is empty. */ | |
420 | if (!namelen) | |
421 | return -1; | |
422 | branch = make_branch(remote_state, name, namelen); | |
423 | if (!strcmp(subkey, "remote")) { | |
424 | FREE_AND_NULL(branch->remote_name); | |
425 | return git_config_string(&branch->remote_name, key, value); | |
426 | } else if (!strcmp(subkey, "pushremote")) { | |
427 | FREE_AND_NULL(branch->pushremote_name); | |
428 | return git_config_string(&branch->pushremote_name, key, value); | |
429 | } else if (!strcmp(subkey, "merge")) { | |
430 | if (!value) | |
431 | return config_error_nonbool(key); | |
432 | add_merge(branch, xstrdup(value)); | |
433 | } | |
434 | return 0; | |
435 | } | |
436 | if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) { | |
437 | struct rewrite *rewrite; | |
438 | if (!name) | |
439 | return 0; | |
440 | if (!strcmp(subkey, "insteadof")) { | |
441 | if (!value) | |
442 | return config_error_nonbool(key); | |
443 | rewrite = make_rewrite(&remote_state->rewrites, name, | |
444 | namelen); | |
445 | add_instead_of(rewrite, xstrdup(value)); | |
446 | } else if (!strcmp(subkey, "pushinsteadof")) { | |
447 | if (!value) | |
448 | return config_error_nonbool(key); | |
449 | rewrite = make_rewrite(&remote_state->rewrites_push, | |
450 | name, namelen); | |
451 | add_instead_of(rewrite, xstrdup(value)); | |
452 | } | |
453 | } | |
454 | ||
455 | if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0) | |
456 | return 0; | |
457 | ||
458 | /* Handle remote.* variables */ | |
459 | if (!name && !strcmp(subkey, "pushdefault")) { | |
460 | FREE_AND_NULL(remote_state->pushremote_name); | |
461 | return git_config_string(&remote_state->pushremote_name, key, | |
462 | value); | |
463 | } | |
464 | ||
465 | if (!name) | |
466 | return 0; | |
467 | /* Handle remote.<name>.* variables */ | |
468 | if (*name == '/') { | |
469 | warning(_("config remote shorthand cannot begin with '/': %s"), | |
470 | name); | |
471 | return 0; | |
472 | } | |
473 | remote = make_remote(remote_state, name, namelen); | |
474 | remote->origin = REMOTE_CONFIG; | |
475 | if (kvi->scope == CONFIG_SCOPE_LOCAL || | |
476 | kvi->scope == CONFIG_SCOPE_WORKTREE) | |
477 | remote->configured_in_repo = 1; | |
478 | if (!strcmp(subkey, "mirror")) | |
479 | remote->mirror = git_config_bool(key, value); | |
480 | else if (!strcmp(subkey, "skipdefaultupdate")) | |
481 | remote->skip_default_update = git_config_bool(key, value); | |
482 | else if (!strcmp(subkey, "skipfetchall")) | |
483 | remote->skip_default_update = git_config_bool(key, value); | |
484 | else if (!strcmp(subkey, "prune")) | |
485 | remote->prune = git_config_bool(key, value); | |
486 | else if (!strcmp(subkey, "prunetags")) | |
487 | remote->prune_tags = git_config_bool(key, value); | |
488 | else if (!strcmp(subkey, "url")) { | |
489 | if (!value) | |
490 | return config_error_nonbool(key); | |
491 | add_url(remote, value); | |
492 | } else if (!strcmp(subkey, "pushurl")) { | |
493 | if (!value) | |
494 | return config_error_nonbool(key); | |
495 | add_pushurl(remote, value); | |
496 | } else if (!strcmp(subkey, "push")) { | |
497 | char *v; | |
498 | if (git_config_string(&v, key, value)) | |
499 | return -1; | |
500 | refspec_append(&remote->push, v); | |
501 | free(v); | |
502 | } else if (!strcmp(subkey, "fetch")) { | |
503 | char *v; | |
504 | if (git_config_string(&v, key, value)) | |
505 | return -1; | |
506 | refspec_append(&remote->fetch, v); | |
507 | free(v); | |
508 | } else if (!strcmp(subkey, "receivepack")) { | |
509 | char *v; | |
510 | if (git_config_string(&v, key, value)) | |
511 | return -1; | |
512 | if (!remote->receivepack) | |
513 | remote->receivepack = v; | |
514 | else | |
515 | error(_("more than one receivepack given, using the first")); | |
516 | } else if (!strcmp(subkey, "uploadpack")) { | |
517 | char *v; | |
518 | if (git_config_string(&v, key, value)) | |
519 | return -1; | |
520 | if (!remote->uploadpack) | |
521 | remote->uploadpack = v; | |
522 | else | |
523 | error(_("more than one uploadpack given, using the first")); | |
524 | } else if (!strcmp(subkey, "tagopt")) { | |
525 | if (!strcmp(value, "--no-tags")) | |
526 | remote->fetch_tags = -1; | |
527 | else if (!strcmp(value, "--tags")) | |
528 | remote->fetch_tags = 2; | |
529 | } else if (!strcmp(subkey, "proxy")) { | |
530 | FREE_AND_NULL(remote->http_proxy); | |
531 | return git_config_string(&remote->http_proxy, | |
532 | key, value); | |
533 | } else if (!strcmp(subkey, "proxyauthmethod")) { | |
534 | FREE_AND_NULL(remote->http_proxy_authmethod); | |
535 | return git_config_string(&remote->http_proxy_authmethod, | |
536 | key, value); | |
537 | } else if (!strcmp(subkey, "vcs")) { | |
538 | FREE_AND_NULL(remote->foreign_vcs); | |
539 | return git_config_string(&remote->foreign_vcs, key, value); | |
540 | } else if (!strcmp(subkey, "serveroption")) { | |
541 | return parse_transport_option(key, value, | |
542 | &remote->server_options); | |
543 | } else if (!strcmp(subkey, "followremotehead")) { | |
544 | const char *no_warn_branch; | |
545 | if (!strcmp(value, "never")) | |
546 | remote->follow_remote_head = FOLLOW_REMOTE_NEVER; | |
547 | else if (!strcmp(value, "create")) | |
548 | remote->follow_remote_head = FOLLOW_REMOTE_CREATE; | |
549 | else if (!strcmp(value, "warn")) { | |
550 | remote->follow_remote_head = FOLLOW_REMOTE_WARN; | |
551 | remote->no_warn_branch = NULL; | |
552 | } else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) { | |
553 | remote->follow_remote_head = FOLLOW_REMOTE_WARN; | |
554 | remote->no_warn_branch = no_warn_branch; | |
555 | } else if (!strcmp(value, "always")) { | |
556 | remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS; | |
557 | } else { | |
558 | warning(_("unrecognized followRemoteHEAD value '%s' ignored"), | |
559 | value); | |
560 | } | |
561 | } | |
562 | return 0; | |
563 | } | |
564 | ||
565 | static void alias_all_urls(struct remote_state *remote_state) | |
566 | { | |
567 | int i, j; | |
568 | for (i = 0; i < remote_state->remotes_nr; i++) { | |
569 | int add_pushurl_aliases; | |
570 | if (!remote_state->remotes[i]) | |
571 | continue; | |
572 | for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) { | |
573 | char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j], | |
574 | &remote_state->rewrites); | |
575 | if (alias) | |
576 | strvec_replace(&remote_state->remotes[i]->pushurl, | |
577 | j, alias); | |
578 | free(alias); | |
579 | } | |
580 | add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0; | |
581 | for (j = 0; j < remote_state->remotes[i]->url.nr; j++) { | |
582 | char *alias; | |
583 | if (add_pushurl_aliases) | |
584 | add_pushurl_alias( | |
585 | remote_state, remote_state->remotes[i], | |
586 | remote_state->remotes[i]->url.v[j]); | |
587 | alias = alias_url(remote_state->remotes[i]->url.v[j], | |
588 | &remote_state->rewrites); | |
589 | if (alias) | |
590 | strvec_replace(&remote_state->remotes[i]->url, | |
591 | j, alias); | |
592 | free(alias); | |
593 | } | |
594 | } | |
595 | } | |
596 | ||
597 | static void read_config(struct repository *repo, int early) | |
598 | { | |
599 | int flag; | |
600 | ||
601 | if (repo->remote_state->initialized) | |
602 | return; | |
603 | repo->remote_state->initialized = 1; | |
604 | ||
605 | repo->remote_state->current_branch = NULL; | |
606 | if (startup_info->have_repository && !early) { | |
607 | const char *head_ref = refs_resolve_ref_unsafe( | |
608 | get_main_ref_store(repo), "HEAD", 0, NULL, &flag); | |
609 | if (head_ref && (flag & REF_ISSYMREF) && | |
610 | skip_prefix(head_ref, "refs/heads/", &head_ref)) { | |
611 | repo->remote_state->current_branch = make_branch( | |
612 | repo->remote_state, head_ref, strlen(head_ref)); | |
613 | } | |
614 | } | |
615 | repo_config(repo, handle_config, repo->remote_state); | |
616 | alias_all_urls(repo->remote_state); | |
617 | } | |
618 | ||
619 | #ifndef WITH_BREAKING_CHANGES | |
620 | static int valid_remote_nick(const char *name) | |
621 | { | |
622 | if (!name[0] || is_dot_or_dotdot(name)) | |
623 | return 0; | |
624 | ||
625 | /* remote nicknames cannot contain slashes */ | |
626 | while (*name) | |
627 | if (is_dir_sep(*name++)) | |
628 | return 0; | |
629 | return 1; | |
630 | } | |
631 | #endif /* WITH_BREAKING_CHANGES */ | |
632 | ||
633 | static const char *remotes_remote_for_branch(struct remote_state *remote_state, | |
634 | struct branch *branch, | |
635 | int *explicit) | |
636 | { | |
637 | if (branch && branch->remote_name) { | |
638 | if (explicit) | |
639 | *explicit = 1; | |
640 | return branch->remote_name; | |
641 | } | |
642 | if (explicit) | |
643 | *explicit = 0; | |
644 | if (remote_state->remotes_nr == 1) | |
645 | return remote_state->remotes[0]->name; | |
646 | return "origin"; | |
647 | } | |
648 | ||
649 | const char *remote_for_branch(struct branch *branch, int *explicit) | |
650 | { | |
651 | read_config(the_repository, 0); | |
652 | die_on_missing_branch(the_repository, branch); | |
653 | ||
654 | return remotes_remote_for_branch(the_repository->remote_state, branch, | |
655 | explicit); | |
656 | } | |
657 | ||
658 | static const char * | |
659 | remotes_pushremote_for_branch(struct remote_state *remote_state, | |
660 | struct branch *branch, int *explicit) | |
661 | { | |
662 | if (branch && branch->pushremote_name) { | |
663 | if (explicit) | |
664 | *explicit = 1; | |
665 | return branch->pushremote_name; | |
666 | } | |
667 | if (remote_state->pushremote_name) { | |
668 | if (explicit) | |
669 | *explicit = 1; | |
670 | return remote_state->pushremote_name; | |
671 | } | |
672 | return remotes_remote_for_branch(remote_state, branch, explicit); | |
673 | } | |
674 | ||
675 | const char *pushremote_for_branch(struct branch *branch, int *explicit) | |
676 | { | |
677 | read_config(the_repository, 0); | |
678 | die_on_missing_branch(the_repository, branch); | |
679 | ||
680 | return remotes_pushremote_for_branch(the_repository->remote_state, | |
681 | branch, explicit); | |
682 | } | |
683 | ||
684 | static struct remote *remotes_remote_get(struct remote_state *remote_state, | |
685 | const char *name); | |
686 | ||
687 | char *remote_ref_for_branch(struct branch *branch, int for_push) | |
688 | { | |
689 | read_config(the_repository, 0); | |
690 | die_on_missing_branch(the_repository, branch); | |
691 | ||
692 | if (branch) { | |
693 | if (!for_push) { | |
694 | if (branch->merge_nr) { | |
695 | return xstrdup(branch->merge_name[0]); | |
696 | } | |
697 | } else { | |
698 | char *dst; | |
699 | const char *remote_name = remotes_pushremote_for_branch( | |
700 | the_repository->remote_state, branch, | |
701 | NULL); | |
702 | struct remote *remote = remotes_remote_get( | |
703 | the_repository->remote_state, remote_name); | |
704 | ||
705 | if (remote && remote->push.nr && | |
706 | (dst = apply_refspecs(&remote->push, | |
707 | branch->refname))) { | |
708 | return dst; | |
709 | } | |
710 | } | |
711 | } | |
712 | return NULL; | |
713 | } | |
714 | ||
715 | static void validate_remote_url(struct remote *remote) | |
716 | { | |
717 | int i; | |
718 | const char *value; | |
719 | struct strbuf redacted = STRBUF_INIT; | |
720 | int warn_not_die; | |
721 | ||
722 | if (git_config_get_string_tmp("transfer.credentialsinurl", &value)) | |
723 | return; | |
724 | ||
725 | if (!strcmp("warn", value)) | |
726 | warn_not_die = 1; | |
727 | else if (!strcmp("die", value)) | |
728 | warn_not_die = 0; | |
729 | else if (!strcmp("allow", value)) | |
730 | return; | |
731 | else | |
732 | die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value); | |
733 | ||
734 | for (i = 0; i < remote->url.nr; i++) { | |
735 | struct url_info url_info = { 0 }; | |
736 | ||
737 | if (!url_normalize(remote->url.v[i], &url_info) || | |
738 | !url_info.passwd_off) | |
739 | goto loop_cleanup; | |
740 | ||
741 | strbuf_reset(&redacted); | |
742 | strbuf_add(&redacted, url_info.url, url_info.passwd_off); | |
743 | strbuf_addstr(&redacted, "<redacted>"); | |
744 | strbuf_addstr(&redacted, | |
745 | url_info.url + url_info.passwd_off + url_info.passwd_len); | |
746 | ||
747 | if (warn_not_die) | |
748 | warning(_("URL '%s' uses plaintext credentials"), redacted.buf); | |
749 | else | |
750 | die(_("URL '%s' uses plaintext credentials"), redacted.buf); | |
751 | ||
752 | loop_cleanup: | |
753 | free(url_info.url); | |
754 | } | |
755 | ||
756 | strbuf_release(&redacted); | |
757 | } | |
758 | ||
759 | static struct remote * | |
760 | remotes_remote_get_1(struct remote_state *remote_state, const char *name, | |
761 | const char *(*get_default)(struct remote_state *, | |
762 | struct branch *, int *)) | |
763 | { | |
764 | struct remote *ret; | |
765 | int name_given = 0; | |
766 | ||
767 | if (name) | |
768 | name_given = 1; | |
769 | else | |
770 | name = get_default(remote_state, remote_state->current_branch, | |
771 | &name_given); | |
772 | ||
773 | ret = make_remote(remote_state, name, 0); | |
774 | #ifndef WITH_BREAKING_CHANGES | |
775 | if (valid_remote_nick(name) && have_git_dir()) { | |
776 | if (!valid_remote(ret)) | |
777 | read_remotes_file(remote_state, ret); | |
778 | if (!valid_remote(ret)) | |
779 | read_branches_file(remote_state, ret); | |
780 | } | |
781 | #endif /* WITH_BREAKING_CHANGES */ | |
782 | if (name_given && !valid_remote(ret)) | |
783 | add_url_alias(remote_state, ret, name); | |
784 | if (!valid_remote(ret)) | |
785 | return NULL; | |
786 | ||
787 | validate_remote_url(ret); | |
788 | ||
789 | return ret; | |
790 | } | |
791 | ||
792 | static inline struct remote * | |
793 | remotes_remote_get(struct remote_state *remote_state, const char *name) | |
794 | { | |
795 | return remotes_remote_get_1(remote_state, name, | |
796 | remotes_remote_for_branch); | |
797 | } | |
798 | ||
799 | struct remote *remote_get(const char *name) | |
800 | { | |
801 | read_config(the_repository, 0); | |
802 | return remotes_remote_get(the_repository->remote_state, name); | |
803 | } | |
804 | ||
805 | struct remote *remote_get_early(const char *name) | |
806 | { | |
807 | read_config(the_repository, 1); | |
808 | return remotes_remote_get(the_repository->remote_state, name); | |
809 | } | |
810 | ||
811 | static inline struct remote * | |
812 | remotes_pushremote_get(struct remote_state *remote_state, const char *name) | |
813 | { | |
814 | return remotes_remote_get_1(remote_state, name, | |
815 | remotes_pushremote_for_branch); | |
816 | } | |
817 | ||
818 | struct remote *pushremote_get(const char *name) | |
819 | { | |
820 | read_config(the_repository, 0); | |
821 | return remotes_pushremote_get(the_repository->remote_state, name); | |
822 | } | |
823 | ||
824 | int remote_is_configured(struct remote *remote, int in_repo) | |
825 | { | |
826 | if (!remote) | |
827 | return 0; | |
828 | if (in_repo) | |
829 | return remote->configured_in_repo; | |
830 | return !!remote->origin; | |
831 | } | |
832 | ||
833 | int for_each_remote(each_remote_fn fn, void *priv) | |
834 | { | |
835 | int i, result = 0; | |
836 | read_config(the_repository, 0); | |
837 | for (i = 0; i < the_repository->remote_state->remotes_nr && !result; | |
838 | i++) { | |
839 | struct remote *remote = | |
840 | the_repository->remote_state->remotes[i]; | |
841 | if (!remote) | |
842 | continue; | |
843 | result = fn(remote, priv); | |
844 | } | |
845 | return result; | |
846 | } | |
847 | ||
848 | static void handle_duplicate(struct ref *ref1, struct ref *ref2) | |
849 | { | |
850 | if (strcmp(ref1->name, ref2->name)) { | |
851 | if (ref1->fetch_head_status != FETCH_HEAD_IGNORE && | |
852 | ref2->fetch_head_status != FETCH_HEAD_IGNORE) { | |
853 | die(_("Cannot fetch both %s and %s to %s"), | |
854 | ref1->name, ref2->name, ref2->peer_ref->name); | |
855 | } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE && | |
856 | ref2->fetch_head_status == FETCH_HEAD_IGNORE) { | |
857 | warning(_("%s usually tracks %s, not %s"), | |
858 | ref2->peer_ref->name, ref2->name, ref1->name); | |
859 | } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE && | |
860 | ref2->fetch_head_status == FETCH_HEAD_IGNORE) { | |
861 | die(_("%s tracks both %s and %s"), | |
862 | ref2->peer_ref->name, ref1->name, ref2->name); | |
863 | } else { | |
864 | /* | |
865 | * This last possibility doesn't occur because | |
866 | * FETCH_HEAD_IGNORE entries always appear at | |
867 | * the end of the list. | |
868 | */ | |
869 | BUG("Internal error"); | |
870 | } | |
871 | } | |
872 | free(ref2->peer_ref); | |
873 | free(ref2); | |
874 | } | |
875 | ||
876 | struct ref *ref_remove_duplicates(struct ref *ref_map) | |
877 | { | |
878 | struct string_list refs = STRING_LIST_INIT_NODUP; | |
879 | struct ref *retval = NULL; | |
880 | struct ref **p = &retval; | |
881 | ||
882 | while (ref_map) { | |
883 | struct ref *ref = ref_map; | |
884 | ||
885 | ref_map = ref_map->next; | |
886 | ref->next = NULL; | |
887 | ||
888 | if (!ref->peer_ref) { | |
889 | *p = ref; | |
890 | p = &ref->next; | |
891 | } else { | |
892 | struct string_list_item *item = | |
893 | string_list_insert(&refs, ref->peer_ref->name); | |
894 | ||
895 | if (item->util) { | |
896 | /* Entry already existed */ | |
897 | handle_duplicate((struct ref *)item->util, ref); | |
898 | } else { | |
899 | *p = ref; | |
900 | p = &ref->next; | |
901 | item->util = ref; | |
902 | } | |
903 | } | |
904 | } | |
905 | ||
906 | string_list_clear(&refs, 0); | |
907 | return retval; | |
908 | } | |
909 | ||
910 | int remote_has_url(struct remote *remote, const char *url) | |
911 | { | |
912 | int i; | |
913 | for (i = 0; i < remote->url.nr; i++) { | |
914 | if (!strcmp(remote->url.v[i], url)) | |
915 | return 1; | |
916 | } | |
917 | return 0; | |
918 | } | |
919 | ||
920 | struct strvec *push_url_of_remote(struct remote *remote) | |
921 | { | |
922 | return remote->pushurl.nr ? &remote->pushurl : &remote->url; | |
923 | } | |
924 | ||
925 | void ref_push_report_free(struct ref_push_report *report) | |
926 | { | |
927 | while (report) { | |
928 | struct ref_push_report *next = report->next; | |
929 | ||
930 | free(report->ref_name); | |
931 | free(report->old_oid); | |
932 | free(report->new_oid); | |
933 | free(report); | |
934 | ||
935 | report = next; | |
936 | } | |
937 | } | |
938 | ||
939 | int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) | |
940 | { | |
941 | return refspec_find_match(&remote->fetch, refspec); | |
942 | } | |
943 | ||
944 | static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, | |
945 | const char *name) | |
946 | { | |
947 | size_t len = strlen(name); | |
948 | struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1)); | |
949 | memcpy(ref->name, prefix, prefixlen); | |
950 | memcpy(ref->name + prefixlen, name, len); | |
951 | return ref; | |
952 | } | |
953 | ||
954 | struct ref *alloc_ref(const char *name) | |
955 | { | |
956 | return alloc_ref_with_prefix("", 0, name); | |
957 | } | |
958 | ||
959 | struct ref *copy_ref(const struct ref *ref) | |
960 | { | |
961 | struct ref *cpy; | |
962 | size_t len; | |
963 | if (!ref) | |
964 | return NULL; | |
965 | len = st_add3(sizeof(struct ref), strlen(ref->name), 1); | |
966 | cpy = xmalloc(len); | |
967 | memcpy(cpy, ref, len); | |
968 | cpy->next = NULL; | |
969 | cpy->symref = xstrdup_or_null(ref->symref); | |
970 | cpy->remote_status = xstrdup_or_null(ref->remote_status); | |
971 | cpy->peer_ref = copy_ref(ref->peer_ref); | |
972 | return cpy; | |
973 | } | |
974 | ||
975 | struct ref *copy_ref_list(const struct ref *ref) | |
976 | { | |
977 | struct ref *ret = NULL; | |
978 | struct ref **tail = &ret; | |
979 | while (ref) { | |
980 | *tail = copy_ref(ref); | |
981 | ref = ref->next; | |
982 | tail = &((*tail)->next); | |
983 | } | |
984 | return ret; | |
985 | } | |
986 | ||
987 | void free_one_ref(struct ref *ref) | |
988 | { | |
989 | if (!ref) | |
990 | return; | |
991 | free_one_ref(ref->peer_ref); | |
992 | ref_push_report_free(ref->report); | |
993 | free(ref->remote_status); | |
994 | free(ref->tracking_ref); | |
995 | free(ref->symref); | |
996 | free(ref); | |
997 | } | |
998 | ||
999 | void free_refs(struct ref *ref) | |
1000 | { | |
1001 | struct ref *next; | |
1002 | while (ref) { | |
1003 | next = ref->next; | |
1004 | free_one_ref(ref); | |
1005 | ref = next; | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | int count_refspec_match(const char *pattern, | |
1010 | struct ref *refs, | |
1011 | struct ref **matched_ref) | |
1012 | { | |
1013 | int patlen = strlen(pattern); | |
1014 | struct ref *matched_weak = NULL; | |
1015 | struct ref *matched = NULL; | |
1016 | int weak_match = 0; | |
1017 | int match = 0; | |
1018 | ||
1019 | for (weak_match = match = 0; refs; refs = refs->next) { | |
1020 | char *name = refs->name; | |
1021 | int namelen = strlen(name); | |
1022 | ||
1023 | if (!refname_match(pattern, name)) | |
1024 | continue; | |
1025 | ||
1026 | /* A match is "weak" if it is with refs outside | |
1027 | * heads or tags, and did not specify the pattern | |
1028 | * in full (e.g. "refs/remotes/origin/master") or at | |
1029 | * least from the toplevel (e.g. "remotes/origin/master"); | |
1030 | * otherwise "git push $URL master" would result in | |
1031 | * ambiguity between remotes/origin/master and heads/master | |
1032 | * at the remote site. | |
1033 | */ | |
1034 | if (namelen != patlen && | |
1035 | patlen != namelen - 5 && | |
1036 | !starts_with(name, "refs/heads/") && | |
1037 | !starts_with(name, "refs/tags/")) { | |
1038 | /* We want to catch the case where only weak | |
1039 | * matches are found and there are multiple | |
1040 | * matches, and where more than one strong | |
1041 | * matches are found, as ambiguous. One | |
1042 | * strong match with zero or more weak matches | |
1043 | * are acceptable as a unique match. | |
1044 | */ | |
1045 | matched_weak = refs; | |
1046 | weak_match++; | |
1047 | } | |
1048 | else { | |
1049 | matched = refs; | |
1050 | match++; | |
1051 | } | |
1052 | } | |
1053 | if (!matched) { | |
1054 | if (matched_ref) | |
1055 | *matched_ref = matched_weak; | |
1056 | return weak_match; | |
1057 | } | |
1058 | else { | |
1059 | if (matched_ref) | |
1060 | *matched_ref = matched; | |
1061 | return match; | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | void tail_link_ref(struct ref *ref, struct ref ***tail) | |
1066 | { | |
1067 | **tail = ref; | |
1068 | while (ref->next) | |
1069 | ref = ref->next; | |
1070 | *tail = &ref->next; | |
1071 | } | |
1072 | ||
1073 | static struct ref *alloc_delete_ref(void) | |
1074 | { | |
1075 | struct ref *ref = alloc_ref("(delete)"); | |
1076 | oidclr(&ref->new_oid, the_repository->hash_algo); | |
1077 | return ref; | |
1078 | } | |
1079 | ||
1080 | static int try_explicit_object_name(const char *name, | |
1081 | struct ref **match) | |
1082 | { | |
1083 | struct object_id oid; | |
1084 | ||
1085 | if (!*name) { | |
1086 | if (match) | |
1087 | *match = alloc_delete_ref(); | |
1088 | return 0; | |
1089 | } | |
1090 | ||
1091 | if (repo_get_oid(the_repository, name, &oid)) | |
1092 | return -1; | |
1093 | ||
1094 | if (match) { | |
1095 | *match = alloc_ref(name); | |
1096 | oidcpy(&(*match)->new_oid, &oid); | |
1097 | } | |
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) | |
1102 | { | |
1103 | struct ref *ret = alloc_ref(name); | |
1104 | tail_link_ref(ret, tail); | |
1105 | return ret; | |
1106 | } | |
1107 | ||
1108 | static char *guess_ref(const char *name, struct ref *peer) | |
1109 | { | |
1110 | struct strbuf buf = STRBUF_INIT; | |
1111 | ||
1112 | const char *r = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), | |
1113 | peer->name, | |
1114 | RESOLVE_REF_READING, | |
1115 | NULL, NULL); | |
1116 | if (!r) | |
1117 | return NULL; | |
1118 | ||
1119 | if (starts_with(r, "refs/heads/")) { | |
1120 | strbuf_addstr(&buf, "refs/heads/"); | |
1121 | } else if (starts_with(r, "refs/tags/")) { | |
1122 | strbuf_addstr(&buf, "refs/tags/"); | |
1123 | } else { | |
1124 | return NULL; | |
1125 | } | |
1126 | ||
1127 | strbuf_addstr(&buf, name); | |
1128 | return strbuf_detach(&buf, NULL); | |
1129 | } | |
1130 | ||
1131 | static int match_explicit_lhs(struct ref *src, | |
1132 | struct refspec_item *rs, | |
1133 | struct ref **match, | |
1134 | int *allocated_match) | |
1135 | { | |
1136 | switch (count_refspec_match(rs->src, src, match)) { | |
1137 | case 1: | |
1138 | if (allocated_match) | |
1139 | *allocated_match = 0; | |
1140 | return 0; | |
1141 | case 0: | |
1142 | /* The source could be in the get_sha1() format | |
1143 | * not a reference name. :refs/other is a | |
1144 | * way to delete 'other' ref at the remote end. | |
1145 | */ | |
1146 | if (try_explicit_object_name(rs->src, match) < 0) | |
1147 | return error(_("src refspec %s does not match any"), rs->src); | |
1148 | if (allocated_match) | |
1149 | *allocated_match = 1; | |
1150 | return 0; | |
1151 | default: | |
1152 | return error(_("src refspec %s matches more than one"), rs->src); | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | static void show_push_unqualified_ref_name_error(const char *dst_value, | |
1157 | const char *matched_src_name) | |
1158 | { | |
1159 | struct object_id oid; | |
1160 | enum object_type type; | |
1161 | ||
1162 | /* | |
1163 | * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push | |
1164 | * <remote> <src>:<dst>" push, and "being pushed ('%s')" is | |
1165 | * the <src>. | |
1166 | */ | |
1167 | error(_("The destination you provided is not a full refname (i.e.,\n" | |
1168 | "starting with \"refs/\"). We tried to guess what you meant by:\n" | |
1169 | "\n" | |
1170 | "- Looking for a ref that matches '%s' on the remote side.\n" | |
1171 | "- Checking if the <src> being pushed ('%s')\n" | |
1172 | " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n" | |
1173 | " refs/{heads,tags}/ prefix on the remote side.\n" | |
1174 | "\n" | |
1175 | "Neither worked, so we gave up. You must fully qualify the ref."), | |
1176 | dst_value, matched_src_name); | |
1177 | ||
1178 | if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME)) | |
1179 | return; | |
1180 | ||
1181 | if (repo_get_oid(the_repository, matched_src_name, &oid)) | |
1182 | BUG("'%s' is not a valid object, " | |
1183 | "match_explicit_lhs() should catch this!", | |
1184 | matched_src_name); | |
1185 | type = oid_object_info(the_repository, &oid, NULL); | |
1186 | if (type == OBJ_COMMIT) { | |
1187 | advise(_("The <src> part of the refspec is a commit object.\n" | |
1188 | "Did you mean to create a new branch by pushing to\n" | |
1189 | "'%s:refs/heads/%s'?"), | |
1190 | matched_src_name, dst_value); | |
1191 | } else if (type == OBJ_TAG) { | |
1192 | advise(_("The <src> part of the refspec is a tag object.\n" | |
1193 | "Did you mean to create a new tag by pushing to\n" | |
1194 | "'%s:refs/tags/%s'?"), | |
1195 | matched_src_name, dst_value); | |
1196 | } else if (type == OBJ_TREE) { | |
1197 | advise(_("The <src> part of the refspec is a tree object.\n" | |
1198 | "Did you mean to tag a new tree by pushing to\n" | |
1199 | "'%s:refs/tags/%s'?"), | |
1200 | matched_src_name, dst_value); | |
1201 | } else if (type == OBJ_BLOB) { | |
1202 | advise(_("The <src> part of the refspec is a blob object.\n" | |
1203 | "Did you mean to tag a new blob by pushing to\n" | |
1204 | "'%s:refs/tags/%s'?"), | |
1205 | matched_src_name, dst_value); | |
1206 | } else { | |
1207 | BUG("'%s' should be commit/tag/tree/blob, is '%d'", | |
1208 | matched_src_name, type); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | static int match_explicit(struct ref *src, struct ref *dst, | |
1213 | struct ref ***dst_tail, | |
1214 | struct refspec_item *rs) | |
1215 | { | |
1216 | struct ref *matched_src = NULL, *matched_dst = NULL; | |
1217 | int allocated_src = 0, ret; | |
1218 | ||
1219 | const char *dst_value = rs->dst; | |
1220 | char *dst_guess; | |
1221 | ||
1222 | if (rs->pattern || rs->matching || rs->negative) { | |
1223 | ret = 0; | |
1224 | goto out; | |
1225 | } | |
1226 | ||
1227 | if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0) { | |
1228 | ret = -1; | |
1229 | goto out; | |
1230 | } | |
1231 | ||
1232 | if (!dst_value) { | |
1233 | int flag; | |
1234 | ||
1235 | dst_value = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), | |
1236 | matched_src->name, | |
1237 | RESOLVE_REF_READING, | |
1238 | NULL, &flag); | |
1239 | if (!dst_value || | |
1240 | ((flag & REF_ISSYMREF) && | |
1241 | !starts_with(dst_value, "refs/heads/"))) | |
1242 | die(_("%s cannot be resolved to branch"), | |
1243 | matched_src->name); | |
1244 | } | |
1245 | ||
1246 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { | |
1247 | case 1: | |
1248 | break; | |
1249 | case 0: | |
1250 | if (starts_with(dst_value, "refs/")) { | |
1251 | matched_dst = make_linked_ref(dst_value, dst_tail); | |
1252 | } else if (is_null_oid(&matched_src->new_oid)) { | |
1253 | error(_("unable to delete '%s': remote ref does not exist"), | |
1254 | dst_value); | |
1255 | } else if ((dst_guess = guess_ref(dst_value, matched_src))) { | |
1256 | matched_dst = make_linked_ref(dst_guess, dst_tail); | |
1257 | free(dst_guess); | |
1258 | } else { | |
1259 | show_push_unqualified_ref_name_error(dst_value, | |
1260 | matched_src->name); | |
1261 | } | |
1262 | break; | |
1263 | default: | |
1264 | matched_dst = NULL; | |
1265 | error(_("dst refspec %s matches more than one"), | |
1266 | dst_value); | |
1267 | break; | |
1268 | } | |
1269 | ||
1270 | if (!matched_dst) { | |
1271 | ret = -1; | |
1272 | goto out; | |
1273 | } | |
1274 | ||
1275 | if (matched_dst->peer_ref) { | |
1276 | ret = error(_("dst ref %s receives from more than one src"), | |
1277 | matched_dst->name); | |
1278 | goto out; | |
1279 | } else { | |
1280 | matched_dst->peer_ref = allocated_src ? | |
1281 | matched_src : | |
1282 | copy_ref(matched_src); | |
1283 | matched_dst->force = rs->force; | |
1284 | matched_src = NULL; | |
1285 | } | |
1286 | ||
1287 | ret = 0; | |
1288 | ||
1289 | out: | |
1290 | if (allocated_src) | |
1291 | free_one_ref(matched_src); | |
1292 | return ret; | |
1293 | } | |
1294 | ||
1295 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
1296 | struct ref ***dst_tail, struct refspec *rs) | |
1297 | { | |
1298 | int i, errs; | |
1299 | for (i = errs = 0; i < rs->nr; i++) | |
1300 | errs += match_explicit(src, dst, dst_tail, &rs->items[i]); | |
1301 | return errs; | |
1302 | } | |
1303 | ||
1304 | static char *get_ref_match(const struct refspec *rs, const struct ref *ref, | |
1305 | int send_mirror, int direction, | |
1306 | const struct refspec_item **ret_pat) | |
1307 | { | |
1308 | const struct refspec_item *pat; | |
1309 | char *name; | |
1310 | int i; | |
1311 | int matching_refs = -1; | |
1312 | for (i = 0; i < rs->nr; i++) { | |
1313 | const struct refspec_item *item = &rs->items[i]; | |
1314 | ||
1315 | if (item->negative) | |
1316 | continue; | |
1317 | ||
1318 | if (item->matching && | |
1319 | (matching_refs == -1 || item->force)) { | |
1320 | matching_refs = i; | |
1321 | continue; | |
1322 | } | |
1323 | ||
1324 | if (item->pattern) { | |
1325 | const char *dst_side = item->dst ? item->dst : item->src; | |
1326 | int match; | |
1327 | if (direction == FROM_SRC) | |
1328 | match = match_refname_with_pattern(item->src, ref->name, dst_side, &name); | |
1329 | else | |
1330 | match = match_refname_with_pattern(dst_side, ref->name, item->src, &name); | |
1331 | if (match) { | |
1332 | matching_refs = i; | |
1333 | break; | |
1334 | } | |
1335 | } | |
1336 | } | |
1337 | if (matching_refs == -1) | |
1338 | return NULL; | |
1339 | ||
1340 | pat = &rs->items[matching_refs]; | |
1341 | if (pat->matching) { | |
1342 | /* | |
1343 | * "matching refs"; traditionally we pushed everything | |
1344 | * including refs outside refs/heads/ hierarchy, but | |
1345 | * that does not make much sense these days. | |
1346 | */ | |
1347 | if (!send_mirror && !starts_with(ref->name, "refs/heads/")) | |
1348 | return NULL; | |
1349 | name = xstrdup(ref->name); | |
1350 | } | |
1351 | if (ret_pat) | |
1352 | *ret_pat = pat; | |
1353 | return name; | |
1354 | } | |
1355 | ||
1356 | static struct ref **tail_ref(struct ref **head) | |
1357 | { | |
1358 | struct ref **tail = head; | |
1359 | while (*tail) | |
1360 | tail = &((*tail)->next); | |
1361 | return tail; | |
1362 | } | |
1363 | ||
1364 | struct tips { | |
1365 | struct commit **tip; | |
1366 | size_t nr, alloc; | |
1367 | }; | |
1368 | ||
1369 | static void add_to_tips(struct tips *tips, const struct object_id *oid) | |
1370 | { | |
1371 | struct commit *commit; | |
1372 | ||
1373 | if (is_null_oid(oid)) | |
1374 | return; | |
1375 | commit = lookup_commit_reference_gently(the_repository, oid, 1); | |
1376 | if (!commit || (commit->object.flags & TMP_MARK)) | |
1377 | return; | |
1378 | commit->object.flags |= TMP_MARK; | |
1379 | ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc); | |
1380 | tips->tip[tips->nr++] = commit; | |
1381 | } | |
1382 | ||
1383 | static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail) | |
1384 | { | |
1385 | struct string_list dst_tag = STRING_LIST_INIT_NODUP; | |
1386 | struct string_list src_tag = STRING_LIST_INIT_NODUP; | |
1387 | struct string_list_item *item; | |
1388 | struct ref *ref; | |
1389 | struct tips sent_tips; | |
1390 | ||
1391 | /* | |
1392 | * Collect everything we know they would have at the end of | |
1393 | * this push, and collect all tags they have. | |
1394 | */ | |
1395 | memset(&sent_tips, 0, sizeof(sent_tips)); | |
1396 | for (ref = *dst; ref; ref = ref->next) { | |
1397 | if (ref->peer_ref && | |
1398 | !is_null_oid(&ref->peer_ref->new_oid)) | |
1399 | add_to_tips(&sent_tips, &ref->peer_ref->new_oid); | |
1400 | else | |
1401 | add_to_tips(&sent_tips, &ref->old_oid); | |
1402 | if (starts_with(ref->name, "refs/tags/")) | |
1403 | string_list_append(&dst_tag, ref->name); | |
1404 | } | |
1405 | clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK); | |
1406 | ||
1407 | string_list_sort(&dst_tag); | |
1408 | ||
1409 | /* Collect tags they do not have. */ | |
1410 | for (ref = src; ref; ref = ref->next) { | |
1411 | if (!starts_with(ref->name, "refs/tags/")) | |
1412 | continue; /* not a tag */ | |
1413 | if (string_list_has_string(&dst_tag, ref->name)) | |
1414 | continue; /* they already have it */ | |
1415 | if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG) | |
1416 | continue; /* be conservative */ | |
1417 | item = string_list_append(&src_tag, ref->name); | |
1418 | item->util = ref; | |
1419 | } | |
1420 | string_list_clear(&dst_tag, 0); | |
1421 | ||
1422 | /* | |
1423 | * At this point, src_tag lists tags that are missing from | |
1424 | * dst, and sent_tips lists the tips we are pushing or those | |
1425 | * that we know they already have. An element in the src_tag | |
1426 | * that is an ancestor of any of the sent_tips needs to be | |
1427 | * sent to the other side. | |
1428 | */ | |
1429 | if (sent_tips.nr) { | |
1430 | const int reachable_flag = 1; | |
1431 | struct commit_list *found_commits; | |
1432 | struct commit **src_commits; | |
1433 | size_t nr_src_commits = 0, alloc_src_commits = 16; | |
1434 | ALLOC_ARRAY(src_commits, alloc_src_commits); | |
1435 | ||
1436 | for_each_string_list_item(item, &src_tag) { | |
1437 | struct ref *ref = item->util; | |
1438 | struct commit *commit; | |
1439 | ||
1440 | if (is_null_oid(&ref->new_oid)) | |
1441 | continue; | |
1442 | commit = lookup_commit_reference_gently(the_repository, | |
1443 | &ref->new_oid, | |
1444 | 1); | |
1445 | if (!commit) | |
1446 | /* not pushing a commit, which is not an error */ | |
1447 | continue; | |
1448 | ||
1449 | ALLOC_GROW(src_commits, nr_src_commits + 1, alloc_src_commits); | |
1450 | src_commits[nr_src_commits++] = commit; | |
1451 | } | |
1452 | ||
1453 | found_commits = get_reachable_subset(sent_tips.tip, sent_tips.nr, | |
1454 | src_commits, nr_src_commits, | |
1455 | reachable_flag); | |
1456 | ||
1457 | for_each_string_list_item(item, &src_tag) { | |
1458 | struct ref *dst_ref; | |
1459 | struct ref *ref = item->util; | |
1460 | struct commit *commit; | |
1461 | ||
1462 | if (is_null_oid(&ref->new_oid)) | |
1463 | continue; | |
1464 | commit = lookup_commit_reference_gently(the_repository, | |
1465 | &ref->new_oid, | |
1466 | 1); | |
1467 | if (!commit) | |
1468 | /* not pushing a commit, which is not an error */ | |
1469 | continue; | |
1470 | ||
1471 | /* | |
1472 | * Is this tag, which they do not have, reachable from | |
1473 | * any of the commits we are sending? | |
1474 | */ | |
1475 | if (!(commit->object.flags & reachable_flag)) | |
1476 | continue; | |
1477 | ||
1478 | /* Add it in */ | |
1479 | dst_ref = make_linked_ref(ref->name, dst_tail); | |
1480 | oidcpy(&dst_ref->new_oid, &ref->new_oid); | |
1481 | dst_ref->peer_ref = copy_ref(ref); | |
1482 | } | |
1483 | ||
1484 | clear_commit_marks_many(nr_src_commits, src_commits, reachable_flag); | |
1485 | free(src_commits); | |
1486 | free_commit_list(found_commits); | |
1487 | } | |
1488 | ||
1489 | string_list_clear(&src_tag, 0); | |
1490 | free(sent_tips.tip); | |
1491 | } | |
1492 | ||
1493 | struct ref *find_ref_by_name(const struct ref *list, const char *name) | |
1494 | { | |
1495 | for ( ; list; list = list->next) | |
1496 | if (!strcmp(list->name, name)) | |
1497 | return (struct ref *)list; | |
1498 | return NULL; | |
1499 | } | |
1500 | ||
1501 | static void prepare_ref_index(struct string_list *ref_index, struct ref *ref) | |
1502 | { | |
1503 | for ( ; ref; ref = ref->next) | |
1504 | string_list_append_nodup(ref_index, ref->name)->util = ref; | |
1505 | ||
1506 | string_list_sort(ref_index); | |
1507 | } | |
1508 | ||
1509 | /* | |
1510 | * Given only the set of local refs, sanity-check the set of push | |
1511 | * refspecs. We can't catch all errors that match_push_refs would, | |
1512 | * but we can catch some errors early before even talking to the | |
1513 | * remote side. | |
1514 | */ | |
1515 | int check_push_refs(struct ref *src, struct refspec *rs) | |
1516 | { | |
1517 | int ret = 0; | |
1518 | int i; | |
1519 | ||
1520 | for (i = 0; i < rs->nr; i++) { | |
1521 | struct refspec_item *item = &rs->items[i]; | |
1522 | ||
1523 | if (item->pattern || item->matching || item->negative) | |
1524 | continue; | |
1525 | ||
1526 | ret |= match_explicit_lhs(src, item, NULL, NULL); | |
1527 | } | |
1528 | ||
1529 | return ret; | |
1530 | } | |
1531 | ||
1532 | /* | |
1533 | * Given the set of refs the local repository has, the set of refs the | |
1534 | * remote repository has, and the refspec used for push, determine | |
1535 | * what remote refs we will update and with what value by setting | |
1536 | * peer_ref (which object is being pushed) and force (if the push is | |
1537 | * forced) in elements of "dst". The function may add new elements to | |
1538 | * dst (e.g. pushing to a new branch, done in match_explicit_refs). | |
1539 | */ | |
1540 | int match_push_refs(struct ref *src, struct ref **dst, | |
1541 | struct refspec *rs, int flags) | |
1542 | { | |
1543 | int send_all = flags & MATCH_REFS_ALL; | |
1544 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
1545 | int send_prune = flags & MATCH_REFS_PRUNE; | |
1546 | int errs; | |
1547 | struct ref *ref, **dst_tail = tail_ref(dst); | |
1548 | struct string_list dst_ref_index = STRING_LIST_INIT_NODUP; | |
1549 | ||
1550 | /* If no refspec is provided, use the default ":" */ | |
1551 | if (!rs->nr) | |
1552 | refspec_append(rs, ":"); | |
1553 | ||
1554 | errs = match_explicit_refs(src, *dst, &dst_tail, rs); | |
1555 | ||
1556 | /* pick the remainder */ | |
1557 | for (ref = src; ref; ref = ref->next) { | |
1558 | struct string_list_item *dst_item; | |
1559 | struct ref *dst_peer; | |
1560 | const struct refspec_item *pat = NULL; | |
1561 | char *dst_name; | |
1562 | ||
1563 | dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat); | |
1564 | if (!dst_name) | |
1565 | continue; | |
1566 | ||
1567 | if (!dst_ref_index.nr) | |
1568 | prepare_ref_index(&dst_ref_index, *dst); | |
1569 | ||
1570 | dst_item = string_list_lookup(&dst_ref_index, dst_name); | |
1571 | dst_peer = dst_item ? dst_item->util : NULL; | |
1572 | if (dst_peer) { | |
1573 | if (dst_peer->peer_ref) | |
1574 | /* We're already sending something to this ref. */ | |
1575 | goto free_name; | |
1576 | } else { | |
1577 | if (pat->matching && !(send_all || send_mirror)) | |
1578 | /* | |
1579 | * Remote doesn't have it, and we have no | |
1580 | * explicit pattern, and we don't have | |
1581 | * --all or --mirror. | |
1582 | */ | |
1583 | goto free_name; | |
1584 | ||
1585 | /* Create a new one and link it */ | |
1586 | dst_peer = make_linked_ref(dst_name, &dst_tail); | |
1587 | oidcpy(&dst_peer->new_oid, &ref->new_oid); | |
1588 | string_list_insert(&dst_ref_index, | |
1589 | dst_peer->name)->util = dst_peer; | |
1590 | } | |
1591 | dst_peer->peer_ref = copy_ref(ref); | |
1592 | dst_peer->force = pat->force; | |
1593 | free_name: | |
1594 | free(dst_name); | |
1595 | } | |
1596 | ||
1597 | string_list_clear(&dst_ref_index, 0); | |
1598 | ||
1599 | if (flags & MATCH_REFS_FOLLOW_TAGS) | |
1600 | add_missing_tags(src, dst, &dst_tail); | |
1601 | ||
1602 | if (send_prune) { | |
1603 | struct string_list src_ref_index = STRING_LIST_INIT_NODUP; | |
1604 | /* check for missing refs on the remote */ | |
1605 | for (ref = *dst; ref; ref = ref->next) { | |
1606 | char *src_name; | |
1607 | ||
1608 | if (ref->peer_ref) | |
1609 | /* We're already sending something to this ref. */ | |
1610 | continue; | |
1611 | ||
1612 | src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL); | |
1613 | if (src_name) { | |
1614 | if (!src_ref_index.nr) | |
1615 | prepare_ref_index(&src_ref_index, src); | |
1616 | if (!string_list_has_string(&src_ref_index, | |
1617 | src_name)) | |
1618 | ref->peer_ref = alloc_delete_ref(); | |
1619 | free(src_name); | |
1620 | } | |
1621 | } | |
1622 | string_list_clear(&src_ref_index, 0); | |
1623 | } | |
1624 | ||
1625 | *dst = apply_negative_refspecs(*dst, rs); | |
1626 | ||
1627 | if (errs) | |
1628 | return -1; | |
1629 | return 0; | |
1630 | } | |
1631 | ||
1632 | void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, | |
1633 | int force_update) | |
1634 | { | |
1635 | struct ref *ref; | |
1636 | ||
1637 | for (ref = remote_refs; ref; ref = ref->next) { | |
1638 | int force_ref_update = ref->force || force_update; | |
1639 | int reject_reason = 0; | |
1640 | ||
1641 | if (ref->peer_ref) | |
1642 | oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); | |
1643 | else if (!send_mirror) | |
1644 | continue; | |
1645 | ||
1646 | ref->deletion = is_null_oid(&ref->new_oid); | |
1647 | if (!ref->deletion && | |
1648 | oideq(&ref->old_oid, &ref->new_oid)) { | |
1649 | ref->status = REF_STATUS_UPTODATE; | |
1650 | continue; | |
1651 | } | |
1652 | ||
1653 | /* | |
1654 | * If the remote ref has moved and is now different | |
1655 | * from what we expect, reject any push. | |
1656 | * | |
1657 | * It also is an error if the user told us to check | |
1658 | * with the remote-tracking branch to find the value | |
1659 | * to expect, but we did not have such a tracking | |
1660 | * branch. | |
1661 | * | |
1662 | * If the tip of the remote-tracking ref is unreachable | |
1663 | * from any reflog entry of its local ref indicating a | |
1664 | * possible update since checkout; reject the push. | |
1665 | */ | |
1666 | if (ref->expect_old_sha1) { | |
1667 | if (!oideq(&ref->old_oid, &ref->old_oid_expect)) | |
1668 | reject_reason = REF_STATUS_REJECT_STALE; | |
1669 | else if (ref->check_reachable && ref->unreachable) | |
1670 | reject_reason = | |
1671 | REF_STATUS_REJECT_REMOTE_UPDATED; | |
1672 | else | |
1673 | /* | |
1674 | * If the ref isn't stale, and is reachable | |
1675 | * from one of the reflog entries of | |
1676 | * the local branch, force the update. | |
1677 | */ | |
1678 | force_ref_update = 1; | |
1679 | } | |
1680 | ||
1681 | /* | |
1682 | * If the update isn't already rejected then check | |
1683 | * the usual "must fast-forward" rules. | |
1684 | * | |
1685 | * Decide whether an individual refspec A:B can be | |
1686 | * pushed. The push will succeed if any of the | |
1687 | * following are true: | |
1688 | * | |
1689 | * (1) the remote reference B does not exist | |
1690 | * | |
1691 | * (2) the remote reference B is being removed (i.e., | |
1692 | * pushing :B where no source is specified) | |
1693 | * | |
1694 | * (3) the destination is not under refs/tags/, and | |
1695 | * if the old and new value is a commit, the new | |
1696 | * is a descendant of the old. | |
1697 | * | |
1698 | * (4) it is forced using the +A:B notation, or by | |
1699 | * passing the --force argument | |
1700 | */ | |
1701 | ||
1702 | if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) { | |
1703 | if (starts_with(ref->name, "refs/tags/")) | |
1704 | reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS; | |
1705 | else if (!has_object(the_repository, &ref->old_oid, HAS_OBJECT_RECHECK_PACKED)) | |
1706 | reject_reason = REF_STATUS_REJECT_FETCH_FIRST; | |
1707 | else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) || | |
1708 | !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1)) | |
1709 | reject_reason = REF_STATUS_REJECT_NEEDS_FORCE; | |
1710 | else if (!ref_newer(&ref->new_oid, &ref->old_oid)) | |
1711 | reject_reason = REF_STATUS_REJECT_NONFASTFORWARD; | |
1712 | } | |
1713 | ||
1714 | /* | |
1715 | * "--force" will defeat any rejection implemented | |
1716 | * by the rules above. | |
1717 | */ | |
1718 | if (!force_ref_update) | |
1719 | ref->status = reject_reason; | |
1720 | else if (reject_reason) | |
1721 | ref->forced_update = 1; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | static void set_merge(struct remote_state *remote_state, struct branch *ret) | |
1726 | { | |
1727 | struct remote *remote; | |
1728 | char *ref; | |
1729 | struct object_id oid; | |
1730 | int i; | |
1731 | ||
1732 | if (!ret) | |
1733 | return; /* no branch */ | |
1734 | if (ret->merge) | |
1735 | return; /* already run */ | |
1736 | if (!ret->remote_name || !ret->merge_nr) { | |
1737 | /* | |
1738 | * no merge config; let's make sure we don't confuse callers | |
1739 | * with a non-zero merge_nr but a NULL merge | |
1740 | */ | |
1741 | ret->merge_nr = 0; | |
1742 | return; | |
1743 | } | |
1744 | ||
1745 | remote = remotes_remote_get(remote_state, ret->remote_name); | |
1746 | ||
1747 | CALLOC_ARRAY(ret->merge, ret->merge_nr); | |
1748 | for (i = 0; i < ret->merge_nr; i++) { | |
1749 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); | |
1750 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); | |
1751 | if (!remote_find_tracking(remote, ret->merge[i]) || | |
1752 | strcmp(ret->remote_name, ".")) | |
1753 | continue; | |
1754 | if (repo_dwim_ref(the_repository, ret->merge_name[i], | |
1755 | strlen(ret->merge_name[i]), &oid, &ref, | |
1756 | 0) == 1) | |
1757 | ret->merge[i]->dst = ref; | |
1758 | else | |
1759 | ret->merge[i]->dst = xstrdup(ret->merge_name[i]); | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | struct branch *branch_get(const char *name) | |
1764 | { | |
1765 | struct branch *ret; | |
1766 | ||
1767 | read_config(the_repository, 0); | |
1768 | if (!name || !*name || !strcmp(name, "HEAD")) | |
1769 | ret = the_repository->remote_state->current_branch; | |
1770 | else | |
1771 | ret = make_branch(the_repository->remote_state, name, | |
1772 | strlen(name)); | |
1773 | set_merge(the_repository->remote_state, ret); | |
1774 | return ret; | |
1775 | } | |
1776 | ||
1777 | int branch_has_merge_config(struct branch *branch) | |
1778 | { | |
1779 | return branch && !!branch->merge; | |
1780 | } | |
1781 | ||
1782 | int branch_merge_matches(struct branch *branch, | |
1783 | int i, | |
1784 | const char *refname) | |
1785 | { | |
1786 | if (!branch || i < 0 || i >= branch->merge_nr) | |
1787 | return 0; | |
1788 | return refname_match(branch->merge[i]->src, refname); | |
1789 | } | |
1790 | ||
1791 | __attribute__((format (printf,2,3))) | |
1792 | static const char *error_buf(struct strbuf *err, const char *fmt, ...) | |
1793 | { | |
1794 | if (err) { | |
1795 | va_list ap; | |
1796 | va_start(ap, fmt); | |
1797 | strbuf_vaddf(err, fmt, ap); | |
1798 | va_end(ap); | |
1799 | } | |
1800 | return NULL; | |
1801 | } | |
1802 | ||
1803 | const char *branch_get_upstream(struct branch *branch, struct strbuf *err) | |
1804 | { | |
1805 | if (!branch) | |
1806 | return error_buf(err, _("HEAD does not point to a branch")); | |
1807 | ||
1808 | if (!branch->merge || !branch->merge[0]) { | |
1809 | /* | |
1810 | * no merge config; is it because the user didn't define any, | |
1811 | * or because it is not a real branch, and get_branch | |
1812 | * auto-vivified it? | |
1813 | */ | |
1814 | if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) | |
1815 | return error_buf(err, _("no such branch: '%s'"), | |
1816 | branch->name); | |
1817 | return error_buf(err, | |
1818 | _("no upstream configured for branch '%s'"), | |
1819 | branch->name); | |
1820 | } | |
1821 | ||
1822 | if (!branch->merge[0]->dst) | |
1823 | return error_buf(err, | |
1824 | _("upstream branch '%s' not stored as a remote-tracking branch"), | |
1825 | branch->merge[0]->src); | |
1826 | ||
1827 | return branch->merge[0]->dst; | |
1828 | } | |
1829 | ||
1830 | static const char *tracking_for_push_dest(struct remote *remote, | |
1831 | const char *refname, | |
1832 | struct strbuf *err) | |
1833 | { | |
1834 | char *ret; | |
1835 | ||
1836 | ret = apply_refspecs(&remote->fetch, refname); | |
1837 | if (!ret) | |
1838 | return error_buf(err, | |
1839 | _("push destination '%s' on remote '%s' has no local tracking branch"), | |
1840 | refname, remote->name); | |
1841 | return ret; | |
1842 | } | |
1843 | ||
1844 | static const char *branch_get_push_1(struct remote_state *remote_state, | |
1845 | struct branch *branch, struct strbuf *err) | |
1846 | { | |
1847 | struct remote *remote; | |
1848 | ||
1849 | remote = remotes_remote_get( | |
1850 | remote_state, | |
1851 | remotes_pushremote_for_branch(remote_state, branch, NULL)); | |
1852 | if (!remote) | |
1853 | return error_buf(err, | |
1854 | _("branch '%s' has no remote for pushing"), | |
1855 | branch->name); | |
1856 | ||
1857 | if (remote->push.nr) { | |
1858 | char *dst; | |
1859 | const char *ret; | |
1860 | ||
1861 | dst = apply_refspecs(&remote->push, branch->refname); | |
1862 | if (!dst) | |
1863 | return error_buf(err, | |
1864 | _("push refspecs for '%s' do not include '%s'"), | |
1865 | remote->name, branch->name); | |
1866 | ||
1867 | ret = tracking_for_push_dest(remote, dst, err); | |
1868 | free(dst); | |
1869 | return ret; | |
1870 | } | |
1871 | ||
1872 | if (remote->mirror) | |
1873 | return tracking_for_push_dest(remote, branch->refname, err); | |
1874 | ||
1875 | switch (push_default) { | |
1876 | case PUSH_DEFAULT_NOTHING: | |
1877 | return error_buf(err, _("push has no destination (push.default is 'nothing')")); | |
1878 | ||
1879 | case PUSH_DEFAULT_MATCHING: | |
1880 | case PUSH_DEFAULT_CURRENT: | |
1881 | return tracking_for_push_dest(remote, branch->refname, err); | |
1882 | ||
1883 | case PUSH_DEFAULT_UPSTREAM: | |
1884 | return branch_get_upstream(branch, err); | |
1885 | ||
1886 | case PUSH_DEFAULT_UNSPECIFIED: | |
1887 | case PUSH_DEFAULT_SIMPLE: | |
1888 | { | |
1889 | const char *up, *cur; | |
1890 | ||
1891 | up = branch_get_upstream(branch, err); | |
1892 | if (!up) | |
1893 | return NULL; | |
1894 | cur = tracking_for_push_dest(remote, branch->refname, err); | |
1895 | if (!cur) | |
1896 | return NULL; | |
1897 | if (strcmp(cur, up)) | |
1898 | return error_buf(err, | |
1899 | _("cannot resolve 'simple' push to a single destination")); | |
1900 | return cur; | |
1901 | } | |
1902 | } | |
1903 | ||
1904 | BUG("unhandled push situation"); | |
1905 | } | |
1906 | ||
1907 | const char *branch_get_push(struct branch *branch, struct strbuf *err) | |
1908 | { | |
1909 | read_config(the_repository, 0); | |
1910 | die_on_missing_branch(the_repository, branch); | |
1911 | ||
1912 | if (!branch) | |
1913 | return error_buf(err, _("HEAD does not point to a branch")); | |
1914 | ||
1915 | if (!branch->push_tracking_ref) | |
1916 | branch->push_tracking_ref = branch_get_push_1( | |
1917 | the_repository->remote_state, branch, err); | |
1918 | return branch->push_tracking_ref; | |
1919 | } | |
1920 | ||
1921 | static int ignore_symref_update(const char *refname, struct strbuf *scratch) | |
1922 | { | |
1923 | return !refs_read_symbolic_ref(get_main_ref_store(the_repository), refname, scratch); | |
1924 | } | |
1925 | ||
1926 | /* | |
1927 | * Create and return a list of (struct ref) consisting of copies of | |
1928 | * each remote_ref that matches refspec. refspec must be a pattern. | |
1929 | * Fill in the copies' peer_ref to describe the local tracking refs to | |
1930 | * which they map. Omit any references that would map to an existing | |
1931 | * local symbolic ref. | |
1932 | */ | |
1933 | static struct ref *get_expanded_map(const struct ref *remote_refs, | |
1934 | const struct refspec_item *refspec) | |
1935 | { | |
1936 | struct strbuf scratch = STRBUF_INIT; | |
1937 | const struct ref *ref; | |
1938 | struct ref *ret = NULL; | |
1939 | struct ref **tail = &ret; | |
1940 | ||
1941 | for (ref = remote_refs; ref; ref = ref->next) { | |
1942 | char *expn_name = NULL; | |
1943 | ||
1944 | strbuf_reset(&scratch); | |
1945 | ||
1946 | if (strchr(ref->name, '^')) | |
1947 | continue; /* a dereference item */ | |
1948 | if (match_refname_with_pattern(refspec->src, ref->name, | |
1949 | refspec->dst, &expn_name) && | |
1950 | !ignore_symref_update(expn_name, &scratch)) { | |
1951 | struct ref *cpy = copy_ref(ref); | |
1952 | ||
1953 | if (cpy->peer_ref) | |
1954 | free_one_ref(cpy->peer_ref); | |
1955 | cpy->peer_ref = alloc_ref(expn_name); | |
1956 | if (refspec->force) | |
1957 | cpy->peer_ref->force = 1; | |
1958 | *tail = cpy; | |
1959 | tail = &cpy->next; | |
1960 | } | |
1961 | free(expn_name); | |
1962 | } | |
1963 | ||
1964 | strbuf_release(&scratch); | |
1965 | return ret; | |
1966 | } | |
1967 | ||
1968 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) | |
1969 | { | |
1970 | const struct ref *ref; | |
1971 | const struct ref *best_match = NULL; | |
1972 | int best_score = 0; | |
1973 | ||
1974 | for (ref = refs; ref; ref = ref->next) { | |
1975 | int score = refname_match(name, ref->name); | |
1976 | ||
1977 | if (best_score < score) { | |
1978 | best_match = ref; | |
1979 | best_score = score; | |
1980 | } | |
1981 | } | |
1982 | return best_match; | |
1983 | } | |
1984 | ||
1985 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) | |
1986 | { | |
1987 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); | |
1988 | ||
1989 | if (!ref) | |
1990 | return NULL; | |
1991 | ||
1992 | return copy_ref(ref); | |
1993 | } | |
1994 | ||
1995 | static struct ref *get_local_ref(const char *name) | |
1996 | { | |
1997 | if (!name || name[0] == '\0') | |
1998 | return NULL; | |
1999 | ||
2000 | if (starts_with(name, "refs/")) | |
2001 | return alloc_ref(name); | |
2002 | ||
2003 | if (starts_with(name, "heads/") || | |
2004 | starts_with(name, "tags/") || | |
2005 | starts_with(name, "remotes/")) | |
2006 | return alloc_ref_with_prefix("refs/", 5, name); | |
2007 | ||
2008 | return alloc_ref_with_prefix("refs/heads/", 11, name); | |
2009 | } | |
2010 | ||
2011 | int get_fetch_map(const struct ref *remote_refs, | |
2012 | const struct refspec_item *refspec, | |
2013 | struct ref ***tail, | |
2014 | int missing_ok) | |
2015 | { | |
2016 | struct ref *ref_map, **rmp; | |
2017 | ||
2018 | if (refspec->negative) | |
2019 | return 0; | |
2020 | ||
2021 | if (refspec->pattern) { | |
2022 | ref_map = get_expanded_map(remote_refs, refspec); | |
2023 | } else { | |
2024 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; | |
2025 | ||
2026 | if (refspec->exact_sha1) { | |
2027 | ref_map = alloc_ref(name); | |
2028 | get_oid_hex(name, &ref_map->old_oid); | |
2029 | ref_map->exact_oid = 1; | |
2030 | } else { | |
2031 | ref_map = get_remote_ref(remote_refs, name); | |
2032 | } | |
2033 | if (!missing_ok && !ref_map) | |
2034 | die(_("couldn't find remote ref %s"), name); | |
2035 | if (ref_map) { | |
2036 | ref_map->peer_ref = get_local_ref(refspec->dst); | |
2037 | if (ref_map->peer_ref && refspec->force) | |
2038 | ref_map->peer_ref->force = 1; | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | for (rmp = &ref_map; *rmp; ) { | |
2043 | if ((*rmp)->peer_ref) { | |
2044 | if (!starts_with((*rmp)->peer_ref->name, "refs/") || | |
2045 | check_refname_format((*rmp)->peer_ref->name, 0)) { | |
2046 | struct ref *ignore = *rmp; | |
2047 | error(_("* Ignoring funny ref '%s' locally"), | |
2048 | (*rmp)->peer_ref->name); | |
2049 | *rmp = (*rmp)->next; | |
2050 | free(ignore->peer_ref); | |
2051 | free(ignore); | |
2052 | continue; | |
2053 | } | |
2054 | } | |
2055 | rmp = &((*rmp)->next); | |
2056 | } | |
2057 | ||
2058 | if (ref_map) | |
2059 | tail_link_ref(ref_map, tail); | |
2060 | ||
2061 | return 0; | |
2062 | } | |
2063 | ||
2064 | int resolve_remote_symref(struct ref *ref, struct ref *list) | |
2065 | { | |
2066 | if (!ref->symref) | |
2067 | return 0; | |
2068 | for (; list; list = list->next) | |
2069 | if (!strcmp(ref->symref, list->name)) { | |
2070 | oidcpy(&ref->old_oid, &list->old_oid); | |
2071 | return 0; | |
2072 | } | |
2073 | return 1; | |
2074 | } | |
2075 | ||
2076 | /* | |
2077 | * Compute the commit ahead/behind values for the pair branch_name, base. | |
2078 | * | |
2079 | * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the | |
2080 | * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip | |
2081 | * the (potentially expensive) a/b computation (*num_ours and *num_theirs are | |
2082 | * set to zero). | |
2083 | * | |
2084 | * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref | |
2085 | * does not exist). Returns 0 if the commits are identical. Returns 1 if | |
2086 | * commits are different. | |
2087 | */ | |
2088 | ||
2089 | static int stat_branch_pair(const char *branch_name, const char *base, | |
2090 | int *num_ours, int *num_theirs, | |
2091 | enum ahead_behind_flags abf) | |
2092 | { | |
2093 | struct object_id oid; | |
2094 | struct commit *ours, *theirs; | |
2095 | struct rev_info revs; | |
2096 | struct setup_revision_opt opt = { | |
2097 | .free_removed_argv_elements = 1, | |
2098 | }; | |
2099 | struct strvec argv = STRVEC_INIT; | |
2100 | ||
2101 | /* Cannot stat if what we used to build on no longer exists */ | |
2102 | if (refs_read_ref(get_main_ref_store(the_repository), base, &oid)) | |
2103 | return -1; | |
2104 | theirs = lookup_commit_reference(the_repository, &oid); | |
2105 | if (!theirs) | |
2106 | return -1; | |
2107 | ||
2108 | if (refs_read_ref(get_main_ref_store(the_repository), branch_name, &oid)) | |
2109 | return -1; | |
2110 | ours = lookup_commit_reference(the_repository, &oid); | |
2111 | if (!ours) | |
2112 | return -1; | |
2113 | ||
2114 | *num_theirs = *num_ours = 0; | |
2115 | ||
2116 | /* are we the same? */ | |
2117 | if (theirs == ours) | |
2118 | return 0; | |
2119 | if (abf == AHEAD_BEHIND_QUICK) | |
2120 | return 1; | |
2121 | if (abf != AHEAD_BEHIND_FULL) | |
2122 | BUG("stat_branch_pair: invalid abf '%d'", abf); | |
2123 | ||
2124 | /* Run "rev-list --left-right ours...theirs" internally... */ | |
2125 | strvec_push(&argv, ""); /* ignored */ | |
2126 | strvec_push(&argv, "--left-right"); | |
2127 | strvec_pushf(&argv, "%s...%s", | |
2128 | oid_to_hex(&ours->object.oid), | |
2129 | oid_to_hex(&theirs->object.oid)); | |
2130 | strvec_push(&argv, "--"); | |
2131 | ||
2132 | repo_init_revisions(the_repository, &revs, NULL); | |
2133 | setup_revisions(argv.nr, argv.v, &revs, &opt); | |
2134 | if (prepare_revision_walk(&revs)) | |
2135 | die(_("revision walk setup failed")); | |
2136 | ||
2137 | /* ... and count the commits on each side. */ | |
2138 | while (1) { | |
2139 | struct commit *c = get_revision(&revs); | |
2140 | if (!c) | |
2141 | break; | |
2142 | if (c->object.flags & SYMMETRIC_LEFT) | |
2143 | (*num_ours)++; | |
2144 | else | |
2145 | (*num_theirs)++; | |
2146 | } | |
2147 | ||
2148 | /* clear object flags smudged by the above traversal */ | |
2149 | clear_commit_marks(ours, ALL_REV_FLAGS); | |
2150 | clear_commit_marks(theirs, ALL_REV_FLAGS); | |
2151 | ||
2152 | strvec_clear(&argv); | |
2153 | release_revisions(&revs); | |
2154 | return 1; | |
2155 | } | |
2156 | ||
2157 | /* | |
2158 | * Lookup the tracking branch for the given branch and if present, optionally | |
2159 | * compute the commit ahead/behind values for the pair. | |
2160 | * | |
2161 | * If for_push is true, the tracking branch refers to the push branch, | |
2162 | * otherwise it refers to the upstream branch. | |
2163 | * | |
2164 | * The name of the tracking branch (or NULL if it is not defined) is | |
2165 | * returned via *tracking_name, if it is not itself NULL. | |
2166 | * | |
2167 | * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the | |
2168 | * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip | |
2169 | * the (potentially expensive) a/b computation (*num_ours and *num_theirs are | |
2170 | * set to zero). | |
2171 | * | |
2172 | * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no | |
2173 | * upstream defined, or ref does not exist). Returns 0 if the commits are | |
2174 | * identical. Returns 1 if commits are different. | |
2175 | */ | |
2176 | int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, | |
2177 | const char **tracking_name, int for_push, | |
2178 | enum ahead_behind_flags abf) | |
2179 | { | |
2180 | const char *base; | |
2181 | ||
2182 | /* Cannot stat unless we are marked to build on top of somebody else. */ | |
2183 | base = for_push ? branch_get_push(branch, NULL) : | |
2184 | branch_get_upstream(branch, NULL); | |
2185 | if (tracking_name) | |
2186 | *tracking_name = base; | |
2187 | if (!base) | |
2188 | return -1; | |
2189 | ||
2190 | return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf); | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * Return true when there is anything to report, otherwise false. | |
2195 | */ | |
2196 | int format_tracking_info(struct branch *branch, struct strbuf *sb, | |
2197 | enum ahead_behind_flags abf, | |
2198 | int show_divergence_advice) | |
2199 | { | |
2200 | int ours, theirs, sti; | |
2201 | const char *full_base; | |
2202 | char *base; | |
2203 | int upstream_is_gone = 0; | |
2204 | ||
2205 | sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf); | |
2206 | if (sti < 0) { | |
2207 | if (!full_base) | |
2208 | return 0; | |
2209 | upstream_is_gone = 1; | |
2210 | } | |
2211 | ||
2212 | base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), | |
2213 | full_base, 0); | |
2214 | if (upstream_is_gone) { | |
2215 | strbuf_addf(sb, | |
2216 | _("Your branch is based on '%s', but the upstream is gone.\n"), | |
2217 | base); | |
2218 | if (advice_enabled(ADVICE_STATUS_HINTS)) | |
2219 | strbuf_addstr(sb, | |
2220 | _(" (use \"git branch --unset-upstream\" to fixup)\n")); | |
2221 | } else if (!sti) { | |
2222 | strbuf_addf(sb, | |
2223 | _("Your branch is up to date with '%s'.\n"), | |
2224 | base); | |
2225 | } else if (abf == AHEAD_BEHIND_QUICK) { | |
2226 | strbuf_addf(sb, | |
2227 | _("Your branch and '%s' refer to different commits.\n"), | |
2228 | base); | |
2229 | if (advice_enabled(ADVICE_STATUS_HINTS)) | |
2230 | strbuf_addf(sb, _(" (use \"%s\" for details)\n"), | |
2231 | "git status --ahead-behind"); | |
2232 | } else if (!theirs) { | |
2233 | strbuf_addf(sb, | |
2234 | Q_("Your branch is ahead of '%s' by %d commit.\n", | |
2235 | "Your branch is ahead of '%s' by %d commits.\n", | |
2236 | ours), | |
2237 | base, ours); | |
2238 | if (advice_enabled(ADVICE_STATUS_HINTS)) | |
2239 | strbuf_addstr(sb, | |
2240 | _(" (use \"git push\" to publish your local commits)\n")); | |
2241 | } else if (!ours) { | |
2242 | strbuf_addf(sb, | |
2243 | Q_("Your branch is behind '%s' by %d commit, " | |
2244 | "and can be fast-forwarded.\n", | |
2245 | "Your branch is behind '%s' by %d commits, " | |
2246 | "and can be fast-forwarded.\n", | |
2247 | theirs), | |
2248 | base, theirs); | |
2249 | if (advice_enabled(ADVICE_STATUS_HINTS)) | |
2250 | strbuf_addstr(sb, | |
2251 | _(" (use \"git pull\" to update your local branch)\n")); | |
2252 | } else { | |
2253 | strbuf_addf(sb, | |
2254 | Q_("Your branch and '%s' have diverged,\n" | |
2255 | "and have %d and %d different commit each, " | |
2256 | "respectively.\n", | |
2257 | "Your branch and '%s' have diverged,\n" | |
2258 | "and have %d and %d different commits each, " | |
2259 | "respectively.\n", | |
2260 | ours + theirs), | |
2261 | base, ours, theirs); | |
2262 | if (show_divergence_advice && | |
2263 | advice_enabled(ADVICE_STATUS_HINTS)) | |
2264 | strbuf_addstr(sb, | |
2265 | _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n")); | |
2266 | } | |
2267 | free(base); | |
2268 | return 1; | |
2269 | } | |
2270 | ||
2271 | static int one_local_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid, | |
2272 | int flag UNUSED, | |
2273 | void *cb_data) | |
2274 | { | |
2275 | struct ref ***local_tail = cb_data; | |
2276 | struct ref *ref; | |
2277 | ||
2278 | /* we already know it starts with refs/ to get here */ | |
2279 | if (check_refname_format(refname + 5, 0)) | |
2280 | return 0; | |
2281 | ||
2282 | ref = alloc_ref(refname); | |
2283 | oidcpy(&ref->new_oid, oid); | |
2284 | **local_tail = ref; | |
2285 | *local_tail = &ref->next; | |
2286 | return 0; | |
2287 | } | |
2288 | ||
2289 | struct ref *get_local_heads(void) | |
2290 | { | |
2291 | struct ref *local_refs = NULL, **local_tail = &local_refs; | |
2292 | ||
2293 | refs_for_each_ref(get_main_ref_store(the_repository), one_local_ref, | |
2294 | &local_tail); | |
2295 | return local_refs; | |
2296 | } | |
2297 | ||
2298 | struct ref *guess_remote_head(const struct ref *head, | |
2299 | const struct ref *refs, | |
2300 | unsigned flags) | |
2301 | { | |
2302 | const struct ref *r; | |
2303 | struct ref *list = NULL; | |
2304 | struct ref **tail = &list; | |
2305 | ||
2306 | if (!head) | |
2307 | return NULL; | |
2308 | ||
2309 | /* | |
2310 | * Some transports support directly peeking at | |
2311 | * where HEAD points; if that is the case, then | |
2312 | * we don't have to guess. | |
2313 | */ | |
2314 | if (head->symref) | |
2315 | return copy_ref(find_ref_by_name(refs, head->symref)); | |
2316 | ||
2317 | /* If a remote branch exists with the default branch name, let's use it. */ | |
2318 | if (!(flags & REMOTE_GUESS_HEAD_ALL)) { | |
2319 | char *default_branch = | |
2320 | repo_default_branch_name(the_repository, | |
2321 | flags & REMOTE_GUESS_HEAD_QUIET); | |
2322 | char *ref = xstrfmt("refs/heads/%s", default_branch); | |
2323 | ||
2324 | r = find_ref_by_name(refs, ref); | |
2325 | free(ref); | |
2326 | free(default_branch); | |
2327 | ||
2328 | if (r && oideq(&r->old_oid, &head->old_oid)) | |
2329 | return copy_ref(r); | |
2330 | ||
2331 | /* Fall back to the hard-coded historical default */ | |
2332 | r = find_ref_by_name(refs, "refs/heads/master"); | |
2333 | if (r && oideq(&r->old_oid, &head->old_oid)) | |
2334 | return copy_ref(r); | |
2335 | } | |
2336 | ||
2337 | /* Look for another ref that points there */ | |
2338 | for (r = refs; r; r = r->next) { | |
2339 | if (r != head && | |
2340 | starts_with(r->name, "refs/heads/") && | |
2341 | oideq(&r->old_oid, &head->old_oid)) { | |
2342 | *tail = copy_ref(r); | |
2343 | tail = &((*tail)->next); | |
2344 | if (!(flags & REMOTE_GUESS_HEAD_ALL)) | |
2345 | break; | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | return list; | |
2350 | } | |
2351 | ||
2352 | struct stale_heads_info { | |
2353 | struct string_list *ref_names; | |
2354 | struct ref **stale_refs_tail; | |
2355 | struct refspec *rs; | |
2356 | }; | |
2357 | ||
2358 | static int get_stale_heads_cb(const char *refname, const char *referent UNUSED, const struct object_id *oid, | |
2359 | int flags, void *cb_data) | |
2360 | { | |
2361 | struct stale_heads_info *info = cb_data; | |
2362 | struct string_list matches = STRING_LIST_INIT_DUP; | |
2363 | struct refspec_item query; | |
2364 | int i, stale = 1; | |
2365 | memset(&query, 0, sizeof(struct refspec_item)); | |
2366 | query.dst = (char *)refname; | |
2367 | ||
2368 | refspec_find_all_matches(info->rs, &query, &matches); | |
2369 | if (matches.nr == 0) | |
2370 | goto clean_exit; /* No matches */ | |
2371 | ||
2372 | /* | |
2373 | * If we did find a suitable refspec and it's not a symref and | |
2374 | * it's not in the list of refs that currently exist in that | |
2375 | * remote, we consider it to be stale. In order to deal with | |
2376 | * overlapping refspecs, we need to go over all of the | |
2377 | * matching refs. | |
2378 | */ | |
2379 | if (flags & REF_ISSYMREF) | |
2380 | goto clean_exit; | |
2381 | ||
2382 | for (i = 0; stale && i < matches.nr; i++) | |
2383 | if (string_list_has_string(info->ref_names, matches.items[i].string)) | |
2384 | stale = 0; | |
2385 | ||
2386 | if (stale) { | |
2387 | struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail); | |
2388 | oidcpy(&ref->new_oid, oid); | |
2389 | } | |
2390 | ||
2391 | clean_exit: | |
2392 | string_list_clear(&matches, 0); | |
2393 | return 0; | |
2394 | } | |
2395 | ||
2396 | struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map) | |
2397 | { | |
2398 | struct ref *ref, *stale_refs = NULL; | |
2399 | struct string_list ref_names = STRING_LIST_INIT_NODUP; | |
2400 | struct stale_heads_info info; | |
2401 | ||
2402 | info.ref_names = &ref_names; | |
2403 | info.stale_refs_tail = &stale_refs; | |
2404 | info.rs = rs; | |
2405 | for (ref = fetch_map; ref; ref = ref->next) | |
2406 | string_list_append(&ref_names, ref->name); | |
2407 | string_list_sort(&ref_names); | |
2408 | refs_for_each_ref(get_main_ref_store(the_repository), | |
2409 | get_stale_heads_cb, &info); | |
2410 | string_list_clear(&ref_names, 0); | |
2411 | return stale_refs; | |
2412 | } | |
2413 | ||
2414 | /* | |
2415 | * Compare-and-swap | |
2416 | */ | |
2417 | void clear_cas_option(struct push_cas_option *cas) | |
2418 | { | |
2419 | int i; | |
2420 | ||
2421 | for (i = 0; i < cas->nr; i++) | |
2422 | free(cas->entry[i].refname); | |
2423 | free(cas->entry); | |
2424 | memset(cas, 0, sizeof(*cas)); | |
2425 | } | |
2426 | ||
2427 | static struct push_cas *add_cas_entry(struct push_cas_option *cas, | |
2428 | const char *refname, | |
2429 | size_t refnamelen) | |
2430 | { | |
2431 | struct push_cas *entry; | |
2432 | ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc); | |
2433 | entry = &cas->entry[cas->nr++]; | |
2434 | memset(entry, 0, sizeof(*entry)); | |
2435 | entry->refname = xmemdupz(refname, refnamelen); | |
2436 | return entry; | |
2437 | } | |
2438 | ||
2439 | static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset) | |
2440 | { | |
2441 | const char *colon; | |
2442 | struct push_cas *entry; | |
2443 | ||
2444 | if (unset) { | |
2445 | /* "--no-<option>" */ | |
2446 | clear_cas_option(cas); | |
2447 | return 0; | |
2448 | } | |
2449 | ||
2450 | if (!arg) { | |
2451 | /* just "--<option>" */ | |
2452 | cas->use_tracking_for_rest = 1; | |
2453 | return 0; | |
2454 | } | |
2455 | ||
2456 | /* "--<option>=refname" or "--<option>=refname:value" */ | |
2457 | colon = strchrnul(arg, ':'); | |
2458 | entry = add_cas_entry(cas, arg, colon - arg); | |
2459 | if (!*colon) | |
2460 | entry->use_tracking = 1; | |
2461 | else if (!colon[1]) | |
2462 | oidclr(&entry->expect, the_repository->hash_algo); | |
2463 | else if (repo_get_oid(the_repository, colon + 1, &entry->expect)) | |
2464 | return error(_("cannot parse expected object name '%s'"), | |
2465 | colon + 1); | |
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset) | |
2470 | { | |
2471 | return parse_push_cas_option(opt->value, arg, unset); | |
2472 | } | |
2473 | ||
2474 | int is_empty_cas(const struct push_cas_option *cas) | |
2475 | { | |
2476 | return !cas->use_tracking_for_rest && !cas->nr; | |
2477 | } | |
2478 | ||
2479 | /* | |
2480 | * Look at remote.fetch refspec and see if we have a remote | |
2481 | * tracking branch for the refname there. Fill the name of | |
2482 | * the remote-tracking branch in *dst_refname, and the name | |
2483 | * of the commit object at its tip in oid[]. | |
2484 | * If we cannot do so, return negative to signal an error. | |
2485 | */ | |
2486 | static int remote_tracking(struct remote *remote, const char *refname, | |
2487 | struct object_id *oid, char **dst_refname) | |
2488 | { | |
2489 | char *dst; | |
2490 | ||
2491 | dst = apply_refspecs(&remote->fetch, refname); | |
2492 | if (!dst) | |
2493 | return -1; /* no tracking ref for refname at remote */ | |
2494 | if (refs_read_ref(get_main_ref_store(the_repository), dst, oid)) { | |
2495 | free(dst); | |
2496 | return -1; /* we know what the tracking ref is but we cannot read it */ | |
2497 | } | |
2498 | ||
2499 | *dst_refname = dst; | |
2500 | return 0; | |
2501 | } | |
2502 | ||
2503 | /* | |
2504 | * The struct "reflog_commit_array" and related helper functions | |
2505 | * are used for collecting commits into an array during reflog | |
2506 | * traversals in "check_and_collect_until()". | |
2507 | */ | |
2508 | struct reflog_commit_array { | |
2509 | struct commit **item; | |
2510 | size_t nr, alloc; | |
2511 | }; | |
2512 | ||
2513 | #define REFLOG_COMMIT_ARRAY_INIT { 0 } | |
2514 | ||
2515 | /* Append a commit to the array. */ | |
2516 | static void append_commit(struct reflog_commit_array *arr, | |
2517 | struct commit *commit) | |
2518 | { | |
2519 | ALLOC_GROW(arr->item, arr->nr + 1, arr->alloc); | |
2520 | arr->item[arr->nr++] = commit; | |
2521 | } | |
2522 | ||
2523 | /* Free and reset the array. */ | |
2524 | static void free_commit_array(struct reflog_commit_array *arr) | |
2525 | { | |
2526 | FREE_AND_NULL(arr->item); | |
2527 | arr->nr = arr->alloc = 0; | |
2528 | } | |
2529 | ||
2530 | struct check_and_collect_until_cb_data { | |
2531 | struct commit *remote_commit; | |
2532 | struct reflog_commit_array *local_commits; | |
2533 | timestamp_t remote_reflog_timestamp; | |
2534 | }; | |
2535 | ||
2536 | /* Get the timestamp of the latest entry. */ | |
2537 | static int peek_reflog(struct object_id *o_oid UNUSED, | |
2538 | struct object_id *n_oid UNUSED, | |
2539 | const char *ident UNUSED, | |
2540 | timestamp_t timestamp, int tz UNUSED, | |
2541 | const char *message UNUSED, void *cb_data) | |
2542 | { | |
2543 | timestamp_t *ts = cb_data; | |
2544 | *ts = timestamp; | |
2545 | return 1; | |
2546 | } | |
2547 | ||
2548 | static int check_and_collect_until(struct object_id *o_oid UNUSED, | |
2549 | struct object_id *n_oid, | |
2550 | const char *ident UNUSED, | |
2551 | timestamp_t timestamp, int tz UNUSED, | |
2552 | const char *message UNUSED, void *cb_data) | |
2553 | { | |
2554 | struct commit *commit; | |
2555 | struct check_and_collect_until_cb_data *cb = cb_data; | |
2556 | ||
2557 | /* An entry was found. */ | |
2558 | if (oideq(n_oid, &cb->remote_commit->object.oid)) | |
2559 | return 1; | |
2560 | ||
2561 | if ((commit = lookup_commit_reference(the_repository, n_oid))) | |
2562 | append_commit(cb->local_commits, commit); | |
2563 | ||
2564 | /* | |
2565 | * If the reflog entry timestamp is older than the remote ref's | |
2566 | * latest reflog entry, there is no need to check or collect | |
2567 | * entries older than this one. | |
2568 | */ | |
2569 | if (timestamp < cb->remote_reflog_timestamp) | |
2570 | return -1; | |
2571 | ||
2572 | return 0; | |
2573 | } | |
2574 | ||
2575 | #define MERGE_BASES_BATCH_SIZE 8 | |
2576 | ||
2577 | /* | |
2578 | * Iterate through the reflog of the local ref to check if there is an entry | |
2579 | * for the given remote-tracking ref; runs until the timestamp of an entry is | |
2580 | * older than latest timestamp of remote-tracking ref's reflog. Any commits | |
2581 | * are that seen along the way are collected into an array to check if the | |
2582 | * remote-tracking ref is reachable from any of them. | |
2583 | */ | |
2584 | static int is_reachable_in_reflog(const char *local, const struct ref *remote) | |
2585 | { | |
2586 | timestamp_t date; | |
2587 | struct commit *commit; | |
2588 | struct commit **chunk; | |
2589 | struct check_and_collect_until_cb_data cb; | |
2590 | struct reflog_commit_array arr = REFLOG_COMMIT_ARRAY_INIT; | |
2591 | size_t size = 0; | |
2592 | int ret = 0; | |
2593 | ||
2594 | commit = lookup_commit_reference(the_repository, &remote->old_oid); | |
2595 | if (!commit) | |
2596 | goto cleanup_return; | |
2597 | ||
2598 | /* | |
2599 | * Get the timestamp from the latest entry | |
2600 | * of the remote-tracking ref's reflog. | |
2601 | */ | |
2602 | refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), | |
2603 | remote->tracking_ref, peek_reflog, | |
2604 | &date); | |
2605 | ||
2606 | cb.remote_commit = commit; | |
2607 | cb.local_commits = &arr; | |
2608 | cb.remote_reflog_timestamp = date; | |
2609 | ret = refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), | |
2610 | local, check_and_collect_until, | |
2611 | &cb); | |
2612 | ||
2613 | /* We found an entry in the reflog. */ | |
2614 | if (ret > 0) | |
2615 | goto cleanup_return; | |
2616 | ||
2617 | /* | |
2618 | * Check if the remote commit is reachable from any | |
2619 | * of the commits in the collected array, in batches. | |
2620 | */ | |
2621 | for (chunk = arr.item; chunk < arr.item + arr.nr; chunk += size) { | |
2622 | size = arr.item + arr.nr - chunk; | |
2623 | if (MERGE_BASES_BATCH_SIZE < size) | |
2624 | size = MERGE_BASES_BATCH_SIZE; | |
2625 | ||
2626 | if ((ret = repo_in_merge_bases_many(the_repository, commit, size, chunk, 0))) | |
2627 | break; | |
2628 | } | |
2629 | ||
2630 | cleanup_return: | |
2631 | free_commit_array(&arr); | |
2632 | return ret; | |
2633 | } | |
2634 | ||
2635 | /* | |
2636 | * Check for reachability of a remote-tracking | |
2637 | * ref in the reflog entries of its local ref. | |
2638 | */ | |
2639 | static void check_if_includes_upstream(struct ref *remote) | |
2640 | { | |
2641 | struct ref *local = get_local_ref(remote->name); | |
2642 | if (!local) | |
2643 | return; | |
2644 | ||
2645 | if (is_reachable_in_reflog(local->name, remote) <= 0) | |
2646 | remote->unreachable = 1; | |
2647 | free_one_ref(local); | |
2648 | } | |
2649 | ||
2650 | static void apply_cas(struct push_cas_option *cas, | |
2651 | struct remote *remote, | |
2652 | struct ref *ref) | |
2653 | { | |
2654 | int i; | |
2655 | ||
2656 | /* Find an explicit --<option>=<name>[:<value>] entry */ | |
2657 | for (i = 0; i < cas->nr; i++) { | |
2658 | struct push_cas *entry = &cas->entry[i]; | |
2659 | if (!refname_match(entry->refname, ref->name)) | |
2660 | continue; | |
2661 | ref->expect_old_sha1 = 1; | |
2662 | if (!entry->use_tracking) | |
2663 | oidcpy(&ref->old_oid_expect, &entry->expect); | |
2664 | else if (remote_tracking(remote, ref->name, | |
2665 | &ref->old_oid_expect, | |
2666 | &ref->tracking_ref)) | |
2667 | oidclr(&ref->old_oid_expect, the_repository->hash_algo); | |
2668 | else | |
2669 | ref->check_reachable = cas->use_force_if_includes; | |
2670 | return; | |
2671 | } | |
2672 | ||
2673 | /* Are we using "--<option>" to cover all? */ | |
2674 | if (!cas->use_tracking_for_rest) | |
2675 | return; | |
2676 | ||
2677 | ref->expect_old_sha1 = 1; | |
2678 | if (remote_tracking(remote, ref->name, | |
2679 | &ref->old_oid_expect, | |
2680 | &ref->tracking_ref)) | |
2681 | oidclr(&ref->old_oid_expect, the_repository->hash_algo); | |
2682 | else | |
2683 | ref->check_reachable = cas->use_force_if_includes; | |
2684 | } | |
2685 | ||
2686 | void apply_push_cas(struct push_cas_option *cas, | |
2687 | struct remote *remote, | |
2688 | struct ref *remote_refs) | |
2689 | { | |
2690 | struct ref *ref; | |
2691 | for (ref = remote_refs; ref; ref = ref->next) { | |
2692 | apply_cas(cas, remote, ref); | |
2693 | ||
2694 | /* | |
2695 | * If "compare-and-swap" is in "use_tracking[_for_rest]" | |
2696 | * mode, and if "--force-if-includes" was specified, run | |
2697 | * the check. | |
2698 | */ | |
2699 | if (ref->check_reachable) | |
2700 | check_if_includes_upstream(ref); | |
2701 | } | |
2702 | } | |
2703 | ||
2704 | struct remote_state *remote_state_new(void) | |
2705 | { | |
2706 | struct remote_state *r; | |
2707 | ||
2708 | CALLOC_ARRAY(r, 1); | |
2709 | ||
2710 | hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0); | |
2711 | hashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0); | |
2712 | return r; | |
2713 | } | |
2714 | ||
2715 | void remote_state_clear(struct remote_state *remote_state) | |
2716 | { | |
2717 | struct hashmap_iter iter; | |
2718 | struct branch *b; | |
2719 | int i; | |
2720 | ||
2721 | for (i = 0; i < remote_state->remotes_nr; i++) | |
2722 | remote_clear(remote_state->remotes[i]); | |
2723 | FREE_AND_NULL(remote_state->remotes); | |
2724 | FREE_AND_NULL(remote_state->pushremote_name); | |
2725 | remote_state->remotes_alloc = 0; | |
2726 | remote_state->remotes_nr = 0; | |
2727 | ||
2728 | rewrites_release(&remote_state->rewrites); | |
2729 | rewrites_release(&remote_state->rewrites_push); | |
2730 | ||
2731 | hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); | |
2732 | hashmap_for_each_entry(&remote_state->branches_hash, &iter, b, ent) { | |
2733 | branch_release(b); | |
2734 | free(b); | |
2735 | } | |
2736 | hashmap_clear(&remote_state->branches_hash); | |
2737 | } | |
2738 | ||
2739 | /* | |
2740 | * Returns 1 if it was the last chop before ':'. | |
2741 | */ | |
2742 | static int chop_last_dir(char **remoteurl, int is_relative) | |
2743 | { | |
2744 | char *rfind = find_last_dir_sep(*remoteurl); | |
2745 | if (rfind) { | |
2746 | *rfind = '\0'; | |
2747 | return 0; | |
2748 | } | |
2749 | ||
2750 | rfind = strrchr(*remoteurl, ':'); | |
2751 | if (rfind) { | |
2752 | *rfind = '\0'; | |
2753 | return 1; | |
2754 | } | |
2755 | ||
2756 | if (is_relative || !strcmp(".", *remoteurl)) | |
2757 | die(_("cannot strip one component off url '%s'"), | |
2758 | *remoteurl); | |
2759 | ||
2760 | free(*remoteurl); | |
2761 | *remoteurl = xstrdup("."); | |
2762 | return 0; | |
2763 | } | |
2764 | ||
2765 | char *relative_url(const char *remote_url, const char *url, | |
2766 | const char *up_path) | |
2767 | { | |
2768 | int is_relative = 0; | |
2769 | int colonsep = 0; | |
2770 | char *out; | |
2771 | char *remoteurl; | |
2772 | struct strbuf sb = STRBUF_INIT; | |
2773 | size_t len; | |
2774 | ||
2775 | if (!url_is_local_not_ssh(url) || is_absolute_path(url)) | |
2776 | return xstrdup(url); | |
2777 | ||
2778 | len = strlen(remote_url); | |
2779 | if (!len) | |
2780 | BUG("invalid empty remote_url"); | |
2781 | ||
2782 | remoteurl = xstrdup(remote_url); | |
2783 | if (is_dir_sep(remoteurl[len-1])) | |
2784 | remoteurl[len-1] = '\0'; | |
2785 | ||
2786 | if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl)) | |
2787 | is_relative = 0; | |
2788 | else { | |
2789 | is_relative = 1; | |
2790 | /* | |
2791 | * Prepend a './' to ensure all relative | |
2792 | * remoteurls start with './' or '../' | |
2793 | */ | |
2794 | if (!starts_with_dot_slash_native(remoteurl) && | |
2795 | !starts_with_dot_dot_slash_native(remoteurl)) { | |
2796 | strbuf_reset(&sb); | |
2797 | strbuf_addf(&sb, "./%s", remoteurl); | |
2798 | free(remoteurl); | |
2799 | remoteurl = strbuf_detach(&sb, NULL); | |
2800 | } | |
2801 | } | |
2802 | /* | |
2803 | * When the url starts with '../', remove that and the | |
2804 | * last directory in remoteurl. | |
2805 | */ | |
2806 | while (*url) { | |
2807 | if (starts_with_dot_dot_slash_native(url)) { | |
2808 | url += 3; | |
2809 | colonsep |= chop_last_dir(&remoteurl, is_relative); | |
2810 | } else if (starts_with_dot_slash_native(url)) | |
2811 | url += 2; | |
2812 | else | |
2813 | break; | |
2814 | } | |
2815 | strbuf_reset(&sb); | |
2816 | strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url); | |
2817 | if (ends_with(url, "/")) | |
2818 | strbuf_setlen(&sb, sb.len - 1); | |
2819 | free(remoteurl); | |
2820 | ||
2821 | if (starts_with_dot_slash_native(sb.buf)) | |
2822 | out = xstrdup(sb.buf + 2); | |
2823 | else | |
2824 | out = xstrdup(sb.buf); | |
2825 | ||
2826 | if (!up_path || !is_relative) { | |
2827 | strbuf_release(&sb); | |
2828 | return out; | |
2829 | } | |
2830 | ||
2831 | strbuf_reset(&sb); | |
2832 | strbuf_addf(&sb, "%s%s", up_path, out); | |
2833 | free(out); | |
2834 | return strbuf_detach(&sb, NULL); | |
2835 | } | |
2836 | ||
2837 | int valid_remote_name(const char *name) | |
2838 | { | |
2839 | int result; | |
2840 | struct strbuf refspec = STRBUF_INIT; | |
2841 | strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name); | |
2842 | result = valid_fetch_refspec(refspec.buf); | |
2843 | strbuf_release(&refspec); | |
2844 | return result; | |
2845 | } |