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