{"thread":{"id":"18165","subject":"[PATCH 3/5] Use the matching function to generate the match results","startedAt":"2009-03-06T04:56:20Z","lastAt":"2009-03-07T06:11:34Z","messageCount":4,"participants":["Daniel Barkalow","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"107143","messageId":"alpine.LNX.1.00.0903052355240.19665@iabervon.org","threadId":"18165","inReplyTo":null,"subject":"[PATCH 3/5] Use the matching function to generate the match results","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2009-03-06T04:56:20Z","receivedAt":"2009-03-06T04:56:20Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This puts all of the interpretation of the pattern representation in a\nsingle function for easy manipulation.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n remote.c |   45 +++++++++++++++++++++++----------------------\n 1 files changed, 23 insertions(+), 22 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 709300b..93fd03d 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -719,9 +719,19 @@ int remote_has_url(struct remote *remote, const char *url)\n \treturn 0;\n }\n \n-static int name_fits_pattern(const char *key, const char *name)\n+static int name_fits_pattern(const char *key, const char *name,\n+\t\t\t     const char *value, char **result)\n {\n-\tint ret = !prefixcmp(key, name);\n+\tsize_t klen = strlen(key);\n+\tint ret = !strncmp(key, name, klen);\n+\tif (ret && value) {\n+\t\tsize_t vlen = strlen(value);\n+\t\t*result = xmalloc(vlen +\n+\t\t\t\t  strlen(name) -\n+\t\t\t\t  klen + 1);\n+\t\tstrcpy(*result, value);\n+\t\tstrcpy(*result + vlen, name + klen);\n+\t}\n \treturn ret;\n }\n \n@@ -748,13 +758,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n \t\tif (!fetch->dst)\n \t\t\tcontinue;\n \t\tif (fetch->pattern) {\n-\t\t\tif (name_fits_pattern(key, needle)) {\n-\t\t\t\t*result = xmalloc(strlen(value) +\n-\t\t\t\t\t\t  strlen(needle) -\n-\t\t\t\t\t\t  strlen(key) + 1);\n-\t\t\t\tstrcpy(*result, value);\n-\t\t\t\tstrcpy(*result + strlen(value),\n-\t\t\t\t       needle + strlen(key));\n+\t\t\tif (name_fits_pattern(key, needle, value, result)) {\n \t\t\t\trefspec->force = fetch->force;\n \t\t\t\treturn 0;\n \t\t\t}\n@@ -1026,7 +1030,8 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,\n \t\t\tcontinue;\n \t\t}\n \n-\t\tif (rs[i].pattern && name_fits_pattern(rs[i].src, src->name))\n+\t\tif (rs[i].pattern && name_fits_pattern(rs[i].src, src->name,\n+\t\t\t\t\t\t       NULL, NULL))\n \t\t\treturn rs + i;\n \t}\n \tif (matching_refs != -1)\n@@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n \n \t\t} else {\n \t\t\tconst char *dst_side = pat->dst ? pat->dst : pat->src;\n-\t\t\tdst_name = xmalloc(strlen(dst_side) +\n-\t\t\t\t\t   strlen(src->name) -\n-\t\t\t\t\t   strlen(pat->src) + 2);\n-\t\t\tstrcpy(dst_name, dst_side);\n-\t\t\tstrcat(dst_name, src->name + strlen(pat->src));\n+\t\t\tif (!name_fits_pattern(pat->src, src->name,\n+\t\t\t\t\t       dst_side, &dst_name))\n+\t\t\t\tdie(\"Didn't think it matches any more\");\n \t\t}\n \t\tdst_peer = find_ref_by_name(dst, dst_name);\n \t\tif (dst_peer) {\n@@ -1160,19 +1163,17 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,\n \tstruct ref *ret = NULL;\n \tstruct ref **tail = &ret;\n \n-\tint remote_prefix_len = strlen(refspec->src);\n-\tint local_prefix_len = strlen(refspec->dst);\n+\tchar *expn_name;\n \n \tfor (ref = remote_refs; ref; ref = ref->next) {\n \t\tif (strchr(ref->name, '^'))\n \t\t\tcontinue; /* a dereference item */\n-\t\tif (name_fits_pattern(refspec->src, ref->name)) {\n-\t\t\tconst char *match;\n+\t\tif (name_fits_pattern(refspec->src, ref->name,\n+\t\t\t\t      refspec->dst, &expn_name)) {\n \t\t\tstruct ref *cpy = copy_ref(ref);\n-\t\t\tmatch = ref->name + remote_prefix_len;\n \n-\t\t\tcpy->peer_ref = alloc_ref_with_prefix(refspec->dst,\n-\t\t\t\t\tlocal_prefix_len, match);\n+\t\t\tcpy->peer_ref = alloc_ref(expn_name);\n+\t\t\tfree(expn_name);\n \t\t\tif (refspec->force)\n \t\t\t\tcpy->peer_ref->force = 1;\n \t\t\t*tail = cpy;\n-- \n1.6.1.286.gd33a4.dirty\n"},{"id":"107163","messageId":"7vy6vjt679.fsf@gitster.siamese.dyndns.org","threadId":"18165","inReplyTo":"alpine.LNX.1.00.0903052355240.19665@iabervon.org","subject":"Re: [PATCH 3/5] Use the matching function to generate the match results","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-06T08:18:34Z","receivedAt":"2009-03-06T08:18:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n> This puts all of the interpretation of the pattern representation in a\n> single function for easy manipulation.\n>\n> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n\nI think this makes sense, but the helper function is not about \"does this\nname match the pattern?\" boolean anymore, and may want to be renamed to\ne.g. map_name_with_pattern().\n\n> -static int name_fits_pattern(const char *key, const char *name)\n> +static int name_fits_pattern(const char *key, const char *name,\n> +\t\t\t     const char *value, char **result)\n>  {\n> -\tint ret = !prefixcmp(key, name);\n> +\tsize_t klen = strlen(key);\n> +\tint ret = !strncmp(key, name, klen);\n> +\tif (ret && value) {\n> +\t\tsize_t vlen = strlen(value);\n> +\t\t*result = xmalloc(vlen +\n> +\t\t\t\t  strlen(name) -\n> +\t\t\t\t  klen + 1);\n> +\t\tstrcpy(*result, value);\n> +\t\tstrcpy(*result + vlen, name + klen);\n> +\t}\n\n> @@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n>  \n>  \t\t} else {\n>  \t\t\tconst char *dst_side = pat->dst ? pat->dst : pat->src;\n> -\t\t\tdst_name = xmalloc(strlen(dst_side) +\n> -\t\t\t\t\t   strlen(src->name) -\n> -\t\t\t\t\t   strlen(pat->src) + 2);\n> -\t\t\tstrcpy(dst_name, dst_side);\n> -\t\t\tstrcat(dst_name, src->name + strlen(pat->src));\n> +\t\t\tif (!name_fits_pattern(pat->src, src->name,\n> +\t\t\t\t\t       dst_side, &dst_name))\n> +\t\t\t\tdie(\"Didn't think it matches any more\");\n>  \t\t}\n\nHmm, we have been overallocating and nobody noticed so far?\n"},{"id":"107232","messageId":"alpine.LNX.1.00.0903061151130.19665@iabervon.org","threadId":"18165","inReplyTo":"7vy6vjt679.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 3/5] Use the matching function to generate the match results","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2009-03-06T17:21:31Z","receivedAt":"2009-03-06T17:21:31Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Fri, 6 Mar 2009, Junio C Hamano wrote:\n\n> Daniel Barkalow <barkalow@iabervon.org> writes:\n> \n> > This puts all of the interpretation of the pattern representation in a\n> > single function for easy manipulation.\n> >\n> > Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> \n> I think this makes sense, but the helper function is not about \"does this\n> name match the pattern?\" boolean anymore, and may want to be renamed to\n> e.g. map_name_with_pattern().\n\nI'll just make it \"match_name_with_pattern\" from the beginning, which \ndescribes what it does whether or not the later arguments are NULL.\n\n> > -static int name_fits_pattern(const char *key, const char *name)\n> > +static int name_fits_pattern(const char *key, const char *name,\n> > +\t\t\t     const char *value, char **result)\n> >  {\n> > -\tint ret = !prefixcmp(key, name);\n> > +\tsize_t klen = strlen(key);\n> > +\tint ret = !strncmp(key, name, klen);\n> > +\tif (ret && value) {\n> > +\t\tsize_t vlen = strlen(value);\n> > +\t\t*result = xmalloc(vlen +\n> > +\t\t\t\t  strlen(name) -\n> > +\t\t\t\t  klen + 1);\n> > +\t\tstrcpy(*result, value);\n> > +\t\tstrcpy(*result + vlen, name + klen);\n> > +\t}\n> \n> > @@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n> >  \n> >  \t\t} else {\n> >  \t\t\tconst char *dst_side = pat->dst ? pat->dst : pat->src;\n> > -\t\t\tdst_name = xmalloc(strlen(dst_side) +\n> > -\t\t\t\t\t   strlen(src->name) -\n> > -\t\t\t\t\t   strlen(pat->src) + 2);\n> > -\t\t\tstrcpy(dst_name, dst_side);\n> > -\t\t\tstrcat(dst_name, src->name + strlen(pat->src));\n> > +\t\t\tif (!name_fits_pattern(pat->src, src->name,\n> > +\t\t\t\t\t       dst_side, &dst_name))\n> > +\t\t\t\tdie(\"Didn't think it matches any more\");\n> >  \t\t}\n> \n> Hmm, we have been overallocating and nobody noticed so far?\n\nProbably. I suspect that we're making sure to have room for the '\\0's at \nthe end of both the base and the part that matches the star. Looks like \nit's still how Alex Riesen wrote it, sort of arbitrarily one byte longer \nthan the similar allocation.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"107275","messageId":"alpine.LNX.1.00.0903070105350.19665@iabervon.org","threadId":"18165","inReplyTo":"alpine.LNX.1.00.0903052355240.19665@iabervon.org","subject":"[PATCH v2 3/5] Use the matching function to generate the match results","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2009-03-07T06:11:34Z","receivedAt":"2009-03-07T06:11:34Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This puts all of the interpretation of the pattern representation in a\nsingle function for easy manipulation.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\nAgain, better name for the function.\n\n remote.c |   45 +++++++++++++++++++++++----------------------\n 1 files changed, 23 insertions(+), 22 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 2816723..5638766 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -719,9 +719,19 @@ int remote_has_url(struct remote *remote, const char *url)\n \treturn 0;\n }\n \n-static int match_name_with_pattern(const char *key, const char *name)\n+static int match_name_with_pattern(const char *key, const char *name,\n+\t\t\t\t   const char *value, char **result)\n {\n-\tint ret = !prefixcmp(key, name);\n+\tsize_t klen = strlen(key);\n+\tint ret = !strncmp(key, name, klen);\n+\tif (ret && value) {\n+\t\tsize_t vlen = strlen(value);\n+\t\t*result = xmalloc(vlen +\n+\t\t\t\t  strlen(name) -\n+\t\t\t\t  klen + 1);\n+\t\tstrcpy(*result, value);\n+\t\tstrcpy(*result + vlen, name + klen);\n+\t}\n \treturn ret;\n }\n \n@@ -748,13 +758,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n \t\tif (!fetch->dst)\n \t\t\tcontinue;\n \t\tif (fetch->pattern) {\n-\t\t\tif (match_name_with_pattern(key, needle)) {\n-\t\t\t\t*result = xmalloc(strlen(value) +\n-\t\t\t\t\t\t  strlen(needle) -\n-\t\t\t\t\t\t  strlen(key) + 1);\n-\t\t\t\tstrcpy(*result, value);\n-\t\t\t\tstrcpy(*result + strlen(value),\n-\t\t\t\t       needle + strlen(key));\n+\t\t\tif (match_name_with_pattern(key, needle, value, result)) {\n \t\t\t\trefspec->force = fetch->force;\n \t\t\t\treturn 0;\n \t\t\t}\n@@ -1026,7 +1030,8 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,\n \t\t\tcontinue;\n \t\t}\n \n-\t\tif (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name))\n+\t\tif (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,\n+\t\t\t\t\t\t\t     NULL, NULL))\n \t\t\treturn rs + i;\n \t}\n \tif (matching_refs != -1)\n@@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n \n \t\t} else {\n \t\t\tconst char *dst_side = pat->dst ? pat->dst : pat->src;\n-\t\t\tdst_name = xmalloc(strlen(dst_side) +\n-\t\t\t\t\t   strlen(src->name) -\n-\t\t\t\t\t   strlen(pat->src) + 2);\n-\t\t\tstrcpy(dst_name, dst_side);\n-\t\t\tstrcat(dst_name, src->name + strlen(pat->src));\n+\t\t\tif (!match_name_with_pattern(pat->src, src->name,\n+\t\t\t\t\t\t     dst_side, &dst_name))\n+\t\t\t\tdie(\"Didn't think it matches any more\");\n \t\t}\n \t\tdst_peer = find_ref_by_name(dst, dst_name);\n \t\tif (dst_peer) {\n@@ -1160,19 +1163,17 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,\n \tstruct ref *ret = NULL;\n \tstruct ref **tail = &ret;\n \n-\tint remote_prefix_len = strlen(refspec->src);\n-\tint local_prefix_len = strlen(refspec->dst);\n+\tchar *expn_name;\n \n \tfor (ref = remote_refs; ref; ref = ref->next) {\n \t\tif (strchr(ref->name, '^'))\n \t\t\tcontinue; /* a dereference item */\n-\t\tif (match_name_with_pattern(refspec->src, ref->name)) {\n-\t\t\tconst char *match;\n+\t\tif (match_name_with_pattern(refspec->src, ref->name,\n+\t\t\t\t\t    refspec->dst, &expn_name)) {\n \t\t\tstruct ref *cpy = copy_ref(ref);\n-\t\t\tmatch = ref->name + remote_prefix_len;\n \n-\t\t\tcpy->peer_ref = alloc_ref_with_prefix(refspec->dst,\n-\t\t\t\t\tlocal_prefix_len, match);\n+\t\t\tcpy->peer_ref = alloc_ref(expn_name);\n+\t\t\tfree(expn_name);\n \t\t\tif (refspec->force)\n \t\t\t\tcpy->peer_ref->force = 1;\n \t\t\t*tail = cpy;\n-- \n1.6.1.286.gd33a4.dirty\n"}]}