blob: 9143ec7a1772c45b6c3d4c84f6fc000040400dee [file] [log] [blame]
Daniel Barkalow5751f492007-05-12 11:45:53 -04001#include "cache.h"
2#include "remote.h"
3#include "refs.h"
Junio C Hamano6d21bf92008-07-02 00:51:18 -07004#include "commit.h"
5#include "diff.h"
6#include "revision.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +03007#include "dir.h"
Jay Soffianec8452d2009-02-25 03:32:12 -05008#include "tag.h"
Julian Phillips73cf0822009-10-25 21:28:11 +00009#include "string-list.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -040010
Daniel Barkalowe0aaa292008-04-17 19:32:35 -040011static struct refspec s_tag_refspec = {
12 0,
13 1,
Junio C Hamanob84c3432008-05-25 13:38:44 -070014 0,
Daniel Barkalow08fbdb32009-03-07 01:11:36 -050015 "refs/tags/*",
16 "refs/tags/*"
Daniel Barkalowe0aaa292008-04-17 19:32:35 -040017};
18
19const struct refspec *tag_refspec = &s_tag_refspec;
20
Junio C Hamano844112c2008-02-24 22:25:04 -080021struct counted_string {
22 size_t len;
23 const char *s;
24};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050025struct rewrite {
26 const char *base;
Junio C Hamano844112c2008-02-24 22:25:04 -080027 size_t baselen;
28 struct counted_string *instead_of;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050029 int instead_of_nr;
30 int instead_of_alloc;
31};
Josh Triplettd071d942009-09-07 01:56:00 -070032struct rewrites {
33 struct rewrite **rewrite;
34 int rewrite_alloc;
35 int rewrite_nr;
36};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050037
Daniel Barkalow5751f492007-05-12 11:45:53 -040038static struct remote **remotes;
Daniel Barkalow2d313472008-02-18 23:41:41 -050039static int remotes_alloc;
40static int remotes_nr;
Daniel Barkalow5751f492007-05-12 11:45:53 -040041
Daniel Barkalowcf818342007-09-10 23:02:56 -040042static struct branch **branches;
Daniel Barkalow2d313472008-02-18 23:41:41 -050043static int branches_alloc;
44static int branches_nr;
Daniel Barkalowcf818342007-09-10 23:02:56 -040045
46static struct branch *current_branch;
47static const char *default_remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -040048static int explicit_default_remote_name;
Daniel Barkalowcf818342007-09-10 23:02:56 -040049
Josh Triplettd071d942009-09-07 01:56:00 -070050static struct rewrites rewrites;
Josh Triplett1c2eafb2009-09-07 01:56:33 -070051static struct rewrites rewrites_push;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050052
Daniel Barkalow5751f492007-05-12 11:45:53 -040053#define BUF_SIZE (2048)
54static char buffer[BUF_SIZE];
55
Daniel Barkalow0a4da292009-11-18 02:42:23 +010056static int valid_remote(const struct remote *remote)
57{
Daniel Barkalowc578f512009-11-18 02:42:25 +010058 return (!!remote->url) || (!!remote->foreign_vcs);
Daniel Barkalow0a4da292009-11-18 02:42:23 +010059}
60
Josh Triplettd071d942009-09-07 01:56:00 -070061static const char *alias_url(const char *url, struct rewrites *r)
Daniel Barkalow55029ae2008-02-20 13:43:53 -050062{
63 int i, j;
Junio C Hamano844112c2008-02-24 22:25:04 -080064 char *ret;
65 struct counted_string *longest;
66 int longest_i;
67
68 longest = NULL;
69 longest_i = -1;
Josh Triplettd071d942009-09-07 01:56:00 -070070 for (i = 0; i < r->rewrite_nr; i++) {
71 if (!r->rewrite[i])
Daniel Barkalow55029ae2008-02-20 13:43:53 -050072 continue;
Josh Triplettd071d942009-09-07 01:56:00 -070073 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
74 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
Junio C Hamano844112c2008-02-24 22:25:04 -080075 (!longest ||
Josh Triplettd071d942009-09-07 01:56:00 -070076 longest->len < r->rewrite[i]->instead_of[j].len)) {
77 longest = &(r->rewrite[i]->instead_of[j]);
Junio C Hamano844112c2008-02-24 22:25:04 -080078 longest_i = i;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050079 }
80 }
81 }
Junio C Hamano844112c2008-02-24 22:25:04 -080082 if (!longest)
83 return url;
84
Josh Triplettd071d942009-09-07 01:56:00 -070085 ret = xmalloc(r->rewrite[longest_i]->baselen +
Junio C Hamano844112c2008-02-24 22:25:04 -080086 (strlen(url) - longest->len) + 1);
Josh Triplettd071d942009-09-07 01:56:00 -070087 strcpy(ret, r->rewrite[longest_i]->base);
88 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
Junio C Hamano844112c2008-02-24 22:25:04 -080089 return ret;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050090}
91
Daniel Barkalow5751f492007-05-12 11:45:53 -040092static void add_push_refspec(struct remote *remote, const char *ref)
93{
Daniel Barkalow2d313472008-02-18 23:41:41 -050094 ALLOC_GROW(remote->push_refspec,
95 remote->push_refspec_nr + 1,
96 remote->push_refspec_alloc);
97 remote->push_refspec[remote->push_refspec_nr++] = ref;
Daniel Barkalow5751f492007-05-12 11:45:53 -040098}
99
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400100static void add_fetch_refspec(struct remote *remote, const char *ref)
101{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500102 ALLOC_GROW(remote->fetch_refspec,
103 remote->fetch_refspec_nr + 1,
104 remote->fetch_refspec_alloc);
105 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400106}
107
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400108static void add_url(struct remote *remote, const char *url)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400109{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500110 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
111 remote->url[remote->url_nr++] = url;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400112}
113
Michael J Gruber20346232009-06-09 18:01:34 +0200114static void add_pushurl(struct remote *remote, const char *pushurl)
115{
116 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
117 remote->pushurl[remote->pushurl_nr++] = pushurl;
118}
119
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700120static void add_pushurl_alias(struct remote *remote, const char *url)
121{
122 const char *pushurl = alias_url(url, &rewrites_push);
123 if (pushurl != url)
124 add_pushurl(remote, pushurl);
125}
126
127static void add_url_alias(struct remote *remote, const char *url)
128{
129 add_url(remote, alias_url(url, &rewrites));
130 add_pushurl_alias(remote, url);
131}
132
Daniel Barkalow5751f492007-05-12 11:45:53 -0400133static struct remote *make_remote(const char *name, int len)
134{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500135 struct remote *ret;
136 int i;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400137
Daniel Barkalow2d313472008-02-18 23:41:41 -0500138 for (i = 0; i < remotes_nr; i++) {
139 if (len ? (!strncmp(name, remotes[i]->name, len) &&
140 !remotes[i]->name[len]) :
141 !strcmp(name, remotes[i]->name))
142 return remotes[i];
Daniel Barkalow5751f492007-05-12 11:45:53 -0400143 }
144
Daniel Barkalow2d313472008-02-18 23:41:41 -0500145 ret = xcalloc(1, sizeof(struct remote));
146 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
147 remotes[remotes_nr++] = ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400148 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500149 ret->name = xstrndup(name, len);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400150 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500151 ret->name = xstrdup(name);
152 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400153}
154
Daniel Barkalowcf818342007-09-10 23:02:56 -0400155static void add_merge(struct branch *branch, const char *name)
156{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500157 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
158 branch->merge_alloc);
159 branch->merge_name[branch->merge_nr++] = name;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400160}
161
162static struct branch *make_branch(const char *name, int len)
163{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500164 struct branch *ret;
165 int i;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400166 char *refname;
167
Daniel Barkalow2d313472008-02-18 23:41:41 -0500168 for (i = 0; i < branches_nr; i++) {
169 if (len ? (!strncmp(name, branches[i]->name, len) &&
170 !branches[i]->name[len]) :
171 !strcmp(name, branches[i]->name))
172 return branches[i];
Daniel Barkalowcf818342007-09-10 23:02:56 -0400173 }
174
Daniel Barkalow2d313472008-02-18 23:41:41 -0500175 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
176 ret = xcalloc(1, sizeof(struct branch));
177 branches[branches_nr++] = ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400178 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500179 ret->name = xstrndup(name, len);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400180 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500181 ret->name = xstrdup(name);
Dotan Barake8eec712008-09-09 21:57:10 +0300182 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400183 strcpy(refname, "refs/heads/");
Daniel Barkalow2d313472008-02-18 23:41:41 -0500184 strcpy(refname + strlen("refs/heads/"), ret->name);
185 ret->refname = refname;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400186
Daniel Barkalow2d313472008-02-18 23:41:41 -0500187 return ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400188}
189
Josh Triplettd071d942009-09-07 01:56:00 -0700190static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500191{
192 struct rewrite *ret;
193 int i;
194
Josh Triplettd071d942009-09-07 01:56:00 -0700195 for (i = 0; i < r->rewrite_nr; i++) {
Junio C Hamano844112c2008-02-24 22:25:04 -0800196 if (len
Josh Triplettd071d942009-09-07 01:56:00 -0700197 ? (len == r->rewrite[i]->baselen &&
198 !strncmp(base, r->rewrite[i]->base, len))
199 : !strcmp(base, r->rewrite[i]->base))
200 return r->rewrite[i];
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500201 }
202
Josh Triplettd071d942009-09-07 01:56:00 -0700203 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500204 ret = xcalloc(1, sizeof(struct rewrite));
Josh Triplettd071d942009-09-07 01:56:00 -0700205 r->rewrite[r->rewrite_nr++] = ret;
Junio C Hamano844112c2008-02-24 22:25:04 -0800206 if (len) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500207 ret->base = xstrndup(base, len);
Junio C Hamano844112c2008-02-24 22:25:04 -0800208 ret->baselen = len;
209 }
210 else {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500211 ret->base = xstrdup(base);
Junio C Hamano844112c2008-02-24 22:25:04 -0800212 ret->baselen = strlen(base);
213 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500214 return ret;
215}
216
217static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
218{
219 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
Junio C Hamano844112c2008-02-24 22:25:04 -0800220 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
221 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
222 rewrite->instead_of_nr++;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500223}
224
Daniel Barkalow5751f492007-05-12 11:45:53 -0400225static void read_remotes_file(struct remote *remote)
226{
227 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
228
229 if (!f)
230 return;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100231 remote->origin = REMOTE_REMOTES;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400232 while (fgets(buffer, BUF_SIZE, f)) {
233 int value_list;
234 char *s, *p;
235
236 if (!prefixcmp(buffer, "URL:")) {
237 value_list = 0;
238 s = buffer + 4;
239 } else if (!prefixcmp(buffer, "Push:")) {
240 value_list = 1;
241 s = buffer + 5;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400242 } else if (!prefixcmp(buffer, "Pull:")) {
243 value_list = 2;
244 s = buffer + 5;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400245 } else
246 continue;
247
248 while (isspace(*s))
249 s++;
250 if (!*s)
251 continue;
252
253 p = s + strlen(s);
254 while (isspace(p[-1]))
255 *--p = 0;
256
257 switch (value_list) {
258 case 0:
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500259 add_url_alias(remote, xstrdup(s));
Daniel Barkalow5751f492007-05-12 11:45:53 -0400260 break;
261 case 1:
262 add_push_refspec(remote, xstrdup(s));
263 break;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400264 case 2:
265 add_fetch_refspec(remote, xstrdup(s));
266 break;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400267 }
268 }
269 fclose(f);
270}
271
272static void read_branches_file(struct remote *remote)
273{
274 const char *slash = strchr(remote->name, '/');
Daniel Barkalowcf818342007-09-10 23:02:56 -0400275 char *frag;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500276 struct strbuf branch = STRBUF_INIT;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400277 int n = slash ? slash - remote->name : 1000;
278 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
279 char *s, *p;
280 int len;
281
282 if (!f)
283 return;
284 s = fgets(buffer, BUF_SIZE, f);
285 fclose(f);
286 if (!s)
287 return;
288 while (isspace(*s))
289 s++;
290 if (!*s)
291 return;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100292 remote->origin = REMOTE_BRANCHES;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400293 p = s + strlen(s);
294 while (isspace(p[-1]))
295 *--p = 0;
296 len = p - s;
297 if (slash)
298 len += strlen(slash);
299 p = xmalloc(len + 1);
300 strcpy(p, s);
301 if (slash)
302 strcat(p, slash);
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400303
304 /*
305 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
306 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
307 * the partial URL obtained from the branches file plus
308 * "/netdev-2.6" and does not store it in any tracking ref.
309 * #branch specifier in the file is ignored.
310 *
311 * Otherwise, the branches file would have URL and optionally
312 * #branch specified. The "master" (or specified) branch is
313 * fetched and stored in the local branch of the same name.
314 */
Daniel Barkalowcf818342007-09-10 23:02:56 -0400315 frag = strchr(p, '#');
316 if (frag) {
317 *(frag++) = '\0';
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400318 strbuf_addf(&branch, "refs/heads/%s", frag);
319 } else
320 strbuf_addstr(&branch, "refs/heads/master");
321 if (!slash) {
322 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400323 } else {
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400324 strbuf_reset(&branch);
325 strbuf_addstr(&branch, "HEAD:");
Daniel Barkalowcf818342007-09-10 23:02:56 -0400326 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500327 add_url_alias(remote, p);
Linus Torvalds2af202b2009-06-18 10:28:43 -0700328 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
Martin Koegler18afe102008-11-10 22:47:11 +0100329 /*
330 * Cogito compatible push: push current HEAD to remote #branch
331 * (master if missing)
332 */
333 strbuf_init(&branch, 0);
334 strbuf_addstr(&branch, "HEAD");
335 if (frag)
336 strbuf_addf(&branch, ":refs/heads/%s", frag);
337 else
338 strbuf_addstr(&branch, ":refs/heads/master");
Linus Torvalds2af202b2009-06-18 10:28:43 -0700339 add_push_refspec(remote, strbuf_detach(&branch, NULL));
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400340 remote->fetch_tags = 1; /* always auto-follow */
Daniel Barkalow5751f492007-05-12 11:45:53 -0400341}
342
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100343static int handle_config(const char *key, const char *value, void *cb)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400344{
345 const char *name;
346 const char *subkey;
347 struct remote *remote;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400348 struct branch *branch;
349 if (!prefixcmp(key, "branch.")) {
350 name = key + 7;
351 subkey = strrchr(name, '.');
Daniel Barkalowcf818342007-09-10 23:02:56 -0400352 if (!subkey)
353 return 0;
Junio C Hamano896c0532007-12-14 20:34:56 -0800354 branch = make_branch(name, subkey - name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400355 if (!strcmp(subkey, ".remote")) {
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800356 if (!value)
357 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400358 branch->remote_name = xstrdup(value);
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400359 if (branch == current_branch) {
Daniel Barkalowcf818342007-09-10 23:02:56 -0400360 default_remote_name = branch->remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400361 explicit_default_remote_name = 1;
362 }
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800363 } else if (!strcmp(subkey, ".merge")) {
364 if (!value)
365 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400366 add_merge(branch, xstrdup(value));
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800367 }
Daniel Barkalowcf818342007-09-10 23:02:56 -0400368 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400369 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500370 if (!prefixcmp(key, "url.")) {
371 struct rewrite *rewrite;
Daniel Barkalow60e3aba2008-04-12 15:32:00 -0400372 name = key + 4;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500373 subkey = strrchr(name, '.');
374 if (!subkey)
375 return 0;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500376 if (!strcmp(subkey, ".insteadof")) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700377 rewrite = make_rewrite(&rewrites, name, subkey - name);
378 if (!value)
379 return config_error_nonbool(key);
380 add_instead_of(rewrite, xstrdup(value));
381 } else if (!strcmp(subkey, ".pushinsteadof")) {
382 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500383 if (!value)
384 return config_error_nonbool(key);
385 add_instead_of(rewrite, xstrdup(value));
386 }
387 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400388 if (prefixcmp(key, "remote."))
389 return 0;
390 name = key + 7;
Brandon Caseyc82efaf2008-10-14 15:30:21 -0500391 if (*name == '/') {
392 warning("Config remote shorthand cannot begin with '/': %s",
393 name);
394 return 0;
395 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400396 subkey = strrchr(name, '.');
397 if (!subkey)
Johannes Sixtcd294bc2009-04-23 15:49:05 +0200398 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400399 remote = make_remote(name, subkey - name);
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100400 remote->origin = REMOTE_CONFIG;
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200401 if (!strcmp(subkey, ".mirror"))
402 remote->mirror = git_config_bool(key, value);
403 else if (!strcmp(subkey, ".skipdefaultupdate"))
404 remote->skip_default_update = git_config_bool(key, value);
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100405 else if (!strcmp(subkey, ".skipfetchall"))
406 remote->skip_default_update = git_config_bool(key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200407 else if (!strcmp(subkey, ".url")) {
408 const char *v;
409 if (git_config_string(&v, key, value))
410 return -1;
411 add_url(remote, v);
Michael J Gruber20346232009-06-09 18:01:34 +0200412 } else if (!strcmp(subkey, ".pushurl")) {
413 const char *v;
414 if (git_config_string(&v, key, value))
415 return -1;
416 add_pushurl(remote, v);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400417 } else if (!strcmp(subkey, ".push")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200418 const char *v;
419 if (git_config_string(&v, key, value))
420 return -1;
421 add_push_refspec(remote, v);
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400422 } else if (!strcmp(subkey, ".fetch")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200423 const char *v;
424 if (git_config_string(&v, key, value))
425 return -1;
426 add_fetch_refspec(remote, v);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400427 } else if (!strcmp(subkey, ".receivepack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200428 const char *v;
429 if (git_config_string(&v, key, value))
430 return -1;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400431 if (!remote->receivepack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200432 remote->receivepack = v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400433 else
434 error("more than one receivepack given, using the first");
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400435 } else if (!strcmp(subkey, ".uploadpack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200436 const char *v;
437 if (git_config_string(&v, key, value))
438 return -1;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400439 if (!remote->uploadpack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200440 remote->uploadpack = v;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400441 else
442 error("more than one uploadpack given, using the first");
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400443 } else if (!strcmp(subkey, ".tagopt")) {
444 if (!strcmp(value, "--no-tags"))
445 remote->fetch_tags = -1;
Samuel Tardieu944163a2010-04-20 01:31:25 +0200446 else if (!strcmp(value, "--tags"))
447 remote->fetch_tags = 2;
Sam Vilain14c98212007-12-04 10:48:54 +1300448 } else if (!strcmp(subkey, ".proxy")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200449 return git_config_string((const char **)&remote->http_proxy,
450 key, value);
Daniel Barkalowc578f512009-11-18 02:42:25 +0100451 } else if (!strcmp(subkey, ".vcs")) {
452 return git_config_string(&remote->foreign_vcs, key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200453 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400454 return 0;
455}
456
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500457static void alias_all_urls(void)
458{
459 int i, j;
460 for (i = 0; i < remotes_nr; i++) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700461 int add_pushurl_aliases;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500462 if (!remotes[i])
463 continue;
Michael J Gruber20346232009-06-09 18:01:34 +0200464 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
Josh Triplettd071d942009-09-07 01:56:00 -0700465 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
Michael J Gruber20346232009-06-09 18:01:34 +0200466 }
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700467 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
468 for (j = 0; j < remotes[i]->url_nr; j++) {
469 if (add_pushurl_aliases)
470 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
471 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
472 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500473 }
474}
475
Daniel Barkalow5751f492007-05-12 11:45:53 -0400476static void read_config(void)
477{
478 unsigned char sha1[20];
479 const char *head_ref;
480 int flag;
Tor Arntsen2543d9b2010-06-04 11:32:11 +0200481 if (default_remote_name) /* did this already */
Daniel Barkalow5751f492007-05-12 11:45:53 -0400482 return;
483 default_remote_name = xstrdup("origin");
484 current_branch = NULL;
485 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
486 if (head_ref && (flag & REF_ISSYMREF) &&
487 !prefixcmp(head_ref, "refs/heads/")) {
Daniel Barkalowcf818342007-09-10 23:02:56 -0400488 current_branch =
489 make_branch(head_ref + strlen("refs/heads/"), 0);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400490 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100491 git_config(handle_config, NULL);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500492 alias_all_urls();
Daniel Barkalow5751f492007-05-12 11:45:53 -0400493}
494
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700495/*
496 * We need to make sure the tracking branches are well formed, but a
497 * wildcard refspec in "struct refspec" must have a trailing slash. We
498 * temporarily drop the trailing '/' while calling check_ref_format(),
499 * and put it back. The caller knows that a CHECK_REF_FORMAT_ONELEVEL
500 * error return is Ok for a wildcard refspec.
501 */
502static int verify_refname(char *name, int is_glob)
503{
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500504 int result;
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700505
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700506 result = check_ref_format(name);
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500507 if (is_glob && result == CHECK_REF_FORMAT_WILDCARD)
508 result = CHECK_REF_FORMAT_OK;
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700509 return result;
510}
511
Brandon Casey2cb1f362008-08-21 19:16:30 -0500512/*
513 * This function frees a refspec array.
514 * Warning: code paths should be checked to ensure that the src
515 * and dst pointers are always freeable pointers as well
516 * as the refspec pointer itself.
517 */
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900518static void free_refspecs(struct refspec *refspec, int nr_refspec)
Brandon Casey2cb1f362008-08-21 19:16:30 -0500519{
520 int i;
521
522 if (!refspec)
523 return;
524
525 for (i = 0; i < nr_refspec; i++) {
526 free(refspec[i].src);
527 free(refspec[i].dst);
528 }
529 free(refspec);
530}
531
Jonas Fonseca24b61772008-04-13 11:56:54 +0200532static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400533{
534 int i;
Daniel Barkalowef00d152008-03-17 22:05:23 -0400535 int st;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400536 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700537
Daniel Barkalow6b628162007-05-12 11:45:59 -0400538 for (i = 0; i < nr_refspec; i++) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700539 size_t llen;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700540 int is_glob;
541 const char *lhs, *rhs;
542
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100543 is_glob = 0;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700544
545 lhs = refspec[i];
546 if (*lhs == '+') {
Daniel Barkalow6b628162007-05-12 11:45:59 -0400547 rs[i].force = 1;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700548 lhs++;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400549 }
Junio C Hamano46220ca2008-03-20 23:34:37 -0700550
551 rhs = strrchr(lhs, ':');
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400552
553 /*
554 * Before going on, special case ":" (or "+:") as a refspec
555 * for matching refs.
556 */
557 if (!fetch && rhs == lhs && rhs[1] == '\0') {
558 rs[i].matching = 1;
559 continue;
560 }
561
Junio C Hamano46220ca2008-03-20 23:34:37 -0700562 if (rhs) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700563 size_t rlen = strlen(++rhs);
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500564 is_glob = (1 <= rlen && strchr(rhs, '*'));
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500565 rs[i].dst = xstrndup(rhs, rlen);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700566 }
567
568 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500569 if (1 <= llen && memchr(lhs, '*', llen)) {
Junio C Hamano7d19da42008-03-25 21:15:52 -0700570 if ((rhs && !is_glob) || (!rhs && fetch))
571 goto invalid;
572 is_glob = 1;
Junio C Hamano7d19da42008-03-25 21:15:52 -0700573 } else if (rhs && is_glob) {
574 goto invalid;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700575 }
Junio C Hamano7d19da42008-03-25 21:15:52 -0700576
Junio C Hamano46220ca2008-03-20 23:34:37 -0700577 rs[i].pattern = is_glob;
578 rs[i].src = xstrndup(lhs, llen);
579
580 if (fetch) {
581 /*
582 * LHS
583 * - empty is allowed; it means HEAD.
584 * - otherwise it must be a valid looking ref.
585 */
586 if (!*rs[i].src)
587 ; /* empty is ok */
588 else {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700589 st = verify_refname(rs[i].src, is_glob);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700590 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
591 goto invalid;
592 }
593 /*
594 * RHS
Junio C Hamano7d19da42008-03-25 21:15:52 -0700595 * - missing is ok, and is same as empty.
Junio C Hamano46220ca2008-03-20 23:34:37 -0700596 * - empty is ok; it means not to store.
597 * - otherwise it must be a valid looking ref.
598 */
599 if (!rs[i].dst) {
600 ; /* ok */
601 } else if (!*rs[i].dst) {
602 ; /* ok */
603 } else {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700604 st = verify_refname(rs[i].dst, is_glob);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700605 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
606 goto invalid;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400607 }
608 } else {
Junio C Hamano46220ca2008-03-20 23:34:37 -0700609 /*
610 * LHS
611 * - empty is allowed; it means delete.
612 * - when wildcarded, it must be a valid looking ref.
613 * - otherwise, it must be an extended SHA-1, but
614 * there is no existing way to validate this.
615 */
616 if (!*rs[i].src)
617 ; /* empty is ok */
618 else if (is_glob) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700619 st = verify_refname(rs[i].src, is_glob);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700620 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
621 goto invalid;
622 }
623 else
624 ; /* anything goes, for now */
625 /*
626 * RHS
627 * - missing is allowed, but LHS then must be a
628 * valid looking ref.
629 * - empty is not allowed.
630 * - otherwise it must be a valid looking ref.
631 */
632 if (!rs[i].dst) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700633 st = verify_refname(rs[i].src, is_glob);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700634 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
635 goto invalid;
636 } else if (!*rs[i].dst) {
637 goto invalid;
638 } else {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700639 st = verify_refname(rs[i].dst, is_glob);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700640 if (st && st != CHECK_REF_FORMAT_ONELEVEL)
641 goto invalid;
642 }
Daniel Barkalowef00d152008-03-17 22:05:23 -0400643 }
Daniel Barkalow6b628162007-05-12 11:45:59 -0400644 }
645 return rs;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700646
647 invalid:
Jonas Fonseca24b61772008-04-13 11:56:54 +0200648 if (verify) {
Brandon Casey2cb1f362008-08-21 19:16:30 -0500649 /*
650 * nr_refspec must be greater than zero and i must be valid
651 * since it is only possible to reach this point from within
652 * the for loop above.
653 */
654 free_refspecs(rs, i+1);
Jonas Fonseca24b61772008-04-13 11:56:54 +0200655 return NULL;
656 }
Junio C Hamano46220ca2008-03-20 23:34:37 -0700657 die("Invalid refspec '%s'", refspec[i]);
658}
659
Jonas Fonseca24b61772008-04-13 11:56:54 +0200660int valid_fetch_refspec(const char *fetch_refspec_str)
661{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200662 struct refspec *refspec;
663
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000664 refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
Brandon Casey2cb1f362008-08-21 19:16:30 -0500665 free_refspecs(refspec, 1);
Jonas Fonseca24b61772008-04-13 11:56:54 +0200666 return !!refspec;
667}
668
Junio C Hamano46220ca2008-03-20 23:34:37 -0700669struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
670{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200671 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700672}
673
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900674static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700675{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200676 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400677}
678
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100679void free_refspec(int nr_refspec, struct refspec *refspec)
680{
681 int i;
682 for (i = 0; i < nr_refspec; i++) {
683 free(refspec[i].src);
684 free(refspec[i].dst);
685 }
686 free(refspec);
687}
688
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500689static int valid_remote_nick(const char *name)
690{
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300691 if (!name[0] || is_dot_or_dotdot(name))
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500692 return 0;
693 return !strchr(name, '/'); /* no slash */
694}
695
Daniel Barkalow5751f492007-05-12 11:45:53 -0400696struct remote *remote_get(const char *name)
697{
698 struct remote *ret;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400699 int name_given = 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400700
701 read_config();
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400702 if (name)
703 name_given = 1;
704 else {
Daniel Barkalow5751f492007-05-12 11:45:53 -0400705 name = default_remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400706 name_given = explicit_default_remote_name;
707 }
Junio C Hamano9326d492009-03-16 00:35:09 -0700708
Daniel Barkalow5751f492007-05-12 11:45:53 -0400709 ret = make_remote(name, 0);
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500710 if (valid_remote_nick(name)) {
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100711 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400712 read_remotes_file(ret);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100713 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400714 read_branches_file(ret);
715 }
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100716 if (name_given && !valid_remote(ret))
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500717 add_url_alias(ret, name);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100718 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400719 return NULL;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700720 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
721 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400722 return ret;
723}
Daniel Barkalow6b628162007-05-12 11:45:59 -0400724
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200725int remote_is_configured(const char *name)
726{
727 int i;
728 read_config();
729
730 for (i = 0; i < remotes_nr; i++)
731 if (!strcmp(name, remotes[i]->name))
732 return 1;
733 return 0;
734}
735
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100736int for_each_remote(each_remote_fn fn, void *priv)
737{
738 int i, result = 0;
739 read_config();
Daniel Barkalow2d313472008-02-18 23:41:41 -0500740 for (i = 0; i < remotes_nr && !result; i++) {
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100741 struct remote *r = remotes[i];
742 if (!r)
743 continue;
744 if (!r->fetch)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700745 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
746 r->fetch_refspec);
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100747 if (!r->push)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700748 r->push = parse_push_refspec(r->push_refspec_nr,
749 r->push_refspec);
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100750 result = fn(r, priv);
751 }
752 return result;
753}
754
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400755void ref_remove_duplicates(struct ref *ref_map)
756{
Thiago Farina183113a2010-07-04 16:46:19 -0300757 struct string_list refs = STRING_LIST_INIT_NODUP;
Julian Phillips73cf0822009-10-25 21:28:11 +0000758 struct string_list_item *item = NULL;
759 struct ref *prev = NULL, *next = NULL;
760 for (; ref_map; prev = ref_map, ref_map = next) {
761 next = ref_map->next;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400762 if (!ref_map->peer_ref)
763 continue;
Julian Phillips73cf0822009-10-25 21:28:11 +0000764
Julian Phillipse8c8b712010-06-26 00:41:37 +0100765 item = string_list_lookup(&refs, ref_map->peer_ref->name);
Julian Phillips73cf0822009-10-25 21:28:11 +0000766 if (item) {
767 if (strcmp(((struct ref *)item->util)->name,
768 ref_map->name))
769 die("%s tracks both %s and %s",
770 ref_map->peer_ref->name,
771 ((struct ref *)item->util)->name,
772 ref_map->name);
773 prev->next = ref_map->next;
774 free(ref_map->peer_ref);
775 free(ref_map);
Julian Phillips95c96d42009-11-13 21:25:56 +0000776 ref_map = prev; /* skip this; we freed it */
777 continue;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400778 }
Julian Phillips73cf0822009-10-25 21:28:11 +0000779
Julian Phillips78a395d2010-06-26 00:41:35 +0100780 item = string_list_insert(&refs, ref_map->peer_ref->name);
Julian Phillips73cf0822009-10-25 21:28:11 +0000781 item->util = ref_map;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400782 }
Julian Phillips73cf0822009-10-25 21:28:11 +0000783 string_list_clear(&refs, 0);
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400784}
785
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400786int remote_has_url(struct remote *remote, const char *url)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400787{
788 int i;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400789 for (i = 0; i < remote->url_nr; i++) {
790 if (!strcmp(remote->url[i], url))
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400791 return 1;
792 }
793 return 0;
794}
795
Daniel Barkalowe9282132009-03-07 01:11:34 -0500796static int match_name_with_pattern(const char *key, const char *name,
797 const char *value, char **result)
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500798{
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500799 const char *kstar = strchr(key, '*');
800 size_t klen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500801 size_t ksuffixlen;
802 size_t namelen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500803 int ret;
804 if (!kstar)
805 die("Key '%s' of pattern had no '*'", key);
806 klen = kstar - key;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500807 ksuffixlen = strlen(kstar + 1);
808 namelen = strlen(name);
809 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
810 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500811 if (ret && value) {
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500812 const char *vstar = strchr(value, '*');
813 size_t vlen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500814 size_t vsuffixlen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500815 if (!vstar)
816 die("Value '%s' of pattern has no '*'", value);
817 vlen = vstar - value;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500818 vsuffixlen = strlen(vstar + 1);
819 *result = xmalloc(vlen + vsuffixlen +
Daniel Barkalowe9282132009-03-07 01:11:34 -0500820 strlen(name) -
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500821 klen - ksuffixlen + 1);
822 strncpy(*result, value, vlen);
823 strncpy(*result + vlen,
824 name + klen, namelen - klen - ksuffixlen);
825 strcpy(*result + vlen + namelen - klen - ksuffixlen,
826 vstar + 1);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500827 }
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500828 return ret;
829}
830
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100831char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
832 const char *name)
833{
834 int i;
835 char *ret = NULL;
836 for (i = 0; i < nr_refspec; i++) {
837 struct refspec *refspec = refspecs + i;
838 if (refspec->pattern) {
839 if (match_name_with_pattern(refspec->src, name,
840 refspec->dst, &ret))
841 return ret;
842 } else if (!strcmp(refspec->src, name))
843 return strdup(refspec->dst);
844 }
845 return NULL;
846}
847
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400848int remote_find_tracking(struct remote *remote, struct refspec *refspec)
849{
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100850 int find_src = refspec->src == NULL;
851 char *needle, **result;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400852 int i;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100853
854 if (find_src) {
Shawn O. Pearce009c5bc2007-09-25 00:13:14 -0400855 if (!refspec->dst)
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100856 return error("find_tracking: need either src or dst");
857 needle = refspec->dst;
858 result = &refspec->src;
859 } else {
860 needle = refspec->src;
861 result = &refspec->dst;
862 }
863
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400864 for (i = 0; i < remote->fetch_refspec_nr; i++) {
865 struct refspec *fetch = &remote->fetch[i];
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100866 const char *key = find_src ? fetch->dst : fetch->src;
867 const char *value = find_src ? fetch->src : fetch->dst;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400868 if (!fetch->dst)
869 continue;
870 if (fetch->pattern) {
Daniel Barkalowe9282132009-03-07 01:11:34 -0500871 if (match_name_with_pattern(key, needle, value, result)) {
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400872 refspec->force = fetch->force;
873 return 0;
874 }
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100875 } else if (!strcmp(needle, key)) {
876 *result = xstrdup(value);
877 refspec->force = fetch->force;
878 return 0;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400879 }
880 }
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400881 return -1;
882}
883
René Scharfe80097682008-10-18 10:37:40 +0200884static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
885 const char *name)
886{
887 size_t len = strlen(name);
888 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
889 memcpy(ref->name, prefix, prefixlen);
890 memcpy(ref->name + prefixlen, name, len);
891 return ref;
892}
893
René Scharfe59c69c02008-10-18 10:44:18 +0200894struct ref *alloc_ref(const char *name)
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400895{
René Scharfe59c69c02008-10-18 10:44:18 +0200896 return alloc_ref_with_prefix("", 0, name);
Krzysztof Kowalczyk737922a2008-05-10 16:26:58 -0700897}
898
Daniel Barkalow45773702007-10-29 21:05:40 -0400899static struct ref *copy_ref(const struct ref *ref)
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400900{
Jay Soffian7b3db092009-02-27 14:10:04 -0500901 struct ref *cpy;
902 size_t len;
903 if (!ref)
904 return NULL;
905 len = strlen(ref->name);
906 cpy = xmalloc(sizeof(struct ref) + len + 1);
907 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
908 cpy->next = NULL;
909 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
910 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
911 cpy->peer_ref = copy_ref(ref->peer_ref);
912 return cpy;
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400913}
914
Daniel Barkalow45773702007-10-29 21:05:40 -0400915struct ref *copy_ref_list(const struct ref *ref)
916{
917 struct ref *ret = NULL;
918 struct ref **tail = &ret;
919 while (ref) {
920 *tail = copy_ref(ref);
921 ref = ref->next;
922 tail = &((*tail)->next);
923 }
924 return ret;
925}
926
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900927static void free_ref(struct ref *ref)
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400928{
929 if (!ref)
930 return;
Jay Soffian7b3db092009-02-27 14:10:04 -0500931 free_ref(ref->peer_ref);
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400932 free(ref->remote_status);
933 free(ref->symref);
934 free(ref);
935}
936
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400937void free_refs(struct ref *ref)
938{
939 struct ref *next;
940 while (ref) {
941 next = ref->next;
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400942 free_ref(ref);
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400943 ref = next;
944 }
945}
946
Daniel Barkalow6b628162007-05-12 11:45:59 -0400947static int count_refspec_match(const char *pattern,
948 struct ref *refs,
949 struct ref **matched_ref)
950{
951 int patlen = strlen(pattern);
952 struct ref *matched_weak = NULL;
953 struct ref *matched = NULL;
954 int weak_match = 0;
955 int match = 0;
956
957 for (weak_match = match = 0; refs; refs = refs->next) {
958 char *name = refs->name;
959 int namelen = strlen(name);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400960
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100961 if (!refname_match(pattern, name, ref_rev_parse_rules))
Daniel Barkalow6b628162007-05-12 11:45:59 -0400962 continue;
963
964 /* A match is "weak" if it is with refs outside
965 * heads or tags, and did not specify the pattern
966 * in full (e.g. "refs/remotes/origin/master") or at
967 * least from the toplevel (e.g. "remotes/origin/master");
968 * otherwise "git push $URL master" would result in
969 * ambiguity between remotes/origin/master and heads/master
970 * at the remote site.
971 */
972 if (namelen != patlen &&
973 patlen != namelen - 5 &&
974 prefixcmp(name, "refs/heads/") &&
975 prefixcmp(name, "refs/tags/")) {
976 /* We want to catch the case where only weak
977 * matches are found and there are multiple
978 * matches, and where more than one strong
979 * matches are found, as ambiguous. One
980 * strong match with zero or more weak matches
981 * are acceptable as a unique match.
982 */
983 matched_weak = refs;
984 weak_match++;
985 }
986 else {
987 matched = refs;
988 match++;
989 }
990 }
991 if (!matched) {
992 *matched_ref = matched_weak;
993 return weak_match;
994 }
995 else {
996 *matched_ref = matched;
997 return match;
998 }
999}
1000
Daniel Barkalow1d735262007-07-10 00:47:26 -04001001static void tail_link_ref(struct ref *ref, struct ref ***tail)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001002{
1003 **tail = ref;
Daniel Barkalow1d735262007-07-10 00:47:26 -04001004 while (ref->next)
1005 ref = ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001006 *tail = &ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001007}
1008
1009static struct ref *try_explicit_object_name(const char *name)
1010{
1011 unsigned char sha1[20];
1012 struct ref *ref;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001013
1014 if (!*name) {
René Scharfe59c69c02008-10-18 10:44:18 +02001015 ref = alloc_ref("(delete)");
Daniel Barkalow6b628162007-05-12 11:45:59 -04001016 hashclr(ref->new_sha1);
1017 return ref;
1018 }
1019 if (get_sha1(name, sha1))
1020 return NULL;
René Scharfe59c69c02008-10-18 10:44:18 +02001021 ref = alloc_ref(name);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001022 hashcpy(ref->new_sha1, sha1);
1023 return ref;
1024}
1025
Daniel Barkalow1d735262007-07-10 00:47:26 -04001026static struct ref *make_linked_ref(const char *name, struct ref ***tail)
Junio C Hamano163f0ee2007-06-09 00:07:34 -07001027{
René Scharfe59c69c02008-10-18 10:44:18 +02001028 struct ref *ret = alloc_ref(name);
Daniel Barkalow1d735262007-07-10 00:47:26 -04001029 tail_link_ref(ret, tail);
1030 return ret;
Junio C Hamano163f0ee2007-06-09 00:07:34 -07001031}
1032
Jeff Kingf8aae122008-04-23 05:16:06 -04001033static char *guess_ref(const char *name, struct ref *peer)
1034{
1035 struct strbuf buf = STRBUF_INIT;
1036 unsigned char sha1[20];
1037
1038 const char *r = resolve_ref(peer->name, sha1, 1, NULL);
1039 if (!r)
1040 return NULL;
1041
1042 if (!prefixcmp(r, "refs/heads/"))
1043 strbuf_addstr(&buf, "refs/heads/");
1044 else if (!prefixcmp(r, "refs/tags/"))
1045 strbuf_addstr(&buf, "refs/tags/");
1046 else
1047 return NULL;
1048
1049 strbuf_addstr(&buf, name);
1050 return strbuf_detach(&buf, NULL);
1051}
1052
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001053static int match_explicit(struct ref *src, struct ref *dst,
1054 struct ref ***dst_tail,
Jeff King9a7bbd12008-06-16 12:15:02 -04001055 struct refspec *rs)
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001056{
1057 struct ref *matched_src, *matched_dst;
Jay Soffiancdf690e2009-02-25 03:32:16 -05001058 int copy_src;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001059
1060 const char *dst_value = rs->dst;
Jeff Kingf8aae122008-04-23 05:16:06 -04001061 char *dst_guess;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001062
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001063 if (rs->pattern || rs->matching)
Jeff King9a7bbd12008-06-16 12:15:02 -04001064 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001065
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001066 matched_src = matched_dst = NULL;
1067 switch (count_refspec_match(rs->src, src, &matched_src)) {
1068 case 1:
Jay Soffiancdf690e2009-02-25 03:32:16 -05001069 copy_src = 1;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001070 break;
1071 case 0:
1072 /* The source could be in the get_sha1() format
1073 * not a reference name. :refs/other is a
1074 * way to delete 'other' ref at the remote end.
1075 */
1076 matched_src = try_explicit_object_name(rs->src);
Shawn O. Pearce7dfee372007-09-25 00:13:19 -04001077 if (!matched_src)
Jeff King9a7bbd12008-06-16 12:15:02 -04001078 return error("src refspec %s does not match any.", rs->src);
Jay Soffiancdf690e2009-02-25 03:32:16 -05001079 copy_src = 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001080 break;
1081 default:
Jeff King9a7bbd12008-06-16 12:15:02 -04001082 return error("src refspec %s matches more than one.", rs->src);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001083 }
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001084
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001085 if (!dst_value) {
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001086 unsigned char sha1[20];
1087 int flag;
1088
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001089 dst_value = resolve_ref(matched_src->name, sha1, 1, &flag);
1090 if (!dst_value ||
1091 ((flag & REF_ISSYMREF) &&
1092 prefixcmp(dst_value, "refs/heads/")))
1093 die("%s cannot be resolved to branch.",
1094 matched_src->name);
Shawn O. Pearce4491e622007-09-25 00:13:25 -04001095 }
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001096
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001097 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
1098 case 1:
1099 break;
1100 case 0:
Junio C Hamano163f0ee2007-06-09 00:07:34 -07001101 if (!memcmp(dst_value, "refs/", 5))
Daniel Barkalow1d735262007-07-10 00:47:26 -04001102 matched_dst = make_linked_ref(dst_value, dst_tail);
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04001103 else if ((dst_guess = guess_ref(dst_value, matched_src)))
Jeff Kingf8aae122008-04-23 05:16:06 -04001104 matched_dst = make_linked_ref(dst_guess, dst_tail);
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001105 else
Jeff Kingf8aae122008-04-23 05:16:06 -04001106 error("unable to push to unqualified destination: %s\n"
1107 "The destination refspec neither matches an "
1108 "existing ref on the remote nor\n"
1109 "begins with refs/, and we are unable to "
1110 "guess a prefix based on the source ref.",
1111 dst_value);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001112 break;
1113 default:
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001114 matched_dst = NULL;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001115 error("dst refspec %s matches more than one.",
1116 dst_value);
1117 break;
1118 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001119 if (!matched_dst)
1120 return -1;
1121 if (matched_dst->peer_ref)
1122 return error("dst ref %s receives from more than one src.",
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001123 matched_dst->name);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001124 else {
Jay Soffiancdf690e2009-02-25 03:32:16 -05001125 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001126 matched_dst->force = rs->force;
1127 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001128 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001129}
1130
Daniel Barkalow6b628162007-05-12 11:45:59 -04001131static int match_explicit_refs(struct ref *src, struct ref *dst,
1132 struct ref ***dst_tail, struct refspec *rs,
1133 int rs_nr)
1134{
1135 int i, errs;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001136 for (i = errs = 0; i < rs_nr; i++)
Jeff King9a7bbd12008-06-16 12:15:02 -04001137 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1138 return errs;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001139}
1140
Alex Riesen6e66bf32007-06-08 01:43:05 +02001141static const struct refspec *check_pattern_match(const struct refspec *rs,
1142 int rs_nr,
1143 const struct ref *src)
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001144{
1145 int i;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001146 int matching_refs = -1;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001147 for (i = 0; i < rs_nr; i++) {
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001148 if (rs[i].matching &&
1149 (matching_refs == -1 || rs[i].force)) {
1150 matching_refs = i;
1151 continue;
1152 }
1153
Daniel Barkalowe9282132009-03-07 01:11:34 -05001154 if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1155 NULL, NULL))
Alex Riesen6e66bf32007-06-08 01:43:05 +02001156 return rs + i;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001157 }
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001158 if (matching_refs != -1)
1159 return rs + matching_refs;
1160 else
1161 return NULL;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001162}
1163
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001164static struct ref **tail_ref(struct ref **head)
1165{
1166 struct ref **tail = head;
1167 while (*tail)
1168 tail = &((*tail)->next);
1169 return tail;
1170}
1171
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001172/*
1173 * Note. This is used only by "push"; refspec matching rules for
1174 * push and fetch are subtly different, so do not try to reuse it
1175 * without thinking.
1176 */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001177int match_refs(struct ref *src, struct ref **dst,
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001178 int nr_refspec, const char **refspec, int flags)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001179{
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001180 struct refspec *rs;
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001181 int send_all = flags & MATCH_REFS_ALL;
1182 int send_mirror = flags & MATCH_REFS_MIRROR;
Jay Soffian5f48cb92009-02-25 03:32:17 -05001183 int errs;
Linus Torvalds2af202b2009-06-18 10:28:43 -07001184 static const char *default_refspec[] = { ":", NULL };
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001185 struct ref **dst_tail = tail_ref(dst);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001186
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001187 if (!nr_refspec) {
1188 nr_refspec = 1;
1189 refspec = default_refspec;
1190 }
1191 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001192 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001193
1194 /* pick the remainder */
1195 for ( ; src; src = src->next) {
1196 struct ref *dst_peer;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001197 const struct refspec *pat = NULL;
1198 char *dst_name;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001199 if (src->peer_ref)
1200 continue;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001201
1202 pat = check_pattern_match(rs, nr_refspec, src);
1203 if (!pat)
1204 continue;
1205
1206 if (pat->matching) {
Junio C Hamano098e7112007-07-01 19:00:08 -07001207 /*
1208 * "matching refs"; traditionally we pushed everything
1209 * including refs outside refs/heads/ hierarchy, but
1210 * that does not make much sense these days.
1211 */
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001212 if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
1213 continue;
1214 dst_name = xstrdup(src->name);
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001215
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001216 } else {
Daniel Barkalowefd8f792007-06-15 10:22:37 -04001217 const char *dst_side = pat->dst ? pat->dst : pat->src;
Daniel Barkalowe9282132009-03-07 01:11:34 -05001218 if (!match_name_with_pattern(pat->src, src->name,
1219 dst_side, &dst_name))
1220 die("Didn't think it matches any more");
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001221 }
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001222 dst_peer = find_ref_by_name(*dst, dst_name);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001223 if (dst_peer) {
1224 if (dst_peer->peer_ref)
1225 /* We're already sending something to this ref. */
1226 goto free_name;
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001227
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001228 } else {
1229 if (pat->matching && !(send_all || send_mirror))
1230 /*
1231 * Remote doesn't have it, and we have no
1232 * explicit pattern, and we don't have
1233 * --all nor --mirror.
1234 */
1235 goto free_name;
1236
Daniel Barkalow6b628162007-05-12 11:45:59 -04001237 /* Create a new one and link it */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001238 dst_peer = make_linked_ref(dst_name, &dst_tail);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001239 hashcpy(dst_peer->new_sha1, src->new_sha1);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001240 }
Jay Soffiancdf690e2009-02-25 03:32:16 -05001241 dst_peer->peer_ref = copy_ref(src);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001242 dst_peer->force = pat->force;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001243 free_name:
1244 free(dst_name);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001245 }
Jay Soffian5f48cb92009-02-25 03:32:17 -05001246 if (errs)
1247 return -1;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001248 return 0;
1249}
Daniel Barkalowcf818342007-09-10 23:02:56 -04001250
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001251void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1252 int force_update)
1253{
1254 struct ref *ref;
1255
1256 for (ref = remote_refs; ref; ref = ref->next) {
1257 if (ref->peer_ref)
1258 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1259 else if (!send_mirror)
1260 continue;
1261
1262 ref->deletion = is_null_sha1(ref->new_sha1);
1263 if (!ref->deletion &&
1264 !hashcmp(ref->old_sha1, ref->new_sha1)) {
1265 ref->status = REF_STATUS_UPTODATE;
1266 continue;
1267 }
1268
1269 /* This part determines what can overwrite what.
1270 * The rules are:
1271 *
1272 * (0) you can always use --force or +A:B notation to
1273 * selectively force individual ref pairs.
1274 *
1275 * (1) if the old thing does not exist, it is OK.
1276 *
1277 * (2) if you do not have the old thing, you are not allowed
1278 * to overwrite it; you would not know what you are losing
1279 * otherwise.
1280 *
1281 * (3) if both new and old are commit-ish, and new is a
1282 * descendant of old, it is OK.
1283 *
1284 * (4) regardless of all of the above, removing :B is
1285 * always allowed.
1286 */
1287
1288 ref->nonfastforward =
1289 !ref->deletion &&
1290 !is_null_sha1(ref->old_sha1) &&
1291 (!has_sha1_file(ref->old_sha1)
1292 || !ref_newer(ref->new_sha1, ref->old_sha1));
1293
1294 if (ref->nonfastforward && !ref->force && !force_update) {
1295 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
1296 continue;
1297 }
1298 }
1299}
1300
Daniel Barkalowcf818342007-09-10 23:02:56 -04001301struct branch *branch_get(const char *name)
1302{
1303 struct branch *ret;
1304
1305 read_config();
1306 if (!name || !*name || !strcmp(name, "HEAD"))
1307 ret = current_branch;
1308 else
1309 ret = make_branch(name, 0);
1310 if (ret && ret->remote_name) {
1311 ret->remote = remote_get(ret->remote_name);
1312 if (ret->merge_nr) {
1313 int i;
1314 ret->merge = xcalloc(sizeof(*ret->merge),
1315 ret->merge_nr);
1316 for (i = 0; i < ret->merge_nr; i++) {
1317 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1318 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
Michael J Gruber5e6e2b42009-04-01 23:42:49 +02001319 if (remote_find_tracking(ret->remote, ret->merge[i])
1320 && !strcmp(ret->remote_name, "."))
1321 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001322 }
1323 }
1324 }
1325 return ret;
1326}
1327
1328int branch_has_merge_config(struct branch *branch)
1329{
1330 return branch && !!branch->merge;
1331}
1332
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001333int branch_merge_matches(struct branch *branch,
1334 int i,
1335 const char *refname)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001336{
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001337 if (!branch || i < 0 || i >= branch->merge_nr)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001338 return 0;
Steffen Prohaska605b4972007-11-11 15:01:48 +01001339 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001340}
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001341
Daniel Barkalow45773702007-10-29 21:05:40 -04001342static struct ref *get_expanded_map(const struct ref *remote_refs,
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001343 const struct refspec *refspec)
1344{
Daniel Barkalow45773702007-10-29 21:05:40 -04001345 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001346 struct ref *ret = NULL;
1347 struct ref **tail = &ret;
1348
Daniel Barkalowe9282132009-03-07 01:11:34 -05001349 char *expn_name;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001350
1351 for (ref = remote_refs; ref; ref = ref->next) {
1352 if (strchr(ref->name, '^'))
1353 continue; /* a dereference item */
Daniel Barkalowe9282132009-03-07 01:11:34 -05001354 if (match_name_with_pattern(refspec->src, ref->name,
1355 refspec->dst, &expn_name)) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001356 struct ref *cpy = copy_ref(ref);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001357
Daniel Barkalowe9282132009-03-07 01:11:34 -05001358 cpy->peer_ref = alloc_ref(expn_name);
1359 free(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001360 if (refspec->force)
1361 cpy->peer_ref->force = 1;
1362 *tail = cpy;
1363 tail = &cpy->next;
1364 }
1365 }
1366
1367 return ret;
1368}
1369
Daniel Barkalow45773702007-10-29 21:05:40 -04001370static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001371{
Daniel Barkalow45773702007-10-29 21:05:40 -04001372 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001373 for (ref = refs; ref; ref = ref->next) {
Steffen Prohaska605b4972007-11-11 15:01:48 +01001374 if (refname_match(name, ref->name, ref_fetch_rules))
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001375 return ref;
1376 }
1377 return NULL;
1378}
1379
Daniel Barkalow45773702007-10-29 21:05:40 -04001380struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001381{
Daniel Barkalow45773702007-10-29 21:05:40 -04001382 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001383
1384 if (!ref)
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001385 return NULL;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001386
1387 return copy_ref(ref);
1388}
1389
1390static struct ref *get_local_ref(const char *name)
1391{
Clemens Buchacher3eb96992009-06-17 15:38:36 +02001392 if (!name || name[0] == '\0')
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001393 return NULL;
1394
René Scharfe59c69c02008-10-18 10:44:18 +02001395 if (!prefixcmp(name, "refs/"))
1396 return alloc_ref(name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001397
1398 if (!prefixcmp(name, "heads/") ||
1399 !prefixcmp(name, "tags/") ||
René Scharfe80097682008-10-18 10:37:40 +02001400 !prefixcmp(name, "remotes/"))
1401 return alloc_ref_with_prefix("refs/", 5, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001402
René Scharfe80097682008-10-18 10:37:40 +02001403 return alloc_ref_with_prefix("refs/heads/", 11, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001404}
1405
Daniel Barkalow45773702007-10-29 21:05:40 -04001406int get_fetch_map(const struct ref *remote_refs,
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001407 const struct refspec *refspec,
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001408 struct ref ***tail,
1409 int missing_ok)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001410{
Daniel Barkalowef00d152008-03-17 22:05:23 -04001411 struct ref *ref_map, **rmp;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001412
1413 if (refspec->pattern) {
1414 ref_map = get_expanded_map(remote_refs, refspec);
1415 } else {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001416 const char *name = refspec->src[0] ? refspec->src : "HEAD";
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001417
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001418 ref_map = get_remote_ref(remote_refs, name);
1419 if (!missing_ok && !ref_map)
1420 die("Couldn't find remote ref %s", name);
1421 if (ref_map) {
1422 ref_map->peer_ref = get_local_ref(refspec->dst);
1423 if (ref_map->peer_ref && refspec->force)
1424 ref_map->peer_ref->force = 1;
1425 }
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001426 }
1427
Daniel Barkalowef00d152008-03-17 22:05:23 -04001428 for (rmp = &ref_map; *rmp; ) {
1429 if ((*rmp)->peer_ref) {
1430 int st = check_ref_format((*rmp)->peer_ref->name + 5);
1431 if (st && st != CHECK_REF_FORMAT_ONELEVEL) {
1432 struct ref *ignore = *rmp;
1433 error("* Ignoring funny ref '%s' locally",
1434 (*rmp)->peer_ref->name);
1435 *rmp = (*rmp)->next;
1436 free(ignore->peer_ref);
1437 free(ignore);
1438 continue;
1439 }
1440 }
1441 rmp = &((*rmp)->next);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001442 }
1443
Alex Riesen8f70a762007-10-12 22:40:04 +02001444 if (ref_map)
1445 tail_link_ref(ref_map, tail);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001446
1447 return 0;
1448}
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001449
1450int resolve_remote_symref(struct ref *ref, struct ref *list)
1451{
1452 if (!ref->symref)
1453 return 0;
1454 for (; list; list = list->next)
1455 if (!strcmp(ref->symref, list->name)) {
1456 hashcpy(ref->old_sha1, list->old_sha1);
1457 return 0;
1458 }
1459 return 1;
1460}
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001461
Jay Soffianec8452d2009-02-25 03:32:12 -05001462static void unmark_and_free(struct commit_list *list, unsigned int mark)
1463{
1464 while (list) {
1465 struct commit_list *temp = list;
1466 temp->item->object.flags &= ~mark;
1467 list = temp->next;
1468 free(temp);
1469 }
1470}
1471
1472int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1473{
1474 struct object *o;
1475 struct commit *old, *new;
1476 struct commit_list *list, *used;
1477 int found = 0;
1478
1479 /* Both new and old must be commit-ish and new is descendant of
1480 * old. Otherwise we require --force.
1481 */
1482 o = deref_tag(parse_object(old_sha1), NULL, 0);
1483 if (!o || o->type != OBJ_COMMIT)
1484 return 0;
1485 old = (struct commit *) o;
1486
1487 o = deref_tag(parse_object(new_sha1), NULL, 0);
1488 if (!o || o->type != OBJ_COMMIT)
1489 return 0;
1490 new = (struct commit *) o;
1491
1492 if (parse_commit(new) < 0)
1493 return 0;
1494
1495 used = list = NULL;
1496 commit_list_insert(new, &list);
1497 while (list) {
1498 new = pop_most_recent_commit(&list, TMP_MARK);
1499 commit_list_insert(new, &used);
1500 if (new == old) {
1501 found = 1;
1502 break;
1503 }
1504 }
1505 unmark_and_free(list, TMP_MARK);
1506 unmark_and_free(used, TMP_MARK);
1507 return found;
1508}
1509
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001510/*
1511 * Return true if there is anything to report, otherwise false.
1512 */
1513int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1514{
1515 unsigned char sha1[20];
1516 struct commit *ours, *theirs;
1517 char symmetric[84];
1518 struct rev_info revs;
1519 const char *rev_argv[10], *base;
1520 int rev_argc;
1521
1522 /*
1523 * Nothing to report unless we are marked to build on top of
1524 * somebody else.
1525 */
1526 if (!branch ||
1527 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1528 return 0;
1529
1530 /*
1531 * If what we used to build on no longer exists, there is
1532 * nothing to report.
1533 */
1534 base = branch->merge[0]->dst;
1535 if (!resolve_ref(base, sha1, 1, NULL))
1536 return 0;
Michael J Gruber57ffc5f2009-05-11 16:42:54 +02001537 theirs = lookup_commit_reference(sha1);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001538 if (!theirs)
1539 return 0;
1540
1541 if (!resolve_ref(branch->refname, sha1, 1, NULL))
1542 return 0;
Michael J Gruber57ffc5f2009-05-11 16:42:54 +02001543 ours = lookup_commit_reference(sha1);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001544 if (!ours)
1545 return 0;
1546
1547 /* are we the same? */
1548 if (theirs == ours)
1549 return 0;
1550
Junio C Hamano8fbf8792009-04-21 16:32:18 -07001551 /* Run "rev-list --left-right ours...theirs" internally... */
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001552 rev_argc = 0;
1553 rev_argv[rev_argc++] = NULL;
1554 rev_argv[rev_argc++] = "--left-right";
1555 rev_argv[rev_argc++] = symmetric;
1556 rev_argv[rev_argc++] = "--";
1557 rev_argv[rev_argc] = NULL;
1558
1559 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1560 strcpy(symmetric + 40, "...");
1561 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1562
1563 init_revisions(&revs, NULL);
1564 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1565 prepare_revision_walk(&revs);
1566
1567 /* ... and count the commits on each side. */
1568 *num_ours = 0;
1569 *num_theirs = 0;
1570 while (1) {
1571 struct commit *c = get_revision(&revs);
1572 if (!c)
1573 break;
1574 if (c->object.flags & SYMMETRIC_LEFT)
1575 (*num_ours)++;
1576 else
1577 (*num_theirs)++;
1578 }
Junio C Hamanoc0234b22008-07-03 12:09:48 -07001579
1580 /* clear object flags smudged by the above traversal */
1581 clear_commit_marks(ours, ALL_REV_FLAGS);
1582 clear_commit_marks(theirs, ALL_REV_FLAGS);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001583 return 1;
1584}
1585
1586/*
1587 * Return true when there is anything to report, otherwise false.
1588 */
1589int format_tracking_info(struct branch *branch, struct strbuf *sb)
1590{
1591 int num_ours, num_theirs;
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001592 const char *base;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001593
1594 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1595 return 0;
1596
1597 base = branch->merge[0]->dst;
Michael J Gruber45972ff2009-04-16 10:20:44 +02001598 base = shorten_unambiguous_ref(base, 0);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001599 if (!num_theirs)
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001600 strbuf_addf(sb, "Your branch is ahead of '%s' "
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001601 "by %d commit%s.\n",
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001602 base, num_ours, (num_ours == 1) ? "" : "s");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001603 else if (!num_ours)
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001604 strbuf_addf(sb, "Your branch is behind '%s' "
1605 "by %d commit%s, "
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001606 "and can be fast-forwarded.\n",
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001607 base, num_theirs, (num_theirs == 1) ? "" : "s");
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001608 else
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001609 strbuf_addf(sb, "Your branch and '%s' have diverged,\n"
1610 "and have %d and %d different commit(s) each, "
1611 "respectively.\n",
1612 base, num_ours, num_theirs);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001613 return 1;
1614}
Jay Soffian454e2022009-02-25 03:32:11 -05001615
1616static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1617{
1618 struct ref ***local_tail = cb_data;
1619 struct ref *ref;
1620 int len;
1621
1622 /* we already know it starts with refs/ to get here */
1623 if (check_ref_format(refname + 5))
1624 return 0;
1625
1626 len = strlen(refname) + 1;
1627 ref = xcalloc(1, sizeof(*ref) + len);
1628 hashcpy(ref->new_sha1, sha1);
1629 memcpy(ref->name, refname, len);
1630 **local_tail = ref;
1631 *local_tail = &ref->next;
1632 return 0;
1633}
1634
1635struct ref *get_local_heads(void)
1636{
Nguyễn Thái Ngọc Duy55f05662009-04-17 08:16:23 +10001637 struct ref *local_refs = NULL, **local_tail = &local_refs;
Jay Soffian454e2022009-02-25 03:32:11 -05001638 for_each_ref(one_local_ref, &local_tail);
1639 return local_refs;
1640}
Jay Soffian8ef51732009-02-25 03:32:13 -05001641
Jay Soffian4229f1f2009-02-27 14:10:05 -05001642struct ref *guess_remote_head(const struct ref *head,
1643 const struct ref *refs,
1644 int all)
Jay Soffian8ef51732009-02-25 03:32:13 -05001645{
Jay Soffian8ef51732009-02-25 03:32:13 -05001646 const struct ref *r;
Jay Soffian4229f1f2009-02-27 14:10:05 -05001647 struct ref *list = NULL;
1648 struct ref **tail = &list;
Jay Soffian8ef51732009-02-25 03:32:13 -05001649
Jay Soffian6cb4e6c2009-02-25 03:32:14 -05001650 if (!head)
Jay Soffian8ef51732009-02-25 03:32:13 -05001651 return NULL;
1652
Jeff Kingfbb074c2009-02-27 14:10:06 -05001653 /*
1654 * Some transports support directly peeking at
1655 * where HEAD points; if that is the case, then
1656 * we don't have to guess.
1657 */
1658 if (head->symref)
1659 return copy_ref(find_ref_by_name(refs, head->symref));
1660
Jay Soffian8ef51732009-02-25 03:32:13 -05001661 /* If refs/heads/master could be right, it is. */
Jay Soffian4229f1f2009-02-27 14:10:05 -05001662 if (!all) {
1663 r = find_ref_by_name(refs, "refs/heads/master");
1664 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1665 return copy_ref(r);
1666 }
Jay Soffian8ef51732009-02-25 03:32:13 -05001667
1668 /* Look for another ref that points there */
Jay Soffian4229f1f2009-02-27 14:10:05 -05001669 for (r = refs; r; r = r->next) {
1670 if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1671 *tail = copy_ref(r);
1672 tail = &((*tail)->next);
1673 if (!all)
1674 break;
1675 }
1676 }
Jay Soffian8ef51732009-02-25 03:32:13 -05001677
Jay Soffian4229f1f2009-02-27 14:10:05 -05001678 return list;
Jay Soffian8ef51732009-02-25 03:32:13 -05001679}
Jay Soffianf2ef6072009-11-10 00:03:31 -05001680
1681struct stale_heads_info {
1682 struct remote *remote;
1683 struct string_list *ref_names;
1684 struct ref **stale_refs_tail;
1685};
1686
1687static int get_stale_heads_cb(const char *refname,
1688 const unsigned char *sha1, int flags, void *cb_data)
1689{
1690 struct stale_heads_info *info = cb_data;
1691 struct refspec refspec;
1692 memset(&refspec, 0, sizeof(refspec));
1693 refspec.dst = (char *)refname;
1694 if (!remote_find_tracking(info->remote, &refspec)) {
1695 if (!((flags & REF_ISSYMREF) ||
1696 string_list_has_string(info->ref_names, refspec.src))) {
1697 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1698 hashcpy(ref->new_sha1, sha1);
1699 }
1700 }
1701 return 0;
1702}
1703
1704struct ref *get_stale_heads(struct remote *remote, struct ref *fetch_map)
1705{
1706 struct ref *ref, *stale_refs = NULL;
Thiago Farina183113a2010-07-04 16:46:19 -03001707 struct string_list ref_names = STRING_LIST_INIT_NODUP;
Jay Soffianf2ef6072009-11-10 00:03:31 -05001708 struct stale_heads_info info;
1709 info.remote = remote;
1710 info.ref_names = &ref_names;
1711 info.stale_refs_tail = &stale_refs;
1712 for (ref = fetch_map; ref; ref = ref->next)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001713 string_list_append(&ref_names, ref->name);
Jay Soffianf2ef6072009-11-10 00:03:31 -05001714 sort_string_list(&ref_names);
1715 for_each_ref(get_stale_heads_cb, &info);
1716 string_list_clear(&ref_names, 0);
1717 return stale_refs;
1718}