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;
33 xrealloc(remote
->uri
, nr
* sizeof(char *));
34 remote
->uri
[nr
-1] = uri
;
38 static struct remote
*make_remote(const char *name
, int len
)
42 for (i
= 0; i
< allocated_remotes
; i
++) {
47 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
48 !remotes
[i
]->name
[len
]) :
49 !strcmp(name
, remotes
[i
]->name
))
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
));
64 remotes
[empty
]->name
= xstrndup(name
, len
);
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");
76 while (fgets(buffer
, BUF_SIZE
, f
)) {
80 if (!prefixcmp(buffer
, "URL:")) {
83 } else if (!prefixcmp(buffer
, "Push:")) {
86 } else if (!prefixcmp(buffer
, "Pull:")) {
98 while (isspace(p
[-1]))
101 switch (value_list
) {
103 add_uri(remote
, xstrdup(s
));
106 add_push_refspec(remote
, xstrdup(s
));
109 add_fetch_refspec(remote
, xstrdup(s
));
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");
126 s
= fgets(buffer
, BUF_SIZE
, f
);
135 while (isspace(p
[-1]))
139 len
+= strlen(slash
);
140 p
= xmalloc(len
+ 1);
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
)
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."))
165 subkey
= strrchr(name
, '.');
167 return error("Config with no key for remote %s", name
);
168 if (*subkey
== '/') {
169 warning("Config remote shorthand cannot begin with '/': %s", name
);
172 remote
= make_remote(name
, subkey
- name
);
174 /* if we ever have a boolean variable, e.g. "remote.*.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);
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
);
198 error("more than one receivepack given, using the first");
199 } else if (!strcmp(subkey
, ".uploadpack")) {
200 if (!remote
->uploadpack
)
201 remote
->uploadpack
= xstrdup(value
);
203 error("more than one uploadpack given, using the first");
208 static void read_config(void)
210 unsigned char sha1
[20];
211 const char *head_ref
;
213 if (default_remote_name
) // did this already
215 default_remote_name
= xstrdup("origin");
216 current_branch
= NULL
;
217 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
218 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
219 !prefixcmp(head_ref
, "refs/heads/")) {
220 current_branch
= head_ref
+ strlen("refs/heads/");
221 current_branch_len
= strlen(current_branch
);
223 git_config(handle_config
);
226 static struct refspec
*parse_ref_spec(int nr_refspec
, const char **refspec
)
229 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
230 for (i
= 0; i
< nr_refspec
; i
++) {
231 const char *sp
, *ep
, *gp
;
237 gp
= strchr(sp
, '*');
238 ep
= strchr(sp
, ':');
239 if (gp
&& ep
&& gp
> ep
)
243 const char *glob
= strchr(ep
+ 1, '*');
247 rs
[i
].dst
= xstrndup(ep
+ 1,
250 rs
[i
].dst
= xstrdup(ep
+ 1);
253 ep
= sp
+ strlen(sp
);
259 rs
[i
].src
= xstrndup(sp
, ep
- sp
);
264 struct remote
*remote_get(const char *name
)
270 name
= default_remote_name
;
271 ret
= make_remote(name
, 0);
272 if (name
[0] != '/') {
274 read_remotes_file(ret
);
276 read_branches_file(ret
);
282 ret
->fetch
= parse_ref_spec(ret
->fetch_refspec_nr
, ret
->fetch_refspec
);
283 ret
->push
= parse_ref_spec(ret
->push_refspec_nr
, ret
->push_refspec
);
287 int for_each_remote(each_remote_fn fn
, void *priv
)
291 for (i
= 0; i
< allocated_remotes
&& !result
; i
++) {
292 struct remote
*r
= remotes
[i
];
296 r
->fetch
= parse_ref_spec(r
->fetch_refspec_nr
,
299 r
->push
= parse_ref_spec(r
->push_refspec_nr
,
301 result
= fn(r
, priv
);
306 int remote_has_uri(struct remote
*remote
, const char *uri
)
309 for (i
= 0; i
< remote
->uri_nr
; i
++) {
310 if (!strcmp(remote
->uri
[i
], uri
))
316 int remote_find_tracking(struct remote
*remote
, struct refspec
*refspec
)
318 int find_src
= refspec
->src
== NULL
;
319 char *needle
, **result
;
323 if (refspec
->dst
== NULL
)
324 return error("find_tracking: need either src or dst");
325 needle
= refspec
->dst
;
326 result
= &refspec
->src
;
328 needle
= refspec
->src
;
329 result
= &refspec
->dst
;
332 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
333 struct refspec
*fetch
= &remote
->fetch
[i
];
334 const char *key
= find_src
? fetch
->dst
: fetch
->src
;
335 const char *value
= find_src
? fetch
->src
: fetch
->dst
;
338 if (fetch
->pattern
) {
339 if (!prefixcmp(needle
, key
)) {
340 *result
= xmalloc(strlen(value
) +
343 strcpy(*result
, value
);
344 strcpy(*result
+ strlen(value
),
345 needle
+ strlen(key
));
346 refspec
->force
= fetch
->force
;
349 } else if (!strcmp(needle
, key
)) {
350 *result
= xstrdup(value
);
351 refspec
->force
= fetch
->force
;
358 struct ref
*alloc_ref(unsigned namelen
)
360 struct ref
*ret
= xmalloc(sizeof(struct ref
) + namelen
);
361 memset(ret
, 0, sizeof(struct ref
) + namelen
);
365 void free_refs(struct ref
*ref
)
377 static int count_refspec_match(const char *pattern
,
379 struct ref
**matched_ref
)
381 int patlen
= strlen(pattern
);
382 struct ref
*matched_weak
= NULL
;
383 struct ref
*matched
= NULL
;
387 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
388 char *name
= refs
->name
;
389 int namelen
= strlen(name
);
391 if (namelen
< patlen
||
392 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
394 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
397 /* A match is "weak" if it is with refs outside
398 * heads or tags, and did not specify the pattern
399 * in full (e.g. "refs/remotes/origin/master") or at
400 * least from the toplevel (e.g. "remotes/origin/master");
401 * otherwise "git push $URL master" would result in
402 * ambiguity between remotes/origin/master and heads/master
403 * at the remote site.
405 if (namelen
!= patlen
&&
406 patlen
!= namelen
- 5 &&
407 prefixcmp(name
, "refs/heads/") &&
408 prefixcmp(name
, "refs/tags/")) {
409 /* We want to catch the case where only weak
410 * matches are found and there are multiple
411 * matches, and where more than one strong
412 * matches are found, as ambiguous. One
413 * strong match with zero or more weak matches
414 * are acceptable as a unique match.
425 *matched_ref
= matched_weak
;
429 *matched_ref
= matched
;
434 static void tail_link_ref(struct ref
*ref
, struct ref
***tail
)
442 static struct ref
*try_explicit_object_name(const char *name
)
444 unsigned char sha1
[20];
450 strcpy(ref
->name
, "(delete)");
451 hashclr(ref
->new_sha1
);
454 if (get_sha1(name
, sha1
))
456 len
= strlen(name
) + 1;
457 ref
= alloc_ref(len
);
458 memcpy(ref
->name
, name
, len
);
459 hashcpy(ref
->new_sha1
, sha1
);
463 static struct ref
*make_linked_ref(const char *name
, struct ref
***tail
)
468 len
= strlen(name
) + 1;
469 ret
= alloc_ref(len
);
470 memcpy(ret
->name
, name
, len
);
471 tail_link_ref(ret
, tail
);
475 static int match_explicit(struct ref
*src
, struct ref
*dst
,
476 struct ref
***dst_tail
,
480 struct ref
*matched_src
, *matched_dst
;
482 const char *dst_value
= rs
->dst
;
487 matched_src
= matched_dst
= NULL
;
488 switch (count_refspec_match(rs
->src
, src
, &matched_src
)) {
492 /* The source could be in the get_sha1() format
493 * not a reference name. :refs/other is a
494 * way to delete 'other' ref at the remote end.
496 matched_src
= try_explicit_object_name(rs
->src
);
499 error("src refspec %s does not match any.",
504 error("src refspec %s matches more than one.",
512 if (dst_value
== NULL
)
513 dst_value
= matched_src
->name
;
515 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
519 if (!memcmp(dst_value
, "refs/", 5))
520 matched_dst
= make_linked_ref(dst_value
, dst_tail
);
522 error("dst refspec %s does not match any "
523 "existing ref on the remote and does "
524 "not start with refs/.", dst_value
);
528 error("dst refspec %s matches more than one.",
532 if (errs
|| matched_dst
== NULL
)
534 if (matched_dst
->peer_ref
) {
536 error("dst ref %s receives from more than one src.",
540 matched_dst
->peer_ref
= matched_src
;
541 matched_dst
->force
= rs
->force
;
546 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
547 struct ref
***dst_tail
, struct refspec
*rs
,
551 for (i
= errs
= 0; i
< rs_nr
; i
++)
552 errs
|= match_explicit(src
, dst
, dst_tail
, &rs
[i
], errs
);
556 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
558 for ( ; list
; list
= list
->next
)
559 if (!strcmp(list
->name
, name
))
564 static const struct refspec
*check_pattern_match(const struct refspec
*rs
,
566 const struct ref
*src
)
569 for (i
= 0; i
< rs_nr
; i
++) {
570 if (rs
[i
].pattern
&& !prefixcmp(src
->name
, rs
[i
].src
))
577 * Note. This is used only by "push"; refspec matching rules for
578 * push and fetch are subtly different, so do not try to reuse it
581 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
582 int nr_refspec
, char **refspec
, int all
)
585 parse_ref_spec(nr_refspec
, (const char **) refspec
);
587 if (match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
))
590 /* pick the remainder */
591 for ( ; src
; src
= src
->next
) {
592 struct ref
*dst_peer
;
593 const struct refspec
*pat
= NULL
;
598 pat
= check_pattern_match(rs
, nr_refspec
, src
);
602 else if (prefixcmp(src
->name
, "refs/heads/"))
604 * "matching refs"; traditionally we pushed everything
605 * including refs outside refs/heads/ hierarchy, but
606 * that does not make much sense these days.
611 const char *dst_side
= pat
->dst
? pat
->dst
: pat
->src
;
612 dst_name
= xmalloc(strlen(dst_side
) +
614 strlen(pat
->src
) + 2);
615 strcpy(dst_name
, dst_side
);
616 strcat(dst_name
, src
->name
+ strlen(pat
->src
));
618 dst_name
= xstrdup(src
->name
);
619 dst_peer
= find_ref_by_name(dst
, dst_name
);
620 if (dst_peer
&& dst_peer
->peer_ref
)
621 /* We're already sending something to this ref. */
623 if (!dst_peer
&& !nr_refspec
&& !all
)
624 /* Remote doesn't have it, and we have no
625 * explicit pattern, and we don't have
629 /* Create a new one and link it */
630 dst_peer
= make_linked_ref(dst_name
, dst_tail
);
631 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
633 dst_peer
->peer_ref
= src
;