# [PATCH 3/5] Use the matching function to generate the match results

4 messages from 2009-03-06 to 2009-03-07. Participants: Daniel Barkalow, Junio C Hamano.
Thread: https://gitlist.dev/t/18165

## Daniel Barkalow, 2009-03-06 04:56

Subject: [PATCH 3/5] Use the matching function to generate the match results
Message-ID: <alpine.LNX.1.00.0903052355240.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0903052355240.19665%40iabervon.org

```
This puts all of the interpretation of the pattern representation in a
single function for easy manipulation.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
 remote.c |   45 +++++++++++++++++++++++----------------------
 1 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/remote.c b/remote.c
index 709300b..93fd03d 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 name_fits_pattern(const char *key, const char *name)
+static int name_fits_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 (name_fits_pattern(key, needle)) {
-				*result = xmalloc(strlen(value) +
-						  strlen(needle) -
-						  strlen(key) + 1);
-				strcpy(*result, value);
-				strcpy(*result + strlen(value),
-				       needle + strlen(key));
+			if (name_fits_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 && name_fits_pattern(rs[i].src, src->name))
+		if (rs[i].pattern && name_fits_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 (!name_fits_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 (name_fits_pattern(refspec->src, ref->name)) {
-			const char *match;
+		if (name_fits_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;
-- 
1.6.1.286.gd33a4.dirty

```

## Junio C Hamano, 2009-03-06 08:18

Subject: Re: [PATCH 3/5] Use the matching function to generate the match results
Message-ID: <7vy6vjt679.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy6vjt679.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.LNX.1.00.0903052355240.19665@iabervon.org>

```
Daniel Barkalow <barkalow@iabervon.org> writes:

> This puts all of the interpretation of the pattern representation in a
> single function for easy manipulation.
>
> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>

I think this makes sense, but the helper function is not about "does this
name match the pattern?" boolean anymore, and may want to be renamed to
e.g. map_name_with_pattern().

> -static int name_fits_pattern(const char *key, const char *name)
> +static int name_fits_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);
> +	}

> @@ -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 (!name_fits_pattern(pat->src, src->name,
> +					       dst_side, &dst_name))
> +				die("Didn't think it matches any more");
>  		}

Hmm, we have been overallocating and nobody noticed so far?

```

## Daniel Barkalow, 2009-03-06 17:21

Subject: Re: [PATCH 3/5] Use the matching function to generate the match results
Message-ID: <alpine.LNX.1.00.0903061151130.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0903061151130.19665%40iabervon.org
In-Reply-To: <7vy6vjt679.fsf@gitster.siamese.dyndns.org>

```
On Fri, 6 Mar 2009, Junio C Hamano wrote:

> Daniel Barkalow <barkalow@iabervon.org> writes:
> 
> > This puts all of the interpretation of the pattern representation in a
> > single function for easy manipulation.
> >
> > Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
> 
> I think this makes sense, but the helper function is not about "does this
> name match the pattern?" boolean anymore, and may want to be renamed to
> e.g. map_name_with_pattern().

I'll just make it "match_name_with_pattern" from the beginning, which 
describes what it does whether or not the later arguments are NULL.

> > -static int name_fits_pattern(const char *key, const char *name)
> > +static int name_fits_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);
> > +	}
> 
> > @@ -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 (!name_fits_pattern(pat->src, src->name,
> > +					       dst_side, &dst_name))
> > +				die("Didn't think it matches any more");
> >  		}
> 
> Hmm, we have been overallocating and nobody noticed so far?

Probably. I suspect that we're making sure to have room for the '\0's at 
the end of both the base and the part that matches the star. Looks like 
it's still how Alex Riesen wrote it, sort of arbitrarily one byte longer 
than the similar allocation.

	-Daniel
*This .sig left intentionally blank*

```

## Daniel Barkalow, 2009-03-07 06:11

Subject: [PATCH v2 3/5] Use the matching function to generate the match results
Message-ID: <alpine.LNX.1.00.0903070105350.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0903070105350.19665%40iabervon.org
In-Reply-To: <alpine.LNX.1.00.0903052355240.19665@iabervon.org>

```
This puts all of the interpretation of the pattern representation in a
single function for easy manipulation.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
Again, better name for the function.

 remote.c |   45 +++++++++++++++++++++++----------------------
 1 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/remote.c b/remote.c
index 2816723..5638766 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;
-- 
1.6.1.286.gd33a4.dirty

```
