sha1_name: refactor interpret_upstream_mark
Now that most of the logic for our local get_upstream_branch has been pushed into the generic branch_get_upstream, we can fold the remainder into interpret_upstream_mark. Furthermore, what remains is generic to any branch-related "@{foo}" we might add in the future, and there's enough boilerplate that we'd like to reuse it. Let's parameterize the two operations (parsing the mark and computing its value) so that we can reuse this for "@{push}" in the near future. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
May 21, 2015 at 00:45 UTC
48c58471c2d4d7293272448a18801cd27555f6b5
1 file changed
+23
-21
sha1_name.c
+23
-21
@@ -1061,35 +1061,36 @@ static void set_shortened_ref(struct strbuf *buf, const char *ref)
1061
free(s);
1062
}
1063
1064
-static const char *get_upstream_branch(const char *branch_buf, int len)
1065
-{
1066
- char *branch = xstrndup(branch_buf, len);
1067
- struct branch *upstream = branch_get(*branch ? branch : NULL);
1068
- struct strbuf err = STRBUF_INIT;
1069
- const char *ret;
1070
-
1071
- free(branch);
1072
-
1073
- ret = branch_get_upstream(upstream, &err);
1074
- if (!ret)
1075
- die("%s", err.buf);
1076
-
1077
- return ret;
1078
-}
1079
-
1080
-static int interpret_upstream_mark(const char *name, int namelen,
1081
- int at, struct strbuf *buf)
1064
+static int interpret_branch_mark(const char *name, int namelen,
1065
+ int at, struct strbuf *buf,
1066
+ int (*get_mark)(const char *, int),
1067
+ const char *(*get_data)(struct branch *,
1068
+ struct strbuf *))
1069
{
1070
int len;
1071
+ struct branch *branch;
1072
+ struct strbuf err = STRBUF_INIT;
1073
+ const char *value;
1074
1085
- len = upstream_mark(name + at, namelen - at);
1075
+ len = get_mark(name + at, namelen - at);
1076
if (!len)
1077
return -1;
1078
1079
if (memchr(name, ':', at))
1080
return -1;
1081
1092
- set_shortened_ref(buf, get_upstream_branch(name, at));
1082
+ if (at) {
1083
+ char *name_str = xmemdupz(name, at);
1084
+ branch = branch_get(name_str);
1085
+ free(name_str);
1086
+ } else
1087
+ branch = branch_get(NULL);
1088
+
1089
+ value = get_data(branch, &err);
1090
+ if (!value)
1091
+ die("%s", err.buf);
1092
+
1093
+ set_shortened_ref(buf, value);
1094
return len + at;
1095
}
1096
@@ -1140,7 +1141,8 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
1141
if (len > 0)
1142
return reinterpret(name, namelen, len, buf);
1143
1143
- len = interpret_upstream_mark(name, namelen, at - name, buf);
1144
+ len = interpret_branch_mark(name, namelen, at - name, buf,
1145
+ upstream_mark, branch_get_upstream);
1146
if (len > 0)
1147
return len;
1148
}