Skip to content

Commit db70a04

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1d8b1f commit db70a04

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

remote.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,10 +1110,11 @@ static int match_explicit_refs(struct ref *src, struct ref *dst,
11101110
return errs;
11111111
}
11121112

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)
11161115
{
1116+
const struct refspec *pat;
1117+
char *name;
11171118
int i;
11181119
int matching_refs = -1;
11191120
for (i = 0; i < rs_nr; i++) {
@@ -1123,14 +1124,31 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
11231124
continue;
11241125
}
11251126

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+
}
11291134
}
1130-
if (matching_refs != -1)
1131-
return rs + matching_refs;
1132-
else
1135+
if (matching_refs == -1)
11331136
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;
11341152
}
11351153

11361154
static struct ref **tail_ref(struct ref **head)
@@ -1171,36 +1189,19 @@ int match_push_refs(struct ref *src, struct ref **dst,
11711189
struct ref *dst_peer;
11721190
const struct refspec *pat = NULL;
11731191
char *dst_name;
1192+
11741193
if (ref->peer_ref)
11751194
continue;
11761195

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)
11791198
continue;
11801199

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-
}
11981200
dst_peer = find_ref_by_name(*dst, dst_name);
11991201
if (dst_peer) {
12001202
if (dst_peer->peer_ref)
12011203
/* We're already sending something to this ref. */
12021204
goto free_name;
1203-
12041205
} else {
12051206
if (pat->matching && !(send_all || send_mirror))
12061207
/*

0 commit comments

Comments
 (0)