@matheusbsbs / matheus-bagliano / commits / 6b62816cb1
Fork 0

Move refspec parser from connect.c and cache.h to remote.{c,h}

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Daniel Barkalow committed May 12, 2007 at 11:45 UTC 6b62816cb12e621c5952f088542bec6dfc7ec5d6
6 files changed +261 -241
cache.h
-2
@@ -467,8 +467,6 @@ struct ref {
467 extern pid_t git_connect(int fd[2], char *url, const char *prog, int flags);
468 extern int finish_connect(pid_t pid);
469 extern int path_match(const char *path, int nr, char **match);
470 -extern int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
471 - int nr_refspec, char **refspec, int all);
470 extern int get_ack(int fd, unsigned char *result_sha1);
471 extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags);
472 extern int server_supports(const char *feature);
connect.c
+1 -239
@@ -4,6 +4,7 @@
4 #include "quote.h"
5 #include "refs.h"
6 #include "run-command.h"
7 +#include "remote.h"
8
9 static char *server_capabilities;
10
@@ -128,245 +129,6 @@ int path_match(const char *path, int nr, char **match)
129 return 0;
130 }
131
131 -struct refspec {
132 - char *src;
133 - char *dst;
134 - char force;
135 -};
136 -
137 -/*
138 - * A:B means fast forward remote B with local A.
139 - * +A:B means overwrite remote B with local A.
140 - * +A is a shorthand for +A:A.
141 - * A is a shorthand for A:A.
142 - * :B means delete remote B.
143 - */
144 -static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
145 -{
146 - int i;
147 - struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
148 - for (i = 0; i < nr_refspec; i++) {
149 - char *sp, *dp, *ep;
150 - sp = refspec[i];
151 - if (*sp == '+') {
152 - rs[i].force = 1;
153 - sp++;
154 - }
155 - ep = strchr(sp, ':');
156 - if (ep) {
157 - dp = ep + 1;
158 - *ep = 0;
159 - }
160 - else
161 - dp = sp;
162 - rs[i].src = sp;
163 - rs[i].dst = dp;
164 - }
165 - rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
166 - return rs;
167 -}
168 -
169 -static int count_refspec_match(const char *pattern,
170 - struct ref *refs,
171 - struct ref **matched_ref)
172 -{
173 - int patlen = strlen(pattern);
174 - struct ref *matched_weak = NULL;
175 - struct ref *matched = NULL;
176 - int weak_match = 0;
177 - int match = 0;
178 -
179 - for (weak_match = match = 0; refs; refs = refs->next) {
180 - char *name = refs->name;
181 - int namelen = strlen(name);
182 - int weak_match;
183 -
184 - if (namelen < patlen ||
185 - memcmp(name + namelen - patlen, pattern, patlen))
186 - continue;
187 - if (namelen != patlen && name[namelen - patlen - 1] != '/')
188 - continue;
189 -
190 - /* A match is "weak" if it is with refs outside
191 - * heads or tags, and did not specify the pattern
192 - * in full (e.g. "refs/remotes/origin/master") or at
193 - * least from the toplevel (e.g. "remotes/origin/master");
194 - * otherwise "git push $URL master" would result in
195 - * ambiguity between remotes/origin/master and heads/master
196 - * at the remote site.
197 - */
198 - if (namelen != patlen &&
199 - patlen != namelen - 5 &&
200 - prefixcmp(name, "refs/heads/") &&
201 - prefixcmp(name, "refs/tags/")) {
202 - /* We want to catch the case where only weak
203 - * matches are found and there are multiple
204 - * matches, and where more than one strong
205 - * matches are found, as ambiguous. One
206 - * strong match with zero or more weak matches
207 - * are acceptable as a unique match.
208 - */
209 - matched_weak = refs;
210 - weak_match++;
211 - }
212 - else {
213 - matched = refs;
214 - match++;
215 - }
216 - }
217 - if (!matched) {
218 - *matched_ref = matched_weak;
219 - return weak_match;
220 - }
221 - else {
222 - *matched_ref = matched;
223 - return match;
224 - }
225 -}
226 -
227 -static void link_dst_tail(struct ref *ref, struct ref ***tail)
228 -{
229 - **tail = ref;
230 - *tail = &ref->next;
231 - **tail = NULL;
232 -}
233 -
234 -static struct ref *try_explicit_object_name(const char *name)
235 -{
236 - unsigned char sha1[20];
237 - struct ref *ref;
238 - int len;
239 -
240 - if (!*name) {
241 - ref = xcalloc(1, sizeof(*ref) + 20);
242 - strcpy(ref->name, "(delete)");
243 - hashclr(ref->new_sha1);
244 - return ref;
245 - }
246 - if (get_sha1(name, sha1))
247 - return NULL;
248 - len = strlen(name) + 1;
249 - ref = xcalloc(1, sizeof(*ref) + len);
250 - memcpy(ref->name, name, len);
251 - hashcpy(ref->new_sha1, sha1);
252 - return ref;
253 -}
254 -
255 -static int match_explicit_refs(struct ref *src, struct ref *dst,
256 - struct ref ***dst_tail, struct refspec *rs)
257 -{
258 - int i, errs;
259 - for (i = errs = 0; rs[i].src; i++) {
260 - struct ref *matched_src, *matched_dst;
261 -
262 - matched_src = matched_dst = NULL;
263 - switch (count_refspec_match(rs[i].src, src, &matched_src)) {
264 - case 1:
265 - break;
266 - case 0:
267 - /* The source could be in the get_sha1() format
268 - * not a reference name. :refs/other is a
269 - * way to delete 'other' ref at the remote end.
270 - */
271 - matched_src = try_explicit_object_name(rs[i].src);
272 - if (matched_src)
273 - break;
274 - errs = 1;
275 - error("src refspec %s does not match any.",
276 - rs[i].src);
277 - break;
278 - default:
279 - errs = 1;
280 - error("src refspec %s matches more than one.",
281 - rs[i].src);
282 - break;
283 - }
284 - switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
285 - case 1:
286 - break;
287 - case 0:
288 - if (!memcmp(rs[i].dst, "refs/", 5)) {
289 - int len = strlen(rs[i].dst) + 1;
290 - matched_dst = xcalloc(1, sizeof(*dst) + len);
291 - memcpy(matched_dst->name, rs[i].dst, len);
292 - link_dst_tail(matched_dst, dst_tail);
293 - }
294 - else if (!strcmp(rs[i].src, rs[i].dst) &&
295 - matched_src) {
296 - /* pushing "master:master" when
297 - * remote does not have master yet.
298 - */
299 - int len = strlen(matched_src->name) + 1;
300 - matched_dst = xcalloc(1, sizeof(*dst) + len);
301 - memcpy(matched_dst->name, matched_src->name,
302 - len);
303 - link_dst_tail(matched_dst, dst_tail);
304 - }
305 - else {
306 - errs = 1;
307 - error("dst refspec %s does not match any "
308 - "existing ref on the remote and does "
309 - "not start with refs/.", rs[i].dst);
310 - }
311 - break;
312 - default:
313 - errs = 1;
314 - error("dst refspec %s matches more than one.",
315 - rs[i].dst);
316 - break;
317 - }
318 - if (errs)
319 - continue;
320 - if (matched_dst->peer_ref) {
321 - errs = 1;
322 - error("dst ref %s receives from more than one src.",
323 - matched_dst->name);
324 - }
325 - else {
326 - matched_dst->peer_ref = matched_src;
327 - matched_dst->force = rs[i].force;
328 - }
329 - }
330 - return -errs;
331 -}
332 -
333 -static struct ref *find_ref_by_name(struct ref *list, const char *name)
334 -{
335 - for ( ; list; list = list->next)
336 - if (!strcmp(list->name, name))
337 - return list;
338 - return NULL;
339 -}
340 -
341 -int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
342 - int nr_refspec, char **refspec, int all)
343 -{
344 - struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
345 -
346 - if (nr_refspec)
347 - return match_explicit_refs(src, dst, dst_tail, rs);
348 -
349 - /* pick the remainder */
350 - for ( ; src; src = src->next) {
351 - struct ref *dst_peer;
352 - if (src->peer_ref)
353 - continue;
354 - dst_peer = find_ref_by_name(dst, src->name);
355 - if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
356 - continue;
357 - if (!dst_peer) {
358 - /* Create a new one and link it */
359 - int len = strlen(src->name) + 1;
360 - dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
361 - memcpy(dst_peer->name, src->name, len);
362 - hashcpy(dst_peer->new_sha1, src->new_sha1);
363 - link_dst_tail(dst_peer, dst_tail);
364 - }
365 - dst_peer->peer_ref = src;
366 - }
367 - return 0;
368 -}
369 -
132 enum protocol {
133 PROTO_LOCAL = 1,
134 PROTO_SSH,
http-push.c
+1
@@ -9,6 +9,7 @@
9 #include "diff.h"
10 #include "revision.h"
11 #include "exec_cmd.h"
12 +#include "remote.h"
13
14 #include <expat.h>
15
remote.c
+246
@@ -201,6 +201,44 @@ static void read_config(void)
201 git_config(handle_config);
202 }
203
204 +static struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)
205 +{
206 + int i;
207 + struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec);
208 + for (i = 0; i < nr_refspec; i++) {
209 + const char *sp, *ep, *gp;
210 + sp = refspec[i];
211 + if (*sp == '+') {
212 + rs[i].force = 1;
213 + sp++;
214 + }
215 + gp = strchr(sp, '*');
216 + ep = strchr(sp, ':');
217 + if (gp && ep && gp > ep)
218 + gp = NULL;
219 + if (ep) {
220 + if (ep[1]) {
221 + const char *glob = strchr(ep + 1, '*');
222 + if (!glob)
223 + gp = NULL;
224 + if (gp)
225 + rs[i].dst = xstrndup(ep + 1,
226 + glob - ep - 1);
227 + else
228 + rs[i].dst = xstrdup(ep + 1);
229 + }
230 + } else {
231 + ep = sp + strlen(sp);
232 + }
233 + if (gp) {
234 + rs[i].pattern = 1;
235 + ep = gp;
236 + }
237 + rs[i].src = xstrndup(sp, ep - sp);
238 + }
239 + return rs;
240 +}
241 +
242 struct remote *remote_get(const char *name)
243 {
244 struct remote *ret;
@@ -219,5 +257,213 @@ struct remote *remote_get(const char *name)
257 add_uri(ret, name);
258 if (!ret->uri)
259 return NULL;
260 + ret->push = parse_ref_spec(ret->push_refspec_nr, ret->push_refspec);
261 return ret;
262 }
263 +
264 +static int count_refspec_match(const char *pattern,
265 + struct ref *refs,
266 + struct ref **matched_ref)
267 +{
268 + int patlen = strlen(pattern);
269 + struct ref *matched_weak = NULL;
270 + struct ref *matched = NULL;
271 + int weak_match = 0;
272 + int match = 0;
273 +
274 + for (weak_match = match = 0; refs; refs = refs->next) {
275 + char *name = refs->name;
276 + int namelen = strlen(name);
277 + int weak_match;
278 +
279 + if (namelen < patlen ||
280 + memcmp(name + namelen - patlen, pattern, patlen))
281 + continue;
282 + if (namelen != patlen && name[namelen - patlen - 1] != '/')
283 + continue;
284 +
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.
292 + */
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.
303 + */
304 + matched_weak = refs;
305 + weak_match++;
306 + }
307 + else {
308 + matched = refs;
309 + match++;
310 + }
311 + }
312 + if (!matched) {
313 + *matched_ref = matched_weak;
314 + return weak_match;
315 + }
316 + else {
317 + *matched_ref = matched;
318 + return match;
319 + }
320 +}
321 +
322 +static void link_dst_tail(struct ref *ref, struct ref ***tail)
323 +{
324 + **tail = ref;
325 + *tail = &ref->next;
326 + **tail = NULL;
327 +}
328 +
329 +static struct ref *try_explicit_object_name(const char *name)
330 +{
331 + unsigned char sha1[20];
332 + struct ref *ref;
333 + int len;
334 +
335 + if (!*name) {
336 + ref = xcalloc(1, sizeof(*ref) + 20);
337 + strcpy(ref->name, "(delete)");
338 + hashclr(ref->new_sha1);
339 + return ref;
340 + }
341 + if (get_sha1(name, sha1))
342 + return NULL;
343 + len = strlen(name) + 1;
344 + ref = xcalloc(1, sizeof(*ref) + len);
345 + memcpy(ref->name, name, len);
346 + hashcpy(ref->new_sha1, sha1);
347 + return ref;
348 +}
349 +
350 +static int match_explicit_refs(struct ref *src, struct ref *dst,
351 + struct ref ***dst_tail, struct refspec *rs,
352 + int rs_nr)
353 +{
354 + int i, errs;
355 + for (i = errs = 0; i < rs_nr; i++) {
356 + struct ref *matched_src, *matched_dst;
357 +
358 + const char *dst_value = rs[i].dst;
359 + if (dst_value == NULL)
360 + dst_value = rs[i].src;
361 +
362 + matched_src = matched_dst = NULL;
363 + switch (count_refspec_match(rs[i].src, src, &matched_src)) {
364 + case 1:
365 + break;
366 + case 0:
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.
370 + */
371 + matched_src = try_explicit_object_name(rs[i].src);
372 + if (matched_src)
373 + break;
374 + errs = 1;
375 + error("src refspec %s does not match any.",
376 + rs[i].src);
377 + break;
378 + default:
379 + errs = 1;
380 + error("src refspec %s matches more than one.",
381 + rs[i].src);
382 + break;
383 + }
384 + switch (count_refspec_match(dst_value, dst, &matched_dst)) {
385 + case 1:
386 + break;
387 + case 0:
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);
393 + }
394 + else if (!strcmp(rs[i].src, dst_value) &&
395 + matched_src) {
396 + /* pushing "master:master" when
397 + * remote does not have master yet.
398 + */
399 + int len = strlen(matched_src->name) + 1;
400 + matched_dst = xcalloc(1, sizeof(*dst) + len);
401 + memcpy(matched_dst->name, matched_src->name,
402 + len);
403 + link_dst_tail(matched_dst, dst_tail);
404 + }
405 + else {
406 + errs = 1;
407 + error("dst refspec %s does not match any "
408 + "existing ref on the remote and does "
409 + "not start with refs/.", dst_value);
410 + }
411 + break;
412 + default:
413 + errs = 1;
414 + error("dst refspec %s matches more than one.",
415 + dst_value);
416 + break;
417 + }
418 + if (errs)
419 + continue;
420 + if (matched_dst->peer_ref) {
421 + errs = 1;
422 + error("dst ref %s receives from more than one src.",
423 + matched_dst->name);
424 + }
425 + else {
426 + matched_dst->peer_ref = matched_src;
427 + matched_dst->force = rs[i].force;
428 + }
429 + }
430 + return -errs;
431 +}
432 +
433 +static struct ref *find_ref_by_name(struct ref *list, const char *name)
434 +{
435 + for ( ; list; list = list->next)
436 + if (!strcmp(list->name, name))
437 + return list;
438 + return NULL;
439 +}
440 +
441 +int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
442 + int nr_refspec, char **refspec, int all)
443 +{
444 + struct refspec *rs =
445 + parse_ref_spec(nr_refspec, (const char **) refspec);
446 +
447 + if (nr_refspec)
448 + return match_explicit_refs(src, dst, dst_tail, rs, nr_refspec);
449 +
450 + /* pick the remainder */
451 + for ( ; src; src = src->next) {
452 + struct ref *dst_peer;
453 + if (src->peer_ref)
454 + continue;
455 + dst_peer = find_ref_by_name(dst, src->name);
456 + if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
457 + continue;
458 + if (!dst_peer) {
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);
465 + }
466 + dst_peer->peer_ref = src;
467 + }
468 + return 0;
469 +}
remote.h
+12
@@ -8,6 +8,7 @@ struct remote {
8 int uri_nr;
9
10 const char **push_refspec;
11 + struct refspec *push;
12 int push_refspec_nr;
13
14 const char *receivepack;
@@ -15,4 +16,15 @@ struct remote {
16
17 struct remote *remote_get(const char *name);
18
19 +struct refspec {
20 + unsigned force : 1;
21 + unsigned pattern : 1;
22 +
23 + const char *src;
24 + char *dst;
25 +};
26 +
27 +int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
28 + int nr_refspec, char **refspec, int all);
29 +
30 #endif
send-pack.c
+1
@@ -4,6 +4,7 @@
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "run-command.h"
7 +#include "remote.h"
8
9 static const char send_pack_usage[] =
10 "git-send-pack [--all] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"