match_explicit: hoist refspec lhs checks into their own function
In preparation for being able to check the left-hand side of our push refspecs separately, this pulls the examination of them out into its own function. There should be no behavior change. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 5, 2014 at 14:03 UTC
f7ade3d36bdffe79c12b563154028f700bb8ed3d
1 file changed
+30
-19
remote.c
+30
-19
@@ -1096,12 +1096,36 @@ static char *guess_ref(const char *name, struct ref *peer)
1096
return strbuf_detach(&buf, NULL);
1097
}
1098
1099
+static int match_explicit_lhs(struct ref *src,
1100
+ struct refspec *rs,
1101
+ struct ref **match,
1102
+ int *allocated_match)
1103
+{
1104
+ switch (count_refspec_match(rs->src, src, match)) {
1105
+ case 1:
1106
+ *allocated_match = 0;
1107
+ return 0;
1108
+ case 0:
1109
+ /* The source could be in the get_sha1() format
1110
+ * not a reference name. :refs/other is a
1111
+ * way to delete 'other' ref at the remote end.
1112
+ */
1113
+ *match = try_explicit_object_name(rs->src);
1114
+ if (!*match)
1115
+ return error("src refspec %s does not match any.", rs->src);
1116
+ *allocated_match = 1;
1117
+ return 0;
1118
+ default:
1119
+ return error("src refspec %s matches more than one.", rs->src);
1120
+ }
1121
+}
1122
+
1123
static int match_explicit(struct ref *src, struct ref *dst,
1124
struct ref ***dst_tail,
1125
struct refspec *rs)
1126
{
1127
struct ref *matched_src, *matched_dst;
1104
- int copy_src;
1128
+ int allocated_src;
1129
1130
const char *dst_value = rs->dst;
1131
char *dst_guess;
@@ -1110,23 +1134,8 @@ static int match_explicit(struct ref *src, struct ref *dst,
1134
return 0;
1135
1136
matched_src = matched_dst = NULL;
1113
- switch (count_refspec_match(rs->src, src, &matched_src)) {
1114
- case 1:
1115
- copy_src = 1;
1116
- break;
1117
- case 0:
1118
- /* The source could be in the get_sha1() format
1119
- * not a reference name. :refs/other is a
1120
- * way to delete 'other' ref at the remote end.
1121
- */
1122
- matched_src = try_explicit_object_name(rs->src);
1123
- if (!matched_src)
1124
- return error("src refspec %s does not match any.", rs->src);
1125
- copy_src = 0;
1126
- break;
1127
- default:
1128
- return error("src refspec %s matches more than one.", rs->src);
1129
- }
1137
+ if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
1138
+ return -1;
1139
1140
if (!dst_value) {
1141
unsigned char sha1[20];
@@ -1171,7 +1180,9 @@ static int match_explicit(struct ref *src, struct ref *dst,
1180
return error("dst ref %s receives from more than one src.",
1181
matched_dst->name);
1182
else {
1174
- matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src;
1183
+ matched_dst->peer_ref = allocated_src ?
1184
+ matched_src :
1185
+ copy_ref(matched_src);
1186
matched_dst->force = rs->force;
1187
}
1188
return 0;