Skip to content

Commit 48c5847

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a1ad0eb commit 48c5847

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

sha1_name.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,35 +1061,36 @@ static void set_shortened_ref(struct strbuf *buf, const char *ref)
10611061
free(s);
10621062
}
10631063

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 *))
10821069
{
10831070
int len;
1071+
struct branch *branch;
1072+
struct strbuf err = STRBUF_INIT;
1073+
const char *value;
10841074

1085-
len = upstream_mark(name + at, namelen - at);
1075+
len = get_mark(name + at, namelen - at);
10861076
if (!len)
10871077
return -1;
10881078

10891079
if (memchr(name, ':', at))
10901080
return -1;
10911081

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);
10931094
return len + at;
10941095
}
10951096

@@ -1140,7 +1141,8 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
11401141
if (len > 0)
11411142
return reinterpret(name, namelen, len, buf);
11421143

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);
11441146
if (len > 0)
11451147
return len;
11461148
}

0 commit comments

Comments
 (0)