Move refspec pattern matching to match_refs().
This means that send-pack and http-push will support pattern refspecs, so builtin-push.c doesn't have to expand them, and also git push can just turn --tags into "refs/tags/*", further simplifying builtin-push.c check_ref_format() gets a third "conditionally okay" result for something that's valid as a pattern but not as a particular ref. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Daniel Barkalow committed
May 25, 2007 at 01:20 UTC
8558fd9ece4c8250a037a6d5482a8040d600ef47
4 files changed
+70
-122
builtin-push.c
+22
-111
@@ -9,7 +9,7 @@
9
10
static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]";
11
12
-static int all, tags, force, thin = 1, verbose;
12
+static int all, force, thin = 1, verbose;
13
static const char *receivepack;
14
15
static const char **refspec;
@@ -23,114 +23,24 @@ static void add_refspec(const char *ref)
23
refspec_nr = nr;
24
}
25
26
-static int expand_one_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
27
-{
28
- /* Ignore the "refs/" at the beginning of the refname */
29
- ref += 5;
30
-
31
- if (!prefixcmp(ref, "tags/"))
32
- add_refspec(xstrdup(ref));
33
- return 0;
34
-}
35
-
36
-static void expand_refspecs(void)
37
-{
38
- if (all) {
39
- if (refspec_nr)
40
- die("cannot mix '--all' and a refspec");
41
-
42
- /*
43
- * No need to expand "--all" - we'll just use
44
- * the "--all" flag to send-pack
45
- */
46
- return;
47
- }
48
- if (!tags)
49
- return;
50
- for_each_ref(expand_one_ref, NULL);
51
-}
52
-
53
-struct wildcard_cb {
54
- const char *from_prefix;
55
- int from_prefix_len;
56
- const char *to_prefix;
57
- int to_prefix_len;
58
- int force;
59
-};
60
-
61
-static int expand_wildcard_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
62
-{
63
- struct wildcard_cb *cb = cb_data;
64
- int len = strlen(ref);
65
- char *expanded, *newref;
66
-
67
- if (len < cb->from_prefix_len ||
68
- memcmp(cb->from_prefix, ref, cb->from_prefix_len))
69
- return 0;
70
- expanded = xmalloc(len * 2 + cb->force +
71
- (cb->to_prefix_len - cb->from_prefix_len) + 2);
72
- newref = expanded + cb->force;
73
- if (cb->force)
74
- expanded[0] = '+';
75
- memcpy(newref, ref, len);
76
- newref[len] = ':';
77
- memcpy(newref + len + 1, cb->to_prefix, cb->to_prefix_len);
78
- strcpy(newref + len + 1 + cb->to_prefix_len,
79
- ref + cb->from_prefix_len);
80
- add_refspec(expanded);
81
- return 0;
82
-}
83
-
84
-static int wildcard_ref(const char *ref)
85
-{
86
- int len;
87
- const char *colon;
88
- struct wildcard_cb cb;
89
-
90
- memset(&cb, 0, sizeof(cb));
91
- if (ref[0] == '+') {
92
- cb.force = 1;
93
- ref++;
94
- }
95
- len = strlen(ref);
96
- colon = strchr(ref, ':');
97
- if (! (colon && ref < colon &&
98
- colon[-2] == '/' && colon[-1] == '*' &&
99
- /* "<mine>/<asterisk>:<yours>/<asterisk>" is at least 7 bytes */
100
- 7 <= len &&
101
- ref[len-2] == '/' && ref[len-1] == '*') )
102
- return 0 ;
103
- cb.from_prefix = ref;
104
- cb.from_prefix_len = colon - ref - 1;
105
- cb.to_prefix = colon + 1;
106
- cb.to_prefix_len = len - (colon - ref) - 2;
107
- for_each_ref(expand_wildcard_ref, &cb);
108
- return 1;
109
-}
110
-
26
static void set_refspecs(const char **refs, int nr)
27
{
113
- if (nr) {
114
- int i;
115
- for (i = 0; i < nr; i++) {
116
- const char *ref = refs[i];
117
- if (!strcmp("tag", ref)) {
118
- char *tag;
119
- int len;
120
- if (nr <= ++i)
121
- die("tag shorthand without <tag>");
122
- len = strlen(refs[i]) + 11;
123
- tag = xmalloc(len);
124
- strcpy(tag, "refs/tags/");
125
- strcat(tag, refs[i]);
126
- ref = tag;
127
- }
128
- else if (wildcard_ref(ref))
129
- continue;
130
- add_refspec(ref);
28
+ int i;
29
+ for (i = 0; i < nr; i++) {
30
+ const char *ref = refs[i];
31
+ if (!strcmp("tag", ref)) {
32
+ char *tag;
33
+ int len;
34
+ if (nr <= ++i)
35
+ die("tag shorthand without <tag>");
36
+ len = strlen(refs[i]) + 11;
37
+ tag = xmalloc(len);
38
+ strcpy(tag, "refs/tags/");
39
+ strcat(tag, refs[i]);
40
+ ref = tag;
41
}
42
+ add_refspec(ref);
43
}
133
- expand_refspecs();
44
}
45
46
static int do_push(const char *repo)
@@ -149,11 +59,9 @@ static int do_push(const char *repo)
59
sprintf(rp, "--receive-pack=%s", remote->receivepack);
60
receivepack = rp;
61
}
152
- if (!refspec && !all && !tags && remote->push_refspec_nr) {
153
- for (i = 0; i < remote->push_refspec_nr; i++) {
154
- if (!wildcard_ref(remote->push_refspec[i]))
155
- add_refspec(remote->push_refspec[i]);
156
- }
62
+ if (!refspec && !all && remote->push_refspec_nr) {
63
+ refspec = remote->push_refspec;
64
+ refspec_nr = remote->push_refspec_nr;
65
}
66
67
argv = xmalloc((refspec_nr + 10) * sizeof(char *));
@@ -240,7 +148,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
148
continue;
149
}
150
if (!strcmp(arg, "--tags")) {
243
- tags = 1;
151
+ add_refspec("refs/tags/*");
152
continue;
153
}
154
if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
@@ -266,5 +174,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)
174
usage(push_usage);
175
}
176
set_refspecs(argv + i, argc - i);
177
+ if (all && refspec)
178
+ usage(push_usage);
179
+
180
return do_push(repo);
181
}
refs.c
+19
-8
@@ -603,15 +603,20 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
603
604
static inline int bad_ref_char(int ch)
605
{
606
- return (((unsigned) ch) <= ' ' ||
607
- ch == '~' || ch == '^' || ch == ':' ||
608
- /* 2.13 Pattern Matching Notation */
609
- ch == '?' || ch == '*' || ch == '[');
606
+ if (((unsigned) ch) <= ' ' ||
607
+ ch == '~' || ch == '^' || ch == ':')
608
+ return 1;
609
+ /* 2.13 Pattern Matching Notation */
610
+ if (ch == '?' || ch == '[') /* Unsupported */
611
+ return 1;
612
+ if (ch == '*') /* Supported at the end */
613
+ return 2;
614
+ return 0;
615
}
616
617
int check_ref_format(const char *ref)
618
{
614
- int ch, level;
619
+ int ch, level, bad_type;
620
const char *cp = ref;
621
622
level = 0;
@@ -622,13 +627,19 @@ int check_ref_format(const char *ref)
627
return -1; /* should not end with slashes */
628
629
/* we are at the beginning of the path component */
625
- if (ch == '.' || bad_ref_char(ch))
630
+ if (ch == '.')
631
return -1;
632
+ bad_type = bad_ref_char(ch);
633
+ if (bad_type) {
634
+ return (bad_type == 2 && !*cp) ? -3 : -1;
635
+ }
636
637
/* scan the rest of the path component */
638
while ((ch = *cp++) != 0) {
630
- if (bad_ref_char(ch))
631
- return -1;
639
+ bad_type = bad_ref_char(ch);
640
+ if (bad_type) {
641
+ return (bad_type == 2 && !*cp) ? -3 : -1;
642
+ }
643
if (ch == '/')
644
break;
645
if (ch == '.' && *cp == '.')
remote.c
+28
-3
@@ -415,6 +415,10 @@ static int match_explicit_refs(struct ref *src, struct ref *dst,
415
struct ref *matched_src, *matched_dst;
416
417
const char *dst_value = rs[i].dst;
418
+
419
+ if (rs[i].pattern)
420
+ continue;
421
+
422
if (dst_value == NULL)
423
dst_value = rs[i].src;
424
@@ -497,22 +501,43 @@ static struct ref *find_ref_by_name(struct ref *list, const char *name)
501
return NULL;
502
}
503
504
+static int check_pattern_match(struct refspec *rs, int rs_nr, struct ref *src)
505
+{
506
+ int i;
507
+ if (!rs_nr)
508
+ return 1;
509
+ for (i = 0; i < rs_nr; i++) {
510
+ if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
511
+ return 1;
512
+ }
513
+ return 0;
514
+}
515
+
516
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
517
int nr_refspec, char **refspec, int all)
518
{
519
struct refspec *rs =
520
parse_ref_spec(nr_refspec, (const char **) refspec);
521
506
- if (nr_refspec)
507
- return match_explicit_refs(src, dst, dst_tail, rs, nr_refspec);
522
+ if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
523
+ return -1;
524
525
/* pick the remainder */
526
for ( ; src; src = src->next) {
527
struct ref *dst_peer;
528
if (src->peer_ref)
529
continue;
530
+ if (!check_pattern_match(rs, nr_refspec, src))
531
+ continue;
532
+
533
dst_peer = find_ref_by_name(dst, src->name);
515
- if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
534
+ if (dst_peer && dst_peer->peer_ref)
535
+ /* We're already sending something to this ref. */
536
+ continue;
537
+ if (!dst_peer && !nr_refspec && !all)
538
+ /* Remote doesn't have it, and we have no
539
+ * explicit pattern, and we don't have
540
+ * --all. */
541
continue;
542
if (!dst_peer) {
543
/* Create a new one and link it */
send-pack.c
+1
@@ -354,6 +354,7 @@ static void verify_remote_names(int nr_heads, char **heads)
354
case -2: /* ok but a single level -- that is fine for
355
* a match pattern.
356
*/
357
+ case -3: /* ok but ends with a pattern-match character */
358
continue;
359
}
360
die("remote part of refspec is not a valid name in %s",