]>
Commit | Line | Data |
---|---|---|
e7da9385 | 1 | #define USE_THE_REPOSITORY_VARIABLE |
41f43b82 | 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
e7da9385 | 3 | |
e93fc5d7 | 4 | #include "git-compat-util.h" |
0b027f6c | 5 | #include "abspath.h" |
b2141fc1 | 6 | #include "config.h" |
32a8f510 | 7 | #include "environment.h" |
f394e093 | 8 | #include "gettext.h" |
41771fa4 | 9 | #include "hex.h" |
5751f490 | 10 | #include "remote.h" |
6dcbdc0d | 11 | #include "urlmatch.h" |
5751f490 | 12 | #include "refs.h" |
ec0cb496 | 13 | #include "refspec.h" |
dabab1d6 | 14 | #include "object-name.h" |
68cd492a | 15 | #include "object-store.h" |
c339932b | 16 | #include "path.h" |
6d21bf96 JH |
17 | #include "commit.h" |
18 | #include "diff.h" | |
19 | #include "revision.h" | |
8ca12c0d | 20 | #include "dir.h" |
e38da487 | 21 | #include "setup.h" |
73cf0822 | 22 | #include "string-list.h" |
dbbcd44f | 23 | #include "strvec.h" |
64043556 | 24 | #include "commit-reach.h" |
dd8dd300 | 25 | #include "advice.h" |
1d04e719 | 26 | #include "connect.h" |
49fd5511 | 27 | #include "parse-options.h" |
72da5cfb | 28 | #include "transport.h" |
5751f490 | 29 | |
6ddba5e2 FC |
30 | enum map_direction { FROM_SRC, FROM_DST }; |
31 | ||
844112ca JH |
32 | struct counted_string { |
33 | size_t len; | |
34 | const char *s; | |
35 | }; | |
55029ae4 | 36 | |
0a4da29d DB |
37 | static int valid_remote(const struct remote *remote) |
38 | { | |
ffce8218 | 39 | return !!remote->url.nr; |
0a4da29d DB |
40 | } |
41 | ||
aa0595fb | 42 | static char *alias_url(const char *url, struct rewrites *r) |
55029ae4 DB |
43 | { |
44 | int i, j; | |
844112ca JH |
45 | struct counted_string *longest; |
46 | int longest_i; | |
47 | ||
48 | longest = NULL; | |
49 | longest_i = -1; | |
d071d942 JT |
50 | for (i = 0; i < r->rewrite_nr; i++) { |
51 | if (!r->rewrite[i]) | |
55029ae4 | 52 | continue; |
d071d942 | 53 | for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) { |
59556548 | 54 | if (starts_with(url, r->rewrite[i]->instead_of[j].s) && |
844112ca | 55 | (!longest || |
d071d942 JT |
56 | longest->len < r->rewrite[i]->instead_of[j].len)) { |
57 | longest = &(r->rewrite[i]->instead_of[j]); | |
844112ca | 58 | longest_i = i; |
55029ae4 DB |
59 | } |
60 | } | |
61 | } | |
844112ca | 62 | if (!longest) |
aa0595fb | 63 | return NULL; |
844112ca | 64 | |
75faa45a | 65 | return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len); |
55029ae4 DB |
66 | } |
67 | ||
28b91f8a | 68 | static void add_url(struct remote *remote, const char *url) |
5751f490 | 69 | { |
9badf97c JK |
70 | if (*url) |
71 | strvec_push(&remote->url, url); | |
72 | else | |
73 | strvec_clear(&remote->url); | |
5751f490 DB |
74 | } |
75 | ||
20346234 MG |
76 | static void add_pushurl(struct remote *remote, const char *pushurl) |
77 | { | |
9badf97c JK |
78 | if (*pushurl) |
79 | strvec_push(&remote->pushurl, pushurl); | |
80 | else | |
81 | strvec_clear(&remote->pushurl); | |
20346234 MG |
82 | } |
83 | ||
085b98f6 GC |
84 | static void add_pushurl_alias(struct remote_state *remote_state, |
85 | struct remote *remote, const char *url) | |
1c2eafb8 | 86 | { |
aa0595fb JK |
87 | char *alias = alias_url(url, &remote_state->rewrites_push); |
88 | if (alias) | |
89 | add_pushurl(remote, alias); | |
52595c15 | 90 | free(alias); |
1c2eafb8 JT |
91 | } |
92 | ||
085b98f6 GC |
93 | static void add_url_alias(struct remote_state *remote_state, |
94 | struct remote *remote, const char *url) | |
1c2eafb8 | 95 | { |
aa0595fb JK |
96 | char *alias = alias_url(url, &remote_state->rewrites); |
97 | add_url(remote, alias ? alias : url); | |
085b98f6 | 98 | add_pushurl_alias(remote_state, remote, url); |
52595c15 | 99 | free(alias); |
1c2eafb8 JT |
100 | } |
101 | ||
d0da003d PR |
102 | struct remotes_hash_key { |
103 | const char *str; | |
104 | int len; | |
105 | }; | |
106 | ||
5cf88fd8 | 107 | static int remotes_hash_cmp(const void *cmp_data UNUSED, |
939af16e EW |
108 | const struct hashmap_entry *eptr, |
109 | const struct hashmap_entry *entry_or_key, | |
45dcb35f | 110 | const void *keydata) |
d0da003d | 111 | { |
939af16e | 112 | const struct remote *a, *b; |
45dcb35f SB |
113 | const struct remotes_hash_key *key = keydata; |
114 | ||
939af16e EW |
115 | a = container_of(eptr, const struct remote, ent); |
116 | b = container_of(entry_or_key, const struct remote, ent); | |
117 | ||
d0da003d | 118 | if (key) |
f0e578c6 | 119 | return !!xstrncmpz(a->name, key->str, key->len); |
d0da003d PR |
120 | else |
121 | return strcmp(a->name, b->name); | |
122 | } | |
123 | ||
085b98f6 GC |
124 | static struct remote *make_remote(struct remote_state *remote_state, |
125 | const char *name, int len) | |
5751f490 | 126 | { |
6540b716 | 127 | struct remote *ret; |
d0da003d | 128 | struct remotes_hash_key lookup; |
f23a4651 | 129 | struct hashmap_entry lookup_entry, *e; |
5751f490 | 130 | |
d0da003d PR |
131 | if (!len) |
132 | len = strlen(name); | |
133 | ||
d0da003d PR |
134 | lookup.str = name; |
135 | lookup.len = len; | |
136 | hashmap_entry_init(&lookup_entry, memhash(name, len)); | |
137 | ||
085b98f6 | 138 | e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup); |
f23a4651 EW |
139 | if (e) |
140 | return container_of(e, struct remote, ent); | |
5751f490 | 141 | |
ca56dadb | 142 | CALLOC_ARRAY(ret, 1); |
737c5a9c | 143 | ret->prune = -1; /* unspecified */ |
97716d21 | 144 | ret->prune_tags = -1; /* unspecified */ |
6bdb304b | 145 | ret->name = xstrndup(name, len); |
0baad1f3 TB |
146 | refspec_init_push(&ret->push); |
147 | refspec_init_fetch(&ret->fetch); | |
72da5cfb | 148 | string_list_init_dup(&ret->server_options); |
6bdb304b | 149 | |
085b98f6 GC |
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; | |
d0da003d | 153 | |
d22245a2 | 154 | hashmap_entry_init(&ret->ent, lookup_entry.hash); |
085b98f6 | 155 | if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent)) |
6540b716 | 156 | BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); |
2d31347b | 157 | return ret; |
5751f490 DB |
158 | } |
159 | ||
fd3cb050 GC |
160 | static void remote_clear(struct remote *remote) |
161 | { | |
fd3cb050 GC |
162 | free((char *)remote->name); |
163 | free((char *)remote->foreign_vcs); | |
164 | ||
8e804415 JK |
165 | strvec_clear(&remote->url); |
166 | strvec_clear(&remote->pushurl); | |
fd3cb050 | 167 | |
fd3cb050 GC |
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); | |
72da5cfb | 172 | string_list_clear(&remote->server_options, 0); |
fd3cb050 GC |
173 | } |
174 | ||
cf818348 DB |
175 | static void add_merge(struct branch *branch, const char *name) |
176 | { | |
2d31347b DB |
177 | ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, |
178 | branch->merge_alloc); | |
179 | branch->merge_name[branch->merge_nr++] = name; | |
cf818348 DB |
180 | } |
181 | ||
4a2dcb1a GC |
182 | struct branches_hash_key { |
183 | const char *str; | |
184 | int len; | |
185 | }; | |
186 | ||
5cf88fd8 | 187 | static int branches_hash_cmp(const void *cmp_data UNUSED, |
4a2dcb1a GC |
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) | |
f0e578c6 | 199 | return !!xstrncmpz(a->name, key->str, key->len); |
4a2dcb1a GC |
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 | ||
4a2dcb1a GC |
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 && | |
91e2e8f6 GC |
226 | branch != find_branch(repo->remote_state, branch->name, |
227 | strlen(branch->name))) | |
4a2dcb1a GC |
228 | die("branch %s was not found in the repository", branch->name); |
229 | } | |
230 | ||
085b98f6 GC |
231 | static struct branch *make_branch(struct remote_state *remote_state, |
232 | const char *name, size_t len) | |
cf818348 | 233 | { |
2d31347b | 234 | struct branch *ret; |
cf818348 | 235 | |
4a2dcb1a GC |
236 | ret = find_branch(remote_state, name, len); |
237 | if (ret) | |
238 | return ret; | |
cf818348 | 239 | |
ca56dadb | 240 | CALLOC_ARRAY(ret, 1); |
021ba32a | 241 | ret->name = xstrndup(name, len); |
fa3f60b7 | 242 | ret->refname = xstrfmt("refs/heads/%s", ret->name); |
cf818348 | 243 | |
4a2dcb1a GC |
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"); | |
2d31347b | 247 | return ret; |
cf818348 DB |
248 | } |
249 | ||
f5ccb535 PS |
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 | ||
021ba32a JK |
261 | static struct rewrite *make_rewrite(struct rewrites *r, |
262 | const char *base, size_t len) | |
55029ae4 DB |
263 | { |
264 | struct rewrite *ret; | |
265 | int i; | |
266 | ||
d071d942 | 267 | for (i = 0; i < r->rewrite_nr; i++) { |
021ba32a JK |
268 | if (len == r->rewrite[i]->baselen && |
269 | !strncmp(base, r->rewrite[i]->base, len)) | |
d071d942 | 270 | return r->rewrite[i]; |
55029ae4 DB |
271 | } |
272 | ||
d071d942 | 273 | ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc); |
ca56dadb | 274 | CALLOC_ARRAY(ret, 1); |
d071d942 | 275 | r->rewrite[r->rewrite_nr++] = ret; |
021ba32a JK |
276 | ret->base = xstrndup(base, len); |
277 | ret->baselen = len; | |
55029ae4 DB |
278 | return ret; |
279 | } | |
280 | ||
f5ccb535 PS |
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 | ||
55029ae4 DB |
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); | |
844112ca JH |
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++; | |
55029ae4 DB |
295 | } |
296 | ||
8ccc75c2 | 297 | #ifndef WITH_BREAKING_CHANGES |
0e265a92 JK |
298 | static const char *skip_spaces(const char *s) |
299 | { | |
300 | while (isspace(*s)) | |
301 | s++; | |
302 | return s; | |
303 | } | |
304 | ||
8ccc75c2 PS |
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 | ||
085b98f6 GC |
320 | static void read_remotes_file(struct remote_state *remote_state, |
321 | struct remote *remote) | |
5751f490 | 322 | { |
0e265a92 | 323 | struct strbuf buf = STRBUF_INIT; |
88dd321c PS |
324 | FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf, |
325 | "remotes/%s", remote->name), "r"); | |
5751f490 DB |
326 | |
327 | if (!f) | |
88dd321c | 328 | goto out; |
8ccc75c2 PS |
329 | |
330 | warn_about_deprecated_remote_type("remotes", remote); | |
331 | ||
e459b073 | 332 | remote->configured_in_repo = 1; |
89cf4c70 | 333 | remote->origin = REMOTE_REMOTES; |
18814d0e | 334 | while (strbuf_getline(&buf, f) != EOF) { |
0e265a92 | 335 | const char *v; |
5751f490 | 336 | |
0e265a92 | 337 | strbuf_rtrim(&buf); |
5751f490 | 338 | |
0e265a92 | 339 | if (skip_prefix(buf.buf, "URL:", &v)) |
085b98f6 | 340 | add_url_alias(remote_state, remote, |
52595c15 | 341 | skip_spaces(v)); |
0e265a92 | 342 | else if (skip_prefix(buf.buf, "Push:", &v)) |
6bdb304b | 343 | refspec_append(&remote->push, skip_spaces(v)); |
0e265a92 | 344 | else if (skip_prefix(buf.buf, "Pull:", &v)) |
e5349abf | 345 | refspec_append(&remote->fetch, skip_spaces(v)); |
5751f490 DB |
346 | } |
347 | fclose(f); | |
88dd321c PS |
348 | |
349 | out: | |
350 | strbuf_release(&buf); | |
5751f490 DB |
351 | } |
352 | ||
085b98f6 GC |
353 | static void read_branches_file(struct remote_state *remote_state, |
354 | struct remote *remote) | |
5751f490 | 355 | { |
97abaab5 | 356 | char *frag, *to_free = NULL; |
f28e3ab2 | 357 | struct strbuf buf = STRBUF_INIT; |
88dd321c PS |
358 | FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf, |
359 | "branches/%s", remote->name), "r"); | |
5751f490 DB |
360 | |
361 | if (!f) | |
88dd321c | 362 | goto out; |
f28e3ab2 | 363 | |
8ccc75c2 PS |
364 | warn_about_deprecated_remote_type("branches", remote); |
365 | ||
8f309aeb | 366 | strbuf_getline_lf(&buf, f); |
0fb19906 | 367 | fclose(f); |
f28e3ab2 | 368 | strbuf_trim(&buf); |
88dd321c PS |
369 | if (!buf.len) |
370 | goto out; | |
f28e3ab2 | 371 | |
e459b073 | 372 | remote->configured_in_repo = 1; |
89cf4c70 | 373 | remote->origin = REMOTE_BRANCHES; |
472fa4cd DB |
374 | |
375 | /* | |
55cfde25 | 376 | * The branches file would have URL and optionally |
a471214b | 377 | * #branch specified. The default (or specified) branch is |
f28e3ab2 JK |
378 | * fetched and stored in the local branch matching the |
379 | * remote name. | |
472fa4cd | 380 | */ |
f28e3ab2 JK |
381 | frag = strchr(buf.buf, '#'); |
382 | if (frag) | |
cf818348 | 383 | *(frag++) = '\0'; |
f28e3ab2 | 384 | else |
97abaab5 | 385 | frag = to_free = repo_default_branch_name(the_repository, 0); |
f28e3ab2 | 386 | |
52595c15 | 387 | add_url_alias(remote_state, remote, buf.buf); |
1af8b8c0 RS |
388 | refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", |
389 | frag, remote->name); | |
55cfde25 | 390 | |
18afe101 MK |
391 | /* |
392 | * Cogito compatible push: push current HEAD to remote #branch | |
393 | * (master if missing) | |
394 | */ | |
1af8b8c0 | 395 | refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag); |
d71ab174 | 396 | remote->fetch_tags = 1; /* always auto-follow */ |
97abaab5 | 397 | |
88dd321c | 398 | out: |
52595c15 | 399 | strbuf_release(&buf); |
97abaab5 | 400 | free(to_free); |
5751f490 | 401 | } |
8ccc75c2 | 402 | #endif /* WITH_BREAKING_CHANGES */ |
5751f490 | 403 | |
a4e7e317 | 404 | static int handle_config(const char *key, const char *value, |
6021e1d1 | 405 | const struct config_context *ctx, void *cb) |
5751f490 DB |
406 | { |
407 | const char *name; | |
f5914f4b | 408 | size_t namelen; |
5751f490 DB |
409 | const char *subkey; |
410 | struct remote *remote; | |
cf818348 | 411 | struct branch *branch; |
085b98f6 | 412 | struct remote_state *remote_state = cb; |
6021e1d1 | 413 | const struct key_value_info *kvi = ctx->kvi; |
085b98f6 | 414 | |
bc60f8a7 | 415 | if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) { |
f1dfbd9e | 416 | /* There is no subsection. */ |
bc60f8a7 | 417 | if (!name) |
cf818348 | 418 | return 0; |
f1dfbd9e GC |
419 | /* There is a subsection, but it is empty. */ |
420 | if (!namelen) | |
421 | return -1; | |
085b98f6 | 422 | branch = make_branch(remote_state, name, namelen); |
bc60f8a7 | 423 | if (!strcmp(subkey, "remote")) { |
f5ccb535 | 424 | FREE_AND_NULL(branch->remote_name); |
e41bf352 | 425 | return git_config_string(&branch->remote_name, key, value); |
bc60f8a7 | 426 | } else if (!strcmp(subkey, "pushremote")) { |
f5ccb535 | 427 | FREE_AND_NULL(branch->pushremote_name); |
da66b274 | 428 | return git_config_string(&branch->pushremote_name, key, value); |
bc60f8a7 | 429 | } else if (!strcmp(subkey, "merge")) { |
d2370cc2 JH |
430 | if (!value) |
431 | return config_error_nonbool(key); | |
cf818348 | 432 | add_merge(branch, xstrdup(value)); |
d2370cc2 | 433 | } |
cf818348 | 434 | return 0; |
5751f490 | 435 | } |
bc60f8a7 | 436 | if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) { |
55029ae4 | 437 | struct rewrite *rewrite; |
bc60f8a7 | 438 | if (!name) |
55029ae4 | 439 | return 0; |
bc60f8a7 | 440 | if (!strcmp(subkey, "insteadof")) { |
1c2eafb8 JT |
441 | if (!value) |
442 | return config_error_nonbool(key); | |
085b98f6 GC |
443 | rewrite = make_rewrite(&remote_state->rewrites, name, |
444 | namelen); | |
1c2eafb8 | 445 | add_instead_of(rewrite, xstrdup(value)); |
bc60f8a7 | 446 | } else if (!strcmp(subkey, "pushinsteadof")) { |
55029ae4 DB |
447 | if (!value) |
448 | return config_error_nonbool(key); | |
085b98f6 GC |
449 | rewrite = make_rewrite(&remote_state->rewrites_push, |
450 | name, namelen); | |
55029ae4 DB |
451 | add_instead_of(rewrite, xstrdup(value)); |
452 | } | |
453 | } | |
224c2171 | 454 | |
bc60f8a7 | 455 | if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0) |
5751f490 | 456 | return 0; |
224c2171 RR |
457 | |
458 | /* Handle remote.* variables */ | |
f5ccb535 PS |
459 | if (!name && !strcmp(subkey, "pushdefault")) { |
460 | FREE_AND_NULL(remote_state->pushremote_name); | |
085b98f6 GC |
461 | return git_config_string(&remote_state->pushremote_name, key, |
462 | value); | |
f5ccb535 | 463 | } |
224c2171 | 464 | |
bc60f8a7 TG |
465 | if (!name) |
466 | return 0; | |
224c2171 | 467 | /* Handle remote.<name>.* variables */ |
c82efafc | 468 | if (*name == '/') { |
0b9c3afd | 469 | warning(_("config remote shorthand cannot begin with '/': %s"), |
c82efafc BC |
470 | name); |
471 | return 0; | |
472 | } | |
085b98f6 | 473 | remote = make_remote(remote_state, name, namelen); |
89cf4c70 | 474 | remote->origin = REMOTE_CONFIG; |
6021e1d1 GC |
475 | if (kvi->scope == CONFIG_SCOPE_LOCAL || |
476 | kvi->scope == CONFIG_SCOPE_WORKTREE) | |
e459b073 | 477 | remote->configured_in_repo = 1; |
bc60f8a7 | 478 | if (!strcmp(subkey, "mirror")) |
84bb2dfd | 479 | remote->mirror = git_config_bool(key, value); |
bc60f8a7 | 480 | else if (!strcmp(subkey, "skipdefaultupdate")) |
84bb2dfd | 481 | remote->skip_default_update = git_config_bool(key, value); |
bc60f8a7 | 482 | else if (!strcmp(subkey, "skipfetchall")) |
7cc91a2f | 483 | remote->skip_default_update = git_config_bool(key, value); |
bc60f8a7 | 484 | else if (!strcmp(subkey, "prune")) |
737c5a9c | 485 | remote->prune = git_config_bool(key, value); |
97716d21 ÆAB |
486 | else if (!strcmp(subkey, "prunetags")) |
487 | remote->prune_tags = git_config_bool(key, value); | |
bc60f8a7 | 488 | else if (!strcmp(subkey, "url")) { |
52595c15 JK |
489 | if (!value) |
490 | return config_error_nonbool(key); | |
491 | add_url(remote, value); | |
bc60f8a7 | 492 | } else if (!strcmp(subkey, "pushurl")) { |
52595c15 JK |
493 | if (!value) |
494 | return config_error_nonbool(key); | |
495 | add_pushurl(remote, value); | |
bc60f8a7 | 496 | } else if (!strcmp(subkey, "push")) { |
1b261c20 | 497 | char *v; |
84bb2dfd PB |
498 | if (git_config_string(&v, key, value)) |
499 | return -1; | |
6bdb304b | 500 | refspec_append(&remote->push, v); |
1b261c20 | 501 | free(v); |
bc60f8a7 | 502 | } else if (!strcmp(subkey, "fetch")) { |
1b261c20 | 503 | char *v; |
84bb2dfd PB |
504 | if (git_config_string(&v, key, value)) |
505 | return -1; | |
e5349abf | 506 | refspec_append(&remote->fetch, v); |
1b261c20 | 507 | free(v); |
bc60f8a7 | 508 | } else if (!strcmp(subkey, "receivepack")) { |
1b261c20 | 509 | char *v; |
84bb2dfd PB |
510 | if (git_config_string(&v, key, value)) |
511 | return -1; | |
5751f490 | 512 | if (!remote->receivepack) |
84bb2dfd | 513 | remote->receivepack = v; |
5751f490 | 514 | else |
0b9c3afd | 515 | error(_("more than one receivepack given, using the first")); |
bc60f8a7 | 516 | } else if (!strcmp(subkey, "uploadpack")) { |
1b261c20 | 517 | char *v; |
84bb2dfd PB |
518 | if (git_config_string(&v, key, value)) |
519 | return -1; | |
0012ba21 | 520 | if (!remote->uploadpack) |
84bb2dfd | 521 | remote->uploadpack = v; |
0012ba21 | 522 | else |
0b9c3afd | 523 | error(_("more than one uploadpack given, using the first")); |
bc60f8a7 | 524 | } else if (!strcmp(subkey, "tagopt")) { |
d71ab174 DB |
525 | if (!strcmp(value, "--no-tags")) |
526 | remote->fetch_tags = -1; | |
944163a4 ST |
527 | else if (!strcmp(value, "--tags")) |
528 | remote->fetch_tags = 2; | |
bc60f8a7 | 529 | } else if (!strcmp(subkey, "proxy")) { |
f5ccb535 | 530 | FREE_AND_NULL(remote->http_proxy); |
1b261c20 | 531 | return git_config_string(&remote->http_proxy, |
84bb2dfd | 532 | key, value); |
bc60f8a7 | 533 | } else if (!strcmp(subkey, "proxyauthmethod")) { |
f5ccb535 | 534 | FREE_AND_NULL(remote->http_proxy_authmethod); |
1b261c20 | 535 | return git_config_string(&remote->http_proxy_authmethod, |
ef976395 | 536 | key, value); |
bc60f8a7 | 537 | } else if (!strcmp(subkey, "vcs")) { |
f5ccb535 | 538 | FREE_AND_NULL(remote->foreign_vcs); |
c578f51d | 539 | return git_config_string(&remote->foreign_vcs, key, value); |
72da5cfb XX |
540 | } else if (!strcmp(subkey, "serveroption")) { |
541 | return parse_transport_option(key, value, | |
542 | &remote->server_options); | |
b7f7d165 | 543 | } else if (!strcmp(subkey, "followremotehead")) { |
9e2b7005 | 544 | const char *no_warn_branch; |
b7f7d165 BF |
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; | |
9e2b7005 | 549 | else if (!strcmp(value, "warn")) { |
b7f7d165 | 550 | remote->follow_remote_head = FOLLOW_REMOTE_WARN; |
9e2b7005 BF |
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")) { | |
b7f7d165 | 556 | remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS; |
9e2b7005 BF |
557 | } else { |
558 | warning(_("unrecognized followRemoteHEAD value '%s' ignored"), | |
559 | value); | |
560 | } | |
84bb2dfd | 561 | } |
5751f490 DB |
562 | return 0; |
563 | } | |
564 | ||
085b98f6 | 565 | static void alias_all_urls(struct remote_state *remote_state) |
55029ae4 DB |
566 | { |
567 | int i, j; | |
085b98f6 | 568 | for (i = 0; i < remote_state->remotes_nr; i++) { |
1c2eafb8 | 569 | int add_pushurl_aliases; |
085b98f6 | 570 | if (!remote_state->remotes[i]) |
55029ae4 | 571 | continue; |
8e804415 JK |
572 | for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) { |
573 | char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j], | |
aa0595fb | 574 | &remote_state->rewrites); |
8e804415 JK |
575 | if (alias) |
576 | strvec_replace(&remote_state->remotes[i]->pushurl, | |
577 | j, alias); | |
ce01f928 | 578 | free(alias); |
20346234 | 579 | } |
8e804415 JK |
580 | add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0; |
581 | for (j = 0; j < remote_state->remotes[i]->url.nr; j++) { | |
aa0595fb | 582 | char *alias; |
1c2eafb8 | 583 | if (add_pushurl_aliases) |
fd3cb050 | 584 | add_pushurl_alias( |
085b98f6 | 585 | remote_state, remote_state->remotes[i], |
8e804415 JK |
586 | remote_state->remotes[i]->url.v[j]); |
587 | alias = alias_url(remote_state->remotes[i]->url.v[j], | |
085b98f6 | 588 | &remote_state->rewrites); |
8e804415 JK |
589 | if (alias) |
590 | strvec_replace(&remote_state->remotes[i]->url, | |
591 | j, alias); | |
ce01f928 | 592 | free(alias); |
1c2eafb8 | 593 | } |
55029ae4 DB |
594 | } |
595 | } | |
596 | ||
3c8f60c6 | 597 | static void read_config(struct repository *repo, int early) |
5751f490 | 598 | { |
5751f490 | 599 | int flag; |
e41bf352 | 600 | |
085b98f6 | 601 | if (repo->remote_state->initialized) |
5751f490 | 602 | return; |
085b98f6 | 603 | repo->remote_state->initialized = 1; |
e41bf352 | 604 | |
085b98f6 | 605 | repo->remote_state->current_branch = NULL; |
3c8f60c6 | 606 | if (startup_info->have_repository && !early) { |
085b98f6 | 607 | const char *head_ref = refs_resolve_ref_unsafe( |
ce14de03 | 608 | get_main_ref_store(repo), "HEAD", 0, NULL, &flag); |
f2f12d16 JK |
609 | if (head_ref && (flag & REF_ISSYMREF) && |
610 | skip_prefix(head_ref, "refs/heads/", &head_ref)) { | |
085b98f6 GC |
611 | repo->remote_state->current_branch = make_branch( |
612 | repo->remote_state, head_ref, strlen(head_ref)); | |
f2f12d16 | 613 | } |
5751f490 | 614 | } |
085b98f6 GC |
615 | repo_config(repo, handle_config, repo->remote_state); |
616 | alias_all_urls(repo->remote_state); | |
5751f490 DB |
617 | } |
618 | ||
8ccc75c2 | 619 | #ifndef WITH_BREAKING_CHANGES |
df93e33c DB |
620 | static int valid_remote_nick(const char *name) |
621 | { | |
8ca12c0d | 622 | if (!name[0] || is_dot_or_dotdot(name)) |
df93e33c | 623 | return 0; |
d9244ecf JS |
624 | |
625 | /* remote nicknames cannot contain slashes */ | |
626 | while (*name) | |
627 | if (is_dir_sep(*name++)) | |
628 | return 0; | |
629 | return 1; | |
df93e33c | 630 | } |
8ccc75c2 | 631 | #endif /* WITH_BREAKING_CHANGES */ |
df93e33c | 632 | |
56eed342 GC |
633 | static const char *remotes_remote_for_branch(struct remote_state *remote_state, |
634 | struct branch *branch, | |
635 | int *explicit) | |
f052154d JK |
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; | |
8a649be7 TK |
644 | if (remote_state->remotes_nr == 1) |
645 | return remote_state->remotes[0]->name; | |
f052154d JK |
646 | return "origin"; |
647 | } | |
648 | ||
56eed342 GC |
649 | const char *remote_for_branch(struct branch *branch, int *explicit) |
650 | { | |
3c8f60c6 | 651 | read_config(the_repository, 0); |
4a2dcb1a GC |
652 | die_on_missing_branch(the_repository, branch); |
653 | ||
56eed342 GC |
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) | |
da66b274 JK |
661 | { |
662 | if (branch && branch->pushremote_name) { | |
663 | if (explicit) | |
664 | *explicit = 1; | |
665 | return branch->pushremote_name; | |
666 | } | |
56eed342 | 667 | if (remote_state->pushremote_name) { |
da66b274 JK |
668 | if (explicit) |
669 | *explicit = 1; | |
56eed342 | 670 | return remote_state->pushremote_name; |
da66b274 | 671 | } |
56eed342 | 672 | return remotes_remote_for_branch(remote_state, branch, explicit); |
da66b274 JK |
673 | } |
674 | ||
56eed342 GC |
675 | const char *pushremote_for_branch(struct branch *branch, int *explicit) |
676 | { | |
3c8f60c6 | 677 | read_config(the_repository, 0); |
4a2dcb1a GC |
678 | die_on_missing_branch(the_repository, branch); |
679 | ||
56eed342 GC |
680 | return remotes_pushremote_for_branch(the_repository->remote_state, |
681 | branch, explicit); | |
da66b274 JK |
682 | } |
683 | ||
56eed342 GC |
684 | static struct remote *remotes_remote_get(struct remote_state *remote_state, |
685 | const char *name); | |
686 | ||
f046127b | 687 | char *remote_ref_for_branch(struct branch *branch, int for_push) |
9700fae5 | 688 | { |
3c8f60c6 | 689 | read_config(the_repository, 0); |
4a2dcb1a GC |
690 | die_on_missing_branch(the_repository, branch); |
691 | ||
9700fae5 W |
692 | if (branch) { |
693 | if (!for_push) { | |
694 | if (branch->merge_nr) { | |
f046127b | 695 | return xstrdup(branch->merge_name[0]); |
9700fae5 W |
696 | } |
697 | } else { | |
f046127b JK |
698 | char *dst; |
699 | const char *remote_name = remotes_pushremote_for_branch( | |
56eed342 GC |
700 | the_repository->remote_state, branch, |
701 | NULL); | |
702 | struct remote *remote = remotes_remote_get( | |
703 | the_repository->remote_state, remote_name); | |
9700fae5 | 704 | |
6bdb304b | 705 | if (remote && remote->push.nr && |
d000414e | 706 | (dst = apply_refspecs(&remote->push, |
9700fae5 | 707 | branch->refname))) { |
9700fae5 W |
708 | return dst; |
709 | } | |
710 | } | |
711 | } | |
af8ccd8a | 712 | return NULL; |
9700fae5 W |
713 | } |
714 | ||
6dcbdc0d DS |
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 | ||
7281c196 | 722 | if (git_config_get_string_tmp("transfer.credentialsinurl", &value)) |
6dcbdc0d DS |
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 | |
b4eda05d | 732 | die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value); |
6dcbdc0d | 733 | |
8e804415 | 734 | for (i = 0; i < remote->url.nr; i++) { |
6dcbdc0d DS |
735 | struct url_info url_info = { 0 }; |
736 | ||
8e804415 | 737 | if (!url_normalize(remote->url.v[i], &url_info) || |
6dcbdc0d DS |
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 | ||
56eed342 GC |
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 *)) | |
5751f490 DB |
763 | { |
764 | struct remote *ret; | |
fa685bdf | 765 | int name_given = 0; |
5751f490 | 766 | |
fa685bdf DB |
767 | if (name) |
768 | name_given = 1; | |
da66b274 | 769 | else |
56eed342 | 770 | name = get_default(remote_state, remote_state->current_branch, |
fd3cb050 | 771 | &name_given); |
9326d494 | 772 | |
56eed342 | 773 | ret = make_remote(remote_state, name, 0); |
8ccc75c2 | 774 | #ifndef WITH_BREAKING_CHANGES |
4539c218 | 775 | if (valid_remote_nick(name) && have_git_dir()) { |
0a4da29d | 776 | if (!valid_remote(ret)) |
56eed342 | 777 | read_remotes_file(remote_state, ret); |
0a4da29d | 778 | if (!valid_remote(ret)) |
56eed342 | 779 | read_branches_file(remote_state, ret); |
5751f490 | 780 | } |
8ccc75c2 | 781 | #endif /* WITH_BREAKING_CHANGES */ |
0a4da29d | 782 | if (name_given && !valid_remote(ret)) |
56eed342 | 783 | add_url_alias(remote_state, ret, name); |
0a4da29d | 784 | if (!valid_remote(ret)) |
5751f490 | 785 | return NULL; |
6dcbdc0d DS |
786 | |
787 | validate_remote_url(ret); | |
788 | ||
5751f490 DB |
789 | return ret; |
790 | } | |
6b62816c | 791 | |
56eed342 GC |
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 | ||
f24f715e RR |
799 | struct remote *remote_get(const char *name) |
800 | { | |
3c8f60c6 PS |
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); | |
56eed342 GC |
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); | |
f24f715e RR |
816 | } |
817 | ||
818 | struct remote *pushremote_get(const char *name) | |
819 | { | |
3c8f60c6 | 820 | read_config(the_repository, 0); |
56eed342 | 821 | return remotes_pushremote_get(the_repository->remote_state, name); |
f24f715e RR |
822 | } |
823 | ||
e459b073 | 824 | int remote_is_configured(struct remote *remote, int in_repo) |
9a23ba33 | 825 | { |
e459b073 JS |
826 | if (!remote) |
827 | return 0; | |
828 | if (in_repo) | |
829 | return remote->configured_in_repo; | |
830 | return !!remote->origin; | |
9a23ba33 FAG |
831 | } |
832 | ||
b42f6927 JS |
833 | int for_each_remote(each_remote_fn fn, void *priv) |
834 | { | |
835 | int i, result = 0; | |
3c8f60c6 | 836 | read_config(the_repository, 0); |
fd3cb050 GC |
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) | |
b42f6927 | 842 | continue; |
fd3cb050 | 843 | result = fn(remote, priv); |
b42f6927 JS |
844 | } |
845 | return result; | |
846 | } | |
847 | ||
df02ebda MH |
848 | static void handle_duplicate(struct ref *ref1, struct ref *ref2) |
849 | { | |
f096e6e8 MH |
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 | */ | |
92ca8680 | 869 | BUG("Internal error"); |
f096e6e8 MH |
870 | } |
871 | } | |
df02ebda MH |
872 | free(ref2->peer_ref); |
873 | free(ref2); | |
874 | } | |
875 | ||
b9afe665 | 876 | struct ref *ref_remove_duplicates(struct ref *ref_map) |
2467a4fa | 877 | { |
183113a5 | 878 | struct string_list refs = STRING_LIST_INIT_NODUP; |
b9afe665 MH |
879 | struct ref *retval = NULL; |
880 | struct ref **p = &retval; | |
73cf0822 | 881 | |
b9afe665 MH |
882 | while (ref_map) { |
883 | struct ref *ref = ref_map; | |
884 | ||
885 | ref_map = ref_map->next; | |
886 | ref->next = NULL; | |
73cf0822 | 887 | |
b9afe665 MH |
888 | if (!ref->peer_ref) { |
889 | *p = ref; | |
890 | p = &ref->next; | |
09ea1f8e | 891 | } else { |
b9afe665 MH |
892 | struct string_list_item *item = |
893 | string_list_insert(&refs, ref->peer_ref->name); | |
894 | ||
895 | if (item->util) { | |
896 | /* Entry already existed */ | |
df02ebda | 897 | handle_duplicate((struct ref *)item->util, ref); |
b9afe665 MH |
898 | } else { |
899 | *p = ref; | |
900 | p = &ref->next; | |
901 | item->util = ref; | |
902 | } | |
2467a4fa DB |
903 | } |
904 | } | |
b9afe665 | 905 | |
73cf0822 | 906 | string_list_clear(&refs, 0); |
b9afe665 | 907 | return retval; |
2467a4fa DB |
908 | } |
909 | ||
28b91f8a | 910 | int remote_has_url(struct remote *remote, const char *url) |
5d46c9d4 DB |
911 | { |
912 | int i; | |
8e804415 JK |
913 | for (i = 0; i < remote->url.nr; i++) { |
914 | if (!strcmp(remote->url.v[i], url)) | |
5d46c9d4 DB |
915 | return 1; |
916 | } | |
917 | return 0; | |
918 | } | |
919 | ||
b68118d2 JK |
920 | struct strvec *push_url_of_remote(struct remote *remote) |
921 | { | |
922 | return remote->pushurl.nr ? &remote->pushurl : &remote->url; | |
923 | } | |
924 | ||
a6c30623 PS |
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 | ||
0ad4a5ff | 939 | int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) |
c500352e | 940 | { |
be0905fe | 941 | return refspec_find_match(&remote->fetch, refspec); |
c500352e CMN |
942 | } |
943 | ||
8009768e RS |
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); | |
50a6c8ef | 948 | struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1)); |
8009768e RS |
949 | memcpy(ref->name, prefix, prefixlen); |
950 | memcpy(ref->name + prefixlen, name, len); | |
951 | return ref; | |
952 | } | |
953 | ||
59c69c0c | 954 | struct ref *alloc_ref(const char *name) |
dfd255dd | 955 | { |
59c69c0c | 956 | return alloc_ref_with_prefix("", 0, name); |
737922aa KK |
957 | } |
958 | ||
59a57757 | 959 | struct ref *copy_ref(const struct ref *ref) |
d71ab174 | 960 | { |
7b3db095 JS |
961 | struct ref *cpy; |
962 | size_t len; | |
963 | if (!ref) | |
964 | return NULL; | |
50a6c8ef JK |
965 | len = st_add3(sizeof(struct ref), strlen(ref->name), 1); |
966 | cpy = xmalloc(len); | |
967 | memcpy(cpy, ref, len); | |
7b3db095 | 968 | cpy->next = NULL; |
8c53f071 JK |
969 | cpy->symref = xstrdup_or_null(ref->symref); |
970 | cpy->remote_status = xstrdup_or_null(ref->remote_status); | |
7b3db095 JS |
971 | cpy->peer_ref = copy_ref(ref->peer_ref); |
972 | return cpy; | |
d71ab174 DB |
973 | } |
974 | ||
4577370e DB |
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 | ||
1027186f | 987 | void free_one_ref(struct ref *ref) |
be885d96 DB |
988 | { |
989 | if (!ref) | |
990 | return; | |
1027186f | 991 | free_one_ref(ref->peer_ref); |
a6c30623 | 992 | ref_push_report_free(ref->report); |
be885d96 | 993 | free(ref->remote_status); |
cdbb7208 | 994 | free(ref->tracking_ref); |
be885d96 DB |
995 | free(ref->symref); |
996 | free(ref); | |
997 | } | |
998 | ||
dfd255dd DB |
999 | void free_refs(struct ref *ref) |
1000 | { | |
1001 | struct ref *next; | |
1002 | while (ref) { | |
1003 | next = ref->next; | |
1027186f | 1004 | free_one_ref(ref); |
dfd255dd DB |
1005 | ref = next; |
1006 | } | |
1007 | } | |
1008 | ||
ca02465b JH |
1009 | int count_refspec_match(const char *pattern, |
1010 | struct ref *refs, | |
1011 | struct ref **matched_ref) | |
6b62816c DB |
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); | |
6b62816c | 1022 | |
54457fe5 | 1023 | if (!refname_match(pattern, name)) |
6b62816c DB |
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 && | |
59556548 CC |
1036 | !starts_with(name, "refs/heads/") && |
1037 | !starts_with(name, "refs/tags/")) { | |
6b62816c DB |
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) { | |
471fd3fe JK |
1054 | if (matched_ref) |
1055 | *matched_ref = matched_weak; | |
6b62816c DB |
1056 | return weak_match; |
1057 | } | |
1058 | else { | |
471fd3fe JK |
1059 | if (matched_ref) |
1060 | *matched_ref = matched; | |
6b62816c DB |
1061 | return match; |
1062 | } | |
1063 | } | |
1064 | ||
7a52a8c7 | 1065 | void tail_link_ref(struct ref *ref, struct ref ***tail) |
6b62816c DB |
1066 | { |
1067 | **tail = ref; | |
1d735267 DB |
1068 | while (ref->next) |
1069 | ref = ref->next; | |
6b62816c | 1070 | *tail = &ref->next; |
6b62816c DB |
1071 | } |
1072 | ||
67655246 FC |
1073 | static struct ref *alloc_delete_ref(void) |
1074 | { | |
1075 | struct ref *ref = alloc_ref("(delete)"); | |
9da95bda | 1076 | oidclr(&ref->new_oid, the_repository->hash_algo); |
67655246 FC |
1077 | return ref; |
1078 | } | |
1079 | ||
471fd3fe JK |
1080 | static int try_explicit_object_name(const char *name, |
1081 | struct ref **match) | |
6b62816c | 1082 | { |
fcd30b13 | 1083 | struct object_id oid; |
6b62816c | 1084 | |
471fd3fe JK |
1085 | if (!*name) { |
1086 | if (match) | |
1087 | *match = alloc_delete_ref(); | |
1088 | return 0; | |
1089 | } | |
1090 | ||
d850b7a5 | 1091 | if (repo_get_oid(the_repository, name, &oid)) |
471fd3fe JK |
1092 | return -1; |
1093 | ||
1094 | if (match) { | |
1095 | *match = alloc_ref(name); | |
fcd30b13 | 1096 | oidcpy(&(*match)->new_oid, &oid); |
471fd3fe JK |
1097 | } |
1098 | return 0; | |
6b62816c DB |
1099 | } |
1100 | ||
1d735267 | 1101 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
6b62816c | 1102 | { |
59c69c0c | 1103 | struct ref *ret = alloc_ref(name); |
1d735267 DB |
1104 | tail_link_ref(ret, tail); |
1105 | return ret; | |
163f0ee5 | 1106 | } |
8558fd9e | 1107 | |
f8aae120 JK |
1108 | static char *guess_ref(const char *name, struct ref *peer) |
1109 | { | |
1110 | struct strbuf buf = STRBUF_INIT; | |
f8aae120 | 1111 | |
2e5c4758 PS |
1112 | const char *r = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
1113 | peer->name, | |
1114 | RESOLVE_REF_READING, | |
1115 | NULL, NULL); | |
f8aae120 JK |
1116 | if (!r) |
1117 | return NULL; | |
1118 | ||
cab53989 | 1119 | if (starts_with(r, "refs/heads/")) { |
f8aae120 | 1120 | strbuf_addstr(&buf, "refs/heads/"); |
cab53989 | 1121 | } else if (starts_with(r, "refs/tags/")) { |
f8aae120 | 1122 | strbuf_addstr(&buf, "refs/tags/"); |
cab53989 | 1123 | } else { |
f8aae120 | 1124 | return NULL; |
cab53989 | 1125 | } |
f8aae120 JK |
1126 | |
1127 | strbuf_addstr(&buf, name); | |
1128 | return strbuf_detach(&buf, NULL); | |
1129 | } | |
1130 | ||
f7ade3d3 | 1131 | static int match_explicit_lhs(struct ref *src, |
0ad4a5ff | 1132 | struct refspec_item *rs, |
f7ade3d3 JK |
1133 | struct ref **match, |
1134 | int *allocated_match) | |
1135 | { | |
1136 | switch (count_refspec_match(rs->src, src, match)) { | |
1137 | case 1: | |
471fd3fe JK |
1138 | if (allocated_match) |
1139 | *allocated_match = 0; | |
f7ade3d3 JK |
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 | */ | |
471fd3fe | 1146 | if (try_explicit_object_name(rs->src, match) < 0) |
0b9c3afd | 1147 | return error(_("src refspec %s does not match any"), rs->src); |
471fd3fe JK |
1148 | if (allocated_match) |
1149 | *allocated_match = 1; | |
f7ade3d3 JK |
1150 | return 0; |
1151 | default: | |
0b9c3afd | 1152 | return error(_("src refspec %s matches more than one"), rs->src); |
f7ade3d3 JK |
1153 | } |
1154 | } | |
1155 | ||
04d17287 ÆAB |
1156 | static void show_push_unqualified_ref_name_error(const char *dst_value, |
1157 | const char *matched_src_name) | |
1158 | { | |
dd8dd300 ÆAB |
1159 | struct object_id oid; |
1160 | enum object_type type; | |
1161 | ||
04d17287 ÆAB |
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); | |
dd8dd300 | 1177 | |
ed9bff08 | 1178 | if (!advice_enabled(ADVICE_PUSH_UNQUALIFIED_REF_NAME)) |
dd8dd300 ÆAB |
1179 | return; |
1180 | ||
d850b7a5 | 1181 | if (repo_get_oid(the_repository, matched_src_name, &oid)) |
dd8dd300 ÆAB |
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 | } | |
04d17287 ÆAB |
1210 | } |
1211 | ||
54a8ad92 JH |
1212 | static int match_explicit(struct ref *src, struct ref *dst, |
1213 | struct ref ***dst_tail, | |
0ad4a5ff | 1214 | struct refspec_item *rs) |
6b62816c | 1215 | { |
5e9e04a0 PS |
1216 | struct ref *matched_src = NULL, *matched_dst = NULL; |
1217 | int allocated_src = 0, ret; | |
8558fd9e | 1218 | |
54a8ad92 | 1219 | const char *dst_value = rs->dst; |
f8aae120 | 1220 | char *dst_guess; |
6b62816c | 1221 | |
5e9e04a0 PS |
1222 | if (rs->pattern || rs->matching || rs->negative) { |
1223 | ret = 0; | |
1224 | goto out; | |
1225 | } | |
8558fd9e | 1226 | |
5e9e04a0 PS |
1227 | if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0) { |
1228 | ret = -1; | |
1229 | goto out; | |
1230 | } | |
3c8b7df1 | 1231 | |
4491e62a | 1232 | if (!dst_value) { |
9f0ea7e8 DB |
1233 | int flag; |
1234 | ||
2e5c4758 PS |
1235 | dst_value = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
1236 | matched_src->name, | |
1237 | RESOLVE_REF_READING, | |
1238 | NULL, &flag); | |
9f0ea7e8 DB |
1239 | if (!dst_value || |
1240 | ((flag & REF_ISSYMREF) && | |
59556548 | 1241 | !starts_with(dst_value, "refs/heads/"))) |
0b9c3afd | 1242 | die(_("%s cannot be resolved to branch"), |
9f0ea7e8 | 1243 | matched_src->name); |
4491e62a | 1244 | } |
3c8b7df1 | 1245 | |
54a8ad92 JH |
1246 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
1247 | case 1: | |
1248 | break; | |
1249 | case 0: | |
cab53989 | 1250 | if (starts_with(dst_value, "refs/")) { |
1d735267 | 1251 | matched_dst = make_linked_ref(dst_value, dst_tail); |
cab53989 | 1252 | } else if (is_null_oid(&matched_src->new_oid)) { |
0b9c3afd | 1253 | error(_("unable to delete '%s': remote ref does not exist"), |
5742c82b | 1254 | dst_value); |
cab53989 | 1255 | } else if ((dst_guess = guess_ref(dst_value, matched_src))) { |
f8aae120 | 1256 | matched_dst = make_linked_ref(dst_guess, dst_tail); |
3dc7ea91 | 1257 | free(dst_guess); |
cab53989 | 1258 | } else { |
04d17287 ÆAB |
1259 | show_push_unqualified_ref_name_error(dst_value, |
1260 | matched_src->name); | |
cab53989 | 1261 | } |
54a8ad92 JH |
1262 | break; |
1263 | default: | |
3c8b7df1 | 1264 | matched_dst = NULL; |
0b9c3afd | 1265 | error(_("dst refspec %s matches more than one"), |
54a8ad92 JH |
1266 | dst_value); |
1267 | break; | |
1268 | } | |
5e9e04a0 PS |
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 { | |
f7ade3d3 JK |
1280 | matched_dst->peer_ref = allocated_src ? |
1281 | matched_src : | |
1282 | copy_ref(matched_src); | |
54a8ad92 | 1283 | matched_dst->force = rs->force; |
5e9e04a0 | 1284 | matched_src = NULL; |
6b62816c | 1285 | } |
5e9e04a0 PS |
1286 | |
1287 | ret = 0; | |
1288 | ||
1289 | out: | |
1290 | if (allocated_src) | |
1291 | free_one_ref(matched_src); | |
1292 | return ret; | |
54a8ad92 JH |
1293 | } |
1294 | ||
1295 | static int match_explicit_refs(struct ref *src, struct ref *dst, | |
9fa2e5e8 | 1296 | struct ref ***dst_tail, struct refspec *rs) |
54a8ad92 JH |
1297 | { |
1298 | int i, errs; | |
9fa2e5e8 BW |
1299 | for (i = errs = 0; i < rs->nr; i++) |
1300 | errs += match_explicit(src, dst, dst_tail, &rs->items[i]); | |
9a7bbd1d | 1301 | return errs; |
6b62816c DB |
1302 | } |
1303 | ||
f3acb830 BW |
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) | |
8558fd9e | 1307 | { |
0ad4a5ff | 1308 | const struct refspec_item *pat; |
db70a04c | 1309 | char *name; |
8558fd9e | 1310 | int i; |
a83619d6 | 1311 | int matching_refs = -1; |
f3acb830 BW |
1312 | for (i = 0; i < rs->nr; i++) { |
1313 | const struct refspec_item *item = &rs->items[i]; | |
c0192df6 JK |
1314 | |
1315 | if (item->negative) | |
1316 | continue; | |
1317 | ||
f3acb830 BW |
1318 | if (item->matching && |
1319 | (matching_refs == -1 || item->force)) { | |
a83619d6 PB |
1320 | matching_refs = i; |
1321 | continue; | |
1322 | } | |
1323 | ||
f3acb830 BW |
1324 | if (item->pattern) { |
1325 | const char *dst_side = item->dst ? item->dst : item->src; | |
6ddba5e2 FC |
1326 | int match; |
1327 | if (direction == FROM_SRC) | |
044b6f04 | 1328 | match = match_refname_with_pattern(item->src, ref->name, dst_side, &name); |
6ddba5e2 | 1329 | else |
044b6f04 | 1330 | match = match_refname_with_pattern(dst_side, ref->name, item->src, &name); |
6ddba5e2 | 1331 | if (match) { |
db70a04c FC |
1332 | matching_refs = i; |
1333 | break; | |
1334 | } | |
1335 | } | |
8558fd9e | 1336 | } |
db70a04c | 1337 | if (matching_refs == -1) |
a83619d6 | 1338 | return NULL; |
db70a04c | 1339 | |
f3acb830 | 1340 | pat = &rs->items[matching_refs]; |
db70a04c FC |
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 | */ | |
59556548 | 1347 | if (!send_mirror && !starts_with(ref->name, "refs/heads/")) |
db70a04c FC |
1348 | return NULL; |
1349 | name = xstrdup(ref->name); | |
1350 | } | |
1351 | if (ret_pat) | |
1352 | *ret_pat = pat; | |
1353 | return name; | |
8558fd9e DB |
1354 | } |
1355 | ||
6d2bf96e CB |
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 | ||
c2aba155 JH |
1364 | struct tips { |
1365 | struct commit **tip; | |
85ee0680 | 1366 | size_t nr, alloc; |
c2aba155 JH |
1367 | }; |
1368 | ||
fcd30b13 | 1369 | static void add_to_tips(struct tips *tips, const struct object_id *oid) |
c2aba155 JH |
1370 | { |
1371 | struct commit *commit; | |
1372 | ||
fcd30b13 | 1373 | if (is_null_oid(oid)) |
c2aba155 | 1374 | return; |
21e1ee8f | 1375 | commit = lookup_commit_reference_gently(the_repository, oid, 1); |
c2aba155 JH |
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 && | |
f4e54d02 | 1398 | !is_null_oid(&ref->peer_ref->new_oid)) |
fcd30b13 | 1399 | add_to_tips(&sent_tips, &ref->peer_ref->new_oid); |
c2aba155 | 1400 | else |
fcd30b13 | 1401 | add_to_tips(&sent_tips, &ref->old_oid); |
59556548 | 1402 | if (starts_with(ref->name, "refs/tags/")) |
c2aba155 JH |
1403 | string_list_append(&dst_tag, ref->name); |
1404 | } | |
1405 | clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK); | |
1406 | ||
3383e199 | 1407 | string_list_sort(&dst_tag); |
c2aba155 JH |
1408 | |
1409 | /* Collect tags they do not have. */ | |
1410 | for (ref = src; ref; ref = ref->next) { | |
59556548 | 1411 | if (!starts_with(ref->name, "refs/tags/")) |
c2aba155 JH |
1412 | continue; /* not a tag */ |
1413 | if (string_list_has_string(&dst_tag, ref->name)) | |
1414 | continue; /* they already have it */ | |
0df8e965 | 1415 | if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG) |
c2aba155 JH |
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) { | |
85daa01f DS |
1430 | const int reachable_flag = 1; |
1431 | struct commit_list *found_commits; | |
1432 | struct commit **src_commits; | |
85ee0680 | 1433 | size_t nr_src_commits = 0, alloc_src_commits = 16; |
85daa01f DS |
1434 | ALLOC_ARRAY(src_commits, alloc_src_commits); |
1435 | ||
c2aba155 JH |
1436 | for_each_string_list_item(item, &src_tag) { |
1437 | struct ref *ref = item->util; | |
85daa01f DS |
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) { | |
c2aba155 | 1458 | struct ref *dst_ref; |
85daa01f | 1459 | struct ref *ref = item->util; |
c2aba155 JH |
1460 | struct commit *commit; |
1461 | ||
f4e54d02 | 1462 | if (is_null_oid(&ref->new_oid)) |
c2aba155 | 1463 | continue; |
21e1ee8f SB |
1464 | commit = lookup_commit_reference_gently(the_repository, |
1465 | &ref->new_oid, | |
bc83266a | 1466 | 1); |
c2aba155 JH |
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 | */ | |
85daa01f | 1475 | if (!(commit->object.flags & reachable_flag)) |
c2aba155 JH |
1476 | continue; |
1477 | ||
1478 | /* Add it in */ | |
1479 | dst_ref = make_linked_ref(ref->name, dst_tail); | |
f4e54d02 | 1480 | oidcpy(&dst_ref->new_oid, &ref->new_oid); |
c2aba155 JH |
1481 | dst_ref->peer_ref = copy_ref(ref); |
1482 | } | |
85daa01f DS |
1483 | |
1484 | clear_commit_marks_many(nr_src_commits, src_commits, reachable_flag); | |
1485 | free(src_commits); | |
1486 | free_commit_list(found_commits); | |
c2aba155 | 1487 | } |
85daa01f | 1488 | |
c2aba155 JH |
1489 | string_list_clear(&src_tag, 0); |
1490 | free(sent_tips.tip); | |
1491 | } | |
1492 | ||
47a59185 JH |
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 | ||
f1bd15ab BC |
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 | ||
3383e199 | 1506 | string_list_sort(ref_index); |
f1bd15ab BC |
1507 | } |
1508 | ||
ba928c13 JK |
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 | */ | |
afb1aed4 | 1515 | int check_push_refs(struct ref *src, struct refspec *rs) |
ba928c13 | 1516 | { |
ba928c13 JK |
1517 | int ret = 0; |
1518 | int i; | |
1519 | ||
afb1aed4 BW |
1520 | for (i = 0; i < rs->nr; i++) { |
1521 | struct refspec_item *item = &rs->items[i]; | |
ba928c13 | 1522 | |
c0192df6 | 1523 | if (item->pattern || item->matching || item->negative) |
ba928c13 JK |
1524 | continue; |
1525 | ||
afb1aed4 | 1526 | ret |= match_explicit_lhs(src, item, NULL, NULL); |
ba928c13 JK |
1527 | } |
1528 | ||
ba928c13 JK |
1529 | return ret; |
1530 | } | |
1531 | ||
54a8ad92 | 1532 | /* |
29753cdd JH |
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). | |
54a8ad92 | 1539 | */ |
29753cdd | 1540 | int match_push_refs(struct ref *src, struct ref **dst, |
5c7ec846 | 1541 | struct refspec *rs, int flags) |
6b62816c | 1542 | { |
28b9d6e5 AW |
1543 | int send_all = flags & MATCH_REFS_ALL; |
1544 | int send_mirror = flags & MATCH_REFS_MIRROR; | |
6ddba5e2 | 1545 | int send_prune = flags & MATCH_REFS_PRUNE; |
5f48cb95 | 1546 | int errs; |
b1d8b1f3 | 1547 | struct ref *ref, **dst_tail = tail_ref(dst); |
f1bd15ab | 1548 | struct string_list dst_ref_index = STRING_LIST_INIT_NODUP; |
6b62816c | 1549 | |
5c7ec846 BW |
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); | |
6b62816c DB |
1555 | |
1556 | /* pick the remainder */ | |
b1d8b1f3 | 1557 | for (ref = src; ref; ref = ref->next) { |
f1bd15ab | 1558 | struct string_list_item *dst_item; |
6b62816c | 1559 | struct ref *dst_peer; |
0ad4a5ff | 1560 | const struct refspec_item *pat = NULL; |
6e66bf3c | 1561 | char *dst_name; |
db70a04c | 1562 | |
5c7ec846 | 1563 | dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat); |
db70a04c | 1564 | if (!dst_name) |
a83619d6 PB |
1565 | continue; |
1566 | ||
f1bd15ab BC |
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; | |
a83619d6 PB |
1572 | if (dst_peer) { |
1573 | if (dst_peer->peer_ref) | |
1574 | /* We're already sending something to this ref. */ | |
1575 | goto free_name; | |
a83619d6 PB |
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 | |
01689909 | 1581 | * --all or --mirror. |
a83619d6 PB |
1582 | */ |
1583 | goto free_name; | |
28b9d6e5 | 1584 | |
6b62816c | 1585 | /* Create a new one and link it */ |
6d2bf96e | 1586 | dst_peer = make_linked_ref(dst_name, &dst_tail); |
f4e54d02 | 1587 | oidcpy(&dst_peer->new_oid, &ref->new_oid); |
f1bd15ab BC |
1588 | string_list_insert(&dst_ref_index, |
1589 | dst_peer->name)->util = dst_peer; | |
6b62816c | 1590 | } |
b1d8b1f3 | 1591 | dst_peer->peer_ref = copy_ref(ref); |
a83619d6 | 1592 | dst_peer->force = pat->force; |
6e66bf3c AR |
1593 | free_name: |
1594 | free(dst_name); | |
6b62816c | 1595 | } |
c2aba155 | 1596 | |
f1bd15ab BC |
1597 | string_list_clear(&dst_ref_index, 0); |
1598 | ||
c2aba155 JH |
1599 | if (flags & MATCH_REFS_FOLLOW_TAGS) |
1600 | add_missing_tags(src, dst, &dst_tail); | |
1601 | ||
6ddba5e2 | 1602 | if (send_prune) { |
f1bd15ab | 1603 | struct string_list src_ref_index = STRING_LIST_INIT_NODUP; |
6ddba5e2 FC |
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 | ||
5c7ec846 | 1612 | src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL); |
6ddba5e2 | 1613 | if (src_name) { |
f1bd15ab BC |
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)) | |
6ddba5e2 FC |
1618 | ref->peer_ref = alloc_delete_ref(); |
1619 | free(src_name); | |
1620 | } | |
1621 | } | |
f1bd15ab | 1622 | string_list_clear(&src_ref_index, 0); |
6ddba5e2 | 1623 | } |
8ca69370 | 1624 | |
c0192df6 JK |
1625 | *dst = apply_negative_refspecs(*dst, rs); |
1626 | ||
5f48cb95 JS |
1627 | if (errs) |
1628 | return -1; | |
6b62816c DB |
1629 | return 0; |
1630 | } | |
cf818348 | 1631 | |
20e8b465 | 1632 | void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, |
631b5ef2 | 1633 | int force_update) |
20e8b465 TRC |
1634 | { |
1635 | struct ref *ref; | |
1636 | ||
1637 | for (ref = remote_refs; ref; ref = ref->next) { | |
8c5f6f71 | 1638 | int force_ref_update = ref->force || force_update; |
631b5ef2 | 1639 | int reject_reason = 0; |
8c5f6f71 | 1640 | |
20e8b465 | 1641 | if (ref->peer_ref) |
f4e54d02 | 1642 | oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); |
20e8b465 TRC |
1643 | else if (!send_mirror) |
1644 | continue; | |
1645 | ||
f4e54d02 | 1646 | ref->deletion = is_null_oid(&ref->new_oid); |
20e8b465 | 1647 | if (!ref->deletion && |
4a7e27e9 | 1648 | oideq(&ref->old_oid, &ref->new_oid)) { |
20e8b465 TRC |
1649 | ref->status = REF_STATUS_UPTODATE; |
1650 | continue; | |
1651 | } | |
1652 | ||
a272b289 | 1653 | /* |
b2e93f88 AW |
1654 | * If the remote ref has moved and is now different |
1655 | * from what we expect, reject any push. | |
631b5ef2 JH |
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. | |
99a1f9ae SK |
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. | |
631b5ef2 JH |
1665 | */ |
1666 | if (ref->expect_old_sha1) { | |
9001dc2a | 1667 | if (!oideq(&ref->old_oid, &ref->old_oid_expect)) |
631b5ef2 | 1668 | reject_reason = REF_STATUS_REJECT_STALE; |
99a1f9ae SK |
1669 | else if (ref->check_reachable && ref->unreachable) |
1670 | reject_reason = | |
1671 | REF_STATUS_REJECT_REMOTE_UPDATED; | |
b2e93f88 | 1672 | else |
99a1f9ae SK |
1673 | /* |
1674 | * If the ref isn't stale, and is reachable | |
abcb66c6 | 1675 | * from one of the reflog entries of |
99a1f9ae SK |
1676 | * the local branch, force the update. |
1677 | */ | |
b2e93f88 | 1678 | force_ref_update = 1; |
631b5ef2 JH |
1679 | } |
1680 | ||
1681 | /* | |
b2e93f88 AW |
1682 | * If the update isn't already rejected then check |
1683 | * the usual "must fast-forward" rules. | |
631b5ef2 | 1684 | * |
256b9d70 JH |
1685 | * Decide whether an individual refspec A:B can be |
1686 | * pushed. The push will succeed if any of the | |
1687 | * following are true: | |
20e8b465 | 1688 | * |
a272b289 | 1689 | * (1) the remote reference B does not exist |
20e8b465 | 1690 | * |
a272b289 CR |
1691 | * (2) the remote reference B is being removed (i.e., |
1692 | * pushing :B where no source is specified) | |
20e8b465 | 1693 | * |
256b9d70 JH |
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. | |
20e8b465 | 1697 | * |
a272b289 CR |
1698 | * (4) it is forced using the +A:B notation, or by |
1699 | * passing the --force argument | |
20e8b465 TRC |
1700 | */ |
1701 | ||
b2e93f88 | 1702 | if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) { |
59556548 | 1703 | if (starts_with(ref->name, "refs/tags/")) |
631b5ef2 | 1704 | reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS; |
062b914c | 1705 | else if (!has_object(the_repository, &ref->old_oid, HAS_OBJECT_RECHECK_PACKED)) |
631b5ef2 | 1706 | reject_reason = REF_STATUS_REJECT_FETCH_FIRST; |
21e1ee8f SB |
1707 | else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) || |
1708 | !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1)) | |
631b5ef2 | 1709 | reject_reason = REF_STATUS_REJECT_NEEDS_FORCE; |
6f3d57b6 | 1710 | else if (!ref_newer(&ref->new_oid, &ref->old_oid)) |
631b5ef2 | 1711 | reject_reason = REF_STATUS_REJECT_NONFASTFORWARD; |
20e8b465 | 1712 | } |
631b5ef2 JH |
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; | |
20e8b465 TRC |
1722 | } |
1723 | } | |
1724 | ||
56eed342 | 1725 | static void set_merge(struct remote_state *remote_state, struct branch *ret) |
05e73682 | 1726 | { |
9e3751d4 | 1727 | struct remote *remote; |
05e73682 | 1728 | char *ref; |
fcd30b13 | 1729 | struct object_id oid; |
05e73682 JH |
1730 | int i; |
1731 | ||
ee2499fe JK |
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 | ||
56eed342 | 1745 | remote = remotes_remote_get(remote_state, ret->remote_name); |
9e3751d4 | 1746 | |
ca56dadb | 1747 | CALLOC_ARRAY(ret->merge, ret->merge_nr); |
05e73682 JH |
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]); | |
9e3751d4 | 1751 | if (!remote_find_tracking(remote, ret->merge[i]) || |
05e73682 JH |
1752 | strcmp(ret->remote_name, ".")) |
1753 | continue; | |
12cb1c10 ÆAB |
1754 | if (repo_dwim_ref(the_repository, ret->merge_name[i], |
1755 | strlen(ret->merge_name[i]), &oid, &ref, | |
1756 | 0) == 1) | |
05e73682 JH |
1757 | ret->merge[i]->dst = ref; |
1758 | else | |
1759 | ret->merge[i]->dst = xstrdup(ret->merge_name[i]); | |
1760 | } | |
1761 | } | |
1762 | ||
cf818348 DB |
1763 | struct branch *branch_get(const char *name) |
1764 | { | |
1765 | struct branch *ret; | |
1766 | ||
3c8f60c6 | 1767 | read_config(the_repository, 0); |
cf818348 | 1768 | if (!name || !*name || !strcmp(name, "HEAD")) |
fd3cb050 | 1769 | ret = the_repository->remote_state->current_branch; |
cf818348 | 1770 | else |
085b98f6 GC |
1771 | ret = make_branch(the_repository->remote_state, name, |
1772 | strlen(name)); | |
56eed342 | 1773 | set_merge(the_repository->remote_state, ret); |
cf818348 DB |
1774 | return ret; |
1775 | } | |
1776 | ||
1777 | int branch_has_merge_config(struct branch *branch) | |
1778 | { | |
1779 | return branch && !!branch->merge; | |
1780 | } | |
1781 | ||
85682c19 SP |
1782 | int branch_merge_matches(struct branch *branch, |
1783 | int i, | |
1784 | const char *refname) | |
cf818348 | 1785 | { |
85682c19 | 1786 | if (!branch || i < 0 || i >= branch->merge_nr) |
cf818348 | 1787 | return 0; |
54457fe5 | 1788 | return refname_match(branch->merge[i]->src, refname); |
cf818348 | 1789 | } |
d71ab174 | 1790 | |
060e7766 | 1791 | __attribute__((format (printf,2,3))) |
3a429d0a JK |
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")); | |
1ca41a19 JK |
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 | */ | |
2e5c4758 | 1814 | if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) |
3a429d0a JK |
1815 | return error_buf(err, _("no such branch: '%s'"), |
1816 | branch->name); | |
1ca41a19 JK |
1817 | return error_buf(err, |
1818 | _("no upstream configured for branch '%s'"), | |
1819 | branch->name); | |
1820 | } | |
1821 | ||
1822 | if (!branch->merge[0]->dst) | |
3a429d0a JK |
1823 | return error_buf(err, |
1824 | _("upstream branch '%s' not stored as a remote-tracking branch"), | |
1825 | branch->merge[0]->src); | |
3a429d0a | 1826 | |
a9f9f8cc JK |
1827 | return branch->merge[0]->dst; |
1828 | } | |
1829 | ||
e291c75a JK |
1830 | static const char *tracking_for_push_dest(struct remote *remote, |
1831 | const char *refname, | |
1832 | struct strbuf *err) | |
1833 | { | |
1834 | char *ret; | |
1835 | ||
d000414e | 1836 | ret = apply_refspecs(&remote->fetch, refname); |
e291c75a JK |
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 | ||
56eed342 GC |
1844 | static const char *branch_get_push_1(struct remote_state *remote_state, |
1845 | struct branch *branch, struct strbuf *err) | |
e291c75a JK |
1846 | { |
1847 | struct remote *remote; | |
1848 | ||
56eed342 GC |
1849 | remote = remotes_remote_get( |
1850 | remote_state, | |
1851 | remotes_pushremote_for_branch(remote_state, branch, NULL)); | |
e291c75a JK |
1852 | if (!remote) |
1853 | return error_buf(err, | |
1854 | _("branch '%s' has no remote for pushing"), | |
1855 | branch->name); | |
1856 | ||
6bdb304b | 1857 | if (remote->push.nr) { |
e291c75a JK |
1858 | char *dst; |
1859 | const char *ret; | |
1860 | ||
d000414e | 1861 | dst = apply_refspecs(&remote->push, branch->refname); |
e291c75a JK |
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 | ||
033abf97 | 1904 | BUG("unhandled push situation"); |
e291c75a JK |
1905 | } |
1906 | ||
1907 | const char *branch_get_push(struct branch *branch, struct strbuf *err) | |
1908 | { | |
3c8f60c6 | 1909 | read_config(the_repository, 0); |
4a2dcb1a GC |
1910 | die_on_missing_branch(the_repository, branch); |
1911 | ||
b10731f4 KM |
1912 | if (!branch) |
1913 | return error_buf(err, _("HEAD does not point to a branch")); | |
1914 | ||
e291c75a | 1915 | if (!branch->push_tracking_ref) |
56eed342 GC |
1916 | branch->push_tracking_ref = branch_get_push_1( |
1917 | the_repository->remote_state, branch, err); | |
e291c75a JK |
1918 | return branch->push_tracking_ref; |
1919 | } | |
1920 | ||
1553f5e7 | 1921 | static int ignore_symref_update(const char *refname, struct strbuf *scratch) |
f8fb971e | 1922 | { |
1553f5e7 | 1923 | return !refs_read_symbolic_ref(get_main_ref_store(the_repository), refname, scratch); |
f8fb971e JH |
1924 | } |
1925 | ||
f166db26 MH |
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 | */ | |
4577370e | 1933 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
0ad4a5ff | 1934 | const struct refspec_item *refspec) |
d71ab174 | 1935 | { |
1553f5e7 | 1936 | struct strbuf scratch = STRBUF_INIT; |
4577370e | 1937 | const struct ref *ref; |
d71ab174 DB |
1938 | struct ref *ret = NULL; |
1939 | struct ref **tail = &ret; | |
1940 | ||
d71ab174 | 1941 | for (ref = remote_refs; ref; ref = ref->next) { |
e31a17f7 MH |
1942 | char *expn_name = NULL; |
1943 | ||
1553f5e7 PS |
1944 | strbuf_reset(&scratch); |
1945 | ||
d71ab174 DB |
1946 | if (strchr(ref->name, '^')) |
1947 | continue; /* a dereference item */ | |
044b6f04 | 1948 | if (match_refname_with_pattern(refspec->src, ref->name, |
f8fb971e | 1949 | refspec->dst, &expn_name) && |
1553f5e7 | 1950 | !ignore_symref_update(expn_name, &scratch)) { |
d71ab174 | 1951 | struct ref *cpy = copy_ref(ref); |
d71ab174 | 1952 | |
8960819e PS |
1953 | if (cpy->peer_ref) |
1954 | free_one_ref(cpy->peer_ref); | |
e928213f | 1955 | cpy->peer_ref = alloc_ref(expn_name); |
d71ab174 DB |
1956 | if (refspec->force) |
1957 | cpy->peer_ref->force = 1; | |
1958 | *tail = cpy; | |
1959 | tail = &cpy->next; | |
1960 | } | |
e31a17f7 | 1961 | free(expn_name); |
d71ab174 DB |
1962 | } |
1963 | ||
1553f5e7 | 1964 | strbuf_release(&scratch); |
d71ab174 DB |
1965 | return ret; |
1966 | } | |
1967 | ||
4577370e | 1968 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
d71ab174 | 1969 | { |
4577370e | 1970 | const struct ref *ref; |
60650a48 JH |
1971 | const struct ref *best_match = NULL; |
1972 | int best_score = 0; | |
1973 | ||
d71ab174 | 1974 | for (ref = refs; ref; ref = ref->next) { |
60650a48 JH |
1975 | int score = refname_match(name, ref->name); |
1976 | ||
1977 | if (best_score < score) { | |
1978 | best_match = ref; | |
1979 | best_score = score; | |
1980 | } | |
d71ab174 | 1981 | } |
60650a48 | 1982 | return best_match; |
d71ab174 DB |
1983 | } |
1984 | ||
4577370e | 1985 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
d71ab174 | 1986 | { |
4577370e | 1987 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
d71ab174 DB |
1988 | |
1989 | if (!ref) | |
9ad7c5ae | 1990 | return NULL; |
d71ab174 DB |
1991 | |
1992 | return copy_ref(ref); | |
1993 | } | |
1994 | ||
1995 | static struct ref *get_local_ref(const char *name) | |
1996 | { | |
3eb96997 | 1997 | if (!name || name[0] == '\0') |
d71ab174 DB |
1998 | return NULL; |
1999 | ||
59556548 | 2000 | if (starts_with(name, "refs/")) |
59c69c0c | 2001 | return alloc_ref(name); |
d71ab174 | 2002 | |
59556548 CC |
2003 | if (starts_with(name, "heads/") || |
2004 | starts_with(name, "tags/") || | |
2005 | starts_with(name, "remotes/")) | |
8009768e | 2006 | return alloc_ref_with_prefix("refs/", 5, name); |
d71ab174 | 2007 | |
8009768e | 2008 | return alloc_ref_with_prefix("refs/heads/", 11, name); |
d71ab174 DB |
2009 | } |
2010 | ||
4577370e | 2011 | int get_fetch_map(const struct ref *remote_refs, |
0ad4a5ff | 2012 | const struct refspec_item *refspec, |
9ad7c5ae JH |
2013 | struct ref ***tail, |
2014 | int missing_ok) | |
d71ab174 | 2015 | { |
ef00d150 | 2016 | struct ref *ref_map, **rmp; |
d71ab174 | 2017 | |
c0192df6 JK |
2018 | if (refspec->negative) |
2019 | return 0; | |
2020 | ||
d71ab174 DB |
2021 | if (refspec->pattern) { |
2022 | ref_map = get_expanded_map(remote_refs, refspec); | |
2023 | } else { | |
9ad7c5ae JH |
2024 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
2025 | ||
6e7b66ee JH |
2026 | if (refspec->exact_sha1) { |
2027 | ref_map = alloc_ref(name); | |
f4e54d02 | 2028 | get_oid_hex(name, &ref_map->old_oid); |
73302051 | 2029 | ref_map->exact_oid = 1; |
6e7b66ee JH |
2030 | } else { |
2031 | ref_map = get_remote_ref(remote_refs, name); | |
2032 | } | |
9ad7c5ae | 2033 | if (!missing_ok && !ref_map) |
0b9c3afd | 2034 | die(_("couldn't find remote ref %s"), name); |
9ad7c5ae JH |
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 | } | |
d71ab174 DB |
2040 | } |
2041 | ||
ef00d150 DB |
2042 | for (rmp = &ref_map; *rmp; ) { |
2043 | if ((*rmp)->peer_ref) { | |
59556548 | 2044 | if (!starts_with((*rmp)->peer_ref->name, "refs/") || |
5c08c1f2 | 2045 | check_refname_format((*rmp)->peer_ref->name, 0)) { |
ef00d150 | 2046 | struct ref *ignore = *rmp; |
0b9c3afd | 2047 | error(_("* Ignoring funny ref '%s' locally"), |
ef00d150 DB |
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); | |
d71ab174 DB |
2056 | } |
2057 | ||
8f70a765 AR |
2058 | if (ref_map) |
2059 | tail_link_ref(ref_map, tail); | |
d71ab174 DB |
2060 | |
2061 | return 0; | |
2062 | } | |
be885d96 DB |
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)) { | |
f4e54d02 | 2070 | oidcpy(&ref->old_oid, &list->old_oid); |
be885d96 DB |
2071 | return 0; |
2072 | } | |
2073 | return 1; | |
2074 | } | |
6d21bf96 JH |
2075 | |
2076 | /* | |
c646d093 | 2077 | * Compute the commit ahead/behind values for the pair branch_name, base. |
d7d1b496 JH |
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 | * | |
c646d093 DR |
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. | |
6d21bf96 | 2087 | */ |
c646d093 DR |
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) | |
6d21bf96 | 2092 | { |
fcd30b13 | 2093 | struct object_id oid; |
6d21bf96 | 2094 | struct commit *ours, *theirs; |
6d21bf96 | 2095 | struct rev_info revs; |
f92dbdbc ÆAB |
2096 | struct setup_revision_opt opt = { |
2097 | .free_removed_argv_elements = 1, | |
2098 | }; | |
c972bf4c | 2099 | struct strvec argv = STRVEC_INIT; |
6d21bf96 | 2100 | |
f2e08739 | 2101 | /* Cannot stat if what we used to build on no longer exists */ |
2e5c4758 | 2102 | if (refs_read_ref(get_main_ref_store(the_repository), base, &oid)) |
f2e08739 | 2103 | return -1; |
2122f675 | 2104 | theirs = lookup_commit_reference(the_repository, &oid); |
6d21bf96 | 2105 | if (!theirs) |
f2e08739 | 2106 | return -1; |
6d21bf96 | 2107 | |
2e5c4758 | 2108 | if (refs_read_ref(get_main_ref_store(the_repository), branch_name, &oid)) |
f2e08739 | 2109 | return -1; |
2122f675 | 2110 | ours = lookup_commit_reference(the_repository, &oid); |
6d21bf96 | 2111 | if (!ours) |
f2e08739 | 2112 | return -1; |
6d21bf96 | 2113 | |
d7d1b496 JH |
2114 | *num_theirs = *num_ours = 0; |
2115 | ||
6d21bf96 | 2116 | /* are we the same? */ |
d7d1b496 | 2117 | if (theirs == ours) |
979cb245 | 2118 | return 0; |
d7d1b496 JH |
2119 | if (abf == AHEAD_BEHIND_QUICK) |
2120 | return 1; | |
fd9b544a | 2121 | if (abf != AHEAD_BEHIND_FULL) |
c646d093 | 2122 | BUG("stat_branch_pair: invalid abf '%d'", abf); |
6d21bf96 | 2123 | |
8fbf879e | 2124 | /* Run "rev-list --left-right ours...theirs" internally... */ |
c972bf4c JK |
2125 | strvec_push(&argv, ""); /* ignored */ |
2126 | strvec_push(&argv, "--left-right"); | |
2127 | strvec_pushf(&argv, "%s...%s", | |
f6d8942b JK |
2128 | oid_to_hex(&ours->object.oid), |
2129 | oid_to_hex(&theirs->object.oid)); | |
c972bf4c | 2130 | strvec_push(&argv, "--"); |
6d21bf96 | 2131 | |
2abf3503 | 2132 | repo_init_revisions(the_repository, &revs, NULL); |
f92dbdbc | 2133 | setup_revisions(argv.nr, argv.v, &revs, &opt); |
81c3ce3c | 2134 | if (prepare_revision_walk(&revs)) |
0b9c3afd | 2135 | die(_("revision walk setup failed")); |
6d21bf96 JH |
2136 | |
2137 | /* ... and count the commits on each side. */ | |
6d21bf96 JH |
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 | } | |
c0234b2e JH |
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); | |
0b282cc4 | 2151 | |
c972bf4c | 2152 | strvec_clear(&argv); |
2108fe4a | 2153 | release_revisions(&revs); |
d7d1b496 | 2154 | return 1; |
6d21bf96 JH |
2155 | } |
2156 | ||
c646d093 DR |
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 | ||
6d21bf96 JH |
2193 | /* |
2194 | * Return true when there is anything to report, otherwise false. | |
2195 | */ | |
f39a757d | 2196 | int format_tracking_info(struct branch *branch, struct strbuf *sb, |
b6f3da51 AH |
2197 | enum ahead_behind_flags abf, |
2198 | int show_divergence_advice) | |
6d21bf96 | 2199 | { |
f39a757d | 2200 | int ours, theirs, sti; |
979cb245 | 2201 | const char *full_base; |
2f50babe | 2202 | char *base; |
f2e08739 | 2203 | int upstream_is_gone = 0; |
6d21bf96 | 2204 | |
c646d093 | 2205 | sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf); |
f39a757d | 2206 | if (sti < 0) { |
979cb245 JK |
2207 | if (!full_base) |
2208 | return 0; | |
f2e08739 | 2209 | upstream_is_gone = 1; |
f2e08739 | 2210 | } |
6d21bf96 | 2211 | |
2e5c4758 PS |
2212 | base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), |
2213 | full_base, 0); | |
f2e08739 JX |
2214 | if (upstream_is_gone) { |
2215 | strbuf_addf(sb, | |
2216 | _("Your branch is based on '%s', but the upstream is gone.\n"), | |
2217 | base); | |
ed9bff08 | 2218 | if (advice_enabled(ADVICE_STATUS_HINTS)) |
a22ae753 | 2219 | strbuf_addstr(sb, |
f2e08739 | 2220 | _(" (use \"git branch --unset-upstream\" to fixup)\n")); |
f39a757d | 2221 | } else if (!sti) { |
f223459b | 2222 | strbuf_addf(sb, |
7560f547 | 2223 | _("Your branch is up to date with '%s'.\n"), |
f223459b | 2224 | base); |
f39a757d JH |
2225 | } else if (abf == AHEAD_BEHIND_QUICK) { |
2226 | strbuf_addf(sb, | |
2227 | _("Your branch and '%s' refer to different commits.\n"), | |
2228 | base); | |
ed9bff08 | 2229 | if (advice_enabled(ADVICE_STATUS_HINTS)) |
f39a757d JH |
2230 | strbuf_addf(sb, _(" (use \"%s\" for details)\n"), |
2231 | "git status --ahead-behind"); | |
f2e08739 | 2232 | } else if (!theirs) { |
8a5b7494 JX |
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", | |
f2e08739 JX |
2236 | ours), |
2237 | base, ours); | |
ed9bff08 | 2238 | if (advice_enabled(ADVICE_STATUS_HINTS)) |
a22ae753 | 2239 | strbuf_addstr(sb, |
491e3075 | 2240 | _(" (use \"git push\" to publish your local commits)\n")); |
f2e08739 | 2241 | } else if (!ours) { |
8a5b7494 JX |
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", | |
f2e08739 JX |
2247 | theirs), |
2248 | base, theirs); | |
ed9bff08 | 2249 | if (advice_enabled(ADVICE_STATUS_HINTS)) |
a22ae753 | 2250 | strbuf_addstr(sb, |
491e3075 | 2251 | _(" (use \"git pull\" to update your local branch)\n")); |
c190ced6 | 2252 | } else { |
8a5b7494 JX |
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", | |
f54bea44 | 2260 | ours + theirs), |
f2e08739 | 2261 | base, ours, theirs); |
b6f3da51 AH |
2262 | if (show_divergence_advice && |
2263 | advice_enabled(ADVICE_STATUS_HINTS)) | |
a22ae753 | 2264 | strbuf_addstr(sb, |
d92304ff | 2265 | _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n")); |
c190ced6 | 2266 | } |
2f50babe | 2267 | free(base); |
6d21bf96 JH |
2268 | return 1; |
2269 | } | |
454e2025 | 2270 | |
e8207717 | 2271 | static int one_local_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid, |
5cf88fd8 | 2272 | int flag UNUSED, |
63e14ee2 | 2273 | void *cb_data) |
454e2025 JS |
2274 | { |
2275 | struct ref ***local_tail = cb_data; | |
2276 | struct ref *ref; | |
454e2025 JS |
2277 | |
2278 | /* we already know it starts with refs/ to get here */ | |
8d9c5010 | 2279 | if (check_refname_format(refname + 5, 0)) |
454e2025 JS |
2280 | return 0; |
2281 | ||
96ffc06f | 2282 | ref = alloc_ref(refname); |
f4e54d02 | 2283 | oidcpy(&ref->new_oid, oid); |
454e2025 JS |
2284 | **local_tail = ref; |
2285 | *local_tail = &ref->next; | |
2286 | return 0; | |
2287 | } | |
2288 | ||
2289 | struct ref *get_local_heads(void) | |
2290 | { | |
55f0566f | 2291 | struct ref *local_refs = NULL, **local_tail = &local_refs; |
2b2a5be3 | 2292 | |
2e5c4758 PS |
2293 | refs_for_each_ref(get_main_ref_store(the_repository), one_local_ref, |
2294 | &local_tail); | |
454e2025 JS |
2295 | return local_refs; |
2296 | } | |
8ef51733 | 2297 | |
4229f1fa JS |
2298 | struct ref *guess_remote_head(const struct ref *head, |
2299 | const struct ref *refs, | |
d5d284df | 2300 | unsigned flags) |
8ef51733 | 2301 | { |
8ef51733 | 2302 | const struct ref *r; |
4229f1fa JS |
2303 | struct ref *list = NULL; |
2304 | struct ref **tail = &list; | |
8ef51733 | 2305 | |
6cb4e6cc | 2306 | if (!head) |
8ef51733 JS |
2307 | return NULL; |
2308 | ||
fbb074c2 JK |
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 | ||
a471214b | 2317 | /* If a remote branch exists with the default branch name, let's use it. */ |
d5d284df JT |
2318 | if (!(flags & REMOTE_GUESS_HEAD_ALL)) { |
2319 | char *default_branch = | |
2320 | repo_default_branch_name(the_repository, | |
2321 | flags & REMOTE_GUESS_HEAD_QUIET); | |
97abaab5 | 2322 | char *ref = xstrfmt("refs/heads/%s", default_branch); |
a471214b JS |
2323 | |
2324 | r = find_ref_by_name(refs, ref); | |
2325 | free(ref); | |
97abaab5 PS |
2326 | free(default_branch); |
2327 | ||
a471214b JS |
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 */ | |
4229f1fa | 2332 | r = find_ref_by_name(refs, "refs/heads/master"); |
4a7e27e9 | 2333 | if (r && oideq(&r->old_oid, &head->old_oid)) |
4229f1fa JS |
2334 | return copy_ref(r); |
2335 | } | |
8ef51733 JS |
2336 | |
2337 | /* Look for another ref that points there */ | |
4229f1fa | 2338 | for (r = refs; r; r = r->next) { |
61adfd30 | 2339 | if (r != head && |
59556548 | 2340 | starts_with(r->name, "refs/heads/") && |
4a7e27e9 | 2341 | oideq(&r->old_oid, &head->old_oid)) { |
4229f1fa JS |
2342 | *tail = copy_ref(r); |
2343 | tail = &((*tail)->next); | |
d5d284df | 2344 | if (!(flags & REMOTE_GUESS_HEAD_ALL)) |
4229f1fa JS |
2345 | break; |
2346 | } | |
2347 | } | |
8ef51733 | 2348 | |
4229f1fa | 2349 | return list; |
8ef51733 | 2350 | } |
f2ef6075 JS |
2351 | |
2352 | struct stale_heads_info { | |
f2ef6075 JS |
2353 | struct string_list *ref_names; |
2354 | struct ref **stale_refs_tail; | |
a2ac50cb | 2355 | struct refspec *rs; |
f2ef6075 JS |
2356 | }; |
2357 | ||
e8207717 | 2358 | static int get_stale_heads_cb(const char *refname, const char *referent UNUSED, const struct object_id *oid, |
455ade65 | 2359 | int flags, void *cb_data) |
f2ef6075 JS |
2360 | { |
2361 | struct stale_heads_info *info = cb_data; | |
e6f63712 | 2362 | struct string_list matches = STRING_LIST_INIT_DUP; |
0ad4a5ff | 2363 | struct refspec_item query; |
e6f63712 | 2364 | int i, stale = 1; |
0ad4a5ff | 2365 | memset(&query, 0, sizeof(struct refspec_item)); |
ed43de6e CMN |
2366 | query.dst = (char *)refname; |
2367 | ||
be0905fe | 2368 | refspec_find_all_matches(info->rs, &query, &matches); |
e6f63712 CMN |
2369 | if (matches.nr == 0) |
2370 | goto clean_exit; /* No matches */ | |
ed43de6e CMN |
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 | |
e6f63712 CMN |
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. | |
ed43de6e | 2378 | */ |
e6f63712 CMN |
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) { | |
ed43de6e | 2387 | struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail); |
f4e54d02 | 2388 | oidcpy(&ref->new_oid, oid); |
f2ef6075 | 2389 | } |
ed43de6e | 2390 | |
e6f63712 CMN |
2391 | clean_exit: |
2392 | string_list_clear(&matches, 0); | |
f2ef6075 JS |
2393 | return 0; |
2394 | } | |
2395 | ||
a2ac50cb | 2396 | struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map) |
f2ef6075 JS |
2397 | { |
2398 | struct ref *ref, *stale_refs = NULL; | |
183113a5 | 2399 | struct string_list ref_names = STRING_LIST_INIT_NODUP; |
f2ef6075 | 2400 | struct stale_heads_info info; |
2b2a5be3 | 2401 | |
f2ef6075 JS |
2402 | info.ref_names = &ref_names; |
2403 | info.stale_refs_tail = &stale_refs; | |
a2ac50cb | 2404 | info.rs = rs; |
f2ef6075 | 2405 | for (ref = fetch_map; ref; ref = ref->next) |
1d2f80fa | 2406 | string_list_append(&ref_names, ref->name); |
3383e199 | 2407 | string_list_sort(&ref_names); |
2e5c4758 PS |
2408 | refs_for_each_ref(get_main_ref_store(the_repository), |
2409 | get_stale_heads_cb, &info); | |
f2ef6075 JS |
2410 | string_list_clear(&ref_names, 0); |
2411 | return stale_refs; | |
2412 | } | |
28f5d176 JH |
2413 | |
2414 | /* | |
2415 | * Compare-and-swap | |
2416 | */ | |
05372c28 | 2417 | void clear_cas_option(struct push_cas_option *cas) |
28f5d176 JH |
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 | ||
8668976b | 2439 | static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset) |
28f5d176 JH |
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; | |
eee98e74 | 2461 | else if (!colon[1]) |
9da95bda | 2462 | oidclr(&entry->expect, the_repository->hash_algo); |
d850b7a5 | 2463 | else if (repo_get_oid(the_repository, colon + 1, &entry->expect)) |
0b9c3afd NTND |
2464 | return error(_("cannot parse expected object name '%s'"), |
2465 | colon + 1); | |
28f5d176 JH |
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 | } | |
91048a95 JH |
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 | |
99a1f9ae SK |
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[]. | |
91048a95 JH |
2484 | * If we cannot do so, return negative to signal an error. |
2485 | */ | |
2486 | static int remote_tracking(struct remote *remote, const char *refname, | |
99a1f9ae | 2487 | struct object_id *oid, char **dst_refname) |
91048a95 JH |
2488 | { |
2489 | char *dst; | |
2490 | ||
d000414e | 2491 | dst = apply_refspecs(&remote->fetch, refname); |
91048a95 JH |
2492 | if (!dst) |
2493 | return -1; /* no tracking ref for refname at remote */ | |
cdbb7208 PS |
2494 | if (refs_read_ref(get_main_ref_store(the_repository), dst, oid)) { |
2495 | free(dst); | |
91048a95 | 2496 | return -1; /* we know what the tracking ref is but we cannot read it */ |
cdbb7208 | 2497 | } |
99a1f9ae SK |
2498 | |
2499 | *dst_refname = dst; | |
91048a95 JH |
2500 | return 0; |
2501 | } | |
2502 | ||
99a1f9ae SK |
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 | ||
9865b6e6 | 2513 | #define REFLOG_COMMIT_ARRAY_INIT { 0 } |
99a1f9ae SK |
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. */ | |
5cf88fd8 ÆAB |
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) | |
99a1f9ae SK |
2542 | { |
2543 | timestamp_t *ts = cb_data; | |
2544 | *ts = timestamp; | |
2545 | return 1; | |
2546 | } | |
2547 | ||
5cf88fd8 | 2548 | static int check_and_collect_until(struct object_id *o_oid UNUSED, |
99a1f9ae | 2549 | struct object_id *n_oid, |
5cf88fd8 ÆAB |
2550 | const char *ident UNUSED, |
2551 | timestamp_t timestamp, int tz UNUSED, | |
2552 | const char *message UNUSED, void *cb_data) | |
99a1f9ae SK |
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 | */ | |
2e5c4758 PS |
2602 | refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), |
2603 | remote->tracking_ref, peek_reflog, | |
2604 | &date); | |
99a1f9ae SK |
2605 | |
2606 | cb.remote_commit = commit; | |
2607 | cb.local_commits = &arr; | |
2608 | cb.remote_reflog_timestamp = date; | |
2e5c4758 PS |
2609 | ret = refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), |
2610 | local, check_and_collect_until, | |
2611 | &cb); | |
99a1f9ae SK |
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 | ||
207c40e1 | 2626 | if ((ret = repo_in_merge_bases_many(the_repository, commit, size, chunk, 0))) |
99a1f9ae SK |
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; | |
42c153e1 | 2647 | free_one_ref(local); |
99a1f9ae SK |
2648 | } |
2649 | ||
91048a95 JH |
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]; | |
54457fe5 | 2659 | if (!refname_match(entry->refname, ref->name)) |
91048a95 JH |
2660 | continue; |
2661 | ref->expect_old_sha1 = 1; | |
2662 | if (!entry->use_tracking) | |
b8566f8f | 2663 | oidcpy(&ref->old_oid_expect, &entry->expect); |
99a1f9ae SK |
2664 | else if (remote_tracking(remote, ref->name, |
2665 | &ref->old_oid_expect, | |
2666 | &ref->tracking_ref)) | |
9da95bda | 2667 | oidclr(&ref->old_oid_expect, the_repository->hash_algo); |
99a1f9ae SK |
2668 | else |
2669 | ref->check_reachable = cas->use_force_if_includes; | |
91048a95 JH |
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; | |
99a1f9ae SK |
2678 | if (remote_tracking(remote, ref->name, |
2679 | &ref->old_oid_expect, | |
2680 | &ref->tracking_ref)) | |
9da95bda | 2681 | oidclr(&ref->old_oid_expect, the_repository->hash_algo); |
99a1f9ae SK |
2682 | else |
2683 | ref->check_reachable = cas->use_force_if_includes; | |
91048a95 JH |
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; | |
99a1f9ae | 2691 | for (ref = remote_refs; ref; ref = ref->next) { |
91048a95 | 2692 | apply_cas(cas, remote, ref); |
99a1f9ae SK |
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 | } | |
91048a95 | 2702 | } |
fd3cb050 GC |
2703 | |
2704 | struct remote_state *remote_state_new(void) | |
2705 | { | |
7525cd8c | 2706 | struct remote_state *r; |
fd3cb050 | 2707 | |
7525cd8c | 2708 | CALLOC_ARRAY(r, 1); |
fd3cb050 GC |
2709 | |
2710 | hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0); | |
4a2dcb1a | 2711 | hashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0); |
fd3cb050 GC |
2712 | return r; |
2713 | } | |
2714 | ||
2715 | void remote_state_clear(struct remote_state *remote_state) | |
2716 | { | |
f5ccb535 PS |
2717 | struct hashmap_iter iter; |
2718 | struct branch *b; | |
fd3cb050 GC |
2719 | int i; |
2720 | ||
338959da | 2721 | for (i = 0; i < remote_state->remotes_nr; i++) |
fd3cb050 | 2722 | remote_clear(remote_state->remotes[i]); |
fd3cb050 | 2723 | FREE_AND_NULL(remote_state->remotes); |
f5ccb535 | 2724 | FREE_AND_NULL(remote_state->pushremote_name); |
fd3cb050 GC |
2725 | remote_state->remotes_alloc = 0; |
2726 | remote_state->remotes_nr = 0; | |
2727 | ||
f5ccb535 PS |
2728 | rewrites_release(&remote_state->rewrites); |
2729 | rewrites_release(&remote_state->rewrites_push); | |
2730 | ||
fd3cb050 | 2731 | hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); |
f5ccb535 PS |
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); | |
fd3cb050 | 2737 | } |
1d04e719 DS |
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; | |
834e3520 | 2771 | char *remoteurl; |
1d04e719 | 2772 | struct strbuf sb = STRBUF_INIT; |
834e3520 DS |
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"); | |
1d04e719 | 2781 | |
834e3520 | 2782 | remoteurl = xstrdup(remote_url); |
1d04e719 DS |
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 | */ | |
c918f5c1 | 2806 | while (*url) { |
1d04e719 DS |
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 | } | |
f21ea69d MS |
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 | } |