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