From 689f03964360114bc1139d82b1f6a0d7b897bbf0 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Thu, 5 Mar 2009 23:56:16 -0500 Subject: [PATCH 1/5] Make clone parse the default refspec with the normal code Instead of creating a refspec by hand, go through the refspec parsing code, so that changes in the refspec storage will be accounted for. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- builtin-clone.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/builtin-clone.c b/builtin-clone.c index c338910b1c..06b5a7fc39 100644 --- a/builtin-clone.c +++ b/builtin-clone.c @@ -378,7 +378,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix) struct transport *transport = NULL; char *src_ref_prefix = "refs/heads/"; - struct refspec refspec; + struct refspec *refspec; + const char *fetch_pattern; junk_pid = getpid(); @@ -487,8 +488,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix) strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin); } + strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf); + if (option_mirror || !option_bare) { /* Configure the remote */ + strbuf_addf(&key, "remote.%s.fetch", option_origin); + git_config_set_multivar(key.buf, value.buf, "^$", 0); + strbuf_reset(&key); + if (option_mirror) { strbuf_addf(&key, "remote.%s.mirror", option_origin); git_config_set(key.buf, "true"); @@ -497,19 +504,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix) strbuf_addf(&key, "remote.%s.url", option_origin); git_config_set(key.buf, repo); - strbuf_reset(&key); - - strbuf_addf(&key, "remote.%s.fetch", option_origin); - strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf); - git_config_set_multivar(key.buf, value.buf, "^$", 0); strbuf_reset(&key); - strbuf_reset(&value); } - refspec.force = 0; - refspec.pattern = 1; - refspec.src = src_ref_prefix; - refspec.dst = branch_top.buf; + fetch_pattern = value.buf; + refspec = parse_fetch_refspec(1, &fetch_pattern); + + strbuf_reset(&value); if (path && !is_bundle) refs = clone_local(path, git_dir); @@ -543,7 +544,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) if (refs) { clear_extra_refs(); - mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf); + mapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf); head_points_at = locate_head(refs, mapped_refs, &remote_head); } From a3c8423901ec4996369b137b46354ddf1cfc0e0e Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sat, 7 Mar 2009 01:11:29 -0500 Subject: [PATCH 2/5] Use a single function to match names against patterns This will help when the matching changes. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- remote.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/remote.c b/remote.c index d7079c6dd8..2816723bb9 100644 --- a/remote.c +++ b/remote.c @@ -719,6 +719,12 @@ int remote_has_url(struct remote *remote, const char *url) return 0; } +static int match_name_with_pattern(const char *key, const char *name) +{ + int ret = !prefixcmp(key, name); + return ret; +} + int remote_find_tracking(struct remote *remote, struct refspec *refspec) { int find_src = refspec->src == NULL; @@ -742,7 +748,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec) if (!fetch->dst) continue; if (fetch->pattern) { - if (!prefixcmp(needle, key)) { + if (match_name_with_pattern(key, needle)) { *result = xmalloc(strlen(value) + strlen(needle) - strlen(key) + 1); @@ -1020,7 +1026,7 @@ static const struct refspec *check_pattern_match(const struct refspec *rs, continue; } - if (rs[i].pattern && !prefixcmp(src->name, rs[i].src)) + if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name)) return rs + i; } if (matching_refs != -1) @@ -1160,7 +1166,7 @@ static struct ref *get_expanded_map(const struct ref *remote_refs, for (ref = remote_refs; ref; ref = ref->next) { if (strchr(ref->name, '^')) continue; /* a dereference item */ - if (!prefixcmp(ref->name, refspec->src)) { + if (match_name_with_pattern(refspec->src, ref->name)) { const char *match; struct ref *cpy = copy_ref(ref); match = ref->name + remote_prefix_len; From e928213fb40c106650dca2632b5e830cfaffb86a Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sat, 7 Mar 2009 01:11:34 -0500 Subject: [PATCH 3/5] Use the matching function to generate the match results This puts all of the interpretation of the pattern representation in a single function for easy manipulation. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- remote.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/remote.c b/remote.c index 2816723bb9..01b8f91c5b 100644 --- a/remote.c +++ b/remote.c @@ -719,9 +719,19 @@ int remote_has_url(struct remote *remote, const char *url) return 0; } -static int match_name_with_pattern(const char *key, const char *name) +static int match_name_with_pattern(const char *key, const char *name, + const char *value, char **result) { - int ret = !prefixcmp(key, name); + size_t klen = strlen(key); + int ret = !strncmp(key, name, klen); + if (ret && value) { + size_t vlen = strlen(value); + *result = xmalloc(vlen + + strlen(name) - + klen + 1); + strcpy(*result, value); + strcpy(*result + vlen, name + klen); + } return ret; } @@ -748,13 +758,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec) if (!fetch->dst) continue; if (fetch->pattern) { - if (match_name_with_pattern(key, needle)) { - *result = xmalloc(strlen(value) + - strlen(needle) - - strlen(key) + 1); - strcpy(*result, value); - strcpy(*result + strlen(value), - needle + strlen(key)); + if (match_name_with_pattern(key, needle, value, result)) { refspec->force = fetch->force; return 0; } @@ -1026,7 +1030,8 @@ static const struct refspec *check_pattern_match(const struct refspec *rs, continue; } - if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name)) + if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name, + NULL, NULL)) return rs + i; } if (matching_refs != -1) @@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, } else { const char *dst_side = pat->dst ? pat->dst : pat->src; - dst_name = xmalloc(strlen(dst_side) + - strlen(src->name) - - strlen(pat->src) + 2); - strcpy(dst_name, dst_side); - strcat(dst_name, src->name + strlen(pat->src)); + if (!match_name_with_pattern(pat->src, src->name, + dst_side, &dst_name)) + die("Didn't think it matches any more"); } dst_peer = find_ref_by_name(dst, dst_name); if (dst_peer) { @@ -1160,19 +1163,17 @@ static struct ref *get_expanded_map(const struct ref *remote_refs, struct ref *ret = NULL; struct ref **tail = &ret; - int remote_prefix_len = strlen(refspec->src); - int local_prefix_len = strlen(refspec->dst); + char *expn_name; for (ref = remote_refs; ref; ref = ref->next) { if (strchr(ref->name, '^')) continue; /* a dereference item */ - if (match_name_with_pattern(refspec->src, ref->name)) { - const char *match; + if (match_name_with_pattern(refspec->src, ref->name, + refspec->dst, &expn_name)) { struct ref *cpy = copy_ref(ref); - match = ref->name + remote_prefix_len; - cpy->peer_ref = alloc_ref_with_prefix(refspec->dst, - local_prefix_len, match); + cpy->peer_ref = alloc_ref(expn_name); + free(expn_name); if (refspec->force) cpy->peer_ref->force = 1; *tail = cpy; From 08fbdb30438fd7087c5abe15840a22fe21094515 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sat, 7 Mar 2009 01:11:36 -0500 Subject: [PATCH 4/5] Keep '*' in pattern refspecs In order to do anything more capable with refspecs, the first step is to keep the entire input. Additionally, validate patterns by checking for the ref matching the rules for a pattern as given by check_ref_format(). This requires a slight change to check_ref_format() to make it enforce the requirement that the '*' immediately follow a '/'. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- refs.c | 4 +--- remote.c | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/refs.c b/refs.c index 6eb5f53846..a50ba79270 100644 --- a/refs.c +++ b/refs.c @@ -718,9 +718,7 @@ int check_ref_format(const char *ref) while ((ch = *cp++) != 0) { bad_type = bad_ref_char(ch); if (bad_type) { - return (bad_type == 2 && !*cp) - ? CHECK_REF_FORMAT_WILDCARD - : CHECK_REF_FORMAT_ERROR; + return CHECK_REF_FORMAT_ERROR; } if (ch == '/') break; diff --git a/remote.c b/remote.c index 01b8f91c5b..d596a48651 100644 --- a/remote.c +++ b/remote.c @@ -10,8 +10,8 @@ static struct refspec s_tag_refspec = { 0, 1, 0, - "refs/tags/", - "refs/tags/" + "refs/tags/*", + "refs/tags/*" }; const struct refspec *tag_refspec = &s_tag_refspec; @@ -451,16 +451,11 @@ static void read_config(void) */ static int verify_refname(char *name, int is_glob) { - int result, len = -1; + int result; - if (is_glob) { - len = strlen(name); - assert(name[len - 1] == '/'); - name[len - 1] = '\0'; - } result = check_ref_format(name); - if (is_glob) - name[len - 1] = '/'; + if (is_glob && result == CHECK_REF_FORMAT_WILDCARD) + result = CHECK_REF_FORMAT_OK; return result; } @@ -517,7 +512,7 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp if (rhs) { size_t rlen = strlen(++rhs); is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*")); - rs[i].dst = xstrndup(rhs, rlen - is_glob); + rs[i].dst = xstrndup(rhs, rlen); } llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); @@ -525,7 +520,6 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp if ((rhs && !is_glob) || (!rhs && fetch)) goto invalid; is_glob = 1; - llen--; } else if (rhs && is_glob) { goto invalid; } @@ -722,10 +716,19 @@ int remote_has_url(struct remote *remote, const char *url) static int match_name_with_pattern(const char *key, const char *name, const char *value, char **result) { - size_t klen = strlen(key); - int ret = !strncmp(key, name, klen); + const char *kstar = strchr(key, '*'); + size_t klen; + int ret; + if (!kstar) + die("Key '%s' of pattern had no '*'", key); + klen = kstar - key; + ret = !strncmp(key, name, klen); if (ret && value) { - size_t vlen = strlen(value); + const char *vstar = strchr(value, '*'); + size_t vlen; + if (!vstar) + die("Value '%s' of pattern has no '*'", value); + vlen = vstar - value; *result = xmalloc(vlen + strlen(name) - klen + 1); From abd2bde78bd994166900290434a2048e660dabed Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sat, 7 Mar 2009 01:11:39 -0500 Subject: [PATCH 5/5] Support '*' in the middle of a refspec In order to keep the requirements strict, each * has to be a full path component, and there may only be one * per side. This requirement is enforced entirely by check_ref_format(); the matching implementation will substitute the whatever matches the * in the lhs for the * in the rhs. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- refs.c | 11 +++++++---- remote.c | 24 +++++++++++++++++------- t/t5511-refspec.sh | 12 ++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/refs.c b/refs.c index a50ba79270..fef7c9f26d 100644 --- a/refs.c +++ b/refs.c @@ -694,6 +694,7 @@ static inline int bad_ref_char(int ch) int check_ref_format(const char *ref) { int ch, level, bad_type; + int ret = CHECK_REF_FORMAT_OK; const char *cp = ref; level = 0; @@ -709,9 +710,11 @@ int check_ref_format(const char *ref) return CHECK_REF_FORMAT_ERROR; bad_type = bad_ref_char(ch); if (bad_type) { - return (bad_type == 2 && !*cp) - ? CHECK_REF_FORMAT_WILDCARD - : CHECK_REF_FORMAT_ERROR; + if (bad_type == 2 && (!*cp || *cp == '/') && + ret == CHECK_REF_FORMAT_OK) + ret = CHECK_REF_FORMAT_WILDCARD; + else + return CHECK_REF_FORMAT_ERROR; } /* scan the rest of the path component */ @@ -729,7 +732,7 @@ int check_ref_format(const char *ref) if (!ch) { if (level < 2) return CHECK_REF_FORMAT_ONELEVEL; - return CHECK_REF_FORMAT_OK; + return ret; } } } diff --git a/remote.c b/remote.c index d596a48651..90203e2fe1 100644 --- a/remote.c +++ b/remote.c @@ -511,12 +511,12 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp if (rhs) { size_t rlen = strlen(++rhs); - is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*")); + is_glob = (1 <= rlen && strchr(rhs, '*')); rs[i].dst = xstrndup(rhs, rlen); } llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); - if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) { + if (1 <= llen && memchr(lhs, '*', llen)) { if ((rhs && !is_glob) || (!rhs && fetch)) goto invalid; is_glob = 1; @@ -718,22 +718,32 @@ static int match_name_with_pattern(const char *key, const char *name, { const char *kstar = strchr(key, '*'); size_t klen; + size_t ksuffixlen; + size_t namelen; int ret; if (!kstar) die("Key '%s' of pattern had no '*'", key); klen = kstar - key; - ret = !strncmp(key, name, klen); + ksuffixlen = strlen(kstar + 1); + namelen = strlen(name); + ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && + !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); if (ret && value) { const char *vstar = strchr(value, '*'); size_t vlen; + size_t vsuffixlen; if (!vstar) die("Value '%s' of pattern has no '*'", value); vlen = vstar - value; - *result = xmalloc(vlen + + vsuffixlen = strlen(vstar + 1); + *result = xmalloc(vlen + vsuffixlen + strlen(name) - - klen + 1); - strcpy(*result, value); - strcpy(*result + vlen, name + klen); + klen - ksuffixlen + 1); + strncpy(*result, value, vlen); + strncpy(*result + vlen, + name + klen, namelen - klen - ksuffixlen); + strcpy(*result + vlen + namelen - klen - ksuffixlen, + vstar + 1); } return ret; } diff --git a/t/t5511-refspec.sh b/t/t5511-refspec.sh index 22ba380034..c28932216b 100755 --- a/t/t5511-refspec.sh +++ b/t/t5511-refspec.sh @@ -72,4 +72,16 @@ test_refspec fetch ':refs/remotes/frotz/HEAD-to-me' test_refspec push ':refs/remotes/frotz/delete me' invalid test_refspec fetch ':refs/remotes/frotz/HEAD to me' invalid +test_refspec fetch 'refs/heads/*/for-linus:refs/remotes/mine/*-blah' invalid +test_refspec push 'refs/heads/*/for-linus:refs/remotes/mine/*-blah' invalid + +test_refspec fetch 'refs/heads*/for-linus:refs/remotes/mine/*' invalid +test_refspec push 'refs/heads*/for-linus:refs/remotes/mine/*' invalid + +test_refspec fetch 'refs/heads/*/*/for-linus:refs/remotes/mine/*' invalid +test_refspec push 'refs/heads/*/*/for-linus:refs/remotes/mine/*' invalid + +test_refspec fetch 'refs/heads/*/for-linus:refs/remotes/mine/*' +test_refspec push 'refs/heads/*/for-linus:refs/remotes/mine/*' + test_done