blob: 682f2a01f949ce942597a9190cecfc991db0108e [file] [log] [blame]
Daniel Barkalow5751f492007-05-12 11:45:53 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -04003#include "remote.h"
4#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -07005#include "refspec.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Junio C Hamano6d21bf92008-07-02 00:51:18 -07007#include "commit.h"
8#include "diff.h"
9#include "revision.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +030010#include "dir.h"
Jay Soffianec8452d2009-02-25 03:32:12 -050011#include "tag.h"
Julian Phillips73cf0822009-10-25 21:28:11 +000012#include "string-list.h"
Jeff Kinged81c762012-05-21 18:19:28 -040013#include "mergesort.h"
Jeff King0b282cc2015-09-24 17:07:58 -040014#include "argv-array.h"
Derrick Stolee64043552018-07-20 16:33:04 +000015#include "commit-reach.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -040016
Felipe Contreras6ddba5e2012-02-23 00:43:41 +020017enum map_direction { FROM_SRC, FROM_DST };
18
Junio C Hamano844112c2008-02-24 22:25:04 -080019struct counted_string {
20 size_t len;
21 const char *s;
22};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050023struct rewrite {
24 const char *base;
Junio C Hamano844112c2008-02-24 22:25:04 -080025 size_t baselen;
26 struct counted_string *instead_of;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050027 int instead_of_nr;
28 int instead_of_alloc;
29};
Josh Triplettd071d942009-09-07 01:56:00 -070030struct rewrites {
31 struct rewrite **rewrite;
32 int rewrite_alloc;
33 int rewrite_nr;
34};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050035
Daniel Barkalow5751f492007-05-12 11:45:53 -040036static struct remote **remotes;
Daniel Barkalow2d313472008-02-18 23:41:41 -050037static int remotes_alloc;
38static int remotes_nr;
Patrick Reynoldsd0da0032014-07-29 14:43:39 +000039static struct hashmap remotes_hash;
Daniel Barkalow5751f492007-05-12 11:45:53 -040040
Daniel Barkalowcf818342007-09-10 23:02:56 -040041static struct branch **branches;
Daniel Barkalow2d313472008-02-18 23:41:41 -050042static int branches_alloc;
43static int branches_nr;
Daniel Barkalowcf818342007-09-10 23:02:56 -040044
45static struct branch *current_branch;
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +053046static const char *pushremote_name;
Daniel Barkalowcf818342007-09-10 23:02:56 -040047
Josh Triplettd071d942009-09-07 01:56:00 -070048static struct rewrites rewrites;
Josh Triplett1c2eafb2009-09-07 01:56:33 -070049static struct rewrites rewrites_push;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050050
Daniel Barkalow0a4da292009-11-18 02:42:23 +010051static int valid_remote(const struct remote *remote)
52{
Daniel Barkalowc578f512009-11-18 02:42:25 +010053 return (!!remote->url) || (!!remote->foreign_vcs);
Daniel Barkalow0a4da292009-11-18 02:42:23 +010054}
55
Josh Triplettd071d942009-09-07 01:56:00 -070056static const char *alias_url(const char *url, struct rewrites *r)
Daniel Barkalow55029ae2008-02-20 13:43:53 -050057{
58 int i, j;
Junio C Hamano844112c2008-02-24 22:25:04 -080059 struct counted_string *longest;
60 int longest_i;
61
62 longest = NULL;
63 longest_i = -1;
Josh Triplettd071d942009-09-07 01:56:00 -070064 for (i = 0; i < r->rewrite_nr; i++) {
65 if (!r->rewrite[i])
Daniel Barkalow55029ae2008-02-20 13:43:53 -050066 continue;
Josh Triplettd071d942009-09-07 01:56:00 -070067 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
Christian Couder59556542013-11-30 21:55:40 +010068 if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
Junio C Hamano844112c2008-02-24 22:25:04 -080069 (!longest ||
Josh Triplettd071d942009-09-07 01:56:00 -070070 longest->len < r->rewrite[i]->instead_of[j].len)) {
71 longest = &(r->rewrite[i]->instead_of[j]);
Junio C Hamano844112c2008-02-24 22:25:04 -080072 longest_i = i;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050073 }
74 }
75 }
Junio C Hamano844112c2008-02-24 22:25:04 -080076 if (!longest)
77 return url;
78
Jeff King75faa452015-09-24 17:07:03 -040079 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
Daniel Barkalow55029ae2008-02-20 13:43:53 -050080}
81
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040082static void add_url(struct remote *remote, const char *url)
Daniel Barkalow5751f492007-05-12 11:45:53 -040083{
Daniel Barkalow2d313472008-02-18 23:41:41 -050084 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
85 remote->url[remote->url_nr++] = url;
Daniel Barkalow5751f492007-05-12 11:45:53 -040086}
87
Michael J Gruber20346232009-06-09 18:01:34 +020088static void add_pushurl(struct remote *remote, const char *pushurl)
89{
90 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
91 remote->pushurl[remote->pushurl_nr++] = pushurl;
92}
93
Josh Triplett1c2eafb2009-09-07 01:56:33 -070094static void add_pushurl_alias(struct remote *remote, const char *url)
95{
96 const char *pushurl = alias_url(url, &rewrites_push);
97 if (pushurl != url)
98 add_pushurl(remote, pushurl);
99}
100
101static void add_url_alias(struct remote *remote, const char *url)
102{
103 add_url(remote, alias_url(url, &rewrites));
104 add_pushurl_alias(remote, url);
105}
106
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000107struct remotes_hash_key {
108 const char *str;
109 int len;
110};
111
Stefan Beller7663cdc2017-06-30 12:14:05 -0700112static int remotes_hash_cmp(const void *unused_cmp_data,
Stefan Beller45dcb352017-06-30 17:28:35 -0700113 const void *entry,
114 const void *entry_or_key,
115 const void *keydata)
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000116{
Stefan Beller45dcb352017-06-30 17:28:35 -0700117 const struct remote *a = entry;
118 const struct remote *b = entry_or_key;
119 const struct remotes_hash_key *key = keydata;
120
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000121 if (key)
122 return strncmp(a->name, key->str, key->len) || a->name[key->len];
123 else
124 return strcmp(a->name, b->name);
125}
126
127static inline void init_remotes_hash(void)
128{
129 if (!remotes_hash.cmpfn)
Stefan Beller45dcb352017-06-30 17:28:35 -0700130 hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000131}
132
Daniel Barkalow5751f492007-05-12 11:45:53 -0400133static struct remote *make_remote(const char *name, int len)
134{
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000135 struct remote *ret, *replaced;
136 struct remotes_hash_key lookup;
137 struct hashmap_entry lookup_entry;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400138
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000139 if (!len)
140 len = strlen(name);
141
142 init_remotes_hash();
143 lookup.str = name;
144 lookup.len = len;
145 hashmap_entry_init(&lookup_entry, memhash(name, len));
146
147 if ((ret = hashmap_get(&remotes_hash, &lookup_entry, &lookup)) != NULL)
148 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400149
Daniel Barkalow2d313472008-02-18 23:41:41 -0500150 ret = xcalloc(1, sizeof(struct remote));
Michael Schubert737c5a92013-07-13 11:36:24 +0200151 ret->prune = -1; /* unspecified */
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000152 ret->prune_tags = -1; /* unspecified */
Brandon Williams6bdb3042018-05-16 15:58:00 -0700153 ret->name = xstrndup(name, len);
154 refspec_init(&ret->push, REFSPEC_PUSH);
Brandon Williamse5349ab2018-05-16 15:58:01 -0700155 refspec_init(&ret->fetch, REFSPEC_FETCH);
Brandon Williams6bdb3042018-05-16 15:58:00 -0700156
Daniel Barkalow2d313472008-02-18 23:41:41 -0500157 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
158 remotes[remotes_nr++] = ret;
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000159
160 hashmap_entry_init(ret, lookup_entry.hash);
161 replaced = hashmap_put(&remotes_hash, ret);
162 assert(replaced == NULL); /* no previous entry overwritten */
Daniel Barkalow2d313472008-02-18 23:41:41 -0500163 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400164}
165
Daniel Barkalowcf818342007-09-10 23:02:56 -0400166static void add_merge(struct branch *branch, const char *name)
167{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500168 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
169 branch->merge_alloc);
170 branch->merge_name[branch->merge_nr++] = name;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400171}
172
173static struct branch *make_branch(const char *name, int len)
174{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500175 struct branch *ret;
176 int i;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400177
Daniel Barkalow2d313472008-02-18 23:41:41 -0500178 for (i = 0; i < branches_nr; i++) {
179 if (len ? (!strncmp(name, branches[i]->name, len) &&
180 !branches[i]->name[len]) :
181 !strcmp(name, branches[i]->name))
182 return branches[i];
Daniel Barkalowcf818342007-09-10 23:02:56 -0400183 }
184
Daniel Barkalow2d313472008-02-18 23:41:41 -0500185 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
186 ret = xcalloc(1, sizeof(struct branch));
187 branches[branches_nr++] = ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400188 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500189 ret->name = xstrndup(name, len);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400190 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500191 ret->name = xstrdup(name);
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400192 ret->refname = xstrfmt("refs/heads/%s", ret->name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400193
Daniel Barkalow2d313472008-02-18 23:41:41 -0500194 return ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400195}
196
Josh Triplettd071d942009-09-07 01:56:00 -0700197static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500198{
199 struct rewrite *ret;
200 int i;
201
Josh Triplettd071d942009-09-07 01:56:00 -0700202 for (i = 0; i < r->rewrite_nr; i++) {
Junio C Hamano844112c2008-02-24 22:25:04 -0800203 if (len
Josh Triplettd071d942009-09-07 01:56:00 -0700204 ? (len == r->rewrite[i]->baselen &&
205 !strncmp(base, r->rewrite[i]->base, len))
206 : !strcmp(base, r->rewrite[i]->base))
207 return r->rewrite[i];
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500208 }
209
Josh Triplettd071d942009-09-07 01:56:00 -0700210 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500211 ret = xcalloc(1, sizeof(struct rewrite));
Josh Triplettd071d942009-09-07 01:56:00 -0700212 r->rewrite[r->rewrite_nr++] = ret;
Junio C Hamano844112c2008-02-24 22:25:04 -0800213 if (len) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500214 ret->base = xstrndup(base, len);
Junio C Hamano844112c2008-02-24 22:25:04 -0800215 ret->baselen = len;
216 }
217 else {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500218 ret->base = xstrdup(base);
Junio C Hamano844112c2008-02-24 22:25:04 -0800219 ret->baselen = strlen(base);
220 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500221 return ret;
222}
223
224static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
225{
226 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
Junio C Hamano844112c2008-02-24 22:25:04 -0800227 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
228 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
229 rewrite->instead_of_nr++;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500230}
231
Jeff King0e265a92015-09-24 17:07:20 -0400232static const char *skip_spaces(const char *s)
233{
234 while (isspace(*s))
235 s++;
236 return s;
237}
238
Daniel Barkalow5751f492007-05-12 11:45:53 -0400239static void read_remotes_file(struct remote *remote)
240{
Jeff King0e265a92015-09-24 17:07:20 -0400241 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700242 FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
Daniel Barkalow5751f492007-05-12 11:45:53 -0400243
244 if (!f)
245 return;
Johannes Schindeline459b072017-01-19 22:20:02 +0100246 remote->configured_in_repo = 1;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100247 remote->origin = REMOTE_REMOTES;
Junio C Hamano18814d02015-10-28 13:27:33 -0700248 while (strbuf_getline(&buf, f) != EOF) {
Jeff King0e265a92015-09-24 17:07:20 -0400249 const char *v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400250
Jeff King0e265a92015-09-24 17:07:20 -0400251 strbuf_rtrim(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400252
Jeff King0e265a92015-09-24 17:07:20 -0400253 if (skip_prefix(buf.buf, "URL:", &v))
254 add_url_alias(remote, xstrdup(skip_spaces(v)));
255 else if (skip_prefix(buf.buf, "Push:", &v))
Brandon Williams6bdb3042018-05-16 15:58:00 -0700256 refspec_append(&remote->push, skip_spaces(v));
Jeff King0e265a92015-09-24 17:07:20 -0400257 else if (skip_prefix(buf.buf, "Pull:", &v))
Brandon Williamse5349ab2018-05-16 15:58:01 -0700258 refspec_append(&remote->fetch, skip_spaces(v));
Daniel Barkalow5751f492007-05-12 11:45:53 -0400259 }
Jeff King0e265a92015-09-24 17:07:20 -0400260 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400261 fclose(f);
262}
263
264static void read_branches_file(struct remote *remote)
265{
Daniel Barkalowcf818342007-09-10 23:02:56 -0400266 char *frag;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400267 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700268 FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
Daniel Barkalow5751f492007-05-12 11:45:53 -0400269
270 if (!f)
271 return;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400272
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800273 strbuf_getline_lf(&buf, f);
Johannes Sixt0fb19902015-10-23 08:02:51 +0200274 fclose(f);
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400275 strbuf_trim(&buf);
276 if (!buf.len) {
277 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400278 return;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400279 }
280
Johannes Schindeline459b072017-01-19 22:20:02 +0100281 remote->configured_in_repo = 1;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100282 remote->origin = REMOTE_BRANCHES;
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400283
284 /*
Ramkumar Ramachandra55cfde22013-06-22 13:28:12 +0530285 * The branches file would have URL and optionally
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400286 * #branch specified. The "master" (or specified) branch is
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400287 * fetched and stored in the local branch matching the
288 * remote name.
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400289 */
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400290 frag = strchr(buf.buf, '#');
291 if (frag)
Daniel Barkalowcf818342007-09-10 23:02:56 -0400292 *(frag++) = '\0';
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400293 else
294 frag = "master";
Ramkumar Ramachandra55cfde22013-06-22 13:28:12 +0530295
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400296 add_url_alias(remote, strbuf_detach(&buf, NULL));
Brandon Williamse5349ab2018-05-16 15:58:01 -0700297 strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
298 frag, remote->name);
299 refspec_append(&remote->fetch, buf.buf);
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400300
Martin Koegler18afe102008-11-10 22:47:11 +0100301 /*
302 * Cogito compatible push: push current HEAD to remote #branch
303 * (master if missing)
304 */
Brandon Williamse5349ab2018-05-16 15:58:01 -0700305 strbuf_reset(&buf);
Brandon Williams6bdb3042018-05-16 15:58:00 -0700306 strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
307 refspec_append(&remote->push, buf.buf);
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400308 remote->fetch_tags = 1; /* always auto-follow */
Brandon Williams6bdb3042018-05-16 15:58:00 -0700309 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400310}
311
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100312static int handle_config(const char *key, const char *value, void *cb)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400313{
314 const char *name;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100315 int namelen;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400316 const char *subkey;
317 struct remote *remote;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400318 struct branch *branch;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100319 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
320 if (!name)
Daniel Barkalowcf818342007-09-10 23:02:56 -0400321 return 0;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100322 branch = make_branch(name, namelen);
323 if (!strcmp(subkey, "remote")) {
Jeff Kinge41bf352015-05-01 18:44:41 -0400324 return git_config_string(&branch->remote_name, key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100325 } else if (!strcmp(subkey, "pushremote")) {
Jeff Kingda66b272015-05-21 00:45:20 -0400326 return git_config_string(&branch->pushremote_name, key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100327 } else if (!strcmp(subkey, "merge")) {
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800328 if (!value)
329 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400330 add_merge(branch, xstrdup(value));
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800331 }
Daniel Barkalowcf818342007-09-10 23:02:56 -0400332 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400333 }
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100334 if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500335 struct rewrite *rewrite;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100336 if (!name)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500337 return 0;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100338 if (!strcmp(subkey, "insteadof")) {
339 rewrite = make_rewrite(&rewrites, name, namelen);
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700340 if (!value)
341 return config_error_nonbool(key);
342 add_instead_of(rewrite, xstrdup(value));
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100343 } else if (!strcmp(subkey, "pushinsteadof")) {
344 rewrite = make_rewrite(&rewrites_push, name, namelen);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500345 if (!value)
346 return config_error_nonbool(key);
347 add_instead_of(rewrite, xstrdup(value));
348 }
349 }
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530350
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100351 if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400352 return 0;
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530353
354 /* Handle remote.* variables */
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100355 if (!name && !strcmp(subkey, "pushdefault"))
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530356 return git_config_string(&pushremote_name, key, value);
357
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100358 if (!name)
359 return 0;
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530360 /* Handle remote.<name>.* variables */
Brandon Caseyc82efaf2008-10-14 15:30:21 -0500361 if (*name == '/') {
362 warning("Config remote shorthand cannot begin with '/': %s",
363 name);
364 return 0;
365 }
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100366 remote = make_remote(name, namelen);
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100367 remote->origin = REMOTE_CONFIG;
Johannes Schindeline459b072017-01-19 22:20:02 +0100368 if (current_config_scope() == CONFIG_SCOPE_REPO)
369 remote->configured_in_repo = 1;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100370 if (!strcmp(subkey, "mirror"))
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200371 remote->mirror = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100372 else if (!strcmp(subkey, "skipdefaultupdate"))
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200373 remote->skip_default_update = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100374 else if (!strcmp(subkey, "skipfetchall"))
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100375 remote->skip_default_update = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100376 else if (!strcmp(subkey, "prune"))
Michael Schubert737c5a92013-07-13 11:36:24 +0200377 remote->prune = git_config_bool(key, value);
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000378 else if (!strcmp(subkey, "prunetags"))
379 remote->prune_tags = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100380 else if (!strcmp(subkey, "url")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200381 const char *v;
382 if (git_config_string(&v, key, value))
383 return -1;
384 add_url(remote, v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100385 } else if (!strcmp(subkey, "pushurl")) {
Michael J Gruber20346232009-06-09 18:01:34 +0200386 const char *v;
387 if (git_config_string(&v, key, value))
388 return -1;
389 add_pushurl(remote, v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100390 } else if (!strcmp(subkey, "push")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200391 const char *v;
392 if (git_config_string(&v, key, value))
393 return -1;
Brandon Williams6bdb3042018-05-16 15:58:00 -0700394 refspec_append(&remote->push, v);
395 free((char *)v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100396 } else if (!strcmp(subkey, "fetch")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200397 const char *v;
398 if (git_config_string(&v, key, value))
399 return -1;
Brandon Williamse5349ab2018-05-16 15:58:01 -0700400 refspec_append(&remote->fetch, v);
401 free((char *)v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100402 } else if (!strcmp(subkey, "receivepack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200403 const char *v;
404 if (git_config_string(&v, key, value))
405 return -1;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400406 if (!remote->receivepack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200407 remote->receivepack = v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400408 else
409 error("more than one receivepack given, using the first");
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100410 } else if (!strcmp(subkey, "uploadpack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200411 const char *v;
412 if (git_config_string(&v, key, value))
413 return -1;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400414 if (!remote->uploadpack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200415 remote->uploadpack = v;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400416 else
417 error("more than one uploadpack given, using the first");
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100418 } else if (!strcmp(subkey, "tagopt")) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400419 if (!strcmp(value, "--no-tags"))
420 remote->fetch_tags = -1;
Samuel Tardieu944163a2010-04-20 01:31:25 +0200421 else if (!strcmp(value, "--tags"))
422 remote->fetch_tags = 2;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100423 } else if (!strcmp(subkey, "proxy")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200424 return git_config_string((const char **)&remote->http_proxy,
425 key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100426 } else if (!strcmp(subkey, "proxyauthmethod")) {
Knut Frankeef976392016-01-26 13:02:47 +0000427 return git_config_string((const char **)&remote->http_proxy_authmethod,
428 key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100429 } else if (!strcmp(subkey, "vcs")) {
Daniel Barkalowc578f512009-11-18 02:42:25 +0100430 return git_config_string(&remote->foreign_vcs, key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200431 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400432 return 0;
433}
434
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500435static void alias_all_urls(void)
436{
437 int i, j;
438 for (i = 0; i < remotes_nr; i++) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700439 int add_pushurl_aliases;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500440 if (!remotes[i])
441 continue;
Michael J Gruber20346232009-06-09 18:01:34 +0200442 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
Josh Triplettd071d942009-09-07 01:56:00 -0700443 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
Michael J Gruber20346232009-06-09 18:01:34 +0200444 }
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700445 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
446 for (j = 0; j < remotes[i]->url_nr; j++) {
447 if (add_pushurl_aliases)
448 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
449 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
450 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500451 }
452}
453
Daniel Barkalow5751f492007-05-12 11:45:53 -0400454static void read_config(void)
455{
Jeff Kinge41bf352015-05-01 18:44:41 -0400456 static int loaded;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400457 int flag;
Jeff Kinge41bf352015-05-01 18:44:41 -0400458
459 if (loaded)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400460 return;
Jeff Kinge41bf352015-05-01 18:44:41 -0400461 loaded = 1;
462
Daniel Barkalow5751f492007-05-12 11:45:53 -0400463 current_branch = NULL;
Jeff Kingf2f12d12016-03-05 17:11:57 -0500464 if (startup_info->have_repository) {
René Scharfe744c0402017-09-23 11:45:04 +0200465 const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
Jeff Kingf2f12d12016-03-05 17:11:57 -0500466 if (head_ref && (flag & REF_ISSYMREF) &&
467 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
468 current_branch = make_branch(head_ref, 0);
469 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400470 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100471 git_config(handle_config, NULL);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500472 alias_all_urls();
Daniel Barkalow5751f492007-05-12 11:45:53 -0400473}
474
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500475static int valid_remote_nick(const char *name)
476{
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300477 if (!name[0] || is_dot_or_dotdot(name))
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500478 return 0;
Johannes Sixtd9244ec2017-05-25 14:00:13 +0200479
480 /* remote nicknames cannot contain slashes */
481 while (*name)
482 if (is_dir_sep(*name++))
483 return 0;
484 return 1;
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500485}
486
Jeff Kingf0521542015-05-21 00:45:16 -0400487const char *remote_for_branch(struct branch *branch, int *explicit)
488{
489 if (branch && branch->remote_name) {
490 if (explicit)
491 *explicit = 1;
492 return branch->remote_name;
493 }
494 if (explicit)
495 *explicit = 0;
496 return "origin";
497}
498
Jeff Kingda66b272015-05-21 00:45:20 -0400499const char *pushremote_for_branch(struct branch *branch, int *explicit)
500{
501 if (branch && branch->pushremote_name) {
502 if (explicit)
503 *explicit = 1;
504 return branch->pushremote_name;
505 }
506 if (pushremote_name) {
507 if (explicit)
508 *explicit = 1;
509 return pushremote_name;
510 }
511 return remote_for_branch(branch, explicit);
512}
513
J Wyman9700fae2017-11-07 17:31:08 +0100514const char *remote_ref_for_branch(struct branch *branch, int for_push,
515 int *explicit)
516{
517 if (branch) {
518 if (!for_push) {
519 if (branch->merge_nr) {
520 if (explicit)
521 *explicit = 1;
522 return branch->merge_name[0];
523 }
524 } else {
525 const char *dst, *remote_name =
526 pushremote_for_branch(branch, NULL);
527 struct remote *remote = remote_get(remote_name);
528
Brandon Williams6bdb3042018-05-16 15:58:00 -0700529 if (remote && remote->push.nr &&
Brandon Williamsd0004142018-05-16 15:58:11 -0700530 (dst = apply_refspecs(&remote->push,
J Wyman9700fae2017-11-07 17:31:08 +0100531 branch->refname))) {
532 if (explicit)
533 *explicit = 1;
534 return dst;
535 }
536 }
537 }
538 if (explicit)
539 *explicit = 0;
540 return "";
541}
542
Jeff Kingda66b272015-05-21 00:45:20 -0400543static struct remote *remote_get_1(const char *name,
544 const char *(*get_default)(struct branch *, int *))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400545{
546 struct remote *ret;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400547 int name_given = 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400548
Jeff King8770e6f2015-05-21 00:45:23 -0400549 read_config();
550
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400551 if (name)
552 name_given = 1;
Jeff Kingda66b272015-05-21 00:45:20 -0400553 else
554 name = get_default(current_branch, &name_given);
Junio C Hamano9326d492009-03-16 00:35:09 -0700555
Daniel Barkalow5751f492007-05-12 11:45:53 -0400556 ret = make_remote(name, 0);
Jeff King4539c212017-02-14 15:33:28 -0500557 if (valid_remote_nick(name) && have_git_dir()) {
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100558 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400559 read_remotes_file(ret);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100560 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400561 read_branches_file(ret);
562 }
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100563 if (name_given && !valid_remote(ret))
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500564 add_url_alias(ret, name);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100565 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400566 return NULL;
567 return ret;
568}
Daniel Barkalow6b628162007-05-12 11:45:59 -0400569
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530570struct remote *remote_get(const char *name)
571{
Jeff Kingda66b272015-05-21 00:45:20 -0400572 return remote_get_1(name, remote_for_branch);
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530573}
574
575struct remote *pushremote_get(const char *name)
576{
Jeff Kingda66b272015-05-21 00:45:20 -0400577 return remote_get_1(name, pushremote_for_branch);
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530578}
579
Johannes Schindeline459b072017-01-19 22:20:02 +0100580int remote_is_configured(struct remote *remote, int in_repo)
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200581{
Johannes Schindeline459b072017-01-19 22:20:02 +0100582 if (!remote)
583 return 0;
584 if (in_repo)
585 return remote->configured_in_repo;
586 return !!remote->origin;
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200587}
588
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100589int for_each_remote(each_remote_fn fn, void *priv)
590{
591 int i, result = 0;
592 read_config();
Daniel Barkalow2d313472008-02-18 23:41:41 -0500593 for (i = 0; i < remotes_nr && !result; i++) {
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100594 struct remote *r = remotes[i];
595 if (!r)
596 continue;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100597 result = fn(r, priv);
598 }
599 return result;
600}
601
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100602static void handle_duplicate(struct ref *ref1, struct ref *ref2)
603{
Michael Haggertyf096e6e2013-10-30 06:33:12 +0100604 if (strcmp(ref1->name, ref2->name)) {
605 if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
606 ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
607 die(_("Cannot fetch both %s and %s to %s"),
608 ref1->name, ref2->name, ref2->peer_ref->name);
609 } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
610 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
611 warning(_("%s usually tracks %s, not %s"),
612 ref2->peer_ref->name, ref2->name, ref1->name);
613 } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
614 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
615 die(_("%s tracks both %s and %s"),
616 ref2->peer_ref->name, ref1->name, ref2->name);
617 } else {
618 /*
619 * This last possibility doesn't occur because
620 * FETCH_HEAD_IGNORE entries always appear at
621 * the end of the list.
622 */
623 die(_("Internal error"));
624 }
625 }
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100626 free(ref2->peer_ref);
627 free(ref2);
628}
629
Michael Haggertyb9afe662013-10-30 06:33:09 +0100630struct ref *ref_remove_duplicates(struct ref *ref_map)
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400631{
Thiago Farina183113a2010-07-04 16:46:19 -0300632 struct string_list refs = STRING_LIST_INIT_NODUP;
Michael Haggertyb9afe662013-10-30 06:33:09 +0100633 struct ref *retval = NULL;
634 struct ref **p = &retval;
Julian Phillips73cf0822009-10-25 21:28:11 +0000635
Michael Haggertyb9afe662013-10-30 06:33:09 +0100636 while (ref_map) {
637 struct ref *ref = ref_map;
Julian Phillips73cf0822009-10-25 21:28:11 +0000638
Michael Haggertyb9afe662013-10-30 06:33:09 +0100639 ref_map = ref_map->next;
640 ref->next = NULL;
641
642 if (!ref->peer_ref) {
643 *p = ref;
644 p = &ref->next;
Michael Haggerty09ea1f82013-10-30 06:33:07 +0100645 } else {
Michael Haggertyb9afe662013-10-30 06:33:09 +0100646 struct string_list_item *item =
647 string_list_insert(&refs, ref->peer_ref->name);
648
649 if (item->util) {
650 /* Entry already existed */
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100651 handle_duplicate((struct ref *)item->util, ref);
Michael Haggertyb9afe662013-10-30 06:33:09 +0100652 } else {
653 *p = ref;
654 p = &ref->next;
655 item->util = ref;
656 }
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400657 }
658 }
Michael Haggertyb9afe662013-10-30 06:33:09 +0100659
Julian Phillips73cf0822009-10-25 21:28:11 +0000660 string_list_clear(&refs, 0);
Michael Haggertyb9afe662013-10-30 06:33:09 +0100661 return retval;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400662}
663
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400664int remote_has_url(struct remote *remote, const char *url)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400665{
666 int i;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400667 for (i = 0; i < remote->url_nr; i++) {
668 if (!strcmp(remote->url[i], url))
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400669 return 1;
670 }
671 return 0;
672}
673
Daniel Barkalowe9282132009-03-07 01:11:34 -0500674static int match_name_with_pattern(const char *key, const char *name,
675 const char *value, char **result)
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500676{
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500677 const char *kstar = strchr(key, '*');
678 size_t klen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500679 size_t ksuffixlen;
680 size_t namelen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500681 int ret;
682 if (!kstar)
683 die("Key '%s' of pattern had no '*'", key);
684 klen = kstar - key;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500685 ksuffixlen = strlen(kstar + 1);
686 namelen = strlen(name);
687 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
688 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500689 if (ret && value) {
René Scharfe07bfa572014-09-21 10:23:37 +0200690 struct strbuf sb = STRBUF_INIT;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500691 const char *vstar = strchr(value, '*');
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500692 if (!vstar)
693 die("Value '%s' of pattern has no '*'", value);
René Scharfe07bfa572014-09-21 10:23:37 +0200694 strbuf_add(&sb, value, vstar - value);
695 strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
696 strbuf_addstr(&sb, vstar + 1);
697 *result = strbuf_detach(&sb, NULL);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500698 }
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500699 return ret;
700}
701
Brandon Williamsa2ac50c2018-05-16 15:58:10 -0700702static void query_refspecs_multiple(struct refspec *rs,
703 struct refspec_item *query,
704 struct string_list *results)
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +0100705{
706 int i;
707 int find_src = !query->src;
708
709 if (find_src && !query->dst)
710 error("query_refspecs_multiple: need either src or dst");
711
Brandon Williamsa2ac50c2018-05-16 15:58:10 -0700712 for (i = 0; i < rs->nr; i++) {
713 struct refspec_item *refspec = &rs->items[i];
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +0100714 const char *key = find_src ? refspec->dst : refspec->src;
715 const char *value = find_src ? refspec->src : refspec->dst;
716 const char *needle = find_src ? query->dst : query->src;
717 char **result = find_src ? &query->src : &query->dst;
718
719 if (!refspec->dst)
720 continue;
721 if (refspec->pattern) {
722 if (match_name_with_pattern(key, needle, value, result))
723 string_list_append_nodup(results, *result);
724 } else if (!strcmp(needle, key)) {
725 string_list_append(results, value);
726 }
727 }
728}
729
Brandon Williams86baf822018-05-16 15:58:12 -0700730int query_refspecs(struct refspec *rs, struct refspec_item *query)
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100731{
732 int i;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200733 int find_src = !query->src;
Michael Haggerty049bff82013-10-30 06:33:01 +0100734 const char *needle = find_src ? query->dst : query->src;
735 char **result = find_src ? &query->src : &query->dst;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100736
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200737 if (find_src && !query->dst)
738 return error("query_refspecs: need either src or dst");
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100739
Brandon Williams86baf822018-05-16 15:58:12 -0700740 for (i = 0; i < rs->nr; i++) {
741 struct refspec_item *refspec = &rs->items[i];
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200742 const char *key = find_src ? refspec->dst : refspec->src;
743 const char *value = find_src ? refspec->src : refspec->dst;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200744
Shawn O. Pearce009c5bc2007-09-25 00:13:14 -0400745 if (!refspec->dst)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400746 continue;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200747 if (refspec->pattern) {
Daniel Barkalowe9282132009-03-07 01:11:34 -0500748 if (match_name_with_pattern(key, needle, value, result)) {
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200749 query->force = refspec->force;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400750 return 0;
751 }
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100752 } else if (!strcmp(needle, key)) {
753 *result = xstrdup(value);
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200754 query->force = refspec->force;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100755 return 0;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400756 }
757 }
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400758 return -1;
759}
760
Brandon Williamsd0004142018-05-16 15:58:11 -0700761char *apply_refspecs(struct refspec *rs, const char *name)
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200762{
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700763 struct refspec_item query;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200764
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700765 memset(&query, 0, sizeof(struct refspec_item));
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200766 query.src = (char *)name;
767
Brandon Williams86baf822018-05-16 15:58:12 -0700768 if (query_refspecs(rs, &query))
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200769 return NULL;
770
771 return query.dst;
772}
773
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700774int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200775{
Brandon Williams86baf822018-05-16 15:58:12 -0700776 return query_refspecs(&remote->fetch, refspec);
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200777}
778
René Scharfe80097682008-10-18 10:37:40 +0200779static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
780 const char *name)
781{
782 size_t len = strlen(name);
Jeff King50a6c8e2016-02-22 17:44:35 -0500783 struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
René Scharfe80097682008-10-18 10:37:40 +0200784 memcpy(ref->name, prefix, prefixlen);
785 memcpy(ref->name + prefixlen, name, len);
786 return ref;
787}
788
René Scharfe59c69c02008-10-18 10:44:18 +0200789struct ref *alloc_ref(const char *name)
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400790{
René Scharfe59c69c02008-10-18 10:44:18 +0200791 return alloc_ref_with_prefix("", 0, name);
Krzysztof Kowalczyk737922a2008-05-10 16:26:58 -0700792}
793
Jeff King59a57752011-06-07 19:03:03 -0400794struct ref *copy_ref(const struct ref *ref)
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400795{
Jay Soffian7b3db092009-02-27 14:10:04 -0500796 struct ref *cpy;
797 size_t len;
798 if (!ref)
799 return NULL;
Jeff King50a6c8e2016-02-22 17:44:35 -0500800 len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
801 cpy = xmalloc(len);
802 memcpy(cpy, ref, len);
Jay Soffian7b3db092009-02-27 14:10:04 -0500803 cpy->next = NULL;
Jeff King8c53f072015-01-12 20:59:09 -0500804 cpy->symref = xstrdup_or_null(ref->symref);
805 cpy->remote_status = xstrdup_or_null(ref->remote_status);
Jay Soffian7b3db092009-02-27 14:10:04 -0500806 cpy->peer_ref = copy_ref(ref->peer_ref);
807 return cpy;
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400808}
809
Daniel Barkalow45773702007-10-29 21:05:40 -0400810struct ref *copy_ref_list(const struct ref *ref)
811{
812 struct ref *ret = NULL;
813 struct ref **tail = &ret;
814 while (ref) {
815 *tail = copy_ref(ref);
816 ref = ref->next;
817 tail = &((*tail)->next);
818 }
819 return ret;
820}
821
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900822static void free_ref(struct ref *ref)
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400823{
824 if (!ref)
825 return;
Jay Soffian7b3db092009-02-27 14:10:04 -0500826 free_ref(ref->peer_ref);
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400827 free(ref->remote_status);
828 free(ref->symref);
829 free(ref);
830}
831
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400832void free_refs(struct ref *ref)
833{
834 struct ref *next;
835 while (ref) {
836 next = ref->next;
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400837 free_ref(ref);
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400838 ref = next;
839 }
840}
841
Jeff Kinged81c762012-05-21 18:19:28 -0400842int ref_compare_name(const void *va, const void *vb)
843{
844 const struct ref *a = va, *b = vb;
845 return strcmp(a->name, b->name);
846}
847
848static void *ref_list_get_next(const void *a)
849{
850 return ((const struct ref *)a)->next;
851}
852
853static void ref_list_set_next(void *a, void *next)
854{
855 ((struct ref *)a)->next = next;
856}
857
858void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
859{
860 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
861}
862
Junio C Hamanoca024652013-12-03 15:41:15 -0800863int count_refspec_match(const char *pattern,
864 struct ref *refs,
865 struct ref **matched_ref)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400866{
867 int patlen = strlen(pattern);
868 struct ref *matched_weak = NULL;
869 struct ref *matched = NULL;
870 int weak_match = 0;
871 int match = 0;
872
873 for (weak_match = match = 0; refs; refs = refs->next) {
874 char *name = refs->name;
875 int namelen = strlen(name);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400876
Michael Haggerty54457fe2014-01-14 04:16:07 +0100877 if (!refname_match(pattern, name))
Daniel Barkalow6b628162007-05-12 11:45:59 -0400878 continue;
879
880 /* A match is "weak" if it is with refs outside
881 * heads or tags, and did not specify the pattern
882 * in full (e.g. "refs/remotes/origin/master") or at
883 * least from the toplevel (e.g. "remotes/origin/master");
884 * otherwise "git push $URL master" would result in
885 * ambiguity between remotes/origin/master and heads/master
886 * at the remote site.
887 */
888 if (namelen != patlen &&
889 patlen != namelen - 5 &&
Christian Couder59556542013-11-30 21:55:40 +0100890 !starts_with(name, "refs/heads/") &&
891 !starts_with(name, "refs/tags/")) {
Daniel Barkalow6b628162007-05-12 11:45:59 -0400892 /* We want to catch the case where only weak
893 * matches are found and there are multiple
894 * matches, and where more than one strong
895 * matches are found, as ambiguous. One
896 * strong match with zero or more weak matches
897 * are acceptable as a unique match.
898 */
899 matched_weak = refs;
900 weak_match++;
901 }
902 else {
903 matched = refs;
904 match++;
905 }
906 }
907 if (!matched) {
Jeff King471fd3f2014-03-05 14:03:43 -0500908 if (matched_ref)
909 *matched_ref = matched_weak;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400910 return weak_match;
911 }
912 else {
Jeff King471fd3f2014-03-05 14:03:43 -0500913 if (matched_ref)
914 *matched_ref = matched;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400915 return match;
916 }
917}
918
Daniel Barkalow1d735262007-07-10 00:47:26 -0400919static void tail_link_ref(struct ref *ref, struct ref ***tail)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400920{
921 **tail = ref;
Daniel Barkalow1d735262007-07-10 00:47:26 -0400922 while (ref->next)
923 ref = ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400924 *tail = &ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400925}
926
Felipe Contreras67655242012-02-23 00:43:40 +0200927static struct ref *alloc_delete_ref(void)
928{
929 struct ref *ref = alloc_ref("(delete)");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000930 oidclr(&ref->new_oid);
Felipe Contreras67655242012-02-23 00:43:40 +0200931 return ref;
932}
933
Jeff King471fd3f2014-03-05 14:03:43 -0500934static int try_explicit_object_name(const char *name,
935 struct ref **match)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400936{
brian m. carlsonfcd30b12015-11-10 02:22:30 +0000937 struct object_id oid;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400938
Jeff King471fd3f2014-03-05 14:03:43 -0500939 if (!*name) {
940 if (match)
941 *match = alloc_delete_ref();
942 return 0;
943 }
944
brian m. carlsone82caf32017-07-13 23:49:28 +0000945 if (get_oid(name, &oid))
Jeff King471fd3f2014-03-05 14:03:43 -0500946 return -1;
947
948 if (match) {
949 *match = alloc_ref(name);
brian m. carlsonfcd30b12015-11-10 02:22:30 +0000950 oidcpy(&(*match)->new_oid, &oid);
Jeff King471fd3f2014-03-05 14:03:43 -0500951 }
952 return 0;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400953}
954
Daniel Barkalow1d735262007-07-10 00:47:26 -0400955static struct ref *make_linked_ref(const char *name, struct ref ***tail)
Junio C Hamano163f0ee2007-06-09 00:07:34 -0700956{
René Scharfe59c69c02008-10-18 10:44:18 +0200957 struct ref *ret = alloc_ref(name);
Daniel Barkalow1d735262007-07-10 00:47:26 -0400958 tail_link_ref(ret, tail);
959 return ret;
Junio C Hamano163f0ee2007-06-09 00:07:34 -0700960}
961
Jeff Kingf8aae122008-04-23 05:16:06 -0400962static char *guess_ref(const char *name, struct ref *peer)
963{
964 struct strbuf buf = STRBUF_INIT;
Jeff Kingf8aae122008-04-23 05:16:06 -0400965
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700966 const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
René Scharfe744c0402017-09-23 11:45:04 +0200967 NULL, NULL);
Jeff Kingf8aae122008-04-23 05:16:06 -0400968 if (!r)
969 return NULL;
970
Christian Couder59556542013-11-30 21:55:40 +0100971 if (starts_with(r, "refs/heads/"))
Jeff Kingf8aae122008-04-23 05:16:06 -0400972 strbuf_addstr(&buf, "refs/heads/");
Christian Couder59556542013-11-30 21:55:40 +0100973 else if (starts_with(r, "refs/tags/"))
Jeff Kingf8aae122008-04-23 05:16:06 -0400974 strbuf_addstr(&buf, "refs/tags/");
975 else
976 return NULL;
977
978 strbuf_addstr(&buf, name);
979 return strbuf_detach(&buf, NULL);
980}
981
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500982static int match_explicit_lhs(struct ref *src,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700983 struct refspec_item *rs,
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500984 struct ref **match,
985 int *allocated_match)
986{
987 switch (count_refspec_match(rs->src, src, match)) {
988 case 1:
Jeff King471fd3f2014-03-05 14:03:43 -0500989 if (allocated_match)
990 *allocated_match = 0;
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500991 return 0;
992 case 0:
993 /* The source could be in the get_sha1() format
994 * not a reference name. :refs/other is a
995 * way to delete 'other' ref at the remote end.
996 */
Jeff King471fd3f2014-03-05 14:03:43 -0500997 if (try_explicit_object_name(rs->src, match) < 0)
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500998 return error("src refspec %s does not match any.", rs->src);
Jeff King471fd3f2014-03-05 14:03:43 -0500999 if (allocated_match)
1000 *allocated_match = 1;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001001 return 0;
1002 default:
1003 return error("src refspec %s matches more than one.", rs->src);
1004 }
1005}
1006
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001007static int match_explicit(struct ref *src, struct ref *dst,
1008 struct ref ***dst_tail,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001009 struct refspec_item *rs)
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001010{
1011 struct ref *matched_src, *matched_dst;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001012 int allocated_src;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001013
1014 const char *dst_value = rs->dst;
Jeff Kingf8aae122008-04-23 05:16:06 -04001015 char *dst_guess;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001016
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001017 if (rs->pattern || rs->matching)
Jeff King9a7bbd12008-06-16 12:15:02 -04001018 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001019
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001020 matched_src = matched_dst = NULL;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001021 if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
1022 return -1;
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001023
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001024 if (!dst_value) {
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001025 int flag;
1026
Ronnie Sahlberg7695d112014-07-15 12:59:36 -07001027 dst_value = resolve_ref_unsafe(matched_src->name,
1028 RESOLVE_REF_READING,
René Scharfe744c0402017-09-23 11:45:04 +02001029 NULL, &flag);
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001030 if (!dst_value ||
1031 ((flag & REF_ISSYMREF) &&
Christian Couder59556542013-11-30 21:55:40 +01001032 !starts_with(dst_value, "refs/heads/")))
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001033 die("%s cannot be resolved to branch.",
1034 matched_src->name);
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001035 }
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001036
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001037 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1038 case 1:
1039 break;
1040 case 0:
René Scharfe50e19a82014-06-06 19:24:48 +02001041 if (starts_with(dst_value, "refs/"))
Daniel Barkalow1d735262007-07-10 00:47:26 -04001042 matched_dst = make_linked_ref(dst_value, dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001043 else if (is_null_oid(&matched_src->new_oid))
Jeff King5742c822012-07-03 14:04:39 -04001044 error("unable to delete '%s': remote ref does not exist",
1045 dst_value);
Johannes Schindelin3dc7ea92017-05-04 15:59:01 +02001046 else if ((dst_guess = guess_ref(dst_value, matched_src))) {
Jeff Kingf8aae122008-04-23 05:16:06 -04001047 matched_dst = make_linked_ref(dst_guess, dst_tail);
Johannes Schindelin3dc7ea92017-05-04 15:59:01 +02001048 free(dst_guess);
1049 } else
Jeff Kingf8aae122008-04-23 05:16:06 -04001050 error("unable to push to unqualified destination: %s\n"
1051 "The destination refspec neither matches an "
1052 "existing ref on the remote nor\n"
1053 "begins with refs/, and we are unable to "
1054 "guess a prefix based on the source ref.",
1055 dst_value);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001056 break;
1057 default:
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001058 matched_dst = NULL;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001059 error("dst refspec %s matches more than one.",
1060 dst_value);
1061 break;
1062 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001063 if (!matched_dst)
1064 return -1;
1065 if (matched_dst->peer_ref)
1066 return error("dst ref %s receives from more than one src.",
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001067 matched_dst->name);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001068 else {
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001069 matched_dst->peer_ref = allocated_src ?
1070 matched_src :
1071 copy_ref(matched_src);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001072 matched_dst->force = rs->force;
1073 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001074 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001075}
1076
Daniel Barkalow6b628162007-05-12 11:45:59 -04001077static int match_explicit_refs(struct ref *src, struct ref *dst,
Brandon Williams9fa2e5e2018-05-16 15:58:14 -07001078 struct ref ***dst_tail, struct refspec *rs)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001079{
1080 int i, errs;
Brandon Williams9fa2e5e2018-05-16 15:58:14 -07001081 for (i = errs = 0; i < rs->nr; i++)
1082 errs += match_explicit(src, dst, dst_tail, &rs->items[i]);
Jeff King9a7bbd12008-06-16 12:15:02 -04001083 return errs;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001084}
1085
Brandon Williamsf3acb832018-05-16 15:58:13 -07001086static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
1087 int send_mirror, int direction,
1088 const struct refspec_item **ret_pat)
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001089{
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001090 const struct refspec_item *pat;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001091 char *name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001092 int i;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001093 int matching_refs = -1;
Brandon Williamsf3acb832018-05-16 15:58:13 -07001094 for (i = 0; i < rs->nr; i++) {
1095 const struct refspec_item *item = &rs->items[i];
1096 if (item->matching &&
1097 (matching_refs == -1 || item->force)) {
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001098 matching_refs = i;
1099 continue;
1100 }
1101
Brandon Williamsf3acb832018-05-16 15:58:13 -07001102 if (item->pattern) {
1103 const char *dst_side = item->dst ? item->dst : item->src;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001104 int match;
1105 if (direction == FROM_SRC)
Brandon Williamsf3acb832018-05-16 15:58:13 -07001106 match = match_name_with_pattern(item->src, ref->name, dst_side, &name);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001107 else
Brandon Williamsf3acb832018-05-16 15:58:13 -07001108 match = match_name_with_pattern(dst_side, ref->name, item->src, &name);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001109 if (match) {
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001110 matching_refs = i;
1111 break;
1112 }
1113 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001114 }
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001115 if (matching_refs == -1)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001116 return NULL;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001117
Brandon Williamsf3acb832018-05-16 15:58:13 -07001118 pat = &rs->items[matching_refs];
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001119 if (pat->matching) {
1120 /*
1121 * "matching refs"; traditionally we pushed everything
1122 * including refs outside refs/heads/ hierarchy, but
1123 * that does not make much sense these days.
1124 */
Christian Couder59556542013-11-30 21:55:40 +01001125 if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001126 return NULL;
1127 name = xstrdup(ref->name);
1128 }
1129 if (ret_pat)
1130 *ret_pat = pat;
1131 return name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001132}
1133
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001134static struct ref **tail_ref(struct ref **head)
1135{
1136 struct ref **tail = head;
1137 while (*tail)
1138 tail = &((*tail)->next);
1139 return tail;
1140}
1141
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001142struct tips {
1143 struct commit **tip;
1144 int nr, alloc;
1145};
1146
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001147static void add_to_tips(struct tips *tips, const struct object_id *oid)
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001148{
1149 struct commit *commit;
1150
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001151 if (is_null_oid(oid))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001152 return;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001153 commit = lookup_commit_reference_gently(the_repository, oid, 1);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001154 if (!commit || (commit->object.flags & TMP_MARK))
1155 return;
1156 commit->object.flags |= TMP_MARK;
1157 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1158 tips->tip[tips->nr++] = commit;
1159}
1160
1161static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1162{
1163 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1164 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1165 struct string_list_item *item;
1166 struct ref *ref;
1167 struct tips sent_tips;
1168
1169 /*
1170 * Collect everything we know they would have at the end of
1171 * this push, and collect all tags they have.
1172 */
1173 memset(&sent_tips, 0, sizeof(sent_tips));
1174 for (ref = *dst; ref; ref = ref->next) {
1175 if (ref->peer_ref &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001176 !is_null_oid(&ref->peer_ref->new_oid))
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001177 add_to_tips(&sent_tips, &ref->peer_ref->new_oid);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001178 else
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001179 add_to_tips(&sent_tips, &ref->old_oid);
Christian Couder59556542013-11-30 21:55:40 +01001180 if (starts_with(ref->name, "refs/tags/"))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001181 string_list_append(&dst_tag, ref->name);
1182 }
1183 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1184
Michael Haggerty3383e192014-11-25 09:02:35 +01001185 string_list_sort(&dst_tag);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001186
1187 /* Collect tags they do not have. */
1188 for (ref = src; ref; ref = ref->next) {
Christian Couder59556542013-11-30 21:55:40 +01001189 if (!starts_with(ref->name, "refs/tags/"))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001190 continue; /* not a tag */
1191 if (string_list_has_string(&dst_tag, ref->name))
1192 continue; /* they already have it */
Stefan Beller0df8e962018-04-25 11:20:59 -07001193 if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG)
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001194 continue; /* be conservative */
1195 item = string_list_append(&src_tag, ref->name);
1196 item->util = ref;
1197 }
1198 string_list_clear(&dst_tag, 0);
1199
1200 /*
1201 * At this point, src_tag lists tags that are missing from
1202 * dst, and sent_tips lists the tips we are pushing or those
1203 * that we know they already have. An element in the src_tag
1204 * that is an ancestor of any of the sent_tips needs to be
1205 * sent to the other side.
1206 */
1207 if (sent_tips.nr) {
1208 for_each_string_list_item(item, &src_tag) {
1209 struct ref *ref = item->util;
1210 struct ref *dst_ref;
1211 struct commit *commit;
1212
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001213 if (is_null_oid(&ref->new_oid))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001214 continue;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001215 commit = lookup_commit_reference_gently(the_repository,
1216 &ref->new_oid,
brian m. carlsonbc832662017-05-06 22:10:10 +00001217 1);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001218 if (!commit)
1219 /* not pushing a commit, which is not an error */
1220 continue;
1221
1222 /*
1223 * Is this tag, which they do not have, reachable from
1224 * any of the commits we are sending?
1225 */
1226 if (!in_merge_bases_many(commit, sent_tips.nr, sent_tips.tip))
1227 continue;
1228
1229 /* Add it in */
1230 dst_ref = make_linked_ref(ref->name, dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001231 oidcpy(&dst_ref->new_oid, &ref->new_oid);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001232 dst_ref->peer_ref = copy_ref(ref);
1233 }
1234 }
1235 string_list_clear(&src_tag, 0);
1236 free(sent_tips.tip);
1237}
1238
Junio C Hamano47a59182013-07-08 13:56:53 -07001239struct ref *find_ref_by_name(const struct ref *list, const char *name)
1240{
1241 for ( ; list; list = list->next)
1242 if (!strcmp(list->name, name))
1243 return (struct ref *)list;
1244 return NULL;
1245}
1246
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001247static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
1248{
1249 for ( ; ref; ref = ref->next)
1250 string_list_append_nodup(ref_index, ref->name)->util = ref;
1251
Michael Haggerty3383e192014-11-25 09:02:35 +01001252 string_list_sort(ref_index);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001253}
1254
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001255/*
Jeff Kingba928c12014-03-05 14:04:54 -05001256 * Given only the set of local refs, sanity-check the set of push
1257 * refspecs. We can't catch all errors that match_push_refs would,
1258 * but we can catch some errors early before even talking to the
1259 * remote side.
1260 */
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001261int check_push_refs(struct ref *src, struct refspec *rs)
Jeff Kingba928c12014-03-05 14:04:54 -05001262{
Jeff Kingba928c12014-03-05 14:04:54 -05001263 int ret = 0;
1264 int i;
1265
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001266 for (i = 0; i < rs->nr; i++) {
1267 struct refspec_item *item = &rs->items[i];
Jeff Kingba928c12014-03-05 14:04:54 -05001268
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001269 if (item->pattern || item->matching)
Jeff Kingba928c12014-03-05 14:04:54 -05001270 continue;
1271
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001272 ret |= match_explicit_lhs(src, item, NULL, NULL);
Jeff Kingba928c12014-03-05 14:04:54 -05001273 }
1274
Jeff Kingba928c12014-03-05 14:04:54 -05001275 return ret;
1276}
1277
1278/*
Junio C Hamano29753cd2011-09-09 11:54:58 -07001279 * Given the set of refs the local repository has, the set of refs the
1280 * remote repository has, and the refspec used for push, determine
1281 * what remote refs we will update and with what value by setting
1282 * peer_ref (which object is being pushed) and force (if the push is
1283 * forced) in elements of "dst". The function may add new elements to
1284 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001285 */
Junio C Hamano29753cd2011-09-09 11:54:58 -07001286int match_push_refs(struct ref *src, struct ref **dst,
Brandon Williams5c7ec842018-05-16 15:58:21 -07001287 struct refspec *rs, int flags)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001288{
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001289 int send_all = flags & MATCH_REFS_ALL;
1290 int send_mirror = flags & MATCH_REFS_MIRROR;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001291 int send_prune = flags & MATCH_REFS_PRUNE;
Jay Soffian5f48cb92009-02-25 03:32:17 -05001292 int errs;
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001293 struct ref *ref, **dst_tail = tail_ref(dst);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001294 struct string_list dst_ref_index = STRING_LIST_INIT_NODUP;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001295
Brandon Williams5c7ec842018-05-16 15:58:21 -07001296 /* If no refspec is provided, use the default ":" */
1297 if (!rs->nr)
1298 refspec_append(rs, ":");
1299
1300 errs = match_explicit_refs(src, *dst, &dst_tail, rs);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001301
1302 /* pick the remainder */
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001303 for (ref = src; ref; ref = ref->next) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001304 struct string_list_item *dst_item;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001305 struct ref *dst_peer;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001306 const struct refspec_item *pat = NULL;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001307 char *dst_name;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001308
Brandon Williams5c7ec842018-05-16 15:58:21 -07001309 dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat);
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001310 if (!dst_name)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001311 continue;
1312
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001313 if (!dst_ref_index.nr)
1314 prepare_ref_index(&dst_ref_index, *dst);
1315
1316 dst_item = string_list_lookup(&dst_ref_index, dst_name);
1317 dst_peer = dst_item ? dst_item->util : NULL;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001318 if (dst_peer) {
1319 if (dst_peer->peer_ref)
1320 /* We're already sending something to this ref. */
1321 goto free_name;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001322 } else {
1323 if (pat->matching && !(send_all || send_mirror))
1324 /*
1325 * Remote doesn't have it, and we have no
1326 * explicit pattern, and we don't have
Justin Lebar01689902014-03-31 15:11:46 -07001327 * --all or --mirror.
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001328 */
1329 goto free_name;
1330
Daniel Barkalow6b628162007-05-12 11:45:59 -04001331 /* Create a new one and link it */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001332 dst_peer = make_linked_ref(dst_name, &dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001333 oidcpy(&dst_peer->new_oid, &ref->new_oid);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001334 string_list_insert(&dst_ref_index,
1335 dst_peer->name)->util = dst_peer;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001336 }
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001337 dst_peer->peer_ref = copy_ref(ref);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001338 dst_peer->force = pat->force;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001339 free_name:
1340 free(dst_name);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001341 }
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001342
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001343 string_list_clear(&dst_ref_index, 0);
1344
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001345 if (flags & MATCH_REFS_FOLLOW_TAGS)
1346 add_missing_tags(src, dst, &dst_tail);
1347
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001348 if (send_prune) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001349 struct string_list src_ref_index = STRING_LIST_INIT_NODUP;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001350 /* check for missing refs on the remote */
1351 for (ref = *dst; ref; ref = ref->next) {
1352 char *src_name;
1353
1354 if (ref->peer_ref)
1355 /* We're already sending something to this ref. */
1356 continue;
1357
Brandon Williams5c7ec842018-05-16 15:58:21 -07001358 src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001359 if (src_name) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001360 if (!src_ref_index.nr)
1361 prepare_ref_index(&src_ref_index, src);
1362 if (!string_list_has_string(&src_ref_index,
1363 src_name))
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001364 ref->peer_ref = alloc_delete_ref();
1365 free(src_name);
1366 }
1367 }
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001368 string_list_clear(&src_ref_index, 0);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001369 }
Brandon Williams8ca69372018-05-16 15:57:57 -07001370
Jay Soffian5f48cb92009-02-25 03:32:17 -05001371 if (errs)
1372 return -1;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001373 return 0;
1374}
Daniel Barkalowcf818342007-09-10 23:02:56 -04001375
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001376void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001377 int force_update)
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001378{
1379 struct ref *ref;
1380
1381 for (ref = remote_refs; ref; ref = ref->next) {
Chris Rorvick8c5f6f72012-11-29 19:41:36 -06001382 int force_ref_update = ref->force || force_update;
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001383 int reject_reason = 0;
Chris Rorvick8c5f6f72012-11-29 19:41:36 -06001384
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001385 if (ref->peer_ref)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001386 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001387 else if (!send_mirror)
1388 continue;
1389
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001390 ref->deletion = is_null_oid(&ref->new_oid);
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001391 if (!ref->deletion &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001392 oideq(&ref->old_oid, &ref->new_oid)) {
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001393 ref->status = REF_STATUS_UPTODATE;
1394 continue;
1395 }
1396
Chris Rorvicka272b282012-11-29 19:41:40 -06001397 /*
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001398 * If the remote ref has moved and is now different
1399 * from what we expect, reject any push.
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001400 *
1401 * It also is an error if the user told us to check
1402 * with the remote-tracking branch to find the value
1403 * to expect, but we did not have such a tracking
1404 * branch.
1405 */
1406 if (ref->expect_old_sha1) {
Jeff King9001dc22018-08-28 17:22:48 -04001407 if (!oideq(&ref->old_oid, &ref->old_oid_expect))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001408 reject_reason = REF_STATUS_REJECT_STALE;
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001409 else
1410 /* If the ref isn't stale then force the update. */
1411 force_ref_update = 1;
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001412 }
1413
1414 /*
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001415 * If the update isn't already rejected then check
1416 * the usual "must fast-forward" rules.
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001417 *
Junio C Hamano256b9d72013-01-16 13:02:27 -08001418 * Decide whether an individual refspec A:B can be
1419 * pushed. The push will succeed if any of the
1420 * following are true:
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001421 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001422 * (1) the remote reference B does not exist
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001423 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001424 * (2) the remote reference B is being removed (i.e.,
1425 * pushing :B where no source is specified)
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001426 *
Junio C Hamano256b9d72013-01-16 13:02:27 -08001427 * (3) the destination is not under refs/tags/, and
1428 * if the old and new value is a commit, the new
1429 * is a descendant of the old.
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001430 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001431 * (4) it is forced using the +A:B notation, or by
1432 * passing the --force argument
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001433 */
1434
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001435 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
Christian Couder59556542013-11-30 21:55:40 +01001436 if (starts_with(ref->name, "refs/tags/"))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001437 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001438 else if (!has_object_file(&ref->old_oid))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001439 reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001440 else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) ||
1441 !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001442 reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
brian m. carlson6f3d57b2015-11-10 02:22:25 +00001443 else if (!ref_newer(&ref->new_oid, &ref->old_oid))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001444 reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001445 }
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001446
1447 /*
1448 * "--force" will defeat any rejection implemented
1449 * by the rules above.
1450 */
1451 if (!force_ref_update)
1452 ref->status = reject_reason;
1453 else if (reject_reason)
1454 ref->forced_update = 1;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001455 }
1456}
1457
Junio C Hamano05e73682014-10-14 14:42:04 -07001458static void set_merge(struct branch *ret)
1459{
Jeff King9e3751d2015-05-21 00:45:13 -04001460 struct remote *remote;
Junio C Hamano05e73682014-10-14 14:42:04 -07001461 char *ref;
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001462 struct object_id oid;
Junio C Hamano05e73682014-10-14 14:42:04 -07001463 int i;
1464
Jeff Kingee2499f2015-05-21 00:45:09 -04001465 if (!ret)
1466 return; /* no branch */
1467 if (ret->merge)
1468 return; /* already run */
1469 if (!ret->remote_name || !ret->merge_nr) {
1470 /*
1471 * no merge config; let's make sure we don't confuse callers
1472 * with a non-zero merge_nr but a NULL merge
1473 */
1474 ret->merge_nr = 0;
1475 return;
1476 }
1477
Jeff King9e3751d2015-05-21 00:45:13 -04001478 remote = remote_get(ret->remote_name);
1479
Junio C Hamano05e73682014-10-14 14:42:04 -07001480 ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
1481 for (i = 0; i < ret->merge_nr; i++) {
1482 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1483 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
Jeff King9e3751d2015-05-21 00:45:13 -04001484 if (!remote_find_tracking(remote, ret->merge[i]) ||
Junio C Hamano05e73682014-10-14 14:42:04 -07001485 strcmp(ret->remote_name, "."))
1486 continue;
1487 if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
brian m. carlsoncca5fa62017-10-15 22:06:57 +00001488 &oid, &ref) == 1)
Junio C Hamano05e73682014-10-14 14:42:04 -07001489 ret->merge[i]->dst = ref;
1490 else
1491 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1492 }
1493}
1494
Daniel Barkalowcf818342007-09-10 23:02:56 -04001495struct branch *branch_get(const char *name)
1496{
1497 struct branch *ret;
1498
1499 read_config();
1500 if (!name || !*name || !strcmp(name, "HEAD"))
1501 ret = current_branch;
1502 else
1503 ret = make_branch(name, 0);
Jeff Kingee2499f2015-05-21 00:45:09 -04001504 set_merge(ret);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001505 return ret;
1506}
1507
1508int branch_has_merge_config(struct branch *branch)
1509{
1510 return branch && !!branch->merge;
1511}
1512
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001513int branch_merge_matches(struct branch *branch,
1514 int i,
1515 const char *refname)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001516{
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001517 if (!branch || i < 0 || i >= branch->merge_nr)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001518 return 0;
Michael Haggerty54457fe2014-01-14 04:16:07 +01001519 return refname_match(branch->merge[i]->src, refname);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001520}
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001521
Jeff King060e7762016-04-25 17:15:23 -04001522__attribute__((format (printf,2,3)))
Jeff King3a429d02015-05-21 00:45:32 -04001523static const char *error_buf(struct strbuf *err, const char *fmt, ...)
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001524{
Jeff King3a429d02015-05-21 00:45:32 -04001525 if (err) {
1526 va_list ap;
1527 va_start(ap, fmt);
1528 strbuf_vaddf(err, fmt, ap);
1529 va_end(ap);
1530 }
1531 return NULL;
1532}
1533
1534const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
1535{
1536 if (!branch)
1537 return error_buf(err, _("HEAD does not point to a branch"));
Jeff King1ca41a12015-05-21 20:46:43 -04001538
1539 if (!branch->merge || !branch->merge[0]) {
1540 /*
1541 * no merge config; is it because the user didn't define any,
1542 * or because it is not a real branch, and get_branch
1543 * auto-vivified it?
1544 */
Jeff King3a429d02015-05-21 00:45:32 -04001545 if (!ref_exists(branch->refname))
1546 return error_buf(err, _("no such branch: '%s'"),
1547 branch->name);
Jeff King1ca41a12015-05-21 20:46:43 -04001548 return error_buf(err,
1549 _("no upstream configured for branch '%s'"),
1550 branch->name);
1551 }
1552
1553 if (!branch->merge[0]->dst)
Jeff King3a429d02015-05-21 00:45:32 -04001554 return error_buf(err,
1555 _("upstream branch '%s' not stored as a remote-tracking branch"),
1556 branch->merge[0]->src);
Jeff King3a429d02015-05-21 00:45:32 -04001557
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001558 return branch->merge[0]->dst;
1559}
1560
Jeff Kinge291c752015-05-21 00:45:36 -04001561static const char *tracking_for_push_dest(struct remote *remote,
1562 const char *refname,
1563 struct strbuf *err)
1564{
1565 char *ret;
1566
Brandon Williamsd0004142018-05-16 15:58:11 -07001567 ret = apply_refspecs(&remote->fetch, refname);
Jeff Kinge291c752015-05-21 00:45:36 -04001568 if (!ret)
1569 return error_buf(err,
1570 _("push destination '%s' on remote '%s' has no local tracking branch"),
1571 refname, remote->name);
1572 return ret;
1573}
1574
1575static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1576{
1577 struct remote *remote;
1578
Jeff Kinge291c752015-05-21 00:45:36 -04001579 remote = remote_get(pushremote_for_branch(branch, NULL));
1580 if (!remote)
1581 return error_buf(err,
1582 _("branch '%s' has no remote for pushing"),
1583 branch->name);
1584
Brandon Williams6bdb3042018-05-16 15:58:00 -07001585 if (remote->push.nr) {
Jeff Kinge291c752015-05-21 00:45:36 -04001586 char *dst;
1587 const char *ret;
1588
Brandon Williamsd0004142018-05-16 15:58:11 -07001589 dst = apply_refspecs(&remote->push, branch->refname);
Jeff Kinge291c752015-05-21 00:45:36 -04001590 if (!dst)
1591 return error_buf(err,
1592 _("push refspecs for '%s' do not include '%s'"),
1593 remote->name, branch->name);
1594
1595 ret = tracking_for_push_dest(remote, dst, err);
1596 free(dst);
1597 return ret;
1598 }
1599
1600 if (remote->mirror)
1601 return tracking_for_push_dest(remote, branch->refname, err);
1602
1603 switch (push_default) {
1604 case PUSH_DEFAULT_NOTHING:
1605 return error_buf(err, _("push has no destination (push.default is 'nothing')"));
1606
1607 case PUSH_DEFAULT_MATCHING:
1608 case PUSH_DEFAULT_CURRENT:
1609 return tracking_for_push_dest(remote, branch->refname, err);
1610
1611 case PUSH_DEFAULT_UPSTREAM:
1612 return branch_get_upstream(branch, err);
1613
1614 case PUSH_DEFAULT_UNSPECIFIED:
1615 case PUSH_DEFAULT_SIMPLE:
1616 {
1617 const char *up, *cur;
1618
1619 up = branch_get_upstream(branch, err);
1620 if (!up)
1621 return NULL;
1622 cur = tracking_for_push_dest(remote, branch->refname, err);
1623 if (!cur)
1624 return NULL;
1625 if (strcmp(cur, up))
1626 return error_buf(err,
1627 _("cannot resolve 'simple' push to a single destination"));
1628 return cur;
1629 }
1630 }
1631
Johannes Schindelin033abf92018-05-02 11:38:39 +02001632 BUG("unhandled push situation");
Jeff Kinge291c752015-05-21 00:45:36 -04001633}
1634
1635const char *branch_get_push(struct branch *branch, struct strbuf *err)
1636{
Kyle Meyerb10731f2017-01-06 20:12:15 -05001637 if (!branch)
1638 return error_buf(err, _("HEAD does not point to a branch"));
1639
Jeff Kinge291c752015-05-21 00:45:36 -04001640 if (!branch->push_tracking_ref)
1641 branch->push_tracking_ref = branch_get_push_1(branch, err);
1642 return branch->push_tracking_ref;
1643}
1644
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001645static int ignore_symref_update(const char *refname)
1646{
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001647 int flag;
1648
René Scharfe744c0402017-09-23 11:45:04 +02001649 if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001650 return 0; /* non-existing refs are OK */
1651 return (flag & REF_ISSYMREF);
1652}
1653
Michael Haggertyf166db22013-10-30 06:32:56 +01001654/*
1655 * Create and return a list of (struct ref) consisting of copies of
1656 * each remote_ref that matches refspec. refspec must be a pattern.
1657 * Fill in the copies' peer_ref to describe the local tracking refs to
1658 * which they map. Omit any references that would map to an existing
1659 * local symbolic ref.
1660 */
Daniel Barkalow45773702007-10-29 21:05:40 -04001661static struct ref *get_expanded_map(const struct ref *remote_refs,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001662 const struct refspec_item *refspec)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001663{
Daniel Barkalow45773702007-10-29 21:05:40 -04001664 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001665 struct ref *ret = NULL;
1666 struct ref **tail = &ret;
1667
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001668 for (ref = remote_refs; ref; ref = ref->next) {
Michael Haggertye31a17f2013-10-30 06:32:57 +01001669 char *expn_name = NULL;
1670
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001671 if (strchr(ref->name, '^'))
1672 continue; /* a dereference item */
Daniel Barkalowe9282132009-03-07 01:11:34 -05001673 if (match_name_with_pattern(refspec->src, ref->name,
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001674 refspec->dst, &expn_name) &&
1675 !ignore_symref_update(expn_name)) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001676 struct ref *cpy = copy_ref(ref);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001677
Daniel Barkalowe9282132009-03-07 01:11:34 -05001678 cpy->peer_ref = alloc_ref(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001679 if (refspec->force)
1680 cpy->peer_ref->force = 1;
1681 *tail = cpy;
1682 tail = &cpy->next;
1683 }
Michael Haggertye31a17f2013-10-30 06:32:57 +01001684 free(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001685 }
1686
1687 return ret;
1688}
1689
Daniel Barkalow45773702007-10-29 21:05:40 -04001690static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001691{
Daniel Barkalow45773702007-10-29 21:05:40 -04001692 const struct ref *ref;
Junio C Hamano60650a42018-08-01 09:22:37 -07001693 const struct ref *best_match = NULL;
1694 int best_score = 0;
1695
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001696 for (ref = refs; ref; ref = ref->next) {
Junio C Hamano60650a42018-08-01 09:22:37 -07001697 int score = refname_match(name, ref->name);
1698
1699 if (best_score < score) {
1700 best_match = ref;
1701 best_score = score;
1702 }
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001703 }
Junio C Hamano60650a42018-08-01 09:22:37 -07001704 return best_match;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001705}
1706
Daniel Barkalow45773702007-10-29 21:05:40 -04001707struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001708{
Daniel Barkalow45773702007-10-29 21:05:40 -04001709 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001710
1711 if (!ref)
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001712 return NULL;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001713
1714 return copy_ref(ref);
1715}
1716
1717static struct ref *get_local_ref(const char *name)
1718{
Clemens Buchacher3eb96992009-06-17 15:38:36 +02001719 if (!name || name[0] == '\0')
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001720 return NULL;
1721
Christian Couder59556542013-11-30 21:55:40 +01001722 if (starts_with(name, "refs/"))
René Scharfe59c69c02008-10-18 10:44:18 +02001723 return alloc_ref(name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001724
Christian Couder59556542013-11-30 21:55:40 +01001725 if (starts_with(name, "heads/") ||
1726 starts_with(name, "tags/") ||
1727 starts_with(name, "remotes/"))
René Scharfe80097682008-10-18 10:37:40 +02001728 return alloc_ref_with_prefix("refs/", 5, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001729
René Scharfe80097682008-10-18 10:37:40 +02001730 return alloc_ref_with_prefix("refs/heads/", 11, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001731}
1732
Daniel Barkalow45773702007-10-29 21:05:40 -04001733int get_fetch_map(const struct ref *remote_refs,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001734 const struct refspec_item *refspec,
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001735 struct ref ***tail,
1736 int missing_ok)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001737{
Daniel Barkalowef00d152008-03-17 22:05:23 -04001738 struct ref *ref_map, **rmp;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001739
1740 if (refspec->pattern) {
1741 ref_map = get_expanded_map(remote_refs, refspec);
1742 } else {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001743 const char *name = refspec->src[0] ? refspec->src : "HEAD";
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001744
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001745 if (refspec->exact_sha1) {
1746 ref_map = alloc_ref(name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001747 get_oid_hex(name, &ref_map->old_oid);
Brandon Williams73302052018-06-27 15:30:23 -07001748 ref_map->exact_oid = 1;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001749 } else {
1750 ref_map = get_remote_ref(remote_refs, name);
1751 }
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001752 if (!missing_ok && !ref_map)
1753 die("Couldn't find remote ref %s", name);
1754 if (ref_map) {
1755 ref_map->peer_ref = get_local_ref(refspec->dst);
1756 if (ref_map->peer_ref && refspec->force)
1757 ref_map->peer_ref->force = 1;
1758 }
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001759 }
1760
Daniel Barkalowef00d152008-03-17 22:05:23 -04001761 for (rmp = &ref_map; *rmp; ) {
1762 if ((*rmp)->peer_ref) {
Christian Couder59556542013-11-30 21:55:40 +01001763 if (!starts_with((*rmp)->peer_ref->name, "refs/") ||
Junio C Hamano5c08c1f2012-05-04 15:35:18 -07001764 check_refname_format((*rmp)->peer_ref->name, 0)) {
Daniel Barkalowef00d152008-03-17 22:05:23 -04001765 struct ref *ignore = *rmp;
1766 error("* Ignoring funny ref '%s' locally",
1767 (*rmp)->peer_ref->name);
1768 *rmp = (*rmp)->next;
1769 free(ignore->peer_ref);
1770 free(ignore);
1771 continue;
1772 }
1773 }
1774 rmp = &((*rmp)->next);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001775 }
1776
Alex Riesen8f70a762007-10-12 22:40:04 +02001777 if (ref_map)
1778 tail_link_ref(ref_map, tail);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001779
1780 return 0;
1781}
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001782
1783int resolve_remote_symref(struct ref *ref, struct ref *list)
1784{
1785 if (!ref->symref)
1786 return 0;
1787 for (; list; list = list->next)
1788 if (!strcmp(ref->symref, list->name)) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001789 oidcpy(&ref->old_oid, &list->old_oid);
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001790 return 0;
1791 }
1792 return 1;
1793}
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001794
1795/*
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001796 * Lookup the upstream branch for the given branch and if present, optionally
1797 * compute the commit ahead/behind values for the pair.
1798 *
1799 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
1800 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
1801 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
1802 * set to zero).
1803 *
1804 * The name of the upstream branch (or NULL if no upstream is defined) is
1805 * returned via *upstream_name, if it is not itself NULL.
Jiang Xinf2e08732013-08-26 15:02:48 +08001806 *
Jeff King979cb242015-05-21 20:49:11 -04001807 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001808 * upstream defined, or ref does not exist). Returns 0 if the commits are
1809 * identical. Returns 1 if commits are different.
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001810 */
Jeff King979cb242015-05-21 20:49:11 -04001811int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001812 const char **upstream_name, enum ahead_behind_flags abf)
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001813{
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001814 struct object_id oid;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001815 struct commit *ours, *theirs;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001816 struct rev_info revs;
Jeff King0b282cc2015-09-24 17:07:58 -04001817 const char *base;
1818 struct argv_array argv = ARGV_ARRAY_INIT;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001819
Jiang Xinf2e08732013-08-26 15:02:48 +08001820 /* Cannot stat unless we are marked to build on top of somebody else. */
Jeff King3a429d02015-05-21 00:45:32 -04001821 base = branch_get_upstream(branch, NULL);
Jeff King979cb242015-05-21 20:49:11 -04001822 if (upstream_name)
1823 *upstream_name = base;
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001824 if (!base)
Jeff King979cb242015-05-21 20:49:11 -04001825 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001826
Jiang Xinf2e08732013-08-26 15:02:48 +08001827 /* Cannot stat if what we used to build on no longer exists */
brian m. carlson34c290a2017-10-15 22:06:56 +00001828 if (read_ref(base, &oid))
Jiang Xinf2e08732013-08-26 15:02:48 +08001829 return -1;
Stefan Beller2122f672018-06-28 18:21:58 -07001830 theirs = lookup_commit_reference(the_repository, &oid);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001831 if (!theirs)
Jiang Xinf2e08732013-08-26 15:02:48 +08001832 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001833
brian m. carlson34c290a2017-10-15 22:06:56 +00001834 if (read_ref(branch->refname, &oid))
Jiang Xinf2e08732013-08-26 15:02:48 +08001835 return -1;
Stefan Beller2122f672018-06-28 18:21:58 -07001836 ours = lookup_commit_reference(the_repository, &oid);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001837 if (!ours)
Jiang Xinf2e08732013-08-26 15:02:48 +08001838 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001839
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001840 *num_theirs = *num_ours = 0;
1841
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001842 /* are we the same? */
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001843 if (theirs == ours)
Jeff King979cb242015-05-21 20:49:11 -04001844 return 0;
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001845 if (abf == AHEAD_BEHIND_QUICK)
1846 return 1;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001847 if (abf != AHEAD_BEHIND_FULL)
1848 BUG("stat_tracking_info: invalid abf '%d'", abf);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001849
Junio C Hamano8fbf8792009-04-21 16:32:18 -07001850 /* Run "rev-list --left-right ours...theirs" internally... */
Jeff King0b282cc2015-09-24 17:07:58 -04001851 argv_array_push(&argv, ""); /* ignored */
1852 argv_array_push(&argv, "--left-right");
1853 argv_array_pushf(&argv, "%s...%s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001854 oid_to_hex(&ours->object.oid),
1855 oid_to_hex(&theirs->object.oid));
Jeff King0b282cc2015-09-24 17:07:58 -04001856 argv_array_push(&argv, "--");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001857
1858 init_revisions(&revs, NULL);
Jeff King0b282cc2015-09-24 17:07:58 -04001859 setup_revisions(argv.argc, argv.argv, &revs, NULL);
Stefan Beller81c3ce32014-08-10 23:33:26 +02001860 if (prepare_revision_walk(&revs))
1861 die("revision walk setup failed");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001862
1863 /* ... and count the commits on each side. */
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001864 while (1) {
1865 struct commit *c = get_revision(&revs);
1866 if (!c)
1867 break;
1868 if (c->object.flags & SYMMETRIC_LEFT)
1869 (*num_ours)++;
1870 else
1871 (*num_theirs)++;
1872 }
Junio C Hamanoc0234b22008-07-03 12:09:48 -07001873
1874 /* clear object flags smudged by the above traversal */
1875 clear_commit_marks(ours, ALL_REV_FLAGS);
1876 clear_commit_marks(theirs, ALL_REV_FLAGS);
Jeff King0b282cc2015-09-24 17:07:58 -04001877
1878 argv_array_clear(&argv);
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001879 return 1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001880}
1881
1882/*
1883 * Return true when there is anything to report, otherwise false.
1884 */
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001885int format_tracking_info(struct branch *branch, struct strbuf *sb,
1886 enum ahead_behind_flags abf)
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001887{
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001888 int ours, theirs, sti;
Jeff King979cb242015-05-21 20:49:11 -04001889 const char *full_base;
Stefan Beller2f50bab2014-08-10 21:43:33 +02001890 char *base;
Jiang Xinf2e08732013-08-26 15:02:48 +08001891 int upstream_is_gone = 0;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001892
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001893 sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
1894 if (sti < 0) {
Jeff King979cb242015-05-21 20:49:11 -04001895 if (!full_base)
1896 return 0;
Jiang Xinf2e08732013-08-26 15:02:48 +08001897 upstream_is_gone = 1;
Jiang Xinf2e08732013-08-26 15:02:48 +08001898 }
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001899
Jeff King979cb242015-05-21 20:49:11 -04001900 base = shorten_unambiguous_ref(full_base, 0);
Jiang Xinf2e08732013-08-26 15:02:48 +08001901 if (upstream_is_gone) {
1902 strbuf_addf(sb,
1903 _("Your branch is based on '%s', but the upstream is gone.\n"),
1904 base);
1905 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001906 strbuf_addstr(sb,
Jiang Xinf2e08732013-08-26 15:02:48 +08001907 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001908 } else if (!sti) {
Jiang Xinf2234592013-08-26 15:02:49 +08001909 strbuf_addf(sb,
Martin Ågren7560f542017-08-23 19:49:35 +02001910 _("Your branch is up to date with '%s'.\n"),
Jiang Xinf2234592013-08-26 15:02:49 +08001911 base);
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001912 } else if (abf == AHEAD_BEHIND_QUICK) {
1913 strbuf_addf(sb,
1914 _("Your branch and '%s' refer to different commits.\n"),
1915 base);
1916 if (advice_status_hints)
1917 strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
1918 "git status --ahead-behind");
Jiang Xinf2e08732013-08-26 15:02:48 +08001919 } else if (!theirs) {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001920 strbuf_addf(sb,
1921 Q_("Your branch is ahead of '%s' by %d commit.\n",
1922 "Your branch is ahead of '%s' by %d commits.\n",
Jiang Xinf2e08732013-08-26 15:02:48 +08001923 ours),
1924 base, ours);
Jeff King491e3072012-12-03 01:16:57 -05001925 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001926 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001927 _(" (use \"git push\" to publish your local commits)\n"));
Jiang Xinf2e08732013-08-26 15:02:48 +08001928 } else if (!ours) {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001929 strbuf_addf(sb,
1930 Q_("Your branch is behind '%s' by %d commit, "
1931 "and can be fast-forwarded.\n",
1932 "Your branch is behind '%s' by %d commits, "
1933 "and can be fast-forwarded.\n",
Jiang Xinf2e08732013-08-26 15:02:48 +08001934 theirs),
1935 base, theirs);
Jeff King491e3072012-12-03 01:16:57 -05001936 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001937 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001938 _(" (use \"git pull\" to update your local branch)\n"));
Matthieu Moyc190ced2012-11-15 11:45:00 +01001939 } else {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001940 strbuf_addf(sb,
1941 Q_("Your branch and '%s' have diverged,\n"
1942 "and have %d and %d different commit each, "
1943 "respectively.\n",
1944 "Your branch and '%s' have diverged,\n"
1945 "and have %d and %d different commits each, "
1946 "respectively.\n",
Nguyễn Thái Ngọc Duyf54bea42016-05-03 07:12:30 +07001947 ours + theirs),
Jiang Xinf2e08732013-08-26 15:02:48 +08001948 base, ours, theirs);
Jeff King491e3072012-12-03 01:16:57 -05001949 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001950 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001951 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
Matthieu Moyc190ced2012-11-15 11:45:00 +01001952 }
Stefan Beller2f50bab2014-08-10 21:43:33 +02001953 free(base);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001954 return 1;
1955}
Jay Soffian454e2022009-02-25 03:32:11 -05001956
Michael Haggerty455ade62015-05-25 18:39:01 +00001957static int one_local_ref(const char *refname, const struct object_id *oid,
1958 int flag, void *cb_data)
Jay Soffian454e2022009-02-25 03:32:11 -05001959{
1960 struct ref ***local_tail = cb_data;
1961 struct ref *ref;
Jay Soffian454e2022009-02-25 03:32:11 -05001962
1963 /* we already know it starts with refs/ to get here */
Michael Haggerty8d9c5012011-09-15 23:10:25 +02001964 if (check_refname_format(refname + 5, 0))
Jay Soffian454e2022009-02-25 03:32:11 -05001965 return 0;
1966
Jeff King96ffc062016-02-22 17:44:32 -05001967 ref = alloc_ref(refname);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001968 oidcpy(&ref->new_oid, oid);
Jay Soffian454e2022009-02-25 03:32:11 -05001969 **local_tail = ref;
1970 *local_tail = &ref->next;
1971 return 0;
1972}
1973
1974struct ref *get_local_heads(void)
1975{
Nguyễn Thái Ngọc Duy55f05662009-04-17 08:16:23 +10001976 struct ref *local_refs = NULL, **local_tail = &local_refs;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +00001977
Jay Soffian454e2022009-02-25 03:32:11 -05001978 for_each_ref(one_local_ref, &local_tail);
1979 return local_refs;
1980}
Jay Soffian8ef51732009-02-25 03:32:13 -05001981
Jay Soffian4229f1f2009-02-27 14:10:05 -05001982struct ref *guess_remote_head(const struct ref *head,
1983 const struct ref *refs,
1984 int all)
Jay Soffian8ef51732009-02-25 03:32:13 -05001985{
Jay Soffian8ef51732009-02-25 03:32:13 -05001986 const struct ref *r;
Jay Soffian4229f1f2009-02-27 14:10:05 -05001987 struct ref *list = NULL;
1988 struct ref **tail = &list;
Jay Soffian8ef51732009-02-25 03:32:13 -05001989
Jay Soffian6cb4e6c2009-02-25 03:32:14 -05001990 if (!head)
Jay Soffian8ef51732009-02-25 03:32:13 -05001991 return NULL;
1992
Jeff Kingfbb074c2009-02-27 14:10:06 -05001993 /*
1994 * Some transports support directly peeking at
1995 * where HEAD points; if that is the case, then
1996 * we don't have to guess.
1997 */
1998 if (head->symref)
1999 return copy_ref(find_ref_by_name(refs, head->symref));
2000
Jay Soffian8ef51732009-02-25 03:32:13 -05002001 /* If refs/heads/master could be right, it is. */
Jay Soffian4229f1f2009-02-27 14:10:05 -05002002 if (!all) {
2003 r = find_ref_by_name(refs, "refs/heads/master");
Jeff King4a7e27e2018-08-28 17:22:40 -04002004 if (r && oideq(&r->old_oid, &head->old_oid))
Jay Soffian4229f1f2009-02-27 14:10:05 -05002005 return copy_ref(r);
2006 }
Jay Soffian8ef51732009-02-25 03:32:13 -05002007
2008 /* Look for another ref that points there */
Jay Soffian4229f1f2009-02-27 14:10:05 -05002009 for (r = refs; r; r = r->next) {
Jeff King61adfd32011-06-03 01:11:13 -04002010 if (r != head &&
Christian Couder59556542013-11-30 21:55:40 +01002011 starts_with(r->name, "refs/heads/") &&
Jeff King4a7e27e2018-08-28 17:22:40 -04002012 oideq(&r->old_oid, &head->old_oid)) {
Jay Soffian4229f1f2009-02-27 14:10:05 -05002013 *tail = copy_ref(r);
2014 tail = &((*tail)->next);
2015 if (!all)
2016 break;
2017 }
2018 }
Jay Soffian8ef51732009-02-25 03:32:13 -05002019
Jay Soffian4229f1f2009-02-27 14:10:05 -05002020 return list;
Jay Soffian8ef51732009-02-25 03:32:13 -05002021}
Jay Soffianf2ef6072009-11-10 00:03:31 -05002022
2023struct stale_heads_info {
Jay Soffianf2ef6072009-11-10 00:03:31 -05002024 struct string_list *ref_names;
2025 struct ref **stale_refs_tail;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002026 struct refspec *rs;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002027};
2028
Michael Haggerty455ade62015-05-25 18:39:01 +00002029static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
2030 int flags, void *cb_data)
Jay Soffianf2ef6072009-11-10 00:03:31 -05002031{
2032 struct stale_heads_info *info = cb_data;
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002033 struct string_list matches = STRING_LIST_INIT_DUP;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07002034 struct refspec_item query;
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002035 int i, stale = 1;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07002036 memset(&query, 0, sizeof(struct refspec_item));
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002037 query.dst = (char *)refname;
2038
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002039 query_refspecs_multiple(info->rs, &query, &matches);
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002040 if (matches.nr == 0)
2041 goto clean_exit; /* No matches */
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002042
2043 /*
2044 * If we did find a suitable refspec and it's not a symref and
2045 * it's not in the list of refs that currently exist in that
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002046 * remote, we consider it to be stale. In order to deal with
2047 * overlapping refspecs, we need to go over all of the
2048 * matching refs.
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002049 */
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002050 if (flags & REF_ISSYMREF)
2051 goto clean_exit;
2052
2053 for (i = 0; stale && i < matches.nr; i++)
2054 if (string_list_has_string(info->ref_names, matches.items[i].string))
2055 stale = 0;
2056
2057 if (stale) {
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002058 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002059 oidcpy(&ref->new_oid, oid);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002060 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002061
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002062clean_exit:
2063 string_list_clear(&matches, 0);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002064 return 0;
2065}
2066
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002067struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map)
Jay Soffianf2ef6072009-11-10 00:03:31 -05002068{
2069 struct ref *ref, *stale_refs = NULL;
Thiago Farina183113a2010-07-04 16:46:19 -03002070 struct string_list ref_names = STRING_LIST_INIT_NODUP;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002071 struct stale_heads_info info;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +00002072
Jay Soffianf2ef6072009-11-10 00:03:31 -05002073 info.ref_names = &ref_names;
2074 info.stale_refs_tail = &stale_refs;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002075 info.rs = rs;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002076 for (ref = fetch_map; ref; ref = ref->next)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01002077 string_list_append(&ref_names, ref->name);
Michael Haggerty3383e192014-11-25 09:02:35 +01002078 string_list_sort(&ref_names);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002079 for_each_ref(get_stale_heads_cb, &info);
2080 string_list_clear(&ref_names, 0);
2081 return stale_refs;
2082}
Junio C Hamano28f5d172013-07-08 15:34:36 -07002083
2084/*
2085 * Compare-and-swap
2086 */
Junio C Hamanoa355b112015-01-14 14:58:44 -08002087static void clear_cas_option(struct push_cas_option *cas)
Junio C Hamano28f5d172013-07-08 15:34:36 -07002088{
2089 int i;
2090
2091 for (i = 0; i < cas->nr; i++)
2092 free(cas->entry[i].refname);
2093 free(cas->entry);
2094 memset(cas, 0, sizeof(*cas));
2095}
2096
2097static struct push_cas *add_cas_entry(struct push_cas_option *cas,
2098 const char *refname,
2099 size_t refnamelen)
2100{
2101 struct push_cas *entry;
2102 ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
2103 entry = &cas->entry[cas->nr++];
2104 memset(entry, 0, sizeof(*entry));
2105 entry->refname = xmemdupz(refname, refnamelen);
2106 return entry;
2107}
2108
Junio C Hamano86689762017-03-31 13:20:48 -07002109static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
Junio C Hamano28f5d172013-07-08 15:34:36 -07002110{
2111 const char *colon;
2112 struct push_cas *entry;
2113
2114 if (unset) {
2115 /* "--no-<option>" */
2116 clear_cas_option(cas);
2117 return 0;
2118 }
2119
2120 if (!arg) {
2121 /* just "--<option>" */
2122 cas->use_tracking_for_rest = 1;
2123 return 0;
2124 }
2125
2126 /* "--<option>=refname" or "--<option>=refname:value" */
2127 colon = strchrnul(arg, ':');
2128 entry = add_cas_entry(cas, arg, colon - arg);
2129 if (!*colon)
2130 entry->use_tracking = 1;
John Keepingeee98e72016-07-26 21:44:44 +01002131 else if (!colon[1])
brian m. carlsonb8566f82017-07-13 23:49:21 +00002132 oidclr(&entry->expect);
2133 else if (get_oid(colon + 1, &entry->expect))
Junio C Hamano28f5d172013-07-08 15:34:36 -07002134 return error("cannot parse expected object name '%s'", colon + 1);
2135 return 0;
2136}
2137
2138int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
2139{
2140 return parse_push_cas_option(opt->value, arg, unset);
2141}
Junio C Hamano91048a92013-07-09 11:01:06 -07002142
2143int is_empty_cas(const struct push_cas_option *cas)
2144{
2145 return !cas->use_tracking_for_rest && !cas->nr;
2146}
2147
2148/*
2149 * Look at remote.fetch refspec and see if we have a remote
2150 * tracking branch for the refname there. Fill its current
2151 * value in sha1[].
2152 * If we cannot do so, return negative to signal an error.
2153 */
2154static int remote_tracking(struct remote *remote, const char *refname,
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002155 struct object_id *oid)
Junio C Hamano91048a92013-07-09 11:01:06 -07002156{
2157 char *dst;
2158
Brandon Williamsd0004142018-05-16 15:58:11 -07002159 dst = apply_refspecs(&remote->fetch, refname);
Junio C Hamano91048a92013-07-09 11:01:06 -07002160 if (!dst)
2161 return -1; /* no tracking ref for refname at remote */
brian m. carlson34c290a2017-10-15 22:06:56 +00002162 if (read_ref(dst, oid))
Junio C Hamano91048a92013-07-09 11:01:06 -07002163 return -1; /* we know what the tracking ref is but we cannot read it */
2164 return 0;
2165}
2166
2167static void apply_cas(struct push_cas_option *cas,
2168 struct remote *remote,
2169 struct ref *ref)
2170{
2171 int i;
2172
2173 /* Find an explicit --<option>=<name>[:<value>] entry */
2174 for (i = 0; i < cas->nr; i++) {
2175 struct push_cas *entry = &cas->entry[i];
Michael Haggerty54457fe2014-01-14 04:16:07 +01002176 if (!refname_match(entry->refname, ref->name))
Junio C Hamano91048a92013-07-09 11:01:06 -07002177 continue;
2178 ref->expect_old_sha1 = 1;
2179 if (!entry->use_tracking)
brian m. carlsonb8566f82017-07-13 23:49:21 +00002180 oidcpy(&ref->old_oid_expect, &entry->expect);
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002181 else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
John Keeping64ac39a2016-07-26 21:44:45 +01002182 oidclr(&ref->old_oid_expect);
Junio C Hamano91048a92013-07-09 11:01:06 -07002183 return;
2184 }
2185
2186 /* Are we using "--<option>" to cover all? */
2187 if (!cas->use_tracking_for_rest)
2188 return;
2189
2190 ref->expect_old_sha1 = 1;
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002191 if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
John Keeping64ac39a2016-07-26 21:44:45 +01002192 oidclr(&ref->old_oid_expect);
Junio C Hamano91048a92013-07-09 11:01:06 -07002193}
2194
2195void apply_push_cas(struct push_cas_option *cas,
2196 struct remote *remote,
2197 struct ref *remote_refs)
2198{
2199 struct ref *ref;
2200 for (ref = remote_refs; ref; ref = ref->next)
2201 apply_cas(cas, remote, ref);
2202}