blob: 86e6098774d891a3b7045cf1dd9c11959faf13dc [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"
Daniel Barkalow5751f492007-05-12 11:45:53 -040015
Felipe Contreras6ddba5e2012-02-23 00:43:41 +020016enum map_direction { FROM_SRC, FROM_DST };
17
Junio C Hamano844112c2008-02-24 22:25:04 -080018struct counted_string {
19 size_t len;
20 const char *s;
21};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050022struct rewrite {
23 const char *base;
Junio C Hamano844112c2008-02-24 22:25:04 -080024 size_t baselen;
25 struct counted_string *instead_of;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050026 int instead_of_nr;
27 int instead_of_alloc;
28};
Josh Triplettd071d942009-09-07 01:56:00 -070029struct rewrites {
30 struct rewrite **rewrite;
31 int rewrite_alloc;
32 int rewrite_nr;
33};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050034
Daniel Barkalow5751f492007-05-12 11:45:53 -040035static struct remote **remotes;
Daniel Barkalow2d313472008-02-18 23:41:41 -050036static int remotes_alloc;
37static int remotes_nr;
Patrick Reynoldsd0da0032014-07-29 14:43:39 +000038static struct hashmap remotes_hash;
Daniel Barkalow5751f492007-05-12 11:45:53 -040039
Daniel Barkalowcf818342007-09-10 23:02:56 -040040static struct branch **branches;
Daniel Barkalow2d313472008-02-18 23:41:41 -050041static int branches_alloc;
42static int branches_nr;
Daniel Barkalowcf818342007-09-10 23:02:56 -040043
44static struct branch *current_branch;
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +053045static const char *pushremote_name;
Daniel Barkalowcf818342007-09-10 23:02:56 -040046
Josh Triplettd071d942009-09-07 01:56:00 -070047static struct rewrites rewrites;
Josh Triplett1c2eafb2009-09-07 01:56:33 -070048static struct rewrites rewrites_push;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050049
Daniel Barkalow0a4da292009-11-18 02:42:23 +010050static int valid_remote(const struct remote *remote)
51{
Daniel Barkalowc578f512009-11-18 02:42:25 +010052 return (!!remote->url) || (!!remote->foreign_vcs);
Daniel Barkalow0a4da292009-11-18 02:42:23 +010053}
54
Josh Triplettd071d942009-09-07 01:56:00 -070055static const char *alias_url(const char *url, struct rewrites *r)
Daniel Barkalow55029ae2008-02-20 13:43:53 -050056{
57 int i, j;
Junio C Hamano844112c2008-02-24 22:25:04 -080058 struct counted_string *longest;
59 int longest_i;
60
61 longest = NULL;
62 longest_i = -1;
Josh Triplettd071d942009-09-07 01:56:00 -070063 for (i = 0; i < r->rewrite_nr; i++) {
64 if (!r->rewrite[i])
Daniel Barkalow55029ae2008-02-20 13:43:53 -050065 continue;
Josh Triplettd071d942009-09-07 01:56:00 -070066 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
Christian Couder59556542013-11-30 21:55:40 +010067 if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
Junio C Hamano844112c2008-02-24 22:25:04 -080068 (!longest ||
Josh Triplettd071d942009-09-07 01:56:00 -070069 longest->len < r->rewrite[i]->instead_of[j].len)) {
70 longest = &(r->rewrite[i]->instead_of[j]);
Junio C Hamano844112c2008-02-24 22:25:04 -080071 longest_i = i;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050072 }
73 }
74 }
Junio C Hamano844112c2008-02-24 22:25:04 -080075 if (!longest)
76 return url;
77
Jeff King75faa452015-09-24 17:07:03 -040078 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
Daniel Barkalow55029ae2008-02-20 13:43:53 -050079}
80
Shawn O. Pearce28b91f82007-09-19 00:49:27 -040081static void add_url(struct remote *remote, const char *url)
Daniel Barkalow5751f492007-05-12 11:45:53 -040082{
Daniel Barkalow2d313472008-02-18 23:41:41 -050083 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
84 remote->url[remote->url_nr++] = url;
Daniel Barkalow5751f492007-05-12 11:45:53 -040085}
86
Michael J Gruber20346232009-06-09 18:01:34 +020087static void add_pushurl(struct remote *remote, const char *pushurl)
88{
89 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
90 remote->pushurl[remote->pushurl_nr++] = pushurl;
91}
92
Josh Triplett1c2eafb2009-09-07 01:56:33 -070093static void add_pushurl_alias(struct remote *remote, const char *url)
94{
95 const char *pushurl = alias_url(url, &rewrites_push);
96 if (pushurl != url)
97 add_pushurl(remote, pushurl);
98}
99
100static void add_url_alias(struct remote *remote, const char *url)
101{
102 add_url(remote, alias_url(url, &rewrites));
103 add_pushurl_alias(remote, url);
104}
105
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000106struct remotes_hash_key {
107 const char *str;
108 int len;
109};
110
Stefan Beller7663cdc2017-06-30 12:14:05 -0700111static int remotes_hash_cmp(const void *unused_cmp_data,
Stefan Beller45dcb352017-06-30 17:28:35 -0700112 const void *entry,
113 const void *entry_or_key,
114 const void *keydata)
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000115{
Stefan Beller45dcb352017-06-30 17:28:35 -0700116 const struct remote *a = entry;
117 const struct remote *b = entry_or_key;
118 const struct remotes_hash_key *key = keydata;
119
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000120 if (key)
121 return strncmp(a->name, key->str, key->len) || a->name[key->len];
122 else
123 return strcmp(a->name, b->name);
124}
125
126static inline void init_remotes_hash(void)
127{
128 if (!remotes_hash.cmpfn)
Stefan Beller45dcb352017-06-30 17:28:35 -0700129 hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000130}
131
Daniel Barkalow5751f492007-05-12 11:45:53 -0400132static struct remote *make_remote(const char *name, int len)
133{
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000134 struct remote *ret, *replaced;
135 struct remotes_hash_key lookup;
136 struct hashmap_entry lookup_entry;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400137
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000138 if (!len)
139 len = strlen(name);
140
141 init_remotes_hash();
142 lookup.str = name;
143 lookup.len = len;
144 hashmap_entry_init(&lookup_entry, memhash(name, len));
145
146 if ((ret = hashmap_get(&remotes_hash, &lookup_entry, &lookup)) != NULL)
147 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400148
Daniel Barkalow2d313472008-02-18 23:41:41 -0500149 ret = xcalloc(1, sizeof(struct remote));
Michael Schubert737c5a92013-07-13 11:36:24 +0200150 ret->prune = -1; /* unspecified */
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000151 ret->prune_tags = -1; /* unspecified */
Brandon Williams6bdb3042018-05-16 15:58:00 -0700152 ret->name = xstrndup(name, len);
153 refspec_init(&ret->push, REFSPEC_PUSH);
Brandon Williamse5349ab2018-05-16 15:58:01 -0700154 refspec_init(&ret->fetch, REFSPEC_FETCH);
Brandon Williams6bdb3042018-05-16 15:58:00 -0700155
Daniel Barkalow2d313472008-02-18 23:41:41 -0500156 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
157 remotes[remotes_nr++] = ret;
Patrick Reynoldsd0da0032014-07-29 14:43:39 +0000158
159 hashmap_entry_init(ret, lookup_entry.hash);
160 replaced = hashmap_put(&remotes_hash, ret);
161 assert(replaced == NULL); /* no previous entry overwritten */
Daniel Barkalow2d313472008-02-18 23:41:41 -0500162 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400163}
164
Daniel Barkalowcf818342007-09-10 23:02:56 -0400165static void add_merge(struct branch *branch, const char *name)
166{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500167 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
168 branch->merge_alloc);
169 branch->merge_name[branch->merge_nr++] = name;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400170}
171
172static struct branch *make_branch(const char *name, int len)
173{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500174 struct branch *ret;
175 int i;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400176
Daniel Barkalow2d313472008-02-18 23:41:41 -0500177 for (i = 0; i < branches_nr; i++) {
178 if (len ? (!strncmp(name, branches[i]->name, len) &&
179 !branches[i]->name[len]) :
180 !strcmp(name, branches[i]->name))
181 return branches[i];
Daniel Barkalowcf818342007-09-10 23:02:56 -0400182 }
183
Daniel Barkalow2d313472008-02-18 23:41:41 -0500184 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
185 ret = xcalloc(1, sizeof(struct branch));
186 branches[branches_nr++] = ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400187 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500188 ret->name = xstrndup(name, len);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400189 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500190 ret->name = xstrdup(name);
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400191 ret->refname = xstrfmt("refs/heads/%s", ret->name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400192
Daniel Barkalow2d313472008-02-18 23:41:41 -0500193 return ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400194}
195
Josh Triplettd071d942009-09-07 01:56:00 -0700196static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500197{
198 struct rewrite *ret;
199 int i;
200
Josh Triplettd071d942009-09-07 01:56:00 -0700201 for (i = 0; i < r->rewrite_nr; i++) {
Junio C Hamano844112c2008-02-24 22:25:04 -0800202 if (len
Josh Triplettd071d942009-09-07 01:56:00 -0700203 ? (len == r->rewrite[i]->baselen &&
204 !strncmp(base, r->rewrite[i]->base, len))
205 : !strcmp(base, r->rewrite[i]->base))
206 return r->rewrite[i];
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500207 }
208
Josh Triplettd071d942009-09-07 01:56:00 -0700209 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500210 ret = xcalloc(1, sizeof(struct rewrite));
Josh Triplettd071d942009-09-07 01:56:00 -0700211 r->rewrite[r->rewrite_nr++] = ret;
Junio C Hamano844112c2008-02-24 22:25:04 -0800212 if (len) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500213 ret->base = xstrndup(base, len);
Junio C Hamano844112c2008-02-24 22:25:04 -0800214 ret->baselen = len;
215 }
216 else {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500217 ret->base = xstrdup(base);
Junio C Hamano844112c2008-02-24 22:25:04 -0800218 ret->baselen = strlen(base);
219 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500220 return ret;
221}
222
223static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
224{
225 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
Junio C Hamano844112c2008-02-24 22:25:04 -0800226 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
227 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
228 rewrite->instead_of_nr++;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500229}
230
Jeff King0e265a92015-09-24 17:07:20 -0400231static const char *skip_spaces(const char *s)
232{
233 while (isspace(*s))
234 s++;
235 return s;
236}
237
Daniel Barkalow5751f492007-05-12 11:45:53 -0400238static void read_remotes_file(struct remote *remote)
239{
Jeff King0e265a92015-09-24 17:07:20 -0400240 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700241 FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
Daniel Barkalow5751f492007-05-12 11:45:53 -0400242
243 if (!f)
244 return;
Johannes Schindeline459b072017-01-19 22:20:02 +0100245 remote->configured_in_repo = 1;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100246 remote->origin = REMOTE_REMOTES;
Junio C Hamano18814d02015-10-28 13:27:33 -0700247 while (strbuf_getline(&buf, f) != EOF) {
Jeff King0e265a92015-09-24 17:07:20 -0400248 const char *v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400249
Jeff King0e265a92015-09-24 17:07:20 -0400250 strbuf_rtrim(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400251
Jeff King0e265a92015-09-24 17:07:20 -0400252 if (skip_prefix(buf.buf, "URL:", &v))
253 add_url_alias(remote, xstrdup(skip_spaces(v)));
254 else if (skip_prefix(buf.buf, "Push:", &v))
Brandon Williams6bdb3042018-05-16 15:58:00 -0700255 refspec_append(&remote->push, skip_spaces(v));
Jeff King0e265a92015-09-24 17:07:20 -0400256 else if (skip_prefix(buf.buf, "Pull:", &v))
Brandon Williamse5349ab2018-05-16 15:58:01 -0700257 refspec_append(&remote->fetch, skip_spaces(v));
Daniel Barkalow5751f492007-05-12 11:45:53 -0400258 }
Jeff King0e265a92015-09-24 17:07:20 -0400259 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400260 fclose(f);
261}
262
263static void read_branches_file(struct remote *remote)
264{
Daniel Barkalowcf818342007-09-10 23:02:56 -0400265 char *frag;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400266 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700267 FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
Daniel Barkalow5751f492007-05-12 11:45:53 -0400268
269 if (!f)
270 return;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400271
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800272 strbuf_getline_lf(&buf, f);
Johannes Sixt0fb19902015-10-23 08:02:51 +0200273 fclose(f);
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400274 strbuf_trim(&buf);
275 if (!buf.len) {
276 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400277 return;
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400278 }
279
Johannes Schindeline459b072017-01-19 22:20:02 +0100280 remote->configured_in_repo = 1;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100281 remote->origin = REMOTE_BRANCHES;
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400282
283 /*
Ramkumar Ramachandra55cfde22013-06-22 13:28:12 +0530284 * The branches file would have URL and optionally
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400285 * #branch specified. The "master" (or specified) branch is
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400286 * fetched and stored in the local branch matching the
287 * remote name.
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400288 */
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400289 frag = strchr(buf.buf, '#');
290 if (frag)
Daniel Barkalowcf818342007-09-10 23:02:56 -0400291 *(frag++) = '\0';
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400292 else
293 frag = "master";
Ramkumar Ramachandra55cfde22013-06-22 13:28:12 +0530294
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400295 add_url_alias(remote, strbuf_detach(&buf, NULL));
Brandon Williamse5349ab2018-05-16 15:58:01 -0700296 strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
297 frag, remote->name);
298 refspec_append(&remote->fetch, buf.buf);
Jeff Kingf28e3ab2015-09-24 17:07:18 -0400299
Martin Koegler18afe102008-11-10 22:47:11 +0100300 /*
301 * Cogito compatible push: push current HEAD to remote #branch
302 * (master if missing)
303 */
Brandon Williamse5349ab2018-05-16 15:58:01 -0700304 strbuf_reset(&buf);
Brandon Williams6bdb3042018-05-16 15:58:00 -0700305 strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
306 refspec_append(&remote->push, buf.buf);
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400307 remote->fetch_tags = 1; /* always auto-follow */
Brandon Williams6bdb3042018-05-16 15:58:00 -0700308 strbuf_release(&buf);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400309}
310
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100311static int handle_config(const char *key, const char *value, void *cb)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400312{
313 const char *name;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100314 int namelen;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400315 const char *subkey;
316 struct remote *remote;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400317 struct branch *branch;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100318 if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
319 if (!name)
Daniel Barkalowcf818342007-09-10 23:02:56 -0400320 return 0;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100321 branch = make_branch(name, namelen);
322 if (!strcmp(subkey, "remote")) {
Jeff Kinge41bf352015-05-01 18:44:41 -0400323 return git_config_string(&branch->remote_name, key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100324 } else if (!strcmp(subkey, "pushremote")) {
Jeff Kingda66b272015-05-21 00:45:20 -0400325 return git_config_string(&branch->pushremote_name, key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100326 } else if (!strcmp(subkey, "merge")) {
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800327 if (!value)
328 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400329 add_merge(branch, xstrdup(value));
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800330 }
Daniel Barkalowcf818342007-09-10 23:02:56 -0400331 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400332 }
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100333 if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500334 struct rewrite *rewrite;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100335 if (!name)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500336 return 0;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100337 if (!strcmp(subkey, "insteadof")) {
338 rewrite = make_rewrite(&rewrites, name, namelen);
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700339 if (!value)
340 return config_error_nonbool(key);
341 add_instead_of(rewrite, xstrdup(value));
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100342 } else if (!strcmp(subkey, "pushinsteadof")) {
343 rewrite = make_rewrite(&rewrites_push, name, namelen);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500344 if (!value)
345 return config_error_nonbool(key);
346 add_instead_of(rewrite, xstrdup(value));
347 }
348 }
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530349
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100350 if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400351 return 0;
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530352
353 /* Handle remote.* variables */
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100354 if (!name && !strcmp(subkey, "pushdefault"))
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530355 return git_config_string(&pushremote_name, key, value);
356
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100357 if (!name)
358 return 0;
Ramkumar Ramachandra224c2172013-04-02 13:10:33 +0530359 /* Handle remote.<name>.* variables */
Brandon Caseyc82efaf2008-10-14 15:30:21 -0500360 if (*name == '/') {
361 warning("Config remote shorthand cannot begin with '/': %s",
362 name);
363 return 0;
364 }
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100365 remote = make_remote(name, namelen);
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100366 remote->origin = REMOTE_CONFIG;
Johannes Schindeline459b072017-01-19 22:20:02 +0100367 if (current_config_scope() == CONFIG_SCOPE_REPO)
368 remote->configured_in_repo = 1;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100369 if (!strcmp(subkey, "mirror"))
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200370 remote->mirror = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100371 else if (!strcmp(subkey, "skipdefaultupdate"))
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200372 remote->skip_default_update = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100373 else if (!strcmp(subkey, "skipfetchall"))
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100374 remote->skip_default_update = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100375 else if (!strcmp(subkey, "prune"))
Michael Schubert737c5a92013-07-13 11:36:24 +0200376 remote->prune = git_config_bool(key, value);
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000377 else if (!strcmp(subkey, "prunetags"))
378 remote->prune_tags = git_config_bool(key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100379 else if (!strcmp(subkey, "url")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200380 const char *v;
381 if (git_config_string(&v, key, value))
382 return -1;
383 add_url(remote, v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100384 } else if (!strcmp(subkey, "pushurl")) {
Michael J Gruber20346232009-06-09 18:01:34 +0200385 const char *v;
386 if (git_config_string(&v, key, value))
387 return -1;
388 add_pushurl(remote, v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100389 } else if (!strcmp(subkey, "push")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200390 const char *v;
391 if (git_config_string(&v, key, value))
392 return -1;
Brandon Williams6bdb3042018-05-16 15:58:00 -0700393 refspec_append(&remote->push, v);
394 free((char *)v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100395 } else if (!strcmp(subkey, "fetch")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200396 const char *v;
397 if (git_config_string(&v, key, value))
398 return -1;
Brandon Williamse5349ab2018-05-16 15:58:01 -0700399 refspec_append(&remote->fetch, v);
400 free((char *)v);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100401 } else if (!strcmp(subkey, "receivepack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200402 const char *v;
403 if (git_config_string(&v, key, value))
404 return -1;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400405 if (!remote->receivepack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200406 remote->receivepack = v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400407 else
408 error("more than one receivepack given, using the first");
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100409 } else if (!strcmp(subkey, "uploadpack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200410 const char *v;
411 if (git_config_string(&v, key, value))
412 return -1;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400413 if (!remote->uploadpack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200414 remote->uploadpack = v;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400415 else
416 error("more than one uploadpack given, using the first");
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100417 } else if (!strcmp(subkey, "tagopt")) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400418 if (!strcmp(value, "--no-tags"))
419 remote->fetch_tags = -1;
Samuel Tardieu944163a2010-04-20 01:31:25 +0200420 else if (!strcmp(value, "--tags"))
421 remote->fetch_tags = 2;
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100422 } else if (!strcmp(subkey, "proxy")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200423 return git_config_string((const char **)&remote->http_proxy,
424 key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100425 } else if (!strcmp(subkey, "proxyauthmethod")) {
Knut Frankeef976392016-01-26 13:02:47 +0000426 return git_config_string((const char **)&remote->http_proxy_authmethod,
427 key, value);
Thomas Gummererbc60f8a2016-02-16 10:47:49 +0100428 } else if (!strcmp(subkey, "vcs")) {
Daniel Barkalowc578f512009-11-18 02:42:25 +0100429 return git_config_string(&remote->foreign_vcs, key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200430 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400431 return 0;
432}
433
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500434static void alias_all_urls(void)
435{
436 int i, j;
437 for (i = 0; i < remotes_nr; i++) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700438 int add_pushurl_aliases;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500439 if (!remotes[i])
440 continue;
Michael J Gruber20346232009-06-09 18:01:34 +0200441 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
Josh Triplettd071d942009-09-07 01:56:00 -0700442 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
Michael J Gruber20346232009-06-09 18:01:34 +0200443 }
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700444 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
445 for (j = 0; j < remotes[i]->url_nr; j++) {
446 if (add_pushurl_aliases)
447 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
448 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
449 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500450 }
451}
452
Daniel Barkalow5751f492007-05-12 11:45:53 -0400453static void read_config(void)
454{
Jeff Kinge41bf352015-05-01 18:44:41 -0400455 static int loaded;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400456 int flag;
Jeff Kinge41bf352015-05-01 18:44:41 -0400457
458 if (loaded)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400459 return;
Jeff Kinge41bf352015-05-01 18:44:41 -0400460 loaded = 1;
461
Daniel Barkalow5751f492007-05-12 11:45:53 -0400462 current_branch = NULL;
Jeff Kingf2f12d12016-03-05 17:11:57 -0500463 if (startup_info->have_repository) {
René Scharfe744c0402017-09-23 11:45:04 +0200464 const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
Jeff Kingf2f12d12016-03-05 17:11:57 -0500465 if (head_ref && (flag & REF_ISSYMREF) &&
466 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
467 current_branch = make_branch(head_ref, 0);
468 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400469 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100470 git_config(handle_config, NULL);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500471 alias_all_urls();
Daniel Barkalow5751f492007-05-12 11:45:53 -0400472}
473
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500474static int valid_remote_nick(const char *name)
475{
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300476 if (!name[0] || is_dot_or_dotdot(name))
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500477 return 0;
Johannes Sixtd9244ec2017-05-25 14:00:13 +0200478
479 /* remote nicknames cannot contain slashes */
480 while (*name)
481 if (is_dir_sep(*name++))
482 return 0;
483 return 1;
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500484}
485
Jeff Kingf0521542015-05-21 00:45:16 -0400486const char *remote_for_branch(struct branch *branch, int *explicit)
487{
488 if (branch && branch->remote_name) {
489 if (explicit)
490 *explicit = 1;
491 return branch->remote_name;
492 }
493 if (explicit)
494 *explicit = 0;
495 return "origin";
496}
497
Jeff Kingda66b272015-05-21 00:45:20 -0400498const char *pushremote_for_branch(struct branch *branch, int *explicit)
499{
500 if (branch && branch->pushremote_name) {
501 if (explicit)
502 *explicit = 1;
503 return branch->pushremote_name;
504 }
505 if (pushremote_name) {
506 if (explicit)
507 *explicit = 1;
508 return pushremote_name;
509 }
510 return remote_for_branch(branch, explicit);
511}
512
J Wyman9700fae2017-11-07 17:31:08 +0100513const char *remote_ref_for_branch(struct branch *branch, int for_push,
514 int *explicit)
515{
516 if (branch) {
517 if (!for_push) {
518 if (branch->merge_nr) {
519 if (explicit)
520 *explicit = 1;
521 return branch->merge_name[0];
522 }
523 } else {
524 const char *dst, *remote_name =
525 pushremote_for_branch(branch, NULL);
526 struct remote *remote = remote_get(remote_name);
527
Brandon Williams6bdb3042018-05-16 15:58:00 -0700528 if (remote && remote->push.nr &&
Brandon Williamsd0004142018-05-16 15:58:11 -0700529 (dst = apply_refspecs(&remote->push,
J Wyman9700fae2017-11-07 17:31:08 +0100530 branch->refname))) {
531 if (explicit)
532 *explicit = 1;
533 return dst;
534 }
535 }
536 }
537 if (explicit)
538 *explicit = 0;
539 return "";
540}
541
Jeff Kingda66b272015-05-21 00:45:20 -0400542static struct remote *remote_get_1(const char *name,
543 const char *(*get_default)(struct branch *, int *))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400544{
545 struct remote *ret;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400546 int name_given = 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400547
Jeff King8770e6f2015-05-21 00:45:23 -0400548 read_config();
549
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400550 if (name)
551 name_given = 1;
Jeff Kingda66b272015-05-21 00:45:20 -0400552 else
553 name = get_default(current_branch, &name_given);
Junio C Hamano9326d492009-03-16 00:35:09 -0700554
Daniel Barkalow5751f492007-05-12 11:45:53 -0400555 ret = make_remote(name, 0);
Jeff King4539c212017-02-14 15:33:28 -0500556 if (valid_remote_nick(name) && have_git_dir()) {
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100557 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400558 read_remotes_file(ret);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100559 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400560 read_branches_file(ret);
561 }
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100562 if (name_given && !valid_remote(ret))
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500563 add_url_alias(ret, name);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100564 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400565 return NULL;
566 return ret;
567}
Daniel Barkalow6b628162007-05-12 11:45:59 -0400568
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530569struct remote *remote_get(const char *name)
570{
Jeff Kingda66b272015-05-21 00:45:20 -0400571 return remote_get_1(name, remote_for_branch);
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530572}
573
574struct remote *pushremote_get(const char *name)
575{
Jeff Kingda66b272015-05-21 00:45:20 -0400576 return remote_get_1(name, pushremote_for_branch);
Ramkumar Ramachandraf24f7152013-04-02 13:10:32 +0530577}
578
Johannes Schindeline459b072017-01-19 22:20:02 +0100579int remote_is_configured(struct remote *remote, int in_repo)
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200580{
Johannes Schindeline459b072017-01-19 22:20:02 +0100581 if (!remote)
582 return 0;
583 if (in_repo)
584 return remote->configured_in_repo;
585 return !!remote->origin;
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200586}
587
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100588int for_each_remote(each_remote_fn fn, void *priv)
589{
590 int i, result = 0;
591 read_config();
Daniel Barkalow2d313472008-02-18 23:41:41 -0500592 for (i = 0; i < remotes_nr && !result; i++) {
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100593 struct remote *r = remotes[i];
594 if (!r)
595 continue;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100596 result = fn(r, priv);
597 }
598 return result;
599}
600
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100601static void handle_duplicate(struct ref *ref1, struct ref *ref2)
602{
Michael Haggertyf096e6e2013-10-30 06:33:12 +0100603 if (strcmp(ref1->name, ref2->name)) {
604 if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
605 ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
606 die(_("Cannot fetch both %s and %s to %s"),
607 ref1->name, ref2->name, ref2->peer_ref->name);
608 } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
609 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
610 warning(_("%s usually tracks %s, not %s"),
611 ref2->peer_ref->name, ref2->name, ref1->name);
612 } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
613 ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
614 die(_("%s tracks both %s and %s"),
615 ref2->peer_ref->name, ref1->name, ref2->name);
616 } else {
617 /*
618 * This last possibility doesn't occur because
619 * FETCH_HEAD_IGNORE entries always appear at
620 * the end of the list.
621 */
622 die(_("Internal error"));
623 }
624 }
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100625 free(ref2->peer_ref);
626 free(ref2);
627}
628
Michael Haggertyb9afe662013-10-30 06:33:09 +0100629struct ref *ref_remove_duplicates(struct ref *ref_map)
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400630{
Thiago Farina183113a2010-07-04 16:46:19 -0300631 struct string_list refs = STRING_LIST_INIT_NODUP;
Michael Haggertyb9afe662013-10-30 06:33:09 +0100632 struct ref *retval = NULL;
633 struct ref **p = &retval;
Julian Phillips73cf0822009-10-25 21:28:11 +0000634
Michael Haggertyb9afe662013-10-30 06:33:09 +0100635 while (ref_map) {
636 struct ref *ref = ref_map;
Julian Phillips73cf0822009-10-25 21:28:11 +0000637
Michael Haggertyb9afe662013-10-30 06:33:09 +0100638 ref_map = ref_map->next;
639 ref->next = NULL;
640
641 if (!ref->peer_ref) {
642 *p = ref;
643 p = &ref->next;
Michael Haggerty09ea1f82013-10-30 06:33:07 +0100644 } else {
Michael Haggertyb9afe662013-10-30 06:33:09 +0100645 struct string_list_item *item =
646 string_list_insert(&refs, ref->peer_ref->name);
647
648 if (item->util) {
649 /* Entry already existed */
Michael Haggertydf02ebd2013-10-30 06:33:10 +0100650 handle_duplicate((struct ref *)item->util, ref);
Michael Haggertyb9afe662013-10-30 06:33:09 +0100651 } else {
652 *p = ref;
653 p = &ref->next;
654 item->util = ref;
655 }
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400656 }
657 }
Michael Haggertyb9afe662013-10-30 06:33:09 +0100658
Julian Phillips73cf0822009-10-25 21:28:11 +0000659 string_list_clear(&refs, 0);
Michael Haggertyb9afe662013-10-30 06:33:09 +0100660 return retval;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400661}
662
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400663int remote_has_url(struct remote *remote, const char *url)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400664{
665 int i;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400666 for (i = 0; i < remote->url_nr; i++) {
667 if (!strcmp(remote->url[i], url))
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400668 return 1;
669 }
670 return 0;
671}
672
Daniel Barkalowe9282132009-03-07 01:11:34 -0500673static int match_name_with_pattern(const char *key, const char *name,
674 const char *value, char **result)
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500675{
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500676 const char *kstar = strchr(key, '*');
677 size_t klen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500678 size_t ksuffixlen;
679 size_t namelen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500680 int ret;
681 if (!kstar)
682 die("Key '%s' of pattern had no '*'", key);
683 klen = kstar - key;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500684 ksuffixlen = strlen(kstar + 1);
685 namelen = strlen(name);
686 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
687 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500688 if (ret && value) {
René Scharfe07bfa572014-09-21 10:23:37 +0200689 struct strbuf sb = STRBUF_INIT;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500690 const char *vstar = strchr(value, '*');
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500691 if (!vstar)
692 die("Value '%s' of pattern has no '*'", value);
René Scharfe07bfa572014-09-21 10:23:37 +0200693 strbuf_add(&sb, value, vstar - value);
694 strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
695 strbuf_addstr(&sb, vstar + 1);
696 *result = strbuf_detach(&sb, NULL);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500697 }
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500698 return ret;
699}
700
Brandon Williamsa2ac50c2018-05-16 15:58:10 -0700701static void query_refspecs_multiple(struct refspec *rs,
702 struct refspec_item *query,
703 struct string_list *results)
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +0100704{
705 int i;
706 int find_src = !query->src;
707
708 if (find_src && !query->dst)
709 error("query_refspecs_multiple: need either src or dst");
710
Brandon Williamsa2ac50c2018-05-16 15:58:10 -0700711 for (i = 0; i < rs->nr; i++) {
712 struct refspec_item *refspec = &rs->items[i];
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +0100713 const char *key = find_src ? refspec->dst : refspec->src;
714 const char *value = find_src ? refspec->src : refspec->dst;
715 const char *needle = find_src ? query->dst : query->src;
716 char **result = find_src ? &query->src : &query->dst;
717
718 if (!refspec->dst)
719 continue;
720 if (refspec->pattern) {
721 if (match_name_with_pattern(key, needle, value, result))
722 string_list_append_nodup(results, *result);
723 } else if (!strcmp(needle, key)) {
724 string_list_append(results, value);
725 }
726 }
727}
728
Brandon Williams86baf822018-05-16 15:58:12 -0700729int query_refspecs(struct refspec *rs, struct refspec_item *query)
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100730{
731 int i;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200732 int find_src = !query->src;
Michael Haggerty049bff82013-10-30 06:33:01 +0100733 const char *needle = find_src ? query->dst : query->src;
734 char **result = find_src ? &query->src : &query->dst;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100735
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200736 if (find_src && !query->dst)
737 return error("query_refspecs: need either src or dst");
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100738
Brandon Williams86baf822018-05-16 15:58:12 -0700739 for (i = 0; i < rs->nr; i++) {
740 struct refspec_item *refspec = &rs->items[i];
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200741 const char *key = find_src ? refspec->dst : refspec->src;
742 const char *value = find_src ? refspec->src : refspec->dst;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200743
Shawn O. Pearce009c5bc2007-09-25 00:13:14 -0400744 if (!refspec->dst)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400745 continue;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200746 if (refspec->pattern) {
Daniel Barkalowe9282132009-03-07 01:11:34 -0500747 if (match_name_with_pattern(key, needle, value, result)) {
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200748 query->force = refspec->force;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400749 return 0;
750 }
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100751 } else if (!strcmp(needle, key)) {
752 *result = xstrdup(value);
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200753 query->force = refspec->force;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100754 return 0;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400755 }
756 }
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400757 return -1;
758}
759
Brandon Williamsd0004142018-05-16 15:58:11 -0700760char *apply_refspecs(struct refspec *rs, const char *name)
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200761{
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700762 struct refspec_item query;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200763
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700764 memset(&query, 0, sizeof(struct refspec_item));
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200765 query.src = (char *)name;
766
Brandon Williams86baf822018-05-16 15:58:12 -0700767 if (query_refspecs(rs, &query))
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200768 return NULL;
769
770 return query.dst;
771}
772
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700773int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200774{
Brandon Williams86baf822018-05-16 15:58:12 -0700775 return query_refspecs(&remote->fetch, refspec);
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200776}
777
René Scharfe80097682008-10-18 10:37:40 +0200778static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
779 const char *name)
780{
781 size_t len = strlen(name);
Jeff King50a6c8e2016-02-22 17:44:35 -0500782 struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
René Scharfe80097682008-10-18 10:37:40 +0200783 memcpy(ref->name, prefix, prefixlen);
784 memcpy(ref->name + prefixlen, name, len);
785 return ref;
786}
787
René Scharfe59c69c02008-10-18 10:44:18 +0200788struct ref *alloc_ref(const char *name)
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400789{
René Scharfe59c69c02008-10-18 10:44:18 +0200790 return alloc_ref_with_prefix("", 0, name);
Krzysztof Kowalczyk737922a2008-05-10 16:26:58 -0700791}
792
Jeff King59a57752011-06-07 19:03:03 -0400793struct ref *copy_ref(const struct ref *ref)
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400794{
Jay Soffian7b3db092009-02-27 14:10:04 -0500795 struct ref *cpy;
796 size_t len;
797 if (!ref)
798 return NULL;
Jeff King50a6c8e2016-02-22 17:44:35 -0500799 len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
800 cpy = xmalloc(len);
801 memcpy(cpy, ref, len);
Jay Soffian7b3db092009-02-27 14:10:04 -0500802 cpy->next = NULL;
Jeff King8c53f072015-01-12 20:59:09 -0500803 cpy->symref = xstrdup_or_null(ref->symref);
804 cpy->remote_status = xstrdup_or_null(ref->remote_status);
Jay Soffian7b3db092009-02-27 14:10:04 -0500805 cpy->peer_ref = copy_ref(ref->peer_ref);
806 return cpy;
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400807}
808
Daniel Barkalow45773702007-10-29 21:05:40 -0400809struct ref *copy_ref_list(const struct ref *ref)
810{
811 struct ref *ret = NULL;
812 struct ref **tail = &ret;
813 while (ref) {
814 *tail = copy_ref(ref);
815 ref = ref->next;
816 tail = &((*tail)->next);
817 }
818 return ret;
819}
820
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900821static void free_ref(struct ref *ref)
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400822{
823 if (!ref)
824 return;
Jay Soffian7b3db092009-02-27 14:10:04 -0500825 free_ref(ref->peer_ref);
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400826 free(ref->remote_status);
827 free(ref->symref);
828 free(ref);
829}
830
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400831void free_refs(struct ref *ref)
832{
833 struct ref *next;
834 while (ref) {
835 next = ref->next;
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400836 free_ref(ref);
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400837 ref = next;
838 }
839}
840
Jeff Kinged81c762012-05-21 18:19:28 -0400841int ref_compare_name(const void *va, const void *vb)
842{
843 const struct ref *a = va, *b = vb;
844 return strcmp(a->name, b->name);
845}
846
847static void *ref_list_get_next(const void *a)
848{
849 return ((const struct ref *)a)->next;
850}
851
852static void ref_list_set_next(void *a, void *next)
853{
854 ((struct ref *)a)->next = next;
855}
856
857void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
858{
859 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
860}
861
Junio C Hamanoca024652013-12-03 15:41:15 -0800862int count_refspec_match(const char *pattern,
863 struct ref *refs,
864 struct ref **matched_ref)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400865{
866 int patlen = strlen(pattern);
867 struct ref *matched_weak = NULL;
868 struct ref *matched = NULL;
869 int weak_match = 0;
870 int match = 0;
871
872 for (weak_match = match = 0; refs; refs = refs->next) {
873 char *name = refs->name;
874 int namelen = strlen(name);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400875
Michael Haggerty54457fe2014-01-14 04:16:07 +0100876 if (!refname_match(pattern, name))
Daniel Barkalow6b628162007-05-12 11:45:59 -0400877 continue;
878
879 /* A match is "weak" if it is with refs outside
880 * heads or tags, and did not specify the pattern
881 * in full (e.g. "refs/remotes/origin/master") or at
882 * least from the toplevel (e.g. "remotes/origin/master");
883 * otherwise "git push $URL master" would result in
884 * ambiguity between remotes/origin/master and heads/master
885 * at the remote site.
886 */
887 if (namelen != patlen &&
888 patlen != namelen - 5 &&
Christian Couder59556542013-11-30 21:55:40 +0100889 !starts_with(name, "refs/heads/") &&
890 !starts_with(name, "refs/tags/")) {
Daniel Barkalow6b628162007-05-12 11:45:59 -0400891 /* We want to catch the case where only weak
892 * matches are found and there are multiple
893 * matches, and where more than one strong
894 * matches are found, as ambiguous. One
895 * strong match with zero or more weak matches
896 * are acceptable as a unique match.
897 */
898 matched_weak = refs;
899 weak_match++;
900 }
901 else {
902 matched = refs;
903 match++;
904 }
905 }
906 if (!matched) {
Jeff King471fd3f2014-03-05 14:03:43 -0500907 if (matched_ref)
908 *matched_ref = matched_weak;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400909 return weak_match;
910 }
911 else {
Jeff King471fd3f2014-03-05 14:03:43 -0500912 if (matched_ref)
913 *matched_ref = matched;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400914 return match;
915 }
916}
917
Daniel Barkalow1d735262007-07-10 00:47:26 -0400918static void tail_link_ref(struct ref *ref, struct ref ***tail)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400919{
920 **tail = ref;
Daniel Barkalow1d735262007-07-10 00:47:26 -0400921 while (ref->next)
922 ref = ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400923 *tail = &ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400924}
925
Felipe Contreras67655242012-02-23 00:43:40 +0200926static struct ref *alloc_delete_ref(void)
927{
928 struct ref *ref = alloc_ref("(delete)");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000929 oidclr(&ref->new_oid);
Felipe Contreras67655242012-02-23 00:43:40 +0200930 return ref;
931}
932
Jeff King471fd3f2014-03-05 14:03:43 -0500933static int try_explicit_object_name(const char *name,
934 struct ref **match)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400935{
brian m. carlsonfcd30b12015-11-10 02:22:30 +0000936 struct object_id oid;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400937
Jeff King471fd3f2014-03-05 14:03:43 -0500938 if (!*name) {
939 if (match)
940 *match = alloc_delete_ref();
941 return 0;
942 }
943
brian m. carlsone82caf32017-07-13 23:49:28 +0000944 if (get_oid(name, &oid))
Jeff King471fd3f2014-03-05 14:03:43 -0500945 return -1;
946
947 if (match) {
948 *match = alloc_ref(name);
brian m. carlsonfcd30b12015-11-10 02:22:30 +0000949 oidcpy(&(*match)->new_oid, &oid);
Jeff King471fd3f2014-03-05 14:03:43 -0500950 }
951 return 0;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400952}
953
Daniel Barkalow1d735262007-07-10 00:47:26 -0400954static struct ref *make_linked_ref(const char *name, struct ref ***tail)
Junio C Hamano163f0ee2007-06-09 00:07:34 -0700955{
René Scharfe59c69c02008-10-18 10:44:18 +0200956 struct ref *ret = alloc_ref(name);
Daniel Barkalow1d735262007-07-10 00:47:26 -0400957 tail_link_ref(ret, tail);
958 return ret;
Junio C Hamano163f0ee2007-06-09 00:07:34 -0700959}
960
Jeff Kingf8aae122008-04-23 05:16:06 -0400961static char *guess_ref(const char *name, struct ref *peer)
962{
963 struct strbuf buf = STRBUF_INIT;
Jeff Kingf8aae122008-04-23 05:16:06 -0400964
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700965 const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
René Scharfe744c0402017-09-23 11:45:04 +0200966 NULL, NULL);
Jeff Kingf8aae122008-04-23 05:16:06 -0400967 if (!r)
968 return NULL;
969
Christian Couder59556542013-11-30 21:55:40 +0100970 if (starts_with(r, "refs/heads/"))
Jeff Kingf8aae122008-04-23 05:16:06 -0400971 strbuf_addstr(&buf, "refs/heads/");
Christian Couder59556542013-11-30 21:55:40 +0100972 else if (starts_with(r, "refs/tags/"))
Jeff Kingf8aae122008-04-23 05:16:06 -0400973 strbuf_addstr(&buf, "refs/tags/");
974 else
975 return NULL;
976
977 strbuf_addstr(&buf, name);
978 return strbuf_detach(&buf, NULL);
979}
980
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500981static int match_explicit_lhs(struct ref *src,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700982 struct refspec_item *rs,
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500983 struct ref **match,
984 int *allocated_match)
985{
986 switch (count_refspec_match(rs->src, src, match)) {
987 case 1:
Jeff King471fd3f2014-03-05 14:03:43 -0500988 if (allocated_match)
989 *allocated_match = 0;
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500990 return 0;
991 case 0:
992 /* The source could be in the get_sha1() format
993 * not a reference name. :refs/other is a
994 * way to delete 'other' ref at the remote end.
995 */
Jeff King471fd3f2014-03-05 14:03:43 -0500996 if (try_explicit_object_name(rs->src, match) < 0)
Jeff Kingf7ade3d2014-03-05 14:03:21 -0500997 return error("src refspec %s does not match any.", rs->src);
Jeff King471fd3f2014-03-05 14:03:43 -0500998 if (allocated_match)
999 *allocated_match = 1;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001000 return 0;
1001 default:
1002 return error("src refspec %s matches more than one.", rs->src);
1003 }
1004}
1005
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001006static int match_explicit(struct ref *src, struct ref *dst,
1007 struct ref ***dst_tail,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001008 struct refspec_item *rs)
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001009{
1010 struct ref *matched_src, *matched_dst;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001011 int allocated_src;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001012
1013 const char *dst_value = rs->dst;
Jeff Kingf8aae122008-04-23 05:16:06 -04001014 char *dst_guess;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001015
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001016 if (rs->pattern || rs->matching)
Jeff King9a7bbd12008-06-16 12:15:02 -04001017 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001018
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001019 matched_src = matched_dst = NULL;
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001020 if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
1021 return -1;
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001022
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001023 if (!dst_value) {
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001024 int flag;
1025
Ronnie Sahlberg7695d112014-07-15 12:59:36 -07001026 dst_value = resolve_ref_unsafe(matched_src->name,
1027 RESOLVE_REF_READING,
René Scharfe744c0402017-09-23 11:45:04 +02001028 NULL, &flag);
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001029 if (!dst_value ||
1030 ((flag & REF_ISSYMREF) &&
Christian Couder59556542013-11-30 21:55:40 +01001031 !starts_with(dst_value, "refs/heads/")))
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001032 die("%s cannot be resolved to branch.",
1033 matched_src->name);
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001034 }
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001035
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001036 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1037 case 1:
1038 break;
1039 case 0:
René Scharfe50e19a82014-06-06 19:24:48 +02001040 if (starts_with(dst_value, "refs/"))
Daniel Barkalow1d735262007-07-10 00:47:26 -04001041 matched_dst = make_linked_ref(dst_value, dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001042 else if (is_null_oid(&matched_src->new_oid))
Jeff King5742c822012-07-03 14:04:39 -04001043 error("unable to delete '%s': remote ref does not exist",
1044 dst_value);
Johannes Schindelin3dc7ea92017-05-04 15:59:01 +02001045 else if ((dst_guess = guess_ref(dst_value, matched_src))) {
Jeff Kingf8aae122008-04-23 05:16:06 -04001046 matched_dst = make_linked_ref(dst_guess, dst_tail);
Johannes Schindelin3dc7ea92017-05-04 15:59:01 +02001047 free(dst_guess);
1048 } else
Jeff Kingf8aae122008-04-23 05:16:06 -04001049 error("unable to push to unqualified destination: %s\n"
1050 "The destination refspec neither matches an "
1051 "existing ref on the remote nor\n"
1052 "begins with refs/, and we are unable to "
1053 "guess a prefix based on the source ref.",
1054 dst_value);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001055 break;
1056 default:
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001057 matched_dst = NULL;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001058 error("dst refspec %s matches more than one.",
1059 dst_value);
1060 break;
1061 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001062 if (!matched_dst)
1063 return -1;
1064 if (matched_dst->peer_ref)
1065 return error("dst ref %s receives from more than one src.",
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001066 matched_dst->name);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001067 else {
Jeff Kingf7ade3d2014-03-05 14:03:21 -05001068 matched_dst->peer_ref = allocated_src ?
1069 matched_src :
1070 copy_ref(matched_src);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001071 matched_dst->force = rs->force;
1072 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001073 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001074}
1075
Daniel Barkalow6b628162007-05-12 11:45:59 -04001076static int match_explicit_refs(struct ref *src, struct ref *dst,
Brandon Williams9fa2e5e2018-05-16 15:58:14 -07001077 struct ref ***dst_tail, struct refspec *rs)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001078{
1079 int i, errs;
Brandon Williams9fa2e5e2018-05-16 15:58:14 -07001080 for (i = errs = 0; i < rs->nr; i++)
1081 errs += match_explicit(src, dst, dst_tail, &rs->items[i]);
Jeff King9a7bbd12008-06-16 12:15:02 -04001082 return errs;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001083}
1084
Brandon Williamsf3acb832018-05-16 15:58:13 -07001085static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
1086 int send_mirror, int direction,
1087 const struct refspec_item **ret_pat)
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001088{
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001089 const struct refspec_item *pat;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001090 char *name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001091 int i;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001092 int matching_refs = -1;
Brandon Williamsf3acb832018-05-16 15:58:13 -07001093 for (i = 0; i < rs->nr; i++) {
1094 const struct refspec_item *item = &rs->items[i];
1095 if (item->matching &&
1096 (matching_refs == -1 || item->force)) {
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001097 matching_refs = i;
1098 continue;
1099 }
1100
Brandon Williamsf3acb832018-05-16 15:58:13 -07001101 if (item->pattern) {
1102 const char *dst_side = item->dst ? item->dst : item->src;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001103 int match;
1104 if (direction == FROM_SRC)
Brandon Williamsf3acb832018-05-16 15:58:13 -07001105 match = match_name_with_pattern(item->src, ref->name, dst_side, &name);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001106 else
Brandon Williamsf3acb832018-05-16 15:58:13 -07001107 match = match_name_with_pattern(dst_side, ref->name, item->src, &name);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001108 if (match) {
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001109 matching_refs = i;
1110 break;
1111 }
1112 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001113 }
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001114 if (matching_refs == -1)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001115 return NULL;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001116
Brandon Williamsf3acb832018-05-16 15:58:13 -07001117 pat = &rs->items[matching_refs];
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001118 if (pat->matching) {
1119 /*
1120 * "matching refs"; traditionally we pushed everything
1121 * including refs outside refs/heads/ hierarchy, but
1122 * that does not make much sense these days.
1123 */
Christian Couder59556542013-11-30 21:55:40 +01001124 if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001125 return NULL;
1126 name = xstrdup(ref->name);
1127 }
1128 if (ret_pat)
1129 *ret_pat = pat;
1130 return name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001131}
1132
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001133static struct ref **tail_ref(struct ref **head)
1134{
1135 struct ref **tail = head;
1136 while (*tail)
1137 tail = &((*tail)->next);
1138 return tail;
1139}
1140
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001141struct tips {
1142 struct commit **tip;
1143 int nr, alloc;
1144};
1145
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001146static void add_to_tips(struct tips *tips, const struct object_id *oid)
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001147{
1148 struct commit *commit;
1149
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001150 if (is_null_oid(oid))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001151 return;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001152 commit = lookup_commit_reference_gently(the_repository, oid, 1);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001153 if (!commit || (commit->object.flags & TMP_MARK))
1154 return;
1155 commit->object.flags |= TMP_MARK;
1156 ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
1157 tips->tip[tips->nr++] = commit;
1158}
1159
1160static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
1161{
1162 struct string_list dst_tag = STRING_LIST_INIT_NODUP;
1163 struct string_list src_tag = STRING_LIST_INIT_NODUP;
1164 struct string_list_item *item;
1165 struct ref *ref;
1166 struct tips sent_tips;
1167
1168 /*
1169 * Collect everything we know they would have at the end of
1170 * this push, and collect all tags they have.
1171 */
1172 memset(&sent_tips, 0, sizeof(sent_tips));
1173 for (ref = *dst; ref; ref = ref->next) {
1174 if (ref->peer_ref &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001175 !is_null_oid(&ref->peer_ref->new_oid))
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001176 add_to_tips(&sent_tips, &ref->peer_ref->new_oid);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001177 else
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001178 add_to_tips(&sent_tips, &ref->old_oid);
Christian Couder59556542013-11-30 21:55:40 +01001179 if (starts_with(ref->name, "refs/tags/"))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001180 string_list_append(&dst_tag, ref->name);
1181 }
1182 clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
1183
Michael Haggerty3383e192014-11-25 09:02:35 +01001184 string_list_sort(&dst_tag);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001185
1186 /* Collect tags they do not have. */
1187 for (ref = src; ref; ref = ref->next) {
Christian Couder59556542013-11-30 21:55:40 +01001188 if (!starts_with(ref->name, "refs/tags/"))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001189 continue; /* not a tag */
1190 if (string_list_has_string(&dst_tag, ref->name))
1191 continue; /* they already have it */
Stefan Beller0df8e962018-04-25 11:20:59 -07001192 if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG)
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001193 continue; /* be conservative */
1194 item = string_list_append(&src_tag, ref->name);
1195 item->util = ref;
1196 }
1197 string_list_clear(&dst_tag, 0);
1198
1199 /*
1200 * At this point, src_tag lists tags that are missing from
1201 * dst, and sent_tips lists the tips we are pushing or those
1202 * that we know they already have. An element in the src_tag
1203 * that is an ancestor of any of the sent_tips needs to be
1204 * sent to the other side.
1205 */
1206 if (sent_tips.nr) {
1207 for_each_string_list_item(item, &src_tag) {
1208 struct ref *ref = item->util;
1209 struct ref *dst_ref;
1210 struct commit *commit;
1211
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001212 if (is_null_oid(&ref->new_oid))
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001213 continue;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001214 commit = lookup_commit_reference_gently(the_repository,
1215 &ref->new_oid,
brian m. carlsonbc832662017-05-06 22:10:10 +00001216 1);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001217 if (!commit)
1218 /* not pushing a commit, which is not an error */
1219 continue;
1220
1221 /*
1222 * Is this tag, which they do not have, reachable from
1223 * any of the commits we are sending?
1224 */
1225 if (!in_merge_bases_many(commit, sent_tips.nr, sent_tips.tip))
1226 continue;
1227
1228 /* Add it in */
1229 dst_ref = make_linked_ref(ref->name, dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001230 oidcpy(&dst_ref->new_oid, &ref->new_oid);
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001231 dst_ref->peer_ref = copy_ref(ref);
1232 }
1233 }
1234 string_list_clear(&src_tag, 0);
1235 free(sent_tips.tip);
1236}
1237
Junio C Hamano47a59182013-07-08 13:56:53 -07001238struct ref *find_ref_by_name(const struct ref *list, const char *name)
1239{
1240 for ( ; list; list = list->next)
1241 if (!strcmp(list->name, name))
1242 return (struct ref *)list;
1243 return NULL;
1244}
1245
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001246static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
1247{
1248 for ( ; ref; ref = ref->next)
1249 string_list_append_nodup(ref_index, ref->name)->util = ref;
1250
Michael Haggerty3383e192014-11-25 09:02:35 +01001251 string_list_sort(ref_index);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001252}
1253
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001254/*
Jeff Kingba928c12014-03-05 14:04:54 -05001255 * Given only the set of local refs, sanity-check the set of push
1256 * refspecs. We can't catch all errors that match_push_refs would,
1257 * but we can catch some errors early before even talking to the
1258 * remote side.
1259 */
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001260int check_push_refs(struct ref *src, struct refspec *rs)
Jeff Kingba928c12014-03-05 14:04:54 -05001261{
Jeff Kingba928c12014-03-05 14:04:54 -05001262 int ret = 0;
1263 int i;
1264
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001265 for (i = 0; i < rs->nr; i++) {
1266 struct refspec_item *item = &rs->items[i];
Jeff Kingba928c12014-03-05 14:04:54 -05001267
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001268 if (item->pattern || item->matching)
Jeff Kingba928c12014-03-05 14:04:54 -05001269 continue;
1270
Brandon Williamsafb1aed2018-05-16 15:58:22 -07001271 ret |= match_explicit_lhs(src, item, NULL, NULL);
Jeff Kingba928c12014-03-05 14:04:54 -05001272 }
1273
Jeff Kingba928c12014-03-05 14:04:54 -05001274 return ret;
1275}
1276
1277/*
Junio C Hamano29753cd2011-09-09 11:54:58 -07001278 * Given the set of refs the local repository has, the set of refs the
1279 * remote repository has, and the refspec used for push, determine
1280 * what remote refs we will update and with what value by setting
1281 * peer_ref (which object is being pushed) and force (if the push is
1282 * forced) in elements of "dst". The function may add new elements to
1283 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001284 */
Junio C Hamano29753cd2011-09-09 11:54:58 -07001285int match_push_refs(struct ref *src, struct ref **dst,
Brandon Williams5c7ec842018-05-16 15:58:21 -07001286 struct refspec *rs, int flags)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001287{
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001288 int send_all = flags & MATCH_REFS_ALL;
1289 int send_mirror = flags & MATCH_REFS_MIRROR;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001290 int send_prune = flags & MATCH_REFS_PRUNE;
Jay Soffian5f48cb92009-02-25 03:32:17 -05001291 int errs;
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001292 struct ref *ref, **dst_tail = tail_ref(dst);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001293 struct string_list dst_ref_index = STRING_LIST_INIT_NODUP;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001294
Brandon Williams5c7ec842018-05-16 15:58:21 -07001295 /* If no refspec is provided, use the default ":" */
1296 if (!rs->nr)
1297 refspec_append(rs, ":");
1298
1299 errs = match_explicit_refs(src, *dst, &dst_tail, rs);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001300
1301 /* pick the remainder */
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001302 for (ref = src; ref; ref = ref->next) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001303 struct string_list_item *dst_item;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001304 struct ref *dst_peer;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001305 const struct refspec_item *pat = NULL;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001306 char *dst_name;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001307
Brandon Williams5c7ec842018-05-16 15:58:21 -07001308 dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat);
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001309 if (!dst_name)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001310 continue;
1311
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001312 if (!dst_ref_index.nr)
1313 prepare_ref_index(&dst_ref_index, *dst);
1314
1315 dst_item = string_list_lookup(&dst_ref_index, dst_name);
1316 dst_peer = dst_item ? dst_item->util : NULL;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001317 if (dst_peer) {
1318 if (dst_peer->peer_ref)
1319 /* We're already sending something to this ref. */
1320 goto free_name;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001321 } else {
1322 if (pat->matching && !(send_all || send_mirror))
1323 /*
1324 * Remote doesn't have it, and we have no
1325 * explicit pattern, and we don't have
Justin Lebar01689902014-03-31 15:11:46 -07001326 * --all or --mirror.
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001327 */
1328 goto free_name;
1329
Daniel Barkalow6b628162007-05-12 11:45:59 -04001330 /* Create a new one and link it */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001331 dst_peer = make_linked_ref(dst_name, &dst_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001332 oidcpy(&dst_peer->new_oid, &ref->new_oid);
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001333 string_list_insert(&dst_ref_index,
1334 dst_peer->name)->util = dst_peer;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001335 }
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001336 dst_peer->peer_ref = copy_ref(ref);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001337 dst_peer->force = pat->force;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001338 free_name:
1339 free(dst_name);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001340 }
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001341
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001342 string_list_clear(&dst_ref_index, 0);
1343
Junio C Hamanoc2aba152013-03-04 12:09:50 -08001344 if (flags & MATCH_REFS_FOLLOW_TAGS)
1345 add_missing_tags(src, dst, &dst_tail);
1346
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001347 if (send_prune) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001348 struct string_list src_ref_index = STRING_LIST_INIT_NODUP;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001349 /* check for missing refs on the remote */
1350 for (ref = *dst; ref; ref = ref->next) {
1351 char *src_name;
1352
1353 if (ref->peer_ref)
1354 /* We're already sending something to this ref. */
1355 continue;
1356
Brandon Williams5c7ec842018-05-16 15:58:21 -07001357 src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001358 if (src_name) {
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001359 if (!src_ref_index.nr)
1360 prepare_ref_index(&src_ref_index, src);
1361 if (!string_list_has_string(&src_ref_index,
1362 src_name))
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001363 ref->peer_ref = alloc_delete_ref();
1364 free(src_name);
1365 }
1366 }
Brandon Caseyf1bd15a2013-07-08 01:58:39 -07001367 string_list_clear(&src_ref_index, 0);
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001368 }
Brandon Williams8ca69372018-05-16 15:57:57 -07001369
Jay Soffian5f48cb92009-02-25 03:32:17 -05001370 if (errs)
1371 return -1;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001372 return 0;
1373}
Daniel Barkalowcf818342007-09-10 23:02:56 -04001374
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001375void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001376 int force_update)
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001377{
1378 struct ref *ref;
1379
1380 for (ref = remote_refs; ref; ref = ref->next) {
Chris Rorvick8c5f6f72012-11-29 19:41:36 -06001381 int force_ref_update = ref->force || force_update;
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001382 int reject_reason = 0;
Chris Rorvick8c5f6f72012-11-29 19:41:36 -06001383
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001384 if (ref->peer_ref)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001385 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001386 else if (!send_mirror)
1387 continue;
1388
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001389 ref->deletion = is_null_oid(&ref->new_oid);
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001390 if (!ref->deletion &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001391 !oidcmp(&ref->old_oid, &ref->new_oid)) {
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001392 ref->status = REF_STATUS_UPTODATE;
1393 continue;
1394 }
1395
Chris Rorvicka272b282012-11-29 19:41:40 -06001396 /*
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001397 * If the remote ref has moved and is now different
1398 * from what we expect, reject any push.
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001399 *
1400 * It also is an error if the user told us to check
1401 * with the remote-tracking branch to find the value
1402 * to expect, but we did not have such a tracking
1403 * branch.
1404 */
1405 if (ref->expect_old_sha1) {
John Keeping64ac39a2016-07-26 21:44:45 +01001406 if (oidcmp(&ref->old_oid, &ref->old_oid_expect))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001407 reject_reason = REF_STATUS_REJECT_STALE;
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001408 else
1409 /* If the ref isn't stale then force the update. */
1410 force_ref_update = 1;
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001411 }
1412
1413 /*
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001414 * If the update isn't already rejected then check
1415 * the usual "must fast-forward" rules.
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001416 *
Junio C Hamano256b9d72013-01-16 13:02:27 -08001417 * Decide whether an individual refspec A:B can be
1418 * pushed. The push will succeed if any of the
1419 * following are true:
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001420 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001421 * (1) the remote reference B does not exist
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001422 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001423 * (2) the remote reference B is being removed (i.e.,
1424 * pushing :B where no source is specified)
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001425 *
Junio C Hamano256b9d72013-01-16 13:02:27 -08001426 * (3) the destination is not under refs/tags/, and
1427 * if the old and new value is a commit, the new
1428 * is a descendant of the old.
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001429 *
Chris Rorvicka272b282012-11-29 19:41:40 -06001430 * (4) it is forced using the +A:B notation, or by
1431 * passing the --force argument
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001432 */
1433
Andrew Wheelerb2e93f82016-01-29 17:18:42 -06001434 if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
Christian Couder59556542013-11-30 21:55:40 +01001435 if (starts_with(ref->name, "refs/tags/"))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001436 reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001437 else if (!has_object_file(&ref->old_oid))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001438 reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
Stefan Beller21e1ee82018-06-28 18:21:57 -07001439 else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) ||
1440 !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001441 reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
brian m. carlson6f3d57b2015-11-10 02:22:25 +00001442 else if (!ref_newer(&ref->new_oid, &ref->old_oid))
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001443 reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001444 }
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001445
1446 /*
1447 * "--force" will defeat any rejection implemented
1448 * by the rules above.
1449 */
1450 if (!force_ref_update)
1451 ref->status = reject_reason;
1452 else if (reject_reason)
1453 ref->forced_update = 1;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001454 }
1455}
1456
Junio C Hamano05e73682014-10-14 14:42:04 -07001457static void set_merge(struct branch *ret)
1458{
Jeff King9e3751d2015-05-21 00:45:13 -04001459 struct remote *remote;
Junio C Hamano05e73682014-10-14 14:42:04 -07001460 char *ref;
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001461 struct object_id oid;
Junio C Hamano05e73682014-10-14 14:42:04 -07001462 int i;
1463
Jeff Kingee2499f2015-05-21 00:45:09 -04001464 if (!ret)
1465 return; /* no branch */
1466 if (ret->merge)
1467 return; /* already run */
1468 if (!ret->remote_name || !ret->merge_nr) {
1469 /*
1470 * no merge config; let's make sure we don't confuse callers
1471 * with a non-zero merge_nr but a NULL merge
1472 */
1473 ret->merge_nr = 0;
1474 return;
1475 }
1476
Jeff King9e3751d2015-05-21 00:45:13 -04001477 remote = remote_get(ret->remote_name);
1478
Junio C Hamano05e73682014-10-14 14:42:04 -07001479 ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
1480 for (i = 0; i < ret->merge_nr; i++) {
1481 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1482 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
Jeff King9e3751d2015-05-21 00:45:13 -04001483 if (!remote_find_tracking(remote, ret->merge[i]) ||
Junio C Hamano05e73682014-10-14 14:42:04 -07001484 strcmp(ret->remote_name, "."))
1485 continue;
1486 if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
brian m. carlsoncca5fa62017-10-15 22:06:57 +00001487 &oid, &ref) == 1)
Junio C Hamano05e73682014-10-14 14:42:04 -07001488 ret->merge[i]->dst = ref;
1489 else
1490 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
1491 }
1492}
1493
Daniel Barkalowcf818342007-09-10 23:02:56 -04001494struct branch *branch_get(const char *name)
1495{
1496 struct branch *ret;
1497
1498 read_config();
1499 if (!name || !*name || !strcmp(name, "HEAD"))
1500 ret = current_branch;
1501 else
1502 ret = make_branch(name, 0);
Jeff Kingee2499f2015-05-21 00:45:09 -04001503 set_merge(ret);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001504 return ret;
1505}
1506
1507int branch_has_merge_config(struct branch *branch)
1508{
1509 return branch && !!branch->merge;
1510}
1511
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001512int branch_merge_matches(struct branch *branch,
1513 int i,
1514 const char *refname)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001515{
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001516 if (!branch || i < 0 || i >= branch->merge_nr)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001517 return 0;
Michael Haggerty54457fe2014-01-14 04:16:07 +01001518 return refname_match(branch->merge[i]->src, refname);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001519}
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001520
Jeff King060e7762016-04-25 17:15:23 -04001521__attribute__((format (printf,2,3)))
Jeff King3a429d02015-05-21 00:45:32 -04001522static const char *error_buf(struct strbuf *err, const char *fmt, ...)
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001523{
Jeff King3a429d02015-05-21 00:45:32 -04001524 if (err) {
1525 va_list ap;
1526 va_start(ap, fmt);
1527 strbuf_vaddf(err, fmt, ap);
1528 va_end(ap);
1529 }
1530 return NULL;
1531}
1532
1533const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
1534{
1535 if (!branch)
1536 return error_buf(err, _("HEAD does not point to a branch"));
Jeff King1ca41a12015-05-21 20:46:43 -04001537
1538 if (!branch->merge || !branch->merge[0]) {
1539 /*
1540 * no merge config; is it because the user didn't define any,
1541 * or because it is not a real branch, and get_branch
1542 * auto-vivified it?
1543 */
Jeff King3a429d02015-05-21 00:45:32 -04001544 if (!ref_exists(branch->refname))
1545 return error_buf(err, _("no such branch: '%s'"),
1546 branch->name);
Jeff King1ca41a12015-05-21 20:46:43 -04001547 return error_buf(err,
1548 _("no upstream configured for branch '%s'"),
1549 branch->name);
1550 }
1551
1552 if (!branch->merge[0]->dst)
Jeff King3a429d02015-05-21 00:45:32 -04001553 return error_buf(err,
1554 _("upstream branch '%s' not stored as a remote-tracking branch"),
1555 branch->merge[0]->src);
Jeff King3a429d02015-05-21 00:45:32 -04001556
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001557 return branch->merge[0]->dst;
1558}
1559
Jeff Kinge291c752015-05-21 00:45:36 -04001560static const char *tracking_for_push_dest(struct remote *remote,
1561 const char *refname,
1562 struct strbuf *err)
1563{
1564 char *ret;
1565
Brandon Williamsd0004142018-05-16 15:58:11 -07001566 ret = apply_refspecs(&remote->fetch, refname);
Jeff Kinge291c752015-05-21 00:45:36 -04001567 if (!ret)
1568 return error_buf(err,
1569 _("push destination '%s' on remote '%s' has no local tracking branch"),
1570 refname, remote->name);
1571 return ret;
1572}
1573
1574static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1575{
1576 struct remote *remote;
1577
Jeff Kinge291c752015-05-21 00:45:36 -04001578 remote = remote_get(pushremote_for_branch(branch, NULL));
1579 if (!remote)
1580 return error_buf(err,
1581 _("branch '%s' has no remote for pushing"),
1582 branch->name);
1583
Brandon Williams6bdb3042018-05-16 15:58:00 -07001584 if (remote->push.nr) {
Jeff Kinge291c752015-05-21 00:45:36 -04001585 char *dst;
1586 const char *ret;
1587
Brandon Williamsd0004142018-05-16 15:58:11 -07001588 dst = apply_refspecs(&remote->push, branch->refname);
Jeff Kinge291c752015-05-21 00:45:36 -04001589 if (!dst)
1590 return error_buf(err,
1591 _("push refspecs for '%s' do not include '%s'"),
1592 remote->name, branch->name);
1593
1594 ret = tracking_for_push_dest(remote, dst, err);
1595 free(dst);
1596 return ret;
1597 }
1598
1599 if (remote->mirror)
1600 return tracking_for_push_dest(remote, branch->refname, err);
1601
1602 switch (push_default) {
1603 case PUSH_DEFAULT_NOTHING:
1604 return error_buf(err, _("push has no destination (push.default is 'nothing')"));
1605
1606 case PUSH_DEFAULT_MATCHING:
1607 case PUSH_DEFAULT_CURRENT:
1608 return tracking_for_push_dest(remote, branch->refname, err);
1609
1610 case PUSH_DEFAULT_UPSTREAM:
1611 return branch_get_upstream(branch, err);
1612
1613 case PUSH_DEFAULT_UNSPECIFIED:
1614 case PUSH_DEFAULT_SIMPLE:
1615 {
1616 const char *up, *cur;
1617
1618 up = branch_get_upstream(branch, err);
1619 if (!up)
1620 return NULL;
1621 cur = tracking_for_push_dest(remote, branch->refname, err);
1622 if (!cur)
1623 return NULL;
1624 if (strcmp(cur, up))
1625 return error_buf(err,
1626 _("cannot resolve 'simple' push to a single destination"));
1627 return cur;
1628 }
1629 }
1630
Johannes Schindelin033abf92018-05-02 11:38:39 +02001631 BUG("unhandled push situation");
Jeff Kinge291c752015-05-21 00:45:36 -04001632}
1633
1634const char *branch_get_push(struct branch *branch, struct strbuf *err)
1635{
Kyle Meyerb10731f2017-01-06 20:12:15 -05001636 if (!branch)
1637 return error_buf(err, _("HEAD does not point to a branch"));
1638
Jeff Kinge291c752015-05-21 00:45:36 -04001639 if (!branch->push_tracking_ref)
1640 branch->push_tracking_ref = branch_get_push_1(branch, err);
1641 return branch->push_tracking_ref;
1642}
1643
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001644static int ignore_symref_update(const char *refname)
1645{
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001646 int flag;
1647
René Scharfe744c0402017-09-23 11:45:04 +02001648 if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001649 return 0; /* non-existing refs are OK */
1650 return (flag & REF_ISSYMREF);
1651}
1652
Michael Haggertyf166db22013-10-30 06:32:56 +01001653/*
1654 * Create and return a list of (struct ref) consisting of copies of
1655 * each remote_ref that matches refspec. refspec must be a pattern.
1656 * Fill in the copies' peer_ref to describe the local tracking refs to
1657 * which they map. Omit any references that would map to an existing
1658 * local symbolic ref.
1659 */
Daniel Barkalow45773702007-10-29 21:05:40 -04001660static struct ref *get_expanded_map(const struct ref *remote_refs,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001661 const struct refspec_item *refspec)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001662{
Daniel Barkalow45773702007-10-29 21:05:40 -04001663 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001664 struct ref *ret = NULL;
1665 struct ref **tail = &ret;
1666
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001667 for (ref = remote_refs; ref; ref = ref->next) {
Michael Haggertye31a17f2013-10-30 06:32:57 +01001668 char *expn_name = NULL;
1669
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001670 if (strchr(ref->name, '^'))
1671 continue; /* a dereference item */
Daniel Barkalowe9282132009-03-07 01:11:34 -05001672 if (match_name_with_pattern(refspec->src, ref->name,
Junio C Hamanof8fb9712012-12-11 13:00:52 -08001673 refspec->dst, &expn_name) &&
1674 !ignore_symref_update(expn_name)) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001675 struct ref *cpy = copy_ref(ref);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001676
Daniel Barkalowe9282132009-03-07 01:11:34 -05001677 cpy->peer_ref = alloc_ref(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001678 if (refspec->force)
1679 cpy->peer_ref->force = 1;
1680 *tail = cpy;
1681 tail = &cpy->next;
1682 }
Michael Haggertye31a17f2013-10-30 06:32:57 +01001683 free(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001684 }
1685
1686 return ret;
1687}
1688
Daniel Barkalow45773702007-10-29 21:05:40 -04001689static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001690{
Daniel Barkalow45773702007-10-29 21:05:40 -04001691 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001692 for (ref = refs; ref; ref = ref->next) {
Michael Haggerty54457fe2014-01-14 04:16:07 +01001693 if (refname_match(name, ref->name))
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001694 return ref;
1695 }
1696 return NULL;
1697}
1698
Daniel Barkalow45773702007-10-29 21:05:40 -04001699struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001700{
Daniel Barkalow45773702007-10-29 21:05:40 -04001701 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001702
1703 if (!ref)
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001704 return NULL;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001705
1706 return copy_ref(ref);
1707}
1708
1709static struct ref *get_local_ref(const char *name)
1710{
Clemens Buchacher3eb96992009-06-17 15:38:36 +02001711 if (!name || name[0] == '\0')
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001712 return NULL;
1713
Christian Couder59556542013-11-30 21:55:40 +01001714 if (starts_with(name, "refs/"))
René Scharfe59c69c02008-10-18 10:44:18 +02001715 return alloc_ref(name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001716
Christian Couder59556542013-11-30 21:55:40 +01001717 if (starts_with(name, "heads/") ||
1718 starts_with(name, "tags/") ||
1719 starts_with(name, "remotes/"))
René Scharfe80097682008-10-18 10:37:40 +02001720 return alloc_ref_with_prefix("refs/", 5, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001721
René Scharfe80097682008-10-18 10:37:40 +02001722 return alloc_ref_with_prefix("refs/heads/", 11, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001723}
1724
Daniel Barkalow45773702007-10-29 21:05:40 -04001725int get_fetch_map(const struct ref *remote_refs,
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07001726 const struct refspec_item *refspec,
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001727 struct ref ***tail,
1728 int missing_ok)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001729{
Daniel Barkalowef00d152008-03-17 22:05:23 -04001730 struct ref *ref_map, **rmp;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001731
1732 if (refspec->pattern) {
1733 ref_map = get_expanded_map(remote_refs, refspec);
1734 } else {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001735 const char *name = refspec->src[0] ? refspec->src : "HEAD";
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001736
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001737 if (refspec->exact_sha1) {
1738 ref_map = alloc_ref(name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001739 get_oid_hex(name, &ref_map->old_oid);
Brandon Williams73302052018-06-27 15:30:23 -07001740 ref_map->exact_oid = 1;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -08001741 } else {
1742 ref_map = get_remote_ref(remote_refs, name);
1743 }
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001744 if (!missing_ok && !ref_map)
1745 die("Couldn't find remote ref %s", name);
1746 if (ref_map) {
1747 ref_map->peer_ref = get_local_ref(refspec->dst);
1748 if (ref_map->peer_ref && refspec->force)
1749 ref_map->peer_ref->force = 1;
1750 }
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001751 }
1752
Daniel Barkalowef00d152008-03-17 22:05:23 -04001753 for (rmp = &ref_map; *rmp; ) {
1754 if ((*rmp)->peer_ref) {
Christian Couder59556542013-11-30 21:55:40 +01001755 if (!starts_with((*rmp)->peer_ref->name, "refs/") ||
Junio C Hamano5c08c1f2012-05-04 15:35:18 -07001756 check_refname_format((*rmp)->peer_ref->name, 0)) {
Daniel Barkalowef00d152008-03-17 22:05:23 -04001757 struct ref *ignore = *rmp;
1758 error("* Ignoring funny ref '%s' locally",
1759 (*rmp)->peer_ref->name);
1760 *rmp = (*rmp)->next;
1761 free(ignore->peer_ref);
1762 free(ignore);
1763 continue;
1764 }
1765 }
1766 rmp = &((*rmp)->next);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001767 }
1768
Alex Riesen8f70a762007-10-12 22:40:04 +02001769 if (ref_map)
1770 tail_link_ref(ref_map, tail);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001771
1772 return 0;
1773}
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001774
1775int resolve_remote_symref(struct ref *ref, struct ref *list)
1776{
1777 if (!ref->symref)
1778 return 0;
1779 for (; list; list = list->next)
1780 if (!strcmp(ref->symref, list->name)) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001781 oidcpy(&ref->old_oid, &list->old_oid);
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001782 return 0;
1783 }
1784 return 1;
1785}
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001786
Jay Soffianec8452d2009-02-25 03:32:12 -05001787static void unmark_and_free(struct commit_list *list, unsigned int mark)
1788{
1789 while (list) {
René Scharfee510ab82015-10-24 18:21:31 +02001790 struct commit *commit = pop_commit(&list);
1791 commit->object.flags &= ~mark;
Jay Soffianec8452d2009-02-25 03:32:12 -05001792 }
1793}
1794
brian m. carlson6f3d57b2015-11-10 02:22:25 +00001795int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
Jay Soffianec8452d2009-02-25 03:32:12 -05001796{
1797 struct object *o;
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001798 struct commit *old_commit, *new_commit;
Jay Soffianec8452d2009-02-25 03:32:12 -05001799 struct commit_list *list, *used;
1800 int found = 0;
1801
Junio C Hamano75e5c0d2013-01-23 13:55:30 -08001802 /*
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001803 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
1804 * old_commit. Otherwise we require --force.
Jay Soffianec8452d2009-02-25 03:32:12 -05001805 */
Stefan Bellera74093d2018-06-28 18:22:05 -07001806 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
1807 NULL, 0);
Jay Soffianec8452d2009-02-25 03:32:12 -05001808 if (!o || o->type != OBJ_COMMIT)
1809 return 0;
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001810 old_commit = (struct commit *) o;
Jay Soffianec8452d2009-02-25 03:32:12 -05001811
Stefan Bellera74093d2018-06-28 18:22:05 -07001812 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
1813 NULL, 0);
Jay Soffianec8452d2009-02-25 03:32:12 -05001814 if (!o || o->type != OBJ_COMMIT)
1815 return 0;
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001816 new_commit = (struct commit *) o;
Jay Soffianec8452d2009-02-25 03:32:12 -05001817
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001818 if (parse_commit(new_commit) < 0)
Jay Soffianec8452d2009-02-25 03:32:12 -05001819 return 0;
1820
1821 used = list = NULL;
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001822 commit_list_insert(new_commit, &list);
Jay Soffianec8452d2009-02-25 03:32:12 -05001823 while (list) {
Brandon Williamsf3bbe632018-02-14 10:59:47 -08001824 new_commit = pop_most_recent_commit(&list, TMP_MARK);
1825 commit_list_insert(new_commit, &used);
1826 if (new_commit == old_commit) {
Jay Soffianec8452d2009-02-25 03:32:12 -05001827 found = 1;
1828 break;
1829 }
1830 }
1831 unmark_and_free(list, TMP_MARK);
1832 unmark_and_free(used, TMP_MARK);
1833 return found;
1834}
1835
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001836/*
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001837 * Lookup the upstream branch for the given branch and if present, optionally
1838 * compute the commit ahead/behind values for the pair.
1839 *
1840 * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
1841 * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
1842 * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
1843 * set to zero).
1844 *
1845 * The name of the upstream branch (or NULL if no upstream is defined) is
1846 * returned via *upstream_name, if it is not itself NULL.
Jiang Xinf2e08732013-08-26 15:02:48 +08001847 *
Jeff King979cb242015-05-21 20:49:11 -04001848 * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001849 * upstream defined, or ref does not exist). Returns 0 if the commits are
1850 * identical. Returns 1 if commits are different.
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001851 */
Jeff King979cb242015-05-21 20:49:11 -04001852int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001853 const char **upstream_name, enum ahead_behind_flags abf)
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001854{
brian m. carlsonfcd30b12015-11-10 02:22:30 +00001855 struct object_id oid;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001856 struct commit *ours, *theirs;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001857 struct rev_info revs;
Jeff King0b282cc2015-09-24 17:07:58 -04001858 const char *base;
1859 struct argv_array argv = ARGV_ARRAY_INIT;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001860
Jiang Xinf2e08732013-08-26 15:02:48 +08001861 /* Cannot stat unless we are marked to build on top of somebody else. */
Jeff King3a429d02015-05-21 00:45:32 -04001862 base = branch_get_upstream(branch, NULL);
Jeff King979cb242015-05-21 20:49:11 -04001863 if (upstream_name)
1864 *upstream_name = base;
Jeff Kinga9f9f8c2015-05-21 00:45:28 -04001865 if (!base)
Jeff King979cb242015-05-21 20:49:11 -04001866 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001867
Jiang Xinf2e08732013-08-26 15:02:48 +08001868 /* Cannot stat if what we used to build on no longer exists */
brian m. carlson34c290a2017-10-15 22:06:56 +00001869 if (read_ref(base, &oid))
Jiang Xinf2e08732013-08-26 15:02:48 +08001870 return -1;
Stefan Beller2122f672018-06-28 18:21:58 -07001871 theirs = lookup_commit_reference(the_repository, &oid);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001872 if (!theirs)
Jiang Xinf2e08732013-08-26 15:02:48 +08001873 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001874
brian m. carlson34c290a2017-10-15 22:06:56 +00001875 if (read_ref(branch->refname, &oid))
Jiang Xinf2e08732013-08-26 15:02:48 +08001876 return -1;
Stefan Beller2122f672018-06-28 18:21:58 -07001877 ours = lookup_commit_reference(the_repository, &oid);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001878 if (!ours)
Jiang Xinf2e08732013-08-26 15:02:48 +08001879 return -1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001880
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001881 *num_theirs = *num_ours = 0;
1882
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001883 /* are we the same? */
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001884 if (theirs == ours)
Jeff King979cb242015-05-21 20:49:11 -04001885 return 0;
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001886 if (abf == AHEAD_BEHIND_QUICK)
1887 return 1;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001888 if (abf != AHEAD_BEHIND_FULL)
1889 BUG("stat_tracking_info: invalid abf '%d'", abf);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001890
Junio C Hamano8fbf8792009-04-21 16:32:18 -07001891 /* Run "rev-list --left-right ours...theirs" internally... */
Jeff King0b282cc2015-09-24 17:07:58 -04001892 argv_array_push(&argv, ""); /* ignored */
1893 argv_array_push(&argv, "--left-right");
1894 argv_array_pushf(&argv, "%s...%s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001895 oid_to_hex(&ours->object.oid),
1896 oid_to_hex(&theirs->object.oid));
Jeff King0b282cc2015-09-24 17:07:58 -04001897 argv_array_push(&argv, "--");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001898
1899 init_revisions(&revs, NULL);
Jeff King0b282cc2015-09-24 17:07:58 -04001900 setup_revisions(argv.argc, argv.argv, &revs, NULL);
Stefan Beller81c3ce32014-08-10 23:33:26 +02001901 if (prepare_revision_walk(&revs))
1902 die("revision walk setup failed");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001903
1904 /* ... and count the commits on each side. */
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001905 while (1) {
1906 struct commit *c = get_revision(&revs);
1907 if (!c)
1908 break;
1909 if (c->object.flags & SYMMETRIC_LEFT)
1910 (*num_ours)++;
1911 else
1912 (*num_theirs)++;
1913 }
Junio C Hamanoc0234b22008-07-03 12:09:48 -07001914
1915 /* clear object flags smudged by the above traversal */
1916 clear_commit_marks(ours, ALL_REV_FLAGS);
1917 clear_commit_marks(theirs, ALL_REV_FLAGS);
Jeff King0b282cc2015-09-24 17:07:58 -04001918
1919 argv_array_clear(&argv);
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001920 return 1;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001921}
1922
1923/*
1924 * Return true when there is anything to report, otherwise false.
1925 */
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001926int format_tracking_info(struct branch *branch, struct strbuf *sb,
1927 enum ahead_behind_flags abf)
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001928{
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001929 int ours, theirs, sti;
Jeff King979cb242015-05-21 20:49:11 -04001930 const char *full_base;
Stefan Beller2f50bab2014-08-10 21:43:33 +02001931 char *base;
Jiang Xinf2e08732013-08-26 15:02:48 +08001932 int upstream_is_gone = 0;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001933
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001934 sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
1935 if (sti < 0) {
Jeff King979cb242015-05-21 20:49:11 -04001936 if (!full_base)
1937 return 0;
Jiang Xinf2e08732013-08-26 15:02:48 +08001938 upstream_is_gone = 1;
Jiang Xinf2e08732013-08-26 15:02:48 +08001939 }
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001940
Jeff King979cb242015-05-21 20:49:11 -04001941 base = shorten_unambiguous_ref(full_base, 0);
Jiang Xinf2e08732013-08-26 15:02:48 +08001942 if (upstream_is_gone) {
1943 strbuf_addf(sb,
1944 _("Your branch is based on '%s', but the upstream is gone.\n"),
1945 base);
1946 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001947 strbuf_addstr(sb,
Jiang Xinf2e08732013-08-26 15:02:48 +08001948 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001949 } else if (!sti) {
Jiang Xinf2234592013-08-26 15:02:49 +08001950 strbuf_addf(sb,
Martin Ågren7560f542017-08-23 19:49:35 +02001951 _("Your branch is up to date with '%s'.\n"),
Jiang Xinf2234592013-08-26 15:02:49 +08001952 base);
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001953 } else if (abf == AHEAD_BEHIND_QUICK) {
1954 strbuf_addf(sb,
1955 _("Your branch and '%s' refer to different commits.\n"),
1956 base);
1957 if (advice_status_hints)
1958 strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
1959 "git status --ahead-behind");
Jiang Xinf2e08732013-08-26 15:02:48 +08001960 } else if (!theirs) {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001961 strbuf_addf(sb,
1962 Q_("Your branch is ahead of '%s' by %d commit.\n",
1963 "Your branch is ahead of '%s' by %d commits.\n",
Jiang Xinf2e08732013-08-26 15:02:48 +08001964 ours),
1965 base, ours);
Jeff King491e3072012-12-03 01:16:57 -05001966 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001967 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001968 _(" (use \"git push\" to publish your local commits)\n"));
Jiang Xinf2e08732013-08-26 15:02:48 +08001969 } else if (!ours) {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001970 strbuf_addf(sb,
1971 Q_("Your branch is behind '%s' by %d commit, "
1972 "and can be fast-forwarded.\n",
1973 "Your branch is behind '%s' by %d commits, "
1974 "and can be fast-forwarded.\n",
Jiang Xinf2e08732013-08-26 15:02:48 +08001975 theirs),
1976 base, theirs);
Jeff King491e3072012-12-03 01:16:57 -05001977 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001978 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001979 _(" (use \"git pull\" to update your local branch)\n"));
Matthieu Moyc190ced2012-11-15 11:45:00 +01001980 } else {
Jiang Xin8a5b7492012-02-02 10:02:23 +08001981 strbuf_addf(sb,
1982 Q_("Your branch and '%s' have diverged,\n"
1983 "and have %d and %d different commit each, "
1984 "respectively.\n",
1985 "Your branch and '%s' have diverged,\n"
1986 "and have %d and %d different commits each, "
1987 "respectively.\n",
Nguyễn Thái Ngọc Duyf54bea42016-05-03 07:12:30 +07001988 ours + theirs),
Jiang Xinf2e08732013-08-26 15:02:48 +08001989 base, ours, theirs);
Jeff King491e3072012-12-03 01:16:57 -05001990 if (advice_status_hints)
René Scharfea22ae752016-09-15 20:31:00 +02001991 strbuf_addstr(sb,
Jeff King491e3072012-12-03 01:16:57 -05001992 _(" (use \"git pull\" to merge the remote branch into yours)\n"));
Matthieu Moyc190ced2012-11-15 11:45:00 +01001993 }
Stefan Beller2f50bab2014-08-10 21:43:33 +02001994 free(base);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001995 return 1;
1996}
Jay Soffian454e2022009-02-25 03:32:11 -05001997
Michael Haggerty455ade62015-05-25 18:39:01 +00001998static int one_local_ref(const char *refname, const struct object_id *oid,
1999 int flag, void *cb_data)
Jay Soffian454e2022009-02-25 03:32:11 -05002000{
2001 struct ref ***local_tail = cb_data;
2002 struct ref *ref;
Jay Soffian454e2022009-02-25 03:32:11 -05002003
2004 /* we already know it starts with refs/ to get here */
Michael Haggerty8d9c5012011-09-15 23:10:25 +02002005 if (check_refname_format(refname + 5, 0))
Jay Soffian454e2022009-02-25 03:32:11 -05002006 return 0;
2007
Jeff King96ffc062016-02-22 17:44:32 -05002008 ref = alloc_ref(refname);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002009 oidcpy(&ref->new_oid, oid);
Jay Soffian454e2022009-02-25 03:32:11 -05002010 **local_tail = ref;
2011 *local_tail = &ref->next;
2012 return 0;
2013}
2014
2015struct ref *get_local_heads(void)
2016{
Nguyễn Thái Ngọc Duy55f05662009-04-17 08:16:23 +10002017 struct ref *local_refs = NULL, **local_tail = &local_refs;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +00002018
Jay Soffian454e2022009-02-25 03:32:11 -05002019 for_each_ref(one_local_ref, &local_tail);
2020 return local_refs;
2021}
Jay Soffian8ef51732009-02-25 03:32:13 -05002022
Jay Soffian4229f1f2009-02-27 14:10:05 -05002023struct ref *guess_remote_head(const struct ref *head,
2024 const struct ref *refs,
2025 int all)
Jay Soffian8ef51732009-02-25 03:32:13 -05002026{
Jay Soffian8ef51732009-02-25 03:32:13 -05002027 const struct ref *r;
Jay Soffian4229f1f2009-02-27 14:10:05 -05002028 struct ref *list = NULL;
2029 struct ref **tail = &list;
Jay Soffian8ef51732009-02-25 03:32:13 -05002030
Jay Soffian6cb4e6c2009-02-25 03:32:14 -05002031 if (!head)
Jay Soffian8ef51732009-02-25 03:32:13 -05002032 return NULL;
2033
Jeff Kingfbb074c2009-02-27 14:10:06 -05002034 /*
2035 * Some transports support directly peeking at
2036 * where HEAD points; if that is the case, then
2037 * we don't have to guess.
2038 */
2039 if (head->symref)
2040 return copy_ref(find_ref_by_name(refs, head->symref));
2041
Jay Soffian8ef51732009-02-25 03:32:13 -05002042 /* If refs/heads/master could be right, it is. */
Jay Soffian4229f1f2009-02-27 14:10:05 -05002043 if (!all) {
2044 r = find_ref_by_name(refs, "refs/heads/master");
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002045 if (r && !oidcmp(&r->old_oid, &head->old_oid))
Jay Soffian4229f1f2009-02-27 14:10:05 -05002046 return copy_ref(r);
2047 }
Jay Soffian8ef51732009-02-25 03:32:13 -05002048
2049 /* Look for another ref that points there */
Jay Soffian4229f1f2009-02-27 14:10:05 -05002050 for (r = refs; r; r = r->next) {
Jeff King61adfd32011-06-03 01:11:13 -04002051 if (r != head &&
Christian Couder59556542013-11-30 21:55:40 +01002052 starts_with(r->name, "refs/heads/") &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002053 !oidcmp(&r->old_oid, &head->old_oid)) {
Jay Soffian4229f1f2009-02-27 14:10:05 -05002054 *tail = copy_ref(r);
2055 tail = &((*tail)->next);
2056 if (!all)
2057 break;
2058 }
2059 }
Jay Soffian8ef51732009-02-25 03:32:13 -05002060
Jay Soffian4229f1f2009-02-27 14:10:05 -05002061 return list;
Jay Soffian8ef51732009-02-25 03:32:13 -05002062}
Jay Soffianf2ef6072009-11-10 00:03:31 -05002063
2064struct stale_heads_info {
Jay Soffianf2ef6072009-11-10 00:03:31 -05002065 struct string_list *ref_names;
2066 struct ref **stale_refs_tail;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002067 struct refspec *rs;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002068};
2069
Michael Haggerty455ade62015-05-25 18:39:01 +00002070static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
2071 int flags, void *cb_data)
Jay Soffianf2ef6072009-11-10 00:03:31 -05002072{
2073 struct stale_heads_info *info = cb_data;
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002074 struct string_list matches = STRING_LIST_INIT_DUP;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07002075 struct refspec_item query;
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002076 int i, stale = 1;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -07002077 memset(&query, 0, sizeof(struct refspec_item));
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002078 query.dst = (char *)refname;
2079
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002080 query_refspecs_multiple(info->rs, &query, &matches);
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002081 if (matches.nr == 0)
2082 goto clean_exit; /* No matches */
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002083
2084 /*
2085 * If we did find a suitable refspec and it's not a symref and
2086 * it's not in the list of refs that currently exist in that
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002087 * remote, we consider it to be stale. In order to deal with
2088 * overlapping refspecs, we need to go over all of the
2089 * matching refs.
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002090 */
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002091 if (flags & REF_ISSYMREF)
2092 goto clean_exit;
2093
2094 for (i = 0; stale && i < matches.nr; i++)
2095 if (string_list_has_string(info->ref_names, matches.items[i].string))
2096 stale = 0;
2097
2098 if (stale) {
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002099 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00002100 oidcpy(&ref->new_oid, oid);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002101 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02002102
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +01002103clean_exit:
2104 string_list_clear(&matches, 0);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002105 return 0;
2106}
2107
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002108struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map)
Jay Soffianf2ef6072009-11-10 00:03:31 -05002109{
2110 struct ref *ref, *stale_refs = NULL;
Thiago Farina183113a2010-07-04 16:46:19 -03002111 struct string_list ref_names = STRING_LIST_INIT_NODUP;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002112 struct stale_heads_info info;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +00002113
Jay Soffianf2ef6072009-11-10 00:03:31 -05002114 info.ref_names = &ref_names;
2115 info.stale_refs_tail = &stale_refs;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07002116 info.rs = rs;
Jay Soffianf2ef6072009-11-10 00:03:31 -05002117 for (ref = fetch_map; ref; ref = ref->next)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01002118 string_list_append(&ref_names, ref->name);
Michael Haggerty3383e192014-11-25 09:02:35 +01002119 string_list_sort(&ref_names);
Jay Soffianf2ef6072009-11-10 00:03:31 -05002120 for_each_ref(get_stale_heads_cb, &info);
2121 string_list_clear(&ref_names, 0);
2122 return stale_refs;
2123}
Junio C Hamano28f5d172013-07-08 15:34:36 -07002124
2125/*
2126 * Compare-and-swap
2127 */
Junio C Hamanoa355b112015-01-14 14:58:44 -08002128static void clear_cas_option(struct push_cas_option *cas)
Junio C Hamano28f5d172013-07-08 15:34:36 -07002129{
2130 int i;
2131
2132 for (i = 0; i < cas->nr; i++)
2133 free(cas->entry[i].refname);
2134 free(cas->entry);
2135 memset(cas, 0, sizeof(*cas));
2136}
2137
2138static struct push_cas *add_cas_entry(struct push_cas_option *cas,
2139 const char *refname,
2140 size_t refnamelen)
2141{
2142 struct push_cas *entry;
2143 ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
2144 entry = &cas->entry[cas->nr++];
2145 memset(entry, 0, sizeof(*entry));
2146 entry->refname = xmemdupz(refname, refnamelen);
2147 return entry;
2148}
2149
Junio C Hamano86689762017-03-31 13:20:48 -07002150static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
Junio C Hamano28f5d172013-07-08 15:34:36 -07002151{
2152 const char *colon;
2153 struct push_cas *entry;
2154
2155 if (unset) {
2156 /* "--no-<option>" */
2157 clear_cas_option(cas);
2158 return 0;
2159 }
2160
2161 if (!arg) {
2162 /* just "--<option>" */
2163 cas->use_tracking_for_rest = 1;
2164 return 0;
2165 }
2166
2167 /* "--<option>=refname" or "--<option>=refname:value" */
2168 colon = strchrnul(arg, ':');
2169 entry = add_cas_entry(cas, arg, colon - arg);
2170 if (!*colon)
2171 entry->use_tracking = 1;
John Keepingeee98e72016-07-26 21:44:44 +01002172 else if (!colon[1])
brian m. carlsonb8566f82017-07-13 23:49:21 +00002173 oidclr(&entry->expect);
2174 else if (get_oid(colon + 1, &entry->expect))
Junio C Hamano28f5d172013-07-08 15:34:36 -07002175 return error("cannot parse expected object name '%s'", colon + 1);
2176 return 0;
2177}
2178
2179int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
2180{
2181 return parse_push_cas_option(opt->value, arg, unset);
2182}
Junio C Hamano91048a92013-07-09 11:01:06 -07002183
2184int is_empty_cas(const struct push_cas_option *cas)
2185{
2186 return !cas->use_tracking_for_rest && !cas->nr;
2187}
2188
2189/*
2190 * Look at remote.fetch refspec and see if we have a remote
2191 * tracking branch for the refname there. Fill its current
2192 * value in sha1[].
2193 * If we cannot do so, return negative to signal an error.
2194 */
2195static int remote_tracking(struct remote *remote, const char *refname,
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002196 struct object_id *oid)
Junio C Hamano91048a92013-07-09 11:01:06 -07002197{
2198 char *dst;
2199
Brandon Williamsd0004142018-05-16 15:58:11 -07002200 dst = apply_refspecs(&remote->fetch, refname);
Junio C Hamano91048a92013-07-09 11:01:06 -07002201 if (!dst)
2202 return -1; /* no tracking ref for refname at remote */
brian m. carlson34c290a2017-10-15 22:06:56 +00002203 if (read_ref(dst, oid))
Junio C Hamano91048a92013-07-09 11:01:06 -07002204 return -1; /* we know what the tracking ref is but we cannot read it */
2205 return 0;
2206}
2207
2208static void apply_cas(struct push_cas_option *cas,
2209 struct remote *remote,
2210 struct ref *ref)
2211{
2212 int i;
2213
2214 /* Find an explicit --<option>=<name>[:<value>] entry */
2215 for (i = 0; i < cas->nr; i++) {
2216 struct push_cas *entry = &cas->entry[i];
Michael Haggerty54457fe2014-01-14 04:16:07 +01002217 if (!refname_match(entry->refname, ref->name))
Junio C Hamano91048a92013-07-09 11:01:06 -07002218 continue;
2219 ref->expect_old_sha1 = 1;
2220 if (!entry->use_tracking)
brian m. carlsonb8566f82017-07-13 23:49:21 +00002221 oidcpy(&ref->old_oid_expect, &entry->expect);
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002222 else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
John Keeping64ac39a2016-07-26 21:44:45 +01002223 oidclr(&ref->old_oid_expect);
Junio C Hamano91048a92013-07-09 11:01:06 -07002224 return;
2225 }
2226
2227 /* Are we using "--<option>" to cover all? */
2228 if (!cas->use_tracking_for_rest)
2229 return;
2230
2231 ref->expect_old_sha1 = 1;
brian m. carlsonfcd30b12015-11-10 02:22:30 +00002232 if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
John Keeping64ac39a2016-07-26 21:44:45 +01002233 oidclr(&ref->old_oid_expect);
Junio C Hamano91048a92013-07-09 11:01:06 -07002234}
2235
2236void apply_push_cas(struct push_cas_option *cas,
2237 struct remote *remote,
2238 struct ref *remote_refs)
2239{
2240 struct ref *ref;
2241 for (ref = remote_refs; ref; ref = ref->next)
2242 apply_cas(cas, remote, ref);
2243}