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