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_uri(struct remote
*remote
, const char *uri
)
22 int nr
= remote
->uri_nr
+ 1;
24 xrealloc(remote
->uri
, nr
* sizeof(char *));
25 remote
->uri
[nr
-1] = uri
;
29 static struct remote
*make_remote(const char *name
, int len
)
33 for (i
= 0; i
< allocated_remotes
; i
++) {
38 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
39 !remotes
[i
]->name
[len
]) :
40 !strcmp(name
, remotes
[i
]->name
))
46 empty
= allocated_remotes
;
47 allocated_remotes
+= allocated_remotes
? allocated_remotes
: 1;
48 remotes
= xrealloc(remotes
,
49 sizeof(*remotes
) * allocated_remotes
);
50 memset(remotes
+ empty
, 0,
51 (allocated_remotes
- empty
) * sizeof(*remotes
));
53 remotes
[empty
] = xcalloc(1, sizeof(struct remote
));
55 remotes
[empty
]->name
= xstrndup(name
, len
);
57 remotes
[empty
]->name
= xstrdup(name
);
58 return remotes
[empty
];
61 static void read_remotes_file(struct remote
*remote
)
63 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
67 while (fgets(buffer
, BUF_SIZE
, f
)) {
71 if (!prefixcmp(buffer
, "URL:")) {
74 } else if (!prefixcmp(buffer
, "Push:")) {
86 while (isspace(p
[-1]))
91 add_uri(remote
, xstrdup(s
));
94 add_push_refspec(remote
, xstrdup(s
));
101 static void read_branches_file(struct remote
*remote
)
103 const char *slash
= strchr(remote
->name
, '/');
104 int n
= slash
? slash
- remote
->name
: 1000;
105 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
111 s
= fgets(buffer
, BUF_SIZE
, f
);
120 while (isspace(p
[-1]))
124 len
+= strlen(slash
);
125 p
= xmalloc(len
+ 1);
132 static char *default_remote_name
= NULL
;
133 static const char *current_branch
= NULL
;
134 static int current_branch_len
= 0;
136 static int handle_config(const char *key
, const char *value
)
140 struct remote
*remote
;
141 if (!prefixcmp(key
, "branch.") && current_branch
&&
142 !strncmp(key
+ 7, current_branch
, current_branch_len
) &&
143 !strcmp(key
+ 7 + current_branch_len
, ".remote")) {
144 free(default_remote_name
);
145 default_remote_name
= xstrdup(value
);
147 if (prefixcmp(key
, "remote."))
150 subkey
= strrchr(name
, '.');
152 return error("Config with no key for remote %s", name
);
153 if (*subkey
== '/') {
154 warning("Config remote shorthand cannot begin with '/': %s", name
);
157 remote
= make_remote(name
, subkey
- name
);
159 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
162 * is a valid way to set it to true; we get NULL in value so
163 * we need to handle it here.
165 * if (!strcmp(subkey, ".disabled")) {
166 * val = git_config_bool(key, value);
171 return 0; /* ignore unknown booleans */
173 if (!strcmp(subkey
, ".url")) {
174 add_uri(remote
, xstrdup(value
));
175 } else if (!strcmp(subkey
, ".push")) {
176 add_push_refspec(remote
, xstrdup(value
));
177 } else if (!strcmp(subkey
, ".receivepack")) {
178 if (!remote
->receivepack
)
179 remote
->receivepack
= xstrdup(value
);
181 error("more than one receivepack given, using the first");
186 static void read_config(void)
188 unsigned char sha1
[20];
189 const char *head_ref
;
191 if (default_remote_name
) // did this already
193 default_remote_name
= xstrdup("origin");
194 current_branch
= NULL
;
195 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
196 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
197 !prefixcmp(head_ref
, "refs/heads/")) {
198 current_branch
= head_ref
+ strlen("refs/heads/");
199 current_branch_len
= strlen(current_branch
);
201 git_config(handle_config
);
204 static struct refspec
*parse_ref_spec(int nr_refspec
, const char **refspec
)
207 struct refspec
*rs
= xcalloc(sizeof(*rs
), nr_refspec
);
208 for (i
= 0; i
< nr_refspec
; i
++) {
209 const char *sp
, *ep
, *gp
;
215 gp
= strchr(sp
, '*');
216 ep
= strchr(sp
, ':');
217 if (gp
&& ep
&& gp
> ep
)
221 const char *glob
= strchr(ep
+ 1, '*');
225 rs
[i
].dst
= xstrndup(ep
+ 1,
228 rs
[i
].dst
= xstrdup(ep
+ 1);
231 ep
= sp
+ strlen(sp
);
237 rs
[i
].src
= xstrndup(sp
, ep
- sp
);
242 struct remote
*remote_get(const char *name
)
248 name
= default_remote_name
;
249 ret
= make_remote(name
, 0);
250 if (name
[0] != '/') {
252 read_remotes_file(ret
);
254 read_branches_file(ret
);
260 ret
->push
= parse_ref_spec(ret
->push_refspec_nr
, ret
->push_refspec
);
264 static int count_refspec_match(const char *pattern
,
266 struct ref
**matched_ref
)
268 int patlen
= strlen(pattern
);
269 struct ref
*matched_weak
= NULL
;
270 struct ref
*matched
= NULL
;
274 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
275 char *name
= refs
->name
;
276 int namelen
= strlen(name
);
279 if (namelen
< patlen
||
280 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
282 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
285 /* A match is "weak" if it is with refs outside
286 * heads or tags, and did not specify the pattern
287 * in full (e.g. "refs/remotes/origin/master") or at
288 * least from the toplevel (e.g. "remotes/origin/master");
289 * otherwise "git push $URL master" would result in
290 * ambiguity between remotes/origin/master and heads/master
291 * at the remote site.
293 if (namelen
!= patlen
&&
294 patlen
!= namelen
- 5 &&
295 prefixcmp(name
, "refs/heads/") &&
296 prefixcmp(name
, "refs/tags/")) {
297 /* We want to catch the case where only weak
298 * matches are found and there are multiple
299 * matches, and where more than one strong
300 * matches are found, as ambiguous. One
301 * strong match with zero or more weak matches
302 * are acceptable as a unique match.
313 *matched_ref
= matched_weak
;
317 *matched_ref
= matched
;
322 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
329 static struct ref
*try_explicit_object_name(const char *name
)
331 unsigned char sha1
[20];
336 ref
= xcalloc(1, sizeof(*ref
) + 20);
337 strcpy(ref
->name
, "(delete)");
338 hashclr(ref
->new_sha1
);
341 if (get_sha1(name
, sha1
))
343 len
= strlen(name
) + 1;
344 ref
= xcalloc(1, sizeof(*ref
) + len
);
345 memcpy(ref
->name
, name
, len
);
346 hashcpy(ref
->new_sha1
, sha1
);
350 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
351 struct ref
***dst_tail
, struct refspec
*rs
,
355 for (i
= errs
= 0; i
< rs_nr
; i
++) {
356 struct ref
*matched_src
, *matched_dst
;
358 const char *dst_value
= rs
[i
].dst
;
359 if (dst_value
== NULL
)
360 dst_value
= rs
[i
].src
;
362 matched_src
= matched_dst
= NULL
;
363 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
367 /* The source could be in the get_sha1() format
368 * not a reference name. :refs/other is a
369 * way to delete 'other' ref at the remote end.
371 matched_src
= try_explicit_object_name(rs
[i
].src
);
375 error("src refspec %s does not match any.",
380 error("src refspec %s matches more than one.",
384 switch (count_refspec_match(dst_value
, dst
, &matched_dst
)) {
388 if (!memcmp(dst_value
, "refs/", 5)) {
389 int len
= strlen(dst_value
) + 1;
390 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
391 memcpy(matched_dst
->name
, dst_value
, len
);
392 link_dst_tail(matched_dst
, dst_tail
);
394 else if (!strcmp(rs
[i
].src
, dst_value
) &&
396 /* pushing "master:master" when
397 * remote does not have master yet.
399 int len
= strlen(matched_src
->name
) + 1;
400 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
401 memcpy(matched_dst
->name
, matched_src
->name
,
403 link_dst_tail(matched_dst
, dst_tail
);
407 error("dst refspec %s does not match any "
408 "existing ref on the remote and does "
409 "not start with refs/.", dst_value
);
414 error("dst refspec %s matches more than one.",
420 if (matched_dst
->peer_ref
) {
422 error("dst ref %s receives from more than one src.",
426 matched_dst
->peer_ref
= matched_src
;
427 matched_dst
->force
= rs
[i
].force
;
433 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
435 for ( ; list
; list
= list
->next
)
436 if (!strcmp(list
->name
, name
))
441 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
442 int nr_refspec
, char **refspec
, int all
)
445 parse_ref_spec(nr_refspec
, (const char **) refspec
);
448 return match_explicit_refs(src
, dst
, dst_tail
, rs
, nr_refspec
);
450 /* pick the remainder */
451 for ( ; src
; src
= src
->next
) {
452 struct ref
*dst_peer
;
455 dst_peer
= find_ref_by_name(dst
, src
->name
);
456 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
459 /* Create a new one and link it */
460 int len
= strlen(src
->name
) + 1;
461 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
462 memcpy(dst_peer
->name
, src
->name
, len
);
463 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
464 link_dst_tail(dst_peer
, dst_tail
);
466 dst_peer
->peer_ref
= src
;