blob: 04fd9ea4bd2f99003c9c5abb7bbbad7dafca3937 [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"
Jeff Kinged81c762012-05-21 18:19:28 -040010#include "mergesort.h"
Daniel Barkalow5751f492007-05-12 11:45:53 -040011
Felipe Contreras6ddba5e2012-02-23 00:43:41 +020012enum map_direction { FROM_SRC, FROM_DST };
13
Daniel Barkalowe0aaa292008-04-17 19:32:35 -040014static struct refspec s_tag_refspec = {
15 0,
16 1,
Junio C Hamanob84c3432008-05-25 13:38:44 -070017 0,
Daniel Barkalow08fbdb32009-03-07 01:11:36 -050018 "refs/tags/*",
19 "refs/tags/*"
Daniel Barkalowe0aaa292008-04-17 19:32:35 -040020};
21
22const struct refspec *tag_refspec = &s_tag_refspec;
23
Junio C Hamano844112c2008-02-24 22:25:04 -080024struct counted_string {
25 size_t len;
26 const char *s;
27};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050028struct rewrite {
29 const char *base;
Junio C Hamano844112c2008-02-24 22:25:04 -080030 size_t baselen;
31 struct counted_string *instead_of;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050032 int instead_of_nr;
33 int instead_of_alloc;
34};
Josh Triplettd071d942009-09-07 01:56:00 -070035struct rewrites {
36 struct rewrite **rewrite;
37 int rewrite_alloc;
38 int rewrite_nr;
39};
Daniel Barkalow55029ae2008-02-20 13:43:53 -050040
Daniel Barkalow5751f492007-05-12 11:45:53 -040041static struct remote **remotes;
Daniel Barkalow2d313472008-02-18 23:41:41 -050042static int remotes_alloc;
43static int remotes_nr;
Daniel Barkalow5751f492007-05-12 11:45:53 -040044
Daniel Barkalowcf818342007-09-10 23:02:56 -040045static struct branch **branches;
Daniel Barkalow2d313472008-02-18 23:41:41 -050046static int branches_alloc;
47static int branches_nr;
Daniel Barkalowcf818342007-09-10 23:02:56 -040048
49static struct branch *current_branch;
50static const char *default_remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -040051static int explicit_default_remote_name;
Daniel Barkalowcf818342007-09-10 23:02:56 -040052
Josh Triplettd071d942009-09-07 01:56:00 -070053static struct rewrites rewrites;
Josh Triplett1c2eafb2009-09-07 01:56:33 -070054static struct rewrites rewrites_push;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050055
Daniel Barkalow5751f492007-05-12 11:45:53 -040056#define BUF_SIZE (2048)
57static char buffer[BUF_SIZE];
58
Daniel Barkalow0a4da292009-11-18 02:42:23 +010059static int valid_remote(const struct remote *remote)
60{
Daniel Barkalowc578f512009-11-18 02:42:25 +010061 return (!!remote->url) || (!!remote->foreign_vcs);
Daniel Barkalow0a4da292009-11-18 02:42:23 +010062}
63
Josh Triplettd071d942009-09-07 01:56:00 -070064static const char *alias_url(const char *url, struct rewrites *r)
Daniel Barkalow55029ae2008-02-20 13:43:53 -050065{
66 int i, j;
Junio C Hamano844112c2008-02-24 22:25:04 -080067 char *ret;
68 struct counted_string *longest;
69 int longest_i;
70
71 longest = NULL;
72 longest_i = -1;
Josh Triplettd071d942009-09-07 01:56:00 -070073 for (i = 0; i < r->rewrite_nr; i++) {
74 if (!r->rewrite[i])
Daniel Barkalow55029ae2008-02-20 13:43:53 -050075 continue;
Josh Triplettd071d942009-09-07 01:56:00 -070076 for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
77 if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) &&
Junio C Hamano844112c2008-02-24 22:25:04 -080078 (!longest ||
Josh Triplettd071d942009-09-07 01:56:00 -070079 longest->len < r->rewrite[i]->instead_of[j].len)) {
80 longest = &(r->rewrite[i]->instead_of[j]);
Junio C Hamano844112c2008-02-24 22:25:04 -080081 longest_i = i;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050082 }
83 }
84 }
Junio C Hamano844112c2008-02-24 22:25:04 -080085 if (!longest)
86 return url;
87
Josh Triplettd071d942009-09-07 01:56:00 -070088 ret = xmalloc(r->rewrite[longest_i]->baselen +
Junio C Hamano844112c2008-02-24 22:25:04 -080089 (strlen(url) - longest->len) + 1);
Josh Triplettd071d942009-09-07 01:56:00 -070090 strcpy(ret, r->rewrite[longest_i]->base);
91 strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
Junio C Hamano844112c2008-02-24 22:25:04 -080092 return ret;
Daniel Barkalow55029ae2008-02-20 13:43:53 -050093}
94
Daniel Barkalow5751f492007-05-12 11:45:53 -040095static void add_push_refspec(struct remote *remote, const char *ref)
96{
Daniel Barkalow2d313472008-02-18 23:41:41 -050097 ALLOC_GROW(remote->push_refspec,
98 remote->push_refspec_nr + 1,
99 remote->push_refspec_alloc);
100 remote->push_refspec[remote->push_refspec_nr++] = ref;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400101}
102
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400103static void add_fetch_refspec(struct remote *remote, const char *ref)
104{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500105 ALLOC_GROW(remote->fetch_refspec,
106 remote->fetch_refspec_nr + 1,
107 remote->fetch_refspec_alloc);
108 remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400109}
110
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400111static void add_url(struct remote *remote, const char *url)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400112{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500113 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
114 remote->url[remote->url_nr++] = url;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400115}
116
Michael J Gruber20346232009-06-09 18:01:34 +0200117static void add_pushurl(struct remote *remote, const char *pushurl)
118{
119 ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
120 remote->pushurl[remote->pushurl_nr++] = pushurl;
121}
122
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700123static void add_pushurl_alias(struct remote *remote, const char *url)
124{
125 const char *pushurl = alias_url(url, &rewrites_push);
126 if (pushurl != url)
127 add_pushurl(remote, pushurl);
128}
129
130static void add_url_alias(struct remote *remote, const char *url)
131{
132 add_url(remote, alias_url(url, &rewrites));
133 add_pushurl_alias(remote, url);
134}
135
Daniel Barkalow5751f492007-05-12 11:45:53 -0400136static struct remote *make_remote(const char *name, int len)
137{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500138 struct remote *ret;
139 int i;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400140
Daniel Barkalow2d313472008-02-18 23:41:41 -0500141 for (i = 0; i < remotes_nr; i++) {
142 if (len ? (!strncmp(name, remotes[i]->name, len) &&
143 !remotes[i]->name[len]) :
144 !strcmp(name, remotes[i]->name))
145 return remotes[i];
Daniel Barkalow5751f492007-05-12 11:45:53 -0400146 }
147
Daniel Barkalow2d313472008-02-18 23:41:41 -0500148 ret = xcalloc(1, sizeof(struct remote));
149 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
150 remotes[remotes_nr++] = ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400151 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500152 ret->name = xstrndup(name, len);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400153 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500154 ret->name = xstrdup(name);
155 return ret;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400156}
157
Daniel Barkalowcf818342007-09-10 23:02:56 -0400158static void add_merge(struct branch *branch, const char *name)
159{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500160 ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
161 branch->merge_alloc);
162 branch->merge_name[branch->merge_nr++] = name;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400163}
164
165static struct branch *make_branch(const char *name, int len)
166{
Daniel Barkalow2d313472008-02-18 23:41:41 -0500167 struct branch *ret;
168 int i;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400169 char *refname;
170
Daniel Barkalow2d313472008-02-18 23:41:41 -0500171 for (i = 0; i < branches_nr; i++) {
172 if (len ? (!strncmp(name, branches[i]->name, len) &&
173 !branches[i]->name[len]) :
174 !strcmp(name, branches[i]->name))
175 return branches[i];
Daniel Barkalowcf818342007-09-10 23:02:56 -0400176 }
177
Daniel Barkalow2d313472008-02-18 23:41:41 -0500178 ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
179 ret = xcalloc(1, sizeof(struct branch));
180 branches[branches_nr++] = ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400181 if (len)
Daniel Barkalow2d313472008-02-18 23:41:41 -0500182 ret->name = xstrndup(name, len);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400183 else
Daniel Barkalow2d313472008-02-18 23:41:41 -0500184 ret->name = xstrdup(name);
Dotan Barake8eec712008-09-09 21:57:10 +0300185 refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400186 strcpy(refname, "refs/heads/");
Daniel Barkalow2d313472008-02-18 23:41:41 -0500187 strcpy(refname + strlen("refs/heads/"), ret->name);
188 ret->refname = refname;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400189
Daniel Barkalow2d313472008-02-18 23:41:41 -0500190 return ret;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400191}
192
Josh Triplettd071d942009-09-07 01:56:00 -0700193static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500194{
195 struct rewrite *ret;
196 int i;
197
Josh Triplettd071d942009-09-07 01:56:00 -0700198 for (i = 0; i < r->rewrite_nr; i++) {
Junio C Hamano844112c2008-02-24 22:25:04 -0800199 if (len
Josh Triplettd071d942009-09-07 01:56:00 -0700200 ? (len == r->rewrite[i]->baselen &&
201 !strncmp(base, r->rewrite[i]->base, len))
202 : !strcmp(base, r->rewrite[i]->base))
203 return r->rewrite[i];
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500204 }
205
Josh Triplettd071d942009-09-07 01:56:00 -0700206 ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500207 ret = xcalloc(1, sizeof(struct rewrite));
Josh Triplettd071d942009-09-07 01:56:00 -0700208 r->rewrite[r->rewrite_nr++] = ret;
Junio C Hamano844112c2008-02-24 22:25:04 -0800209 if (len) {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500210 ret->base = xstrndup(base, len);
Junio C Hamano844112c2008-02-24 22:25:04 -0800211 ret->baselen = len;
212 }
213 else {
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500214 ret->base = xstrdup(base);
Junio C Hamano844112c2008-02-24 22:25:04 -0800215 ret->baselen = strlen(base);
216 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500217 return ret;
218}
219
220static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
221{
222 ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
Junio C Hamano844112c2008-02-24 22:25:04 -0800223 rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
224 rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
225 rewrite->instead_of_nr++;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500226}
227
Daniel Barkalow5751f492007-05-12 11:45:53 -0400228static void read_remotes_file(struct remote *remote)
229{
230 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
231
232 if (!f)
233 return;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100234 remote->origin = REMOTE_REMOTES;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400235 while (fgets(buffer, BUF_SIZE, f)) {
236 int value_list;
237 char *s, *p;
238
239 if (!prefixcmp(buffer, "URL:")) {
240 value_list = 0;
241 s = buffer + 4;
242 } else if (!prefixcmp(buffer, "Push:")) {
243 value_list = 1;
244 s = buffer + 5;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400245 } else if (!prefixcmp(buffer, "Pull:")) {
246 value_list = 2;
247 s = buffer + 5;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400248 } else
249 continue;
250
251 while (isspace(*s))
252 s++;
253 if (!*s)
254 continue;
255
256 p = s + strlen(s);
257 while (isspace(p[-1]))
258 *--p = 0;
259
260 switch (value_list) {
261 case 0:
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500262 add_url_alias(remote, xstrdup(s));
Daniel Barkalow5751f492007-05-12 11:45:53 -0400263 break;
264 case 1:
265 add_push_refspec(remote, xstrdup(s));
266 break;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400267 case 2:
268 add_fetch_refspec(remote, xstrdup(s));
269 break;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400270 }
271 }
272 fclose(f);
273}
274
275static void read_branches_file(struct remote *remote)
276{
277 const char *slash = strchr(remote->name, '/');
Daniel Barkalowcf818342007-09-10 23:02:56 -0400278 char *frag;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500279 struct strbuf branch = STRBUF_INIT;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400280 int n = slash ? slash - remote->name : 1000;
281 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
282 char *s, *p;
283 int len;
284
285 if (!f)
286 return;
287 s = fgets(buffer, BUF_SIZE, f);
288 fclose(f);
289 if (!s)
290 return;
291 while (isspace(*s))
292 s++;
293 if (!*s)
294 return;
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100295 remote->origin = REMOTE_BRANCHES;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400296 p = s + strlen(s);
297 while (isspace(p[-1]))
298 *--p = 0;
299 len = p - s;
300 if (slash)
301 len += strlen(slash);
302 p = xmalloc(len + 1);
303 strcpy(p, s);
304 if (slash)
305 strcat(p, slash);
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400306
307 /*
308 * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
309 * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
310 * the partial URL obtained from the branches file plus
311 * "/netdev-2.6" and does not store it in any tracking ref.
312 * #branch specifier in the file is ignored.
313 *
314 * Otherwise, the branches file would have URL and optionally
315 * #branch specified. The "master" (or specified) branch is
316 * fetched and stored in the local branch of the same name.
317 */
Daniel Barkalowcf818342007-09-10 23:02:56 -0400318 frag = strchr(p, '#');
319 if (frag) {
320 *(frag++) = '\0';
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400321 strbuf_addf(&branch, "refs/heads/%s", frag);
322 } else
323 strbuf_addstr(&branch, "refs/heads/master");
324 if (!slash) {
325 strbuf_addf(&branch, ":refs/heads/%s", remote->name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400326 } else {
Daniel Barkalow472fa4c2008-03-25 19:35:28 -0400327 strbuf_reset(&branch);
328 strbuf_addstr(&branch, "HEAD:");
Daniel Barkalowcf818342007-09-10 23:02:56 -0400329 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500330 add_url_alias(remote, p);
Linus Torvalds2af202b2009-06-18 10:28:43 -0700331 add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
Martin Koegler18afe102008-11-10 22:47:11 +0100332 /*
333 * Cogito compatible push: push current HEAD to remote #branch
334 * (master if missing)
335 */
336 strbuf_init(&branch, 0);
337 strbuf_addstr(&branch, "HEAD");
338 if (frag)
339 strbuf_addf(&branch, ":refs/heads/%s", frag);
340 else
341 strbuf_addstr(&branch, ":refs/heads/master");
Linus Torvalds2af202b2009-06-18 10:28:43 -0700342 add_push_refspec(remote, strbuf_detach(&branch, NULL));
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400343 remote->fetch_tags = 1; /* always auto-follow */
Daniel Barkalow5751f492007-05-12 11:45:53 -0400344}
345
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100346static int handle_config(const char *key, const char *value, void *cb)
Daniel Barkalow5751f492007-05-12 11:45:53 -0400347{
348 const char *name;
349 const char *subkey;
350 struct remote *remote;
Daniel Barkalowcf818342007-09-10 23:02:56 -0400351 struct branch *branch;
352 if (!prefixcmp(key, "branch.")) {
353 name = key + 7;
354 subkey = strrchr(name, '.');
Daniel Barkalowcf818342007-09-10 23:02:56 -0400355 if (!subkey)
356 return 0;
Junio C Hamano896c0532007-12-14 20:34:56 -0800357 branch = make_branch(name, subkey - name);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400358 if (!strcmp(subkey, ".remote")) {
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800359 if (!value)
360 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400361 branch->remote_name = xstrdup(value);
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400362 if (branch == current_branch) {
Daniel Barkalowcf818342007-09-10 23:02:56 -0400363 default_remote_name = branch->remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400364 explicit_default_remote_name = 1;
365 }
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800366 } else if (!strcmp(subkey, ".merge")) {
367 if (!value)
368 return config_error_nonbool(key);
Daniel Barkalowcf818342007-09-10 23:02:56 -0400369 add_merge(branch, xstrdup(value));
Junio C Hamanod2370cc2008-02-11 11:00:10 -0800370 }
Daniel Barkalowcf818342007-09-10 23:02:56 -0400371 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400372 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500373 if (!prefixcmp(key, "url.")) {
374 struct rewrite *rewrite;
Daniel Barkalow60e3aba2008-04-12 15:32:00 -0400375 name = key + 4;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500376 subkey = strrchr(name, '.');
377 if (!subkey)
378 return 0;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500379 if (!strcmp(subkey, ".insteadof")) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700380 rewrite = make_rewrite(&rewrites, name, subkey - name);
381 if (!value)
382 return config_error_nonbool(key);
383 add_instead_of(rewrite, xstrdup(value));
384 } else if (!strcmp(subkey, ".pushinsteadof")) {
385 rewrite = make_rewrite(&rewrites_push, name, subkey - name);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500386 if (!value)
387 return config_error_nonbool(key);
388 add_instead_of(rewrite, xstrdup(value));
389 }
390 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400391 if (prefixcmp(key, "remote."))
392 return 0;
393 name = key + 7;
Brandon Caseyc82efaf2008-10-14 15:30:21 -0500394 if (*name == '/') {
395 warning("Config remote shorthand cannot begin with '/': %s",
396 name);
397 return 0;
398 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400399 subkey = strrchr(name, '.');
400 if (!subkey)
Johannes Sixtcd294bc2009-04-23 15:49:05 +0200401 return 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400402 remote = make_remote(name, subkey - name);
Miklos Vajna89cf4c72008-11-10 21:43:00 +0100403 remote->origin = REMOTE_CONFIG;
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200404 if (!strcmp(subkey, ".mirror"))
405 remote->mirror = git_config_bool(key, value);
406 else if (!strcmp(subkey, ".skipdefaultupdate"))
407 remote->skip_default_update = git_config_bool(key, value);
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100408 else if (!strcmp(subkey, ".skipfetchall"))
409 remote->skip_default_update = git_config_bool(key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200410 else if (!strcmp(subkey, ".url")) {
411 const char *v;
412 if (git_config_string(&v, key, value))
413 return -1;
414 add_url(remote, v);
Michael J Gruber20346232009-06-09 18:01:34 +0200415 } else if (!strcmp(subkey, ".pushurl")) {
416 const char *v;
417 if (git_config_string(&v, key, value))
418 return -1;
419 add_pushurl(remote, v);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400420 } else if (!strcmp(subkey, ".push")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200421 const char *v;
422 if (git_config_string(&v, key, value))
423 return -1;
424 add_push_refspec(remote, v);
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400425 } else if (!strcmp(subkey, ".fetch")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200426 const char *v;
427 if (git_config_string(&v, key, value))
428 return -1;
429 add_fetch_refspec(remote, v);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400430 } else if (!strcmp(subkey, ".receivepack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200431 const char *v;
432 if (git_config_string(&v, key, value))
433 return -1;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400434 if (!remote->receivepack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200435 remote->receivepack = v;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400436 else
437 error("more than one receivepack given, using the first");
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400438 } else if (!strcmp(subkey, ".uploadpack")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200439 const char *v;
440 if (git_config_string(&v, key, value))
441 return -1;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400442 if (!remote->uploadpack)
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200443 remote->uploadpack = v;
Daniel Barkalow0012ba22007-09-10 23:02:51 -0400444 else
445 error("more than one uploadpack given, using the first");
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400446 } else if (!strcmp(subkey, ".tagopt")) {
447 if (!strcmp(value, "--no-tags"))
448 remote->fetch_tags = -1;
Samuel Tardieu944163a2010-04-20 01:31:25 +0200449 else if (!strcmp(value, "--tags"))
450 remote->fetch_tags = 2;
Sam Vilain14c98212007-12-04 10:48:54 +1300451 } else if (!strcmp(subkey, ".proxy")) {
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200452 return git_config_string((const char **)&remote->http_proxy,
453 key, value);
Daniel Barkalowc578f512009-11-18 02:42:25 +0100454 } else if (!strcmp(subkey, ".vcs")) {
455 return git_config_string(&remote->foreign_vcs, key, value);
Paolo Bonzini84bb2df2008-04-17 13:17:20 +0200456 }
Daniel Barkalow5751f492007-05-12 11:45:53 -0400457 return 0;
458}
459
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500460static void alias_all_urls(void)
461{
462 int i, j;
463 for (i = 0; i < remotes_nr; i++) {
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700464 int add_pushurl_aliases;
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500465 if (!remotes[i])
466 continue;
Michael J Gruber20346232009-06-09 18:01:34 +0200467 for (j = 0; j < remotes[i]->pushurl_nr; j++) {
Josh Triplettd071d942009-09-07 01:56:00 -0700468 remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
Michael J Gruber20346232009-06-09 18:01:34 +0200469 }
Josh Triplett1c2eafb2009-09-07 01:56:33 -0700470 add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
471 for (j = 0; j < remotes[i]->url_nr; j++) {
472 if (add_pushurl_aliases)
473 add_pushurl_alias(remotes[i], remotes[i]->url[j]);
474 remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
475 }
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500476 }
477}
478
Daniel Barkalow5751f492007-05-12 11:45:53 -0400479static void read_config(void)
480{
481 unsigned char sha1[20];
482 const char *head_ref;
483 int flag;
Tor Arntsen2543d9b2010-06-04 11:32:11 +0200484 if (default_remote_name) /* did this already */
Daniel Barkalow5751f492007-05-12 11:45:53 -0400485 return;
486 default_remote_name = xstrdup("origin");
487 current_branch = NULL;
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +0700488 head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400489 if (head_ref && (flag & REF_ISSYMREF) &&
490 !prefixcmp(head_ref, "refs/heads/")) {
Daniel Barkalowcf818342007-09-10 23:02:56 -0400491 current_branch =
492 make_branch(head_ref + strlen("refs/heads/"), 0);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400493 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100494 git_config(handle_config, NULL);
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500495 alias_all_urls();
Daniel Barkalow5751f492007-05-12 11:45:53 -0400496}
497
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700498/*
Brandon Casey2cb1f362008-08-21 19:16:30 -0500499 * This function frees a refspec array.
500 * Warning: code paths should be checked to ensure that the src
501 * and dst pointers are always freeable pointers as well
502 * as the refspec pointer itself.
503 */
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900504static void free_refspecs(struct refspec *refspec, int nr_refspec)
Brandon Casey2cb1f362008-08-21 19:16:30 -0500505{
506 int i;
507
508 if (!refspec)
509 return;
510
511 for (i = 0; i < nr_refspec; i++) {
512 free(refspec[i].src);
513 free(refspec[i].dst);
514 }
515 free(refspec);
516}
517
Jonas Fonseca24b61772008-04-13 11:56:54 +0200518static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400519{
520 int i;
521 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700522
Daniel Barkalow6b628162007-05-12 11:45:59 -0400523 for (i = 0; i < nr_refspec; i++) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700524 size_t llen;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700525 int is_glob;
526 const char *lhs, *rhs;
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200527 int flags;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700528
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100529 is_glob = 0;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700530
531 lhs = refspec[i];
532 if (*lhs == '+') {
Daniel Barkalow6b628162007-05-12 11:45:59 -0400533 rs[i].force = 1;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700534 lhs++;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400535 }
Junio C Hamano46220ca2008-03-20 23:34:37 -0700536
537 rhs = strrchr(lhs, ':');
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400538
539 /*
540 * Before going on, special case ":" (or "+:") as a refspec
541 * for matching refs.
542 */
543 if (!fetch && rhs == lhs && rhs[1] == '\0') {
544 rs[i].matching = 1;
545 continue;
546 }
547
Junio C Hamano46220ca2008-03-20 23:34:37 -0700548 if (rhs) {
Junio C Hamano47c6ef12008-07-26 23:15:51 -0700549 size_t rlen = strlen(++rhs);
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500550 is_glob = (1 <= rlen && strchr(rhs, '*'));
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500551 rs[i].dst = xstrndup(rhs, rlen);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700552 }
553
554 llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500555 if (1 <= llen && memchr(lhs, '*', llen)) {
Junio C Hamano7d19da42008-03-25 21:15:52 -0700556 if ((rhs && !is_glob) || (!rhs && fetch))
557 goto invalid;
558 is_glob = 1;
Junio C Hamano7d19da42008-03-25 21:15:52 -0700559 } else if (rhs && is_glob) {
560 goto invalid;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700561 }
Junio C Hamano7d19da42008-03-25 21:15:52 -0700562
Junio C Hamano46220ca2008-03-20 23:34:37 -0700563 rs[i].pattern = is_glob;
564 rs[i].src = xstrndup(lhs, llen);
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200565 flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700566
567 if (fetch) {
568 /*
569 * LHS
570 * - empty is allowed; it means HEAD.
571 * - otherwise it must be a valid looking ref.
572 */
573 if (!*rs[i].src)
574 ; /* empty is ok */
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200575 else if (check_refname_format(rs[i].src, flags))
576 goto invalid;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700577 /*
578 * RHS
Junio C Hamano7d19da42008-03-25 21:15:52 -0700579 * - missing is ok, and is same as empty.
Junio C Hamano46220ca2008-03-20 23:34:37 -0700580 * - empty is ok; it means not to store.
581 * - otherwise it must be a valid looking ref.
582 */
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200583 if (!rs[i].dst)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700584 ; /* ok */
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200585 else if (!*rs[i].dst)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700586 ; /* ok */
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200587 else if (check_refname_format(rs[i].dst, flags))
588 goto invalid;
Daniel Barkalow6b628162007-05-12 11:45:59 -0400589 } else {
Junio C Hamano46220ca2008-03-20 23:34:37 -0700590 /*
591 * LHS
592 * - empty is allowed; it means delete.
593 * - when wildcarded, it must be a valid looking ref.
594 * - otherwise, it must be an extended SHA-1, but
595 * there is no existing way to validate this.
596 */
597 if (!*rs[i].src)
598 ; /* empty is ok */
599 else if (is_glob) {
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200600 if (check_refname_format(rs[i].src, flags))
Junio C Hamano46220ca2008-03-20 23:34:37 -0700601 goto invalid;
602 }
603 else
604 ; /* anything goes, for now */
605 /*
606 * RHS
607 * - missing is allowed, but LHS then must be a
608 * valid looking ref.
609 * - empty is not allowed.
610 * - otherwise it must be a valid looking ref.
611 */
612 if (!rs[i].dst) {
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200613 if (check_refname_format(rs[i].src, flags))
Junio C Hamano46220ca2008-03-20 23:34:37 -0700614 goto invalid;
615 } else if (!*rs[i].dst) {
616 goto invalid;
617 } else {
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200618 if (check_refname_format(rs[i].dst, flags))
Junio C Hamano46220ca2008-03-20 23:34:37 -0700619 goto invalid;
620 }
Daniel Barkalowef00d152008-03-17 22:05:23 -0400621 }
Daniel Barkalow6b628162007-05-12 11:45:59 -0400622 }
623 return rs;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700624
625 invalid:
Jonas Fonseca24b61772008-04-13 11:56:54 +0200626 if (verify) {
Brandon Casey2cb1f362008-08-21 19:16:30 -0500627 /*
628 * nr_refspec must be greater than zero and i must be valid
629 * since it is only possible to reach this point from within
630 * the for loop above.
631 */
632 free_refspecs(rs, i+1);
Jonas Fonseca24b61772008-04-13 11:56:54 +0200633 return NULL;
634 }
Junio C Hamano46220ca2008-03-20 23:34:37 -0700635 die("Invalid refspec '%s'", refspec[i]);
636}
637
Jonas Fonseca24b61772008-04-13 11:56:54 +0200638int valid_fetch_refspec(const char *fetch_refspec_str)
639{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200640 struct refspec *refspec;
641
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000642 refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1);
Brandon Casey2cb1f362008-08-21 19:16:30 -0500643 free_refspecs(refspec, 1);
Jonas Fonseca24b61772008-04-13 11:56:54 +0200644 return !!refspec;
645}
646
Junio C Hamano46220ca2008-03-20 23:34:37 -0700647struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
648{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200649 return parse_refspec_internal(nr_refspec, refspec, 1, 0);
Junio C Hamano46220ca2008-03-20 23:34:37 -0700650}
651
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900652static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700653{
Jonas Fonseca24b61772008-04-13 11:56:54 +0200654 return parse_refspec_internal(nr_refspec, refspec, 0, 0);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400655}
656
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100657void free_refspec(int nr_refspec, struct refspec *refspec)
658{
659 int i;
660 for (i = 0; i < nr_refspec; i++) {
661 free(refspec[i].src);
662 free(refspec[i].dst);
663 }
664 free(refspec);
665}
666
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500667static int valid_remote_nick(const char *name)
668{
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300669 if (!name[0] || is_dot_or_dotdot(name))
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500670 return 0;
671 return !strchr(name, '/'); /* no slash */
672}
673
Daniel Barkalow5751f492007-05-12 11:45:53 -0400674struct remote *remote_get(const char *name)
675{
676 struct remote *ret;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400677 int name_given = 0;
Daniel Barkalow5751f492007-05-12 11:45:53 -0400678
679 read_config();
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400680 if (name)
681 name_given = 1;
682 else {
Daniel Barkalow5751f492007-05-12 11:45:53 -0400683 name = default_remote_name;
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400684 name_given = explicit_default_remote_name;
685 }
Junio C Hamano9326d492009-03-16 00:35:09 -0700686
Daniel Barkalow5751f492007-05-12 11:45:53 -0400687 ret = make_remote(name, 0);
Daniel Barkalowdf93e332008-02-15 14:14:18 -0500688 if (valid_remote_nick(name)) {
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100689 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400690 read_remotes_file(ret);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100691 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400692 read_branches_file(ret);
693 }
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100694 if (name_given && !valid_remote(ret))
Daniel Barkalow55029ae2008-02-20 13:43:53 -0500695 add_url_alias(ret, name);
Daniel Barkalow0a4da292009-11-18 02:42:23 +0100696 if (!valid_remote(ret))
Daniel Barkalow5751f492007-05-12 11:45:53 -0400697 return NULL;
Junio C Hamano46220ca2008-03-20 23:34:37 -0700698 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
699 ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
Daniel Barkalow5751f492007-05-12 11:45:53 -0400700 return ret;
701}
Daniel Barkalow6b628162007-05-12 11:45:59 -0400702
Finn Arne Gangstad9a23ba32009-04-06 15:41:01 +0200703int remote_is_configured(const char *name)
704{
705 int i;
706 read_config();
707
708 for (i = 0; i < remotes_nr; i++)
709 if (!strcmp(name, remotes[i]->name))
710 return 1;
711 return 0;
712}
713
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100714int for_each_remote(each_remote_fn fn, void *priv)
715{
716 int i, result = 0;
717 read_config();
Daniel Barkalow2d313472008-02-18 23:41:41 -0500718 for (i = 0; i < remotes_nr && !result; i++) {
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100719 struct remote *r = remotes[i];
720 if (!r)
721 continue;
722 if (!r->fetch)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700723 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
724 r->fetch_refspec);
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100725 if (!r->push)
Junio C Hamano46220ca2008-03-20 23:34:37 -0700726 r->push = parse_push_refspec(r->push_refspec_nr,
727 r->push_refspec);
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100728 result = fn(r, priv);
729 }
730 return result;
731}
732
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400733void ref_remove_duplicates(struct ref *ref_map)
734{
Thiago Farina183113a2010-07-04 16:46:19 -0300735 struct string_list refs = STRING_LIST_INIT_NODUP;
Julian Phillips73cf0822009-10-25 21:28:11 +0000736 struct string_list_item *item = NULL;
737 struct ref *prev = NULL, *next = NULL;
738 for (; ref_map; prev = ref_map, ref_map = next) {
739 next = ref_map->next;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400740 if (!ref_map->peer_ref)
741 continue;
Julian Phillips73cf0822009-10-25 21:28:11 +0000742
Julian Phillipse8c8b712010-06-26 00:41:37 +0100743 item = string_list_lookup(&refs, ref_map->peer_ref->name);
Julian Phillips73cf0822009-10-25 21:28:11 +0000744 if (item) {
745 if (strcmp(((struct ref *)item->util)->name,
746 ref_map->name))
747 die("%s tracks both %s and %s",
748 ref_map->peer_ref->name,
749 ((struct ref *)item->util)->name,
750 ref_map->name);
751 prev->next = ref_map->next;
752 free(ref_map->peer_ref);
753 free(ref_map);
Julian Phillips95c96d42009-11-13 21:25:56 +0000754 ref_map = prev; /* skip this; we freed it */
755 continue;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400756 }
Julian Phillips73cf0822009-10-25 21:28:11 +0000757
Julian Phillips78a395d2010-06-26 00:41:35 +0100758 item = string_list_insert(&refs, ref_map->peer_ref->name);
Julian Phillips73cf0822009-10-25 21:28:11 +0000759 item->util = ref_map;
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400760 }
Julian Phillips73cf0822009-10-25 21:28:11 +0000761 string_list_clear(&refs, 0);
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400762}
763
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400764int remote_has_url(struct remote *remote, const char *url)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400765{
766 int i;
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400767 for (i = 0; i < remote->url_nr; i++) {
768 if (!strcmp(remote->url[i], url))
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400769 return 1;
770 }
771 return 0;
772}
773
Daniel Barkalowe9282132009-03-07 01:11:34 -0500774static int match_name_with_pattern(const char *key, const char *name,
775 const char *value, char **result)
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500776{
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500777 const char *kstar = strchr(key, '*');
778 size_t klen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500779 size_t ksuffixlen;
780 size_t namelen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500781 int ret;
782 if (!kstar)
783 die("Key '%s' of pattern had no '*'", key);
784 klen = kstar - key;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500785 ksuffixlen = strlen(kstar + 1);
786 namelen = strlen(name);
787 ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
788 !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500789 if (ret && value) {
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500790 const char *vstar = strchr(value, '*');
791 size_t vlen;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500792 size_t vsuffixlen;
Daniel Barkalow08fbdb32009-03-07 01:11:36 -0500793 if (!vstar)
794 die("Value '%s' of pattern has no '*'", value);
795 vlen = vstar - value;
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500796 vsuffixlen = strlen(vstar + 1);
797 *result = xmalloc(vlen + vsuffixlen +
Daniel Barkalowe9282132009-03-07 01:11:34 -0500798 strlen(name) -
Daniel Barkalowabd2bde2009-03-07 01:11:39 -0500799 klen - ksuffixlen + 1);
800 strncpy(*result, value, vlen);
801 strncpy(*result + vlen,
802 name + klen, namelen - klen - ksuffixlen);
803 strcpy(*result + vlen + namelen - klen - ksuffixlen,
804 vstar + 1);
Daniel Barkalowe9282132009-03-07 01:11:34 -0500805 }
Daniel Barkalowa3c84232009-03-07 01:11:29 -0500806 return ret;
807}
808
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200809static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100810{
811 int i;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200812 int find_src = !query->src;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100813
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200814 if (find_src && !query->dst)
815 return error("query_refspecs: need either src or dst");
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100816
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200817 for (i = 0; i < ref_count; i++) {
818 struct refspec *refspec = &refs[i];
819 const char *key = find_src ? refspec->dst : refspec->src;
820 const char *value = find_src ? refspec->src : refspec->dst;
821 const char *needle = find_src ? query->dst : query->src;
822 char **result = find_src ? &query->src : &query->dst;
823
Shawn O. Pearce009c5bc2007-09-25 00:13:14 -0400824 if (!refspec->dst)
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400825 continue;
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200826 if (refspec->pattern) {
Daniel Barkalowe9282132009-03-07 01:11:34 -0500827 if (match_name_with_pattern(key, needle, value, result)) {
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200828 query->force = refspec->force;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400829 return 0;
830 }
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100831 } else if (!strcmp(needle, key)) {
832 *result = xstrdup(value);
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200833 query->force = refspec->force;
Johannes Schindelinb42f6922007-07-10 18:48:40 +0100834 return 0;
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400835 }
836 }
Daniel Barkalow5d46c9d2007-05-12 11:46:03 -0400837 return -1;
838}
839
Carlos Martín Nietoc5003522011-10-15 07:04:24 +0200840char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
841 const char *name)
842{
843 struct refspec query;
844
845 memset(&query, 0, sizeof(struct refspec));
846 query.src = (char *)name;
847
848 if (query_refspecs(refspecs, nr_refspec, &query))
849 return NULL;
850
851 return query.dst;
852}
853
854int remote_find_tracking(struct remote *remote, struct refspec *refspec)
855{
856 return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
857}
858
René Scharfe80097682008-10-18 10:37:40 +0200859static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
860 const char *name)
861{
862 size_t len = strlen(name);
863 struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
864 memcpy(ref->name, prefix, prefixlen);
865 memcpy(ref->name + prefixlen, name, len);
866 return ref;
867}
868
René Scharfe59c69c02008-10-18 10:44:18 +0200869struct ref *alloc_ref(const char *name)
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400870{
René Scharfe59c69c02008-10-18 10:44:18 +0200871 return alloc_ref_with_prefix("", 0, name);
Krzysztof Kowalczyk737922a2008-05-10 16:26:58 -0700872}
873
Jeff King59a57752011-06-07 19:03:03 -0400874struct ref *copy_ref(const struct ref *ref)
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400875{
Jay Soffian7b3db092009-02-27 14:10:04 -0500876 struct ref *cpy;
877 size_t len;
878 if (!ref)
879 return NULL;
880 len = strlen(ref->name);
881 cpy = xmalloc(sizeof(struct ref) + len + 1);
882 memcpy(cpy, ref, sizeof(struct ref) + len + 1);
883 cpy->next = NULL;
884 cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
885 cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
886 cpy->peer_ref = copy_ref(ref->peer_ref);
887 return cpy;
Daniel Barkalowd71ab172007-09-10 23:03:08 -0400888}
889
Daniel Barkalow45773702007-10-29 21:05:40 -0400890struct ref *copy_ref_list(const struct ref *ref)
891{
892 struct ref *ret = NULL;
893 struct ref **tail = &ret;
894 while (ref) {
895 *tail = copy_ref(ref);
896 ref = ref->next;
897 tail = &((*tail)->next);
898 }
899 return ret;
900}
901
Nanako Shiraishi697d7f52008-09-25 18:41:00 +0900902static void free_ref(struct ref *ref)
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400903{
904 if (!ref)
905 return;
Jay Soffian7b3db092009-02-27 14:10:04 -0500906 free_ref(ref->peer_ref);
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400907 free(ref->remote_status);
908 free(ref->symref);
909 free(ref);
910}
911
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400912void free_refs(struct ref *ref)
913{
914 struct ref *next;
915 while (ref) {
916 next = ref->next;
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400917 free_ref(ref);
Daniel Barkalowdfd255d2007-07-10 00:47:23 -0400918 ref = next;
919 }
920}
921
Jeff Kinged81c762012-05-21 18:19:28 -0400922int ref_compare_name(const void *va, const void *vb)
923{
924 const struct ref *a = va, *b = vb;
925 return strcmp(a->name, b->name);
926}
927
928static void *ref_list_get_next(const void *a)
929{
930 return ((const struct ref *)a)->next;
931}
932
933static void ref_list_set_next(void *a, void *next)
934{
935 ((struct ref *)a)->next = next;
936}
937
938void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
939{
940 *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
941}
942
Daniel Barkalow6b628162007-05-12 11:45:59 -0400943static int count_refspec_match(const char *pattern,
944 struct ref *refs,
945 struct ref **matched_ref)
946{
947 int patlen = strlen(pattern);
948 struct ref *matched_weak = NULL;
949 struct ref *matched = NULL;
950 int weak_match = 0;
951 int match = 0;
952
953 for (weak_match = match = 0; refs; refs = refs->next) {
954 char *name = refs->name;
955 int namelen = strlen(name);
Daniel Barkalow6b628162007-05-12 11:45:59 -0400956
Steffen Prohaskaae36bdc2007-11-11 15:01:47 +0100957 if (!refname_match(pattern, name, ref_rev_parse_rules))
Daniel Barkalow6b628162007-05-12 11:45:59 -0400958 continue;
959
960 /* A match is "weak" if it is with refs outside
961 * heads or tags, and did not specify the pattern
962 * in full (e.g. "refs/remotes/origin/master") or at
963 * least from the toplevel (e.g. "remotes/origin/master");
964 * otherwise "git push $URL master" would result in
965 * ambiguity between remotes/origin/master and heads/master
966 * at the remote site.
967 */
968 if (namelen != patlen &&
969 patlen != namelen - 5 &&
970 prefixcmp(name, "refs/heads/") &&
971 prefixcmp(name, "refs/tags/")) {
972 /* We want to catch the case where only weak
973 * matches are found and there are multiple
974 * matches, and where more than one strong
975 * matches are found, as ambiguous. One
976 * strong match with zero or more weak matches
977 * are acceptable as a unique match.
978 */
979 matched_weak = refs;
980 weak_match++;
981 }
982 else {
983 matched = refs;
984 match++;
985 }
986 }
987 if (!matched) {
988 *matched_ref = matched_weak;
989 return weak_match;
990 }
991 else {
992 *matched_ref = matched;
993 return match;
994 }
995}
996
Daniel Barkalow1d735262007-07-10 00:47:26 -0400997static void tail_link_ref(struct ref *ref, struct ref ***tail)
Daniel Barkalow6b628162007-05-12 11:45:59 -0400998{
999 **tail = ref;
Daniel Barkalow1d735262007-07-10 00:47:26 -04001000 while (ref->next)
1001 ref = ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001002 *tail = &ref->next;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001003}
1004
Felipe Contreras67655242012-02-23 00:43:40 +02001005static struct ref *alloc_delete_ref(void)
1006{
1007 struct ref *ref = alloc_ref("(delete)");
1008 hashclr(ref->new_sha1);
1009 return ref;
1010}
1011
Daniel Barkalow6b628162007-05-12 11:45:59 -04001012static struct ref *try_explicit_object_name(const char *name)
1013{
1014 unsigned char sha1[20];
1015 struct ref *ref;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001016
Felipe Contreras67655242012-02-23 00:43:40 +02001017 if (!*name)
1018 return alloc_delete_ref();
Daniel Barkalow6b628162007-05-12 11:45:59 -04001019 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
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001038 const char *r = resolve_ref_unsafe(peer->name, sha1, 1, NULL);
Jeff Kingf8aae122008-04-23 05:16:06 -04001039 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
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001089 dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
Daniel Barkalow9f0ea7e2008-02-20 12:54:05 -05001090 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);
Jeff King5742c822012-07-03 14:04:39 -04001103 else if (is_null_sha1(matched_src->new_sha1))
1104 error("unable to delete '%s': remote ref does not exist",
1105 dst_value);
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04001106 else if ((dst_guess = guess_ref(dst_value, matched_src)))
Jeff Kingf8aae122008-04-23 05:16:06 -04001107 matched_dst = make_linked_ref(dst_guess, dst_tail);
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001108 else
Jeff Kingf8aae122008-04-23 05:16:06 -04001109 error("unable to push to unqualified destination: %s\n"
1110 "The destination refspec neither matches an "
1111 "existing ref on the remote nor\n"
1112 "begins with refs/, and we are unable to "
1113 "guess a prefix based on the source ref.",
1114 dst_value);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001115 break;
1116 default:
Junio C Hamano3c8b7df2007-06-09 00:14:04 -07001117 matched_dst = NULL;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001118 error("dst refspec %s matches more than one.",
1119 dst_value);
1120 break;
1121 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001122 if (!matched_dst)
1123 return -1;
1124 if (matched_dst->peer_ref)
1125 return error("dst ref %s receives from more than one src.",
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001126 matched_dst->name);
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001127 else {
Jay Soffiancdf690e2009-02-25 03:32:16 -05001128 matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001129 matched_dst->force = rs->force;
1130 }
Jeff King9a7bbd12008-06-16 12:15:02 -04001131 return 0;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001132}
1133
Daniel Barkalow6b628162007-05-12 11:45:59 -04001134static int match_explicit_refs(struct ref *src, struct ref *dst,
1135 struct ref ***dst_tail, struct refspec *rs,
1136 int rs_nr)
1137{
1138 int i, errs;
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001139 for (i = errs = 0; i < rs_nr; i++)
Jeff King9a7bbd12008-06-16 12:15:02 -04001140 errs += match_explicit(src, dst, dst_tail, &rs[i]);
1141 return errs;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001142}
1143
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001144static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref *ref,
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001145 int send_mirror, int direction, const struct refspec **ret_pat)
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001146{
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001147 const struct refspec *pat;
1148 char *name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001149 int i;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001150 int matching_refs = -1;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001151 for (i = 0; i < rs_nr; i++) {
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001152 if (rs[i].matching &&
1153 (matching_refs == -1 || rs[i].force)) {
1154 matching_refs = i;
1155 continue;
1156 }
1157
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001158 if (rs[i].pattern) {
1159 const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001160 int match;
1161 if (direction == FROM_SRC)
1162 match = match_name_with_pattern(rs[i].src, ref->name, dst_side, &name);
1163 else
1164 match = match_name_with_pattern(dst_side, ref->name, rs[i].src, &name);
1165 if (match) {
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001166 matching_refs = i;
1167 break;
1168 }
1169 }
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001170 }
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001171 if (matching_refs == -1)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001172 return NULL;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001173
1174 pat = rs + matching_refs;
1175 if (pat->matching) {
1176 /*
1177 * "matching refs"; traditionally we pushed everything
1178 * including refs outside refs/heads/ hierarchy, but
1179 * that does not make much sense these days.
1180 */
1181 if (!send_mirror && prefixcmp(ref->name, "refs/heads/"))
1182 return NULL;
1183 name = xstrdup(ref->name);
1184 }
1185 if (ret_pat)
1186 *ret_pat = pat;
1187 return name;
Daniel Barkalow8558fd92007-05-25 01:20:56 -04001188}
1189
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001190static struct ref **tail_ref(struct ref **head)
1191{
1192 struct ref **tail = head;
1193 while (*tail)
1194 tail = &((*tail)->next);
1195 return tail;
1196}
1197
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001198/*
Junio C Hamano29753cd2011-09-09 11:54:58 -07001199 * Given the set of refs the local repository has, the set of refs the
1200 * remote repository has, and the refspec used for push, determine
1201 * what remote refs we will update and with what value by setting
1202 * peer_ref (which object is being pushed) and force (if the push is
1203 * forced) in elements of "dst". The function may add new elements to
1204 * dst (e.g. pushing to a new branch, done in match_explicit_refs).
Junio C Hamano54a8ad92007-06-08 23:22:58 -07001205 */
Junio C Hamano29753cd2011-09-09 11:54:58 -07001206int match_push_refs(struct ref *src, struct ref **dst,
1207 int nr_refspec, const char **refspec, int flags)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001208{
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001209 struct refspec *rs;
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001210 int send_all = flags & MATCH_REFS_ALL;
1211 int send_mirror = flags & MATCH_REFS_MIRROR;
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001212 int send_prune = flags & MATCH_REFS_PRUNE;
Jay Soffian5f48cb92009-02-25 03:32:17 -05001213 int errs;
Linus Torvalds2af202b2009-06-18 10:28:43 -07001214 static const char *default_refspec[] = { ":", NULL };
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001215 struct ref *ref, **dst_tail = tail_ref(dst);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001216
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001217 if (!nr_refspec) {
1218 nr_refspec = 1;
1219 refspec = default_refspec;
1220 }
1221 rs = parse_push_refspec(nr_refspec, (const char **) refspec);
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001222 errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001223
1224 /* pick the remainder */
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001225 for (ref = src; ref; ref = ref->next) {
Daniel Barkalow6b628162007-05-12 11:45:59 -04001226 struct ref *dst_peer;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001227 const struct refspec *pat = NULL;
1228 char *dst_name;
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001229
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001230 if (ref->peer_ref)
Daniel Barkalow6b628162007-05-12 11:45:59 -04001231 continue;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001232
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001233 dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_SRC, &pat);
Felipe Contrerasdb70a042012-02-23 00:43:39 +02001234 if (!dst_name)
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001235 continue;
1236
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001237 dst_peer = find_ref_by_name(*dst, dst_name);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001238 if (dst_peer) {
1239 if (dst_peer->peer_ref)
1240 /* We're already sending something to this ref. */
1241 goto free_name;
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001242 } else {
1243 if (pat->matching && !(send_all || send_mirror))
1244 /*
1245 * Remote doesn't have it, and we have no
1246 * explicit pattern, and we don't have
1247 * --all nor --mirror.
1248 */
1249 goto free_name;
1250
Daniel Barkalow6b628162007-05-12 11:45:59 -04001251 /* Create a new one and link it */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001252 dst_peer = make_linked_ref(dst_name, &dst_tail);
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001253 hashcpy(dst_peer->new_sha1, ref->new_sha1);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001254 }
Felipe Contrerasb1d8b1f2012-02-23 00:43:38 +02001255 dst_peer->peer_ref = copy_ref(ref);
Paolo Bonzinia83619d2008-04-28 11:32:12 -04001256 dst_peer->force = pat->force;
Alex Riesen6e66bf32007-06-08 01:43:05 +02001257 free_name:
1258 free(dst_name);
Daniel Barkalow6b628162007-05-12 11:45:59 -04001259 }
Felipe Contreras6ddba5e2012-02-23 00:43:41 +02001260 if (send_prune) {
1261 /* check for missing refs on the remote */
1262 for (ref = *dst; ref; ref = ref->next) {
1263 char *src_name;
1264
1265 if (ref->peer_ref)
1266 /* We're already sending something to this ref. */
1267 continue;
1268
1269 src_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_DST, NULL);
1270 if (src_name) {
1271 if (!find_ref_by_name(src, src_name))
1272 ref->peer_ref = alloc_delete_ref();
1273 free(src_name);
1274 }
1275 }
1276 }
Jay Soffian5f48cb92009-02-25 03:32:17 -05001277 if (errs)
1278 return -1;
Daniel Barkalow6b628162007-05-12 11:45:59 -04001279 return 0;
1280}
Daniel Barkalowcf818342007-09-10 23:02:56 -04001281
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001282void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1283 int force_update)
1284{
1285 struct ref *ref;
1286
1287 for (ref = remote_refs; ref; ref = ref->next) {
1288 if (ref->peer_ref)
1289 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1290 else if (!send_mirror)
1291 continue;
1292
1293 ref->deletion = is_null_sha1(ref->new_sha1);
1294 if (!ref->deletion &&
1295 !hashcmp(ref->old_sha1, ref->new_sha1)) {
1296 ref->status = REF_STATUS_UPTODATE;
1297 continue;
1298 }
1299
1300 /* This part determines what can overwrite what.
1301 * The rules are:
1302 *
1303 * (0) you can always use --force or +A:B notation to
1304 * selectively force individual ref pairs.
1305 *
1306 * (1) if the old thing does not exist, it is OK.
1307 *
1308 * (2) if you do not have the old thing, you are not allowed
1309 * to overwrite it; you would not know what you are losing
1310 * otherwise.
1311 *
1312 * (3) if both new and old are commit-ish, and new is a
1313 * descendant of old, it is OK.
1314 *
1315 * (4) regardless of all of the above, removing :B is
1316 * always allowed.
1317 */
1318
1319 ref->nonfastforward =
1320 !ref->deletion &&
1321 !is_null_sha1(ref->old_sha1) &&
1322 (!has_sha1_file(ref->old_sha1)
1323 || !ref_newer(ref->new_sha1, ref->old_sha1));
1324
1325 if (ref->nonfastforward && !ref->force && !force_update) {
1326 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
1327 continue;
1328 }
1329 }
1330}
1331
Daniel Barkalowcf818342007-09-10 23:02:56 -04001332struct branch *branch_get(const char *name)
1333{
1334 struct branch *ret;
1335
1336 read_config();
1337 if (!name || !*name || !strcmp(name, "HEAD"))
1338 ret = current_branch;
1339 else
1340 ret = make_branch(name, 0);
1341 if (ret && ret->remote_name) {
1342 ret->remote = remote_get(ret->remote_name);
1343 if (ret->merge_nr) {
1344 int i;
1345 ret->merge = xcalloc(sizeof(*ret->merge),
1346 ret->merge_nr);
1347 for (i = 0; i < ret->merge_nr; i++) {
1348 ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
1349 ret->merge[i]->src = xstrdup(ret->merge_name[i]);
Michael J Gruber5e6e2b42009-04-01 23:42:49 +02001350 if (remote_find_tracking(ret->remote, ret->merge[i])
1351 && !strcmp(ret->remote_name, "."))
1352 ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001353 }
1354 }
1355 }
1356 return ret;
1357}
1358
1359int branch_has_merge_config(struct branch *branch)
1360{
1361 return branch && !!branch->merge;
1362}
1363
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001364int branch_merge_matches(struct branch *branch,
1365 int i,
1366 const char *refname)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001367{
Shawn O. Pearce85682c12007-09-18 04:54:53 -04001368 if (!branch || i < 0 || i >= branch->merge_nr)
Daniel Barkalowcf818342007-09-10 23:02:56 -04001369 return 0;
Steffen Prohaska605b4972007-11-11 15:01:48 +01001370 return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
Daniel Barkalowcf818342007-09-10 23:02:56 -04001371}
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001372
Daniel Barkalow45773702007-10-29 21:05:40 -04001373static struct ref *get_expanded_map(const struct ref *remote_refs,
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001374 const struct refspec *refspec)
1375{
Daniel Barkalow45773702007-10-29 21:05:40 -04001376 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001377 struct ref *ret = NULL;
1378 struct ref **tail = &ret;
1379
Daniel Barkalowe9282132009-03-07 01:11:34 -05001380 char *expn_name;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001381
1382 for (ref = remote_refs; ref; ref = ref->next) {
1383 if (strchr(ref->name, '^'))
1384 continue; /* a dereference item */
Daniel Barkalowe9282132009-03-07 01:11:34 -05001385 if (match_name_with_pattern(refspec->src, ref->name,
1386 refspec->dst, &expn_name)) {
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001387 struct ref *cpy = copy_ref(ref);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001388
Daniel Barkalowe9282132009-03-07 01:11:34 -05001389 cpy->peer_ref = alloc_ref(expn_name);
1390 free(expn_name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001391 if (refspec->force)
1392 cpy->peer_ref->force = 1;
1393 *tail = cpy;
1394 tail = &cpy->next;
1395 }
1396 }
1397
1398 return ret;
1399}
1400
Daniel Barkalow45773702007-10-29 21:05:40 -04001401static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001402{
Daniel Barkalow45773702007-10-29 21:05:40 -04001403 const struct ref *ref;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001404 for (ref = refs; ref; ref = ref->next) {
Steffen Prohaska605b4972007-11-11 15:01:48 +01001405 if (refname_match(name, ref->name, ref_fetch_rules))
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001406 return ref;
1407 }
1408 return NULL;
1409}
1410
Daniel Barkalow45773702007-10-29 21:05:40 -04001411struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001412{
Daniel Barkalow45773702007-10-29 21:05:40 -04001413 const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001414
1415 if (!ref)
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001416 return NULL;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001417
1418 return copy_ref(ref);
1419}
1420
1421static struct ref *get_local_ref(const char *name)
1422{
Clemens Buchacher3eb96992009-06-17 15:38:36 +02001423 if (!name || name[0] == '\0')
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001424 return NULL;
1425
René Scharfe59c69c02008-10-18 10:44:18 +02001426 if (!prefixcmp(name, "refs/"))
1427 return alloc_ref(name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001428
1429 if (!prefixcmp(name, "heads/") ||
1430 !prefixcmp(name, "tags/") ||
René Scharfe80097682008-10-18 10:37:40 +02001431 !prefixcmp(name, "remotes/"))
1432 return alloc_ref_with_prefix("refs/", 5, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001433
René Scharfe80097682008-10-18 10:37:40 +02001434 return alloc_ref_with_prefix("refs/heads/", 11, name);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001435}
1436
Daniel Barkalow45773702007-10-29 21:05:40 -04001437int get_fetch_map(const struct ref *remote_refs,
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001438 const struct refspec *refspec,
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001439 struct ref ***tail,
1440 int missing_ok)
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001441{
Daniel Barkalowef00d152008-03-17 22:05:23 -04001442 struct ref *ref_map, **rmp;
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001443
1444 if (refspec->pattern) {
1445 ref_map = get_expanded_map(remote_refs, refspec);
1446 } else {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001447 const char *name = refspec->src[0] ? refspec->src : "HEAD";
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001448
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -07001449 ref_map = get_remote_ref(remote_refs, name);
1450 if (!missing_ok && !ref_map)
1451 die("Couldn't find remote ref %s", name);
1452 if (ref_map) {
1453 ref_map->peer_ref = get_local_ref(refspec->dst);
1454 if (ref_map->peer_ref && refspec->force)
1455 ref_map->peer_ref->force = 1;
1456 }
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001457 }
1458
Daniel Barkalowef00d152008-03-17 22:05:23 -04001459 for (rmp = &ref_map; *rmp; ) {
1460 if ((*rmp)->peer_ref) {
Michael Haggerty8d9c5012011-09-15 23:10:25 +02001461 if (check_refname_format((*rmp)->peer_ref->name + 5,
1462 REFNAME_ALLOW_ONELEVEL)) {
Daniel Barkalowef00d152008-03-17 22:05:23 -04001463 struct ref *ignore = *rmp;
1464 error("* Ignoring funny ref '%s' locally",
1465 (*rmp)->peer_ref->name);
1466 *rmp = (*rmp)->next;
1467 free(ignore->peer_ref);
1468 free(ignore);
1469 continue;
1470 }
1471 }
1472 rmp = &((*rmp)->next);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001473 }
1474
Alex Riesen8f70a762007-10-12 22:40:04 +02001475 if (ref_map)
1476 tail_link_ref(ref_map, tail);
Daniel Barkalowd71ab172007-09-10 23:03:08 -04001477
1478 return 0;
1479}
Daniel Barkalowbe885d92008-04-26 15:53:12 -04001480
1481int resolve_remote_symref(struct ref *ref, struct ref *list)
1482{
1483 if (!ref->symref)
1484 return 0;
1485 for (; list; list = list->next)
1486 if (!strcmp(ref->symref, list->name)) {
1487 hashcpy(ref->old_sha1, list->old_sha1);
1488 return 0;
1489 }
1490 return 1;
1491}
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001492
Jay Soffianec8452d2009-02-25 03:32:12 -05001493static void unmark_and_free(struct commit_list *list, unsigned int mark)
1494{
1495 while (list) {
1496 struct commit_list *temp = list;
1497 temp->item->object.flags &= ~mark;
1498 list = temp->next;
1499 free(temp);
1500 }
1501}
1502
1503int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
1504{
1505 struct object *o;
1506 struct commit *old, *new;
1507 struct commit_list *list, *used;
1508 int found = 0;
1509
1510 /* Both new and old must be commit-ish and new is descendant of
1511 * old. Otherwise we require --force.
1512 */
1513 o = deref_tag(parse_object(old_sha1), NULL, 0);
1514 if (!o || o->type != OBJ_COMMIT)
1515 return 0;
1516 old = (struct commit *) o;
1517
1518 o = deref_tag(parse_object(new_sha1), NULL, 0);
1519 if (!o || o->type != OBJ_COMMIT)
1520 return 0;
1521 new = (struct commit *) o;
1522
1523 if (parse_commit(new) < 0)
1524 return 0;
1525
1526 used = list = NULL;
1527 commit_list_insert(new, &list);
1528 while (list) {
1529 new = pop_most_recent_commit(&list, TMP_MARK);
1530 commit_list_insert(new, &used);
1531 if (new == old) {
1532 found = 1;
1533 break;
1534 }
1535 }
1536 unmark_and_free(list, TMP_MARK);
1537 unmark_and_free(used, TMP_MARK);
1538 return found;
1539}
1540
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001541/*
1542 * Return true if there is anything to report, otherwise false.
1543 */
1544int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1545{
1546 unsigned char sha1[20];
1547 struct commit *ours, *theirs;
1548 char symmetric[84];
1549 struct rev_info revs;
1550 const char *rev_argv[10], *base;
1551 int rev_argc;
1552
1553 /*
1554 * Nothing to report unless we are marked to build on top of
1555 * somebody else.
1556 */
1557 if (!branch ||
1558 !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1559 return 0;
1560
1561 /*
1562 * If what we used to build on no longer exists, there is
1563 * nothing to report.
1564 */
1565 base = branch->merge[0]->dst;
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001566 if (read_ref(base, sha1))
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001567 return 0;
Michael J Gruber57ffc5f2009-05-11 16:42:54 +02001568 theirs = lookup_commit_reference(sha1);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001569 if (!theirs)
1570 return 0;
1571
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001572 if (read_ref(branch->refname, sha1))
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001573 return 0;
Michael J Gruber57ffc5f2009-05-11 16:42:54 +02001574 ours = lookup_commit_reference(sha1);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001575 if (!ours)
1576 return 0;
1577
1578 /* are we the same? */
1579 if (theirs == ours)
1580 return 0;
1581
Junio C Hamano8fbf8792009-04-21 16:32:18 -07001582 /* Run "rev-list --left-right ours...theirs" internally... */
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001583 rev_argc = 0;
1584 rev_argv[rev_argc++] = NULL;
1585 rev_argv[rev_argc++] = "--left-right";
1586 rev_argv[rev_argc++] = symmetric;
1587 rev_argv[rev_argc++] = "--";
1588 rev_argv[rev_argc] = NULL;
1589
1590 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
1591 strcpy(symmetric + 40, "...");
1592 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
1593
1594 init_revisions(&revs, NULL);
1595 setup_revisions(rev_argc, rev_argv, &revs, NULL);
1596 prepare_revision_walk(&revs);
1597
1598 /* ... and count the commits on each side. */
1599 *num_ours = 0;
1600 *num_theirs = 0;
1601 while (1) {
1602 struct commit *c = get_revision(&revs);
1603 if (!c)
1604 break;
1605 if (c->object.flags & SYMMETRIC_LEFT)
1606 (*num_ours)++;
1607 else
1608 (*num_theirs)++;
1609 }
Junio C Hamanoc0234b22008-07-03 12:09:48 -07001610
1611 /* clear object flags smudged by the above traversal */
1612 clear_commit_marks(ours, ALL_REV_FLAGS);
1613 clear_commit_marks(theirs, ALL_REV_FLAGS);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001614 return 1;
1615}
1616
1617/*
1618 * Return true when there is anything to report, otherwise false.
1619 */
1620int format_tracking_info(struct branch *branch, struct strbuf *sb)
1621{
1622 int num_ours, num_theirs;
Avery Pennarun4de53ce2008-07-16 15:19:27 -04001623 const char *base;
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001624
1625 if (!stat_tracking_info(branch, &num_ours, &num_theirs))
1626 return 0;
1627
1628 base = branch->merge[0]->dst;
Michael J Gruber45972ff2009-04-16 10:20:44 +02001629 base = shorten_unambiguous_ref(base, 0);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001630 if (!num_theirs)
Jiang Xin8a5b7492012-02-02 10:02:23 +08001631 strbuf_addf(sb,
1632 Q_("Your branch is ahead of '%s' by %d commit.\n",
1633 "Your branch is ahead of '%s' by %d commits.\n",
1634 num_ours),
1635 base, num_ours);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001636 else if (!num_ours)
Jiang Xin8a5b7492012-02-02 10:02:23 +08001637 strbuf_addf(sb,
1638 Q_("Your branch is behind '%s' by %d commit, "
1639 "and can be fast-forwarded.\n",
1640 "Your branch is behind '%s' by %d commits, "
1641 "and can be fast-forwarded.\n",
1642 num_theirs),
1643 base, num_theirs);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001644 else
Jiang Xin8a5b7492012-02-02 10:02:23 +08001645 strbuf_addf(sb,
1646 Q_("Your branch and '%s' have diverged,\n"
1647 "and have %d and %d different commit each, "
1648 "respectively.\n",
1649 "Your branch and '%s' have diverged,\n"
1650 "and have %d and %d different commits each, "
1651 "respectively.\n",
1652 num_theirs),
1653 base, num_ours, num_theirs);
Junio C Hamano6d21bf92008-07-02 00:51:18 -07001654 return 1;
1655}
Jay Soffian454e2022009-02-25 03:32:11 -05001656
1657static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1658{
1659 struct ref ***local_tail = cb_data;
1660 struct ref *ref;
1661 int len;
1662
1663 /* we already know it starts with refs/ to get here */
Michael Haggerty8d9c5012011-09-15 23:10:25 +02001664 if (check_refname_format(refname + 5, 0))
Jay Soffian454e2022009-02-25 03:32:11 -05001665 return 0;
1666
1667 len = strlen(refname) + 1;
1668 ref = xcalloc(1, sizeof(*ref) + len);
1669 hashcpy(ref->new_sha1, sha1);
1670 memcpy(ref->name, refname, len);
1671 **local_tail = ref;
1672 *local_tail = &ref->next;
1673 return 0;
1674}
1675
1676struct ref *get_local_heads(void)
1677{
Nguyễn Thái Ngọc Duy55f05662009-04-17 08:16:23 +10001678 struct ref *local_refs = NULL, **local_tail = &local_refs;
Jay Soffian454e2022009-02-25 03:32:11 -05001679 for_each_ref(one_local_ref, &local_tail);
1680 return local_refs;
1681}
Jay Soffian8ef51732009-02-25 03:32:13 -05001682
Jay Soffian4229f1f2009-02-27 14:10:05 -05001683struct ref *guess_remote_head(const struct ref *head,
1684 const struct ref *refs,
1685 int all)
Jay Soffian8ef51732009-02-25 03:32:13 -05001686{
Jay Soffian8ef51732009-02-25 03:32:13 -05001687 const struct ref *r;
Jay Soffian4229f1f2009-02-27 14:10:05 -05001688 struct ref *list = NULL;
1689 struct ref **tail = &list;
Jay Soffian8ef51732009-02-25 03:32:13 -05001690
Jay Soffian6cb4e6c2009-02-25 03:32:14 -05001691 if (!head)
Jay Soffian8ef51732009-02-25 03:32:13 -05001692 return NULL;
1693
Jeff Kingfbb074c2009-02-27 14:10:06 -05001694 /*
1695 * Some transports support directly peeking at
1696 * where HEAD points; if that is the case, then
1697 * we don't have to guess.
1698 */
1699 if (head->symref)
1700 return copy_ref(find_ref_by_name(refs, head->symref));
1701
Jay Soffian8ef51732009-02-25 03:32:13 -05001702 /* If refs/heads/master could be right, it is. */
Jay Soffian4229f1f2009-02-27 14:10:05 -05001703 if (!all) {
1704 r = find_ref_by_name(refs, "refs/heads/master");
1705 if (r && !hashcmp(r->old_sha1, head->old_sha1))
1706 return copy_ref(r);
1707 }
Jay Soffian8ef51732009-02-25 03:32:13 -05001708
1709 /* Look for another ref that points there */
Jay Soffian4229f1f2009-02-27 14:10:05 -05001710 for (r = refs; r; r = r->next) {
Jeff King61adfd32011-06-03 01:11:13 -04001711 if (r != head &&
1712 !prefixcmp(r->name, "refs/heads/") &&
1713 !hashcmp(r->old_sha1, head->old_sha1)) {
Jay Soffian4229f1f2009-02-27 14:10:05 -05001714 *tail = copy_ref(r);
1715 tail = &((*tail)->next);
1716 if (!all)
1717 break;
1718 }
1719 }
Jay Soffian8ef51732009-02-25 03:32:13 -05001720
Jay Soffian4229f1f2009-02-27 14:10:05 -05001721 return list;
Jay Soffian8ef51732009-02-25 03:32:13 -05001722}
Jay Soffianf2ef6072009-11-10 00:03:31 -05001723
1724struct stale_heads_info {
Jay Soffianf2ef6072009-11-10 00:03:31 -05001725 struct string_list *ref_names;
1726 struct ref **stale_refs_tail;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001727 struct refspec *refs;
1728 int ref_count;
Jay Soffianf2ef6072009-11-10 00:03:31 -05001729};
1730
1731static int get_stale_heads_cb(const char *refname,
1732 const unsigned char *sha1, int flags, void *cb_data)
1733{
1734 struct stale_heads_info *info = cb_data;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001735 struct refspec query;
1736 memset(&query, 0, sizeof(struct refspec));
1737 query.dst = (char *)refname;
1738
1739 if (query_refspecs(info->refs, info->ref_count, &query))
1740 return 0; /* No matches */
1741
1742 /*
1743 * If we did find a suitable refspec and it's not a symref and
1744 * it's not in the list of refs that currently exist in that
1745 * remote we consider it to be stale.
1746 */
1747 if (!((flags & REF_ISSYMREF) ||
1748 string_list_has_string(info->ref_names, query.src))) {
1749 struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
1750 hashcpy(ref->new_sha1, sha1);
Jay Soffianf2ef6072009-11-10 00:03:31 -05001751 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001752
1753 free(query.src);
Jay Soffianf2ef6072009-11-10 00:03:31 -05001754 return 0;
1755}
1756
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001757struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map)
Jay Soffianf2ef6072009-11-10 00:03:31 -05001758{
1759 struct ref *ref, *stale_refs = NULL;
Thiago Farina183113a2010-07-04 16:46:19 -03001760 struct string_list ref_names = STRING_LIST_INIT_NODUP;
Jay Soffianf2ef6072009-11-10 00:03:31 -05001761 struct stale_heads_info info;
Jay Soffianf2ef6072009-11-10 00:03:31 -05001762 info.ref_names = &ref_names;
1763 info.stale_refs_tail = &stale_refs;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001764 info.refs = refs;
1765 info.ref_count = ref_count;
Jay Soffianf2ef6072009-11-10 00:03:31 -05001766 for (ref = fetch_map; ref; ref = ref->next)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001767 string_list_append(&ref_names, ref->name);
Jay Soffianf2ef6072009-11-10 00:03:31 -05001768 sort_string_list(&ref_names);
1769 for_each_ref(get_stale_heads_cb, &info);
1770 string_list_clear(&ref_names, 0);
1771 return stale_refs;
1772}