remote: reorganize check_pattern_match()

The function match_name_with_pattern() is called twice, once to see if a pattern matches with the name, and again to learn what the matched pattern maps the name to. Since check_pattern_match() is only used in one place, we can just reorganize it to make a single call and fetch the values at the same time. This changes the meaning of check_pattern_match() that used to check which pattern in the array of refspecs matched the given ref, to return the name of the remote ref the given ref is mapped to. Rename it to get_ref_match() which actually describes more closely what it's actually doing now. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Feb 23, 2012 at 00:43 UTC db70a04cbb343a58e7be93e7f581d049cf901b26
1 file changed +30 -29
remote.c
+30 -29
@@ -1110,10 +1110,11 @@ static int match_explicit_refs(struct ref *src, struct ref *dst,
1110 return errs;
1111 }
1112
1113 -static const struct refspec *check_pattern_match(const struct refspec *rs,
1114 - int rs_nr,
1115 - const struct ref *src)
1113 +static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref *ref,
1114 + int send_mirror, const struct refspec **ret_pat)
1115 {
1116 + const struct refspec *pat;
1117 + char *name;
1118 int i;
1119 int matching_refs = -1;
1120 for (i = 0; i < rs_nr; i++) {
@@ -1123,14 +1124,31 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
1124 continue;
1125 }
1126
1126 - if (rs[i].pattern && match_name_with_pattern(rs[i].src, src->name,
1127 - NULL, NULL))
1128 - return rs + i;
1127 + if (rs[i].pattern) {
1128 + const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src;
1129 + if (match_name_with_pattern(rs[i].src, ref->name, dst_side, &name)) {
1130 + matching_refs = i;
1131 + break;
1132 + }
1133 + }
1134 }
1130 - if (matching_refs != -1)
1131 - return rs + matching_refs;
1132 - else
1135 + if (matching_refs == -1)
1136 return NULL;
1137 +
1138 + pat = rs + matching_refs;
1139 + if (pat->matching) {
1140 + /*
1141 + * "matching refs"; traditionally we pushed everything
1142 + * including refs outside refs/heads/ hierarchy, but
1143 + * that does not make much sense these days.
1144 + */
1145 + if (!send_mirror && prefixcmp(ref->name, "refs/heads/"))
1146 + return NULL;
1147 + name = xstrdup(ref->name);
1148 + }
1149 + if (ret_pat)
1150 + *ret_pat = pat;
1151 + return name;
1152 }
1153
1154 static struct ref **tail_ref(struct ref **head)
@@ -1171,36 +1189,19 @@ int match_push_refs(struct ref *src, struct ref **dst,
1189 struct ref *dst_peer;
1190 const struct refspec *pat = NULL;
1191 char *dst_name;
1192 +
1193 if (ref->peer_ref)
1194 continue;
1195
1177 - pat = check_pattern_match(rs, nr_refspec, ref);
1178 - if (!pat)
1196 + dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, &pat);
1197 + if (!dst_name)
1198 continue;
1199
1181 - if (pat->matching) {
1182 - /*
1183 - * "matching refs"; traditionally we pushed everything
1184 - * including refs outside refs/heads/ hierarchy, but
1185 - * that does not make much sense these days.
1186 - */
1187 - if (!send_mirror && prefixcmp(ref->name, "refs/heads/"))
1188 - continue;
1189 - dst_name = xstrdup(ref->name);
1190 -
1191 -
1192 - } else {
1193 - const char *dst_side = pat->dst ? pat->dst : pat->src;
1194 - if (!match_name_with_pattern(pat->src, ref->name,
1195 - dst_side, &dst_name))
1196 - die("Didn't think it matches any more");
1197 - }
1200 dst_peer = find_ref_by_name(*dst, dst_name);
1201 if (dst_peer) {
1202 if (dst_peer->peer_ref)
1203 /* We're already sending something to this ref. */
1204 goto free_name;
1203 -
1205 } else {
1206 if (pat->matching && !(send_all || send_mirror))
1207 /*