GIT 1.5.3.8
[git/mingw/4msysgit/kblees.git] / remote.c
blob9a88917aab32f32235d0ebaa1ffd0e2f2773a927
1 #include "cache.h"
2 #include "remote.h"
3 #include "refs.h"
5 static struct remote **remotes;
6 static int allocated_remotes;
8 #define BUF_SIZE (2048)
9 static char buffer[BUF_SIZE];
11 static void add_push_refspec(struct remote *remote, const char *ref)
13 int nr = remote->push_refspec_nr + 1;
14 remote->push_refspec =
15 xrealloc(remote->push_refspec, nr * sizeof(char *));
16 remote->push_refspec[nr-1] = ref;
17 remote->push_refspec_nr = nr;
20 static void add_fetch_refspec(struct remote *remote, const char *ref)
22 int nr = remote->fetch_refspec_nr + 1;
23 remote->fetch_refspec =
24 xrealloc(remote->fetch_refspec, nr * sizeof(char *));
25 remote->fetch_refspec[nr-1] = ref;
26 remote->fetch_refspec_nr = nr;
29 static void add_uri(struct remote *remote, const char *uri)
31 int nr = remote->uri_nr + 1;
32 remote->uri =
33 xrealloc(remote->uri, nr * sizeof(char *));
34 remote->uri[nr-1] = uri;
35 remote->uri_nr = nr;
38 static struct remote *make_remote(const char *name, int len)
40 int i, empty = -1;
42 for (i = 0; i < allocated_remotes; i++) {
43 if (!remotes[i]) {
44 if (empty < 0)
45 empty = i;
46 } else {
47 if (len ? (!strncmp(name, remotes[i]->name, len) &&
48 !remotes[i]->name[len]) :
49 !strcmp(name, remotes[i]->name))
50 return remotes[i];
54 if (empty < 0) {
55 empty = allocated_remotes;
56 allocated_remotes += allocated_remotes ? allocated_remotes : 1;
57 remotes = xrealloc(remotes,
58 sizeof(*remotes) * allocated_remotes);
59 memset(remotes + empty, 0,
60 (allocated_remotes - empty) * sizeof(*remotes));
62 remotes[empty] = xcalloc(1, sizeof(struct remote));
63 if (len)
64 remotes[empty]->name = xstrndup(name, len);
65 else
66 remotes[empty]->name = xstrdup(name);
67 return remotes[empty];
70 static void read_remotes_file(struct remote *remote)
72 FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
74 if (!f)
75 return;
76 while (fgets(buffer, BUF_SIZE, f)) {
77 int value_list;
78 char *s, *p;
80 if (!prefixcmp(buffer, "URL:")) {
81 value_list = 0;
82 s = buffer + 4;
83 } else if (!prefixcmp(buffer, "Push:")) {
84 value_list = 1;
85 s = buffer + 5;
86 } else if (!prefixcmp(buffer, "Pull:")) {
87 value_list = 2;
88 s = buffer + 5;
89 } else
90 continue;
92 while (isspace(*s))
93 s++;
94 if (!*s)
95 continue;
97 p = s + strlen(s);
98 while (isspace(p[-1]))
99 *--p = 0;
101 switch (value_list) {
102 case 0:
103 add_uri(remote, xstrdup(s));
104 break;
105 case 1:
106 add_push_refspec(remote, xstrdup(s));
107 break;
108 case 2:
109 add_fetch_refspec(remote, xstrdup(s));
110 break;
113 fclose(f);
116 static void read_branches_file(struct remote *remote)
118 const char *slash = strchr(remote->name, '/');
119 int n = slash ? slash - remote->name : 1000;
120 FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
121 char *s, *p;
122 int len;
124 if (!f)
125 return;
126 s = fgets(buffer, BUF_SIZE, f);
127 fclose(f);
128 if (!s)
129 return;
130 while (isspace(*s))
131 s++;
132 if (!*s)
133 return;
134 p = s + strlen(s);
135 while (isspace(p[-1]))
136 *--p = 0;
137 len = p - s;
138 if (slash)
139 len += strlen(slash);
140 p = xmalloc(len + 1);
141 strcpy(p, s);
142 if (slash)
143 strcat(p, slash);
144 add_uri(remote, p);
147 static char *default_remote_name = NULL;
148 static const char *current_branch = NULL;
149 static int current_branch_len = 0;
151 static int handle_config(const char *key, const char *value)
153 const char *name;
154 const char *subkey;
155 struct remote *remote;
156 if (!prefixcmp(key, "branch.") && current_branch &&
157 !strncmp(key + 7, current_branch, current_branch_len) &&
158 !strcmp(key + 7 + current_branch_len, ".remote")) {
159 free(default_remote_name);
160 default_remote_name = xstrdup(value);
162 if (prefixcmp(key, "remote."))
163 return 0;
164 name = key + 7;
165 subkey = strrchr(name, '.');
166 if (!subkey)
167 return error("Config with no key for remote %s", name);
168 if (*subkey == '/') {
169 warning("Config remote shorthand cannot begin with '/': %s", name);
170 return 0;
172 remote = make_remote(name, subkey - name);
173 if (!value) {
174 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
175 * [remote "frotz"]
176 * disabled
177 * is a valid way to set it to true; we get NULL in value so
178 * we need to handle it here.
180 * if (!strcmp(subkey, ".disabled")) {
181 * val = git_config_bool(key, value);
182 * return 0;
183 * } else
186 return 0; /* ignore unknown booleans */
188 if (!strcmp(subkey, ".url")) {
189 add_uri(remote, xstrdup(value));
190 } else if (!strcmp(subkey, ".push")) {
191 add_push_refspec(remote, xstrdup(value));
192 } else if (!strcmp(subkey, ".fetch")) {
193 add_fetch_refspec(remote, xstrdup(value));
194 } else if (!strcmp(subkey, ".receivepack")) {
195 if (!remote->receivepack)
196 remote->receivepack = xstrdup(value);
197 else
198 error("more than one receivepack given, using the first");
200 return 0;
203 static void read_config(void)
205 unsigned char sha1[20];
206 const char *head_ref;
207 int flag;
208 if (default_remote_name) // did this already
209 return;
210 default_remote_name = xstrdup("origin");
211 current_branch = NULL;
212 head_ref = resolve_ref("HEAD", sha1, 0, &flag);
213 if (head_ref && (flag & REF_ISSYMREF) &&
214 !prefixcmp(head_ref, "refs/heads/")) {
215 current_branch = head_ref + strlen("refs/heads/");
216 current_branch_len = strlen(current_branch);
218 git_config(handle_config);
221 static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
223 int i;
224 struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
225 for (i = 0; i < nr_refspec; i++) {
226 const char *sp, *ep, *gp;
227 sp = refspec[i];
228 if (*sp == '+') {
229 rs[i].force = 1;
230 sp++;
232 gp = strchr(sp, '*');
233 ep = strchr(sp, ':');
234 if (gp && ep && gp > ep)
235 gp = NULL;
236 if (ep) {
237 if (ep[1]) {
238 const char *glob = strchr(ep + 1, '*');
239 if (!glob)
240 gp = NULL;
241 if (gp)
242 rs[i].dst = xstrndup(ep + 1,
243 glob - ep - 1);
244 else
245 rs[i].dst = xstrdup(ep + 1);
247 } else {
248 ep = sp + strlen(sp);
250 if (gp) {
251 rs[i].pattern = 1;
252 ep = gp;
254 rs[i].src = xstrndup(sp, ep - sp);
256 return rs;
259 struct remote *remote_get(const char *name)
261 struct remote *ret;
263 read_config();
264 if (!name)
265 name = default_remote_name;
266 ret = make_remote(name, 0);
267 if (name[0] != '/') {
268 if (!ret->uri)
269 read_remotes_file(ret);
270 if (!ret->uri)
271 read_branches_file(ret);
273 if (!ret->uri)
274 add_uri(ret, name);
275 if (!ret->uri)
276 return NULL;
277 ret->fetch = parse_ref_spec(ret->fetch_refspec_nr, ret->fetch_refspec);
278 ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
279 return ret;
282 int for_each_remote(each_remote_fn fn, void *priv)
284 int i, result = 0;
285 read_config();
286 for (i = 0; i < allocated_remotes && !result; i++) {
287 struct remote *r = remotes[i];
288 if (!r)
289 continue;
290 if (!r->fetch)
291 r->fetch = parse_ref_spec(r->fetch_refspec_nr,
292 r->fetch_refspec);
293 if (!r->push)
294 r->push = parse_ref_spec(r->push_refspec_nr,
295 r->push_refspec);
296 result = fn(r, priv);
298 return result;
301 int remote_has_uri(struct remote *remote, const char *uri)
303 int i;
304 for (i = 0; i < remote->uri_nr; i++) {
305 if (!strcmp(remote->uri[i], uri))
306 return 1;
308 return 0;
311 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
313 int find_src = refspec->src == NULL;
314 char *needle, **result;
315 int i;
317 if (find_src) {
318 if (refspec->dst == NULL)
319 return error("find_tracking: need either src or dst");
320 needle = refspec->dst;
321 result = &refspec->src;
322 } else {
323 needle = refspec->src;
324 result = &refspec->dst;
327 for (i = 0; i < remote->fetch_refspec_nr; i++) {
328 struct refspec *fetch = &remote->fetch[i];
329 const char *key = find_src ? fetch->dst : fetch->src;
330 const char *value = find_src ? fetch->src : fetch->dst;
331 if (!fetch->dst)
332 continue;
333 if (fetch->pattern) {
334 if (!prefixcmp(needle, key)) {
335 *result = xmalloc(strlen(value) +
336 strlen(needle) -
337 strlen(key) + 1);
338 strcpy(*result, value);
339 strcpy(*result + strlen(value),
340 needle + strlen(key));
341 refspec->force = fetch->force;
342 return 0;
344 } else if (!strcmp(needle, key)) {
345 *result = xstrdup(value);
346 refspec->force = fetch->force;
347 return 0;
350 return -1;
353 struct ref *alloc_ref(unsigned namelen)
355 struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
356 memset(ret, 0, sizeof(struct ref) + namelen);
357 return ret;
360 void free_refs(struct ref *ref)
362 struct ref *next;
363 while (ref) {
364 next = ref->next;
365 if (ref->peer_ref)
366 free(ref->peer_ref);
367 free(ref);
368 ref = next;
372 static int count_refspec_match(const char *pattern,
373 struct ref *refs,
374 struct ref **matched_ref)
376 int patlen = strlen(pattern);
377 struct ref *matched_weak = NULL;
378 struct ref *matched = NULL;
379 int weak_match = 0;
380 int match = 0;
382 for (weak_match = match = 0; refs; refs = refs->next) {
383 char *name = refs->name;
384 int namelen = strlen(name);
386 if (namelen < patlen ||
387 memcmp(name + namelen - patlen, pattern, patlen))
388 continue;
389 if (namelen != patlen && name[namelen - patlen - 1] != '/')
390 continue;
392 /* A match is "weak" if it is with refs outside
393 * heads or tags, and did not specify the pattern
394 * in full (e.g. "refs/remotes/origin/master") or at
395 * least from the toplevel (e.g. "remotes/origin/master");
396 * otherwise "git push $URL master" would result in
397 * ambiguity between remotes/origin/master and heads/master
398 * at the remote site.
400 if (namelen != patlen &&
401 patlen != namelen - 5 &&
402 prefixcmp(name, "refs/heads/") &&
403 prefixcmp(name, "refs/tags/")) {
404 /* We want to catch the case where only weak
405 * matches are found and there are multiple
406 * matches, and where more than one strong
407 * matches are found, as ambiguous. One
408 * strong match with zero or more weak matches
409 * are acceptable as a unique match.
411 matched_weak = refs;
412 weak_match++;
414 else {
415 matched = refs;
416 match++;
419 if (!matched) {
420 *matched_ref = matched_weak;
421 return weak_match;
423 else {
424 *matched_ref = matched;
425 return match;
429 static void tail_link_ref(struct ref *ref, struct ref ***tail)
431 **tail = ref;
432 while (ref->next)
433 ref = ref->next;
434 *tail = &ref->next;
437 static struct ref *try_explicit_object_name(const char *name)
439 unsigned char sha1[20];
440 struct ref *ref;
441 int len;
443 if (!*name) {
444 ref = alloc_ref(20);
445 strcpy(ref->name, "(delete)");
446 hashclr(ref->new_sha1);
447 return ref;
449 if (get_sha1(name, sha1))
450 return NULL;
451 len = strlen(name) + 1;
452 ref = alloc_ref(len);
453 memcpy(ref->name, name, len);
454 hashcpy(ref->new_sha1, sha1);
455 return ref;
458 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
460 struct ref *ret;
461 size_t len;
463 len = strlen(name) + 1;
464 ret = alloc_ref(len);
465 memcpy(ret->name, name, len);
466 tail_link_ref(ret, tail);
467 return ret;
470 static int match_explicit(struct ref *src, struct ref *dst,
471 struct ref ***dst_tail,
472 struct refspec *rs,
473 int errs)
475 struct ref *matched_src, *matched_dst;
477 const char *dst_value = rs->dst;
479 if (rs->pattern)
480 return errs;
482 matched_src = matched_dst = NULL;
483 switch (count_refspec_match(rs->src, src, &matched_src)) {
484 case 1:
485 break;
486 case 0:
487 /* The source could be in the get_sha1() format
488 * not a reference name. :refs/other is a
489 * way to delete 'other' ref at the remote end.
491 matched_src = try_explicit_object_name(rs->src);
492 if (matched_src)
493 break;
494 error("src refspec %s does not match any.",
495 rs->src);
496 break;
497 default:
498 matched_src = NULL;
499 error("src refspec %s matches more than one.",
500 rs->src);
501 break;
504 if (!matched_src)
505 errs = 1;
507 if (!dst_value) {
508 if (!matched_src)
509 return errs;
510 dst_value = matched_src->name;
513 switch (count_refspec_match(dst_value, dst, &matched_dst)) {
514 case 1:
515 break;
516 case 0:
517 if (!memcmp(dst_value, "refs/", 5))
518 matched_dst = make_linked_ref(dst_value, dst_tail);
519 else
520 error("dst refspec %s does not match any "
521 "existing ref on the remote and does "
522 "not start with refs/.", dst_value);
523 break;
524 default:
525 matched_dst = NULL;
526 error("dst refspec %s matches more than one.",
527 dst_value);
528 break;
530 if (errs || matched_dst == NULL)
531 return 1;
532 if (matched_dst->peer_ref) {
533 errs = 1;
534 error("dst ref %s receives from more than one src.",
535 matched_dst->name);
537 else {
538 matched_dst->peer_ref = matched_src;
539 matched_dst->force = rs->force;
541 return errs;
544 static int match_explicit_refs(struct ref *src, struct ref *dst,
545 struct ref ***dst_tail, struct refspec *rs,
546 int rs_nr)
548 int i, errs;
549 for (i = errs = 0; i < rs_nr; i++)
550 errs |= match_explicit(src, dst, dst_tail, &rs[i], errs);
551 return -errs;
554 static struct ref *find_ref_by_name(struct ref *list, const char *name)
556 for ( ; list; list = list->next)
557 if (!strcmp(list->name, name))
558 return list;
559 return NULL;
562 static const struct refspec *check_pattern_match(const struct refspec *rs,
563 int rs_nr,
564 const struct ref *src)
566 int i;
567 for (i = 0; i < rs_nr; i++) {
568 if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
569 return rs + i;
571 return NULL;
575 * Note. This is used only by "push"; refspec matching rules for
576 * push and fetch are subtly different, so do not try to reuse it
577 * without thinking.
579 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
580 int nr_refspec, char **refspec, int all)
582 struct refspec *rs =
583 parse_ref_spec(nr_refspec, (const char **) refspec);
585 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
586 return -1;
588 /* pick the remainder */
589 for ( ; src; src = src->next) {
590 struct ref *dst_peer;
591 const struct refspec *pat = NULL;
592 char *dst_name;
593 if (src->peer_ref)
594 continue;
595 if (nr_refspec) {
596 pat = check_pattern_match(rs, nr_refspec, src);
597 if (!pat)
598 continue;
600 else if (prefixcmp(src->name, "refs/heads/"))
602 * "matching refs"; traditionally we pushed everything
603 * including refs outside refs/heads/ hierarchy, but
604 * that does not make much sense these days.
606 continue;
608 if (pat) {
609 const char *dst_side = pat->dst ? pat->dst : pat->src;
610 dst_name = xmalloc(strlen(dst_side) +
611 strlen(src->name) -
612 strlen(pat->src) + 2);
613 strcpy(dst_name, dst_side);
614 strcat(dst_name, src->name + strlen(pat->src));
615 } else
616 dst_name = xstrdup(src->name);
617 dst_peer = find_ref_by_name(dst, dst_name);
618 if (dst_peer && dst_peer->peer_ref)
619 /* We're already sending something to this ref. */
620 goto free_name;
621 if (!dst_peer && !nr_refspec && !all)
622 /* Remote doesn't have it, and we have no
623 * explicit pattern, and we don't have
624 * --all. */
625 goto free_name;
626 if (!dst_peer) {
627 /* Create a new one and link it */
628 dst_peer = make_linked_ref(dst_name, dst_tail);
629 hashcpy(dst_peer->new_sha1, src->new_sha1);
631 dst_peer->peer_ref = src;
632 if (pat)
633 dst_peer->force = pat->force;
634 free_name:
635 free(dst_name);
637 return 0;