@matheusbsbs / matheus-bagliano / commits / e928213fb4
Fork 0

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 <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Mar 7, 2009 at 01:11 UTC e928213fb40c106650dca2632b5e830cfaffb86a
1 file changed +23 -22
remote.c
+23 -22
@@ -719,9 +719,19 @@ int remote_has_url(struct remote *remote, const char *url)
719 return 0;
720 }
721
722 -static int match_name_with_pattern(const char *key, const char *name)
722 +static int match_name_with_pattern(const char *key, const char *name,
723 + const char *value, char **result)
724 {
724 - int ret = !prefixcmp(key, name);
725 + size_t klen = strlen(key);
726 + int ret = !strncmp(key, name, klen);
727 + if (ret && value) {
728 + size_t vlen = strlen(value);
729 + *result = xmalloc(vlen +
730 + strlen(name) -
731 + klen + 1);
732 + strcpy(*result, value);
733 + strcpy(*result + vlen, name + klen);
734 + }
735 return ret;
736 }
737
@@ -748,13 +758,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
758 if (!fetch->dst)
759 continue;
760 if (fetch->pattern) {
751 - if (match_name_with_pattern(key, needle)) {
752 - *result = xmalloc(strlen(value) +
753 - strlen(needle) -
754 - strlen(key) + 1);
755 - strcpy(*result, value);
756 - strcpy(*result + strlen(value),
757 - needle + strlen(key));
761 + if (match_name_with_pattern(key, needle, value, result)) {
762 refspec->force = fetch->force;
763 return 0;
764 }
@@ -1026,7 +1030,8 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
1030 continue;
1031 }
1032
1029 - if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name))
1033 + if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1034 + NULL, NULL))
1035 return rs + i;
1036 }
1037 if (matching_refs != -1)
@@ -1080,11 +1085,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
1085
1086 } else {
1087 const char *dst_side = pat->dst ? pat->dst : pat->src;
1083 - dst_name = xmalloc(strlen(dst_side) +
1084 - strlen(src->name) -
1085 - strlen(pat->src) + 2);
1086 - strcpy(dst_name, dst_side);
1087 - strcat(dst_name, src->name + strlen(pat->src));
1088 + if (!match_name_with_pattern(pat->src, src->name,
1089 + dst_side, &dst_name))
1090 + die("Didn't think it matches any more");
1091 }
1092 dst_peer = find_ref_by_name(dst, dst_name);
1093 if (dst_peer) {
@@ -1160,19 +1163,17 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
1163 struct ref *ret = NULL;
1164 struct ref **tail = &ret;
1165
1163 - int remote_prefix_len = strlen(refspec->src);
1164 - int local_prefix_len = strlen(refspec->dst);
1166 + char *expn_name;
1167
1168 for (ref = remote_refs; ref; ref = ref->next) {
1169 if (strchr(ref->name, '^'))
1170 continue; /* a dereference item */
1169 - if (match_name_with_pattern(refspec->src, ref->name)) {
1170 - const char *match;
1171 + if (match_name_with_pattern(refspec->src, ref->name,
1172 + refspec->dst, &expn_name)) {
1173 struct ref *cpy = copy_ref(ref);
1172 - match = ref->name + remote_prefix_len;
1174
1174 - cpy->peer_ref = alloc_ref_with_prefix(refspec->dst,
1175 - local_prefix_len, match);
1175 + cpy->peer_ref = alloc_ref(expn_name);
1176 + free(expn_name);
1177 if (refspec->force)
1178 cpy->peer_ref->force = 1;
1179 *tail = cpy;