Skip to content

Commit a39c14a

Browse files
peffgitster
authored andcommitted
interpret_branch_name: factor out upstream handling
This function checks a few different @{}-constructs. The early part checks for and dispatches us to helpers for each construct, but the code for handling @{upstream} is inline. Let's factor this out into its own function. This makes interpret_branch_name more readable, and will make it much simpler to further refactor the function in future patches. While we're at it, let's also break apart the refactored code into a few helper functions. These will be useful if we eventually implement similar @{upstream}-like constructs. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4224916 commit a39c14a

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

sha1_name.c

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,54 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
10481048
return ret - used + len;
10491049
}
10501050

1051+
static void set_shortened_ref(struct strbuf *buf, const char *ref)
1052+
{
1053+
char *s = shorten_unambiguous_ref(ref, 0);
1054+
strbuf_reset(buf);
1055+
strbuf_addstr(buf, s);
1056+
free(s);
1057+
}
1058+
1059+
static const char *get_upstream_branch(const char *branch_buf, int len)
1060+
{
1061+
char *branch = xstrndup(branch_buf, len);
1062+
struct branch *upstream = branch_get(*branch ? branch : NULL);
1063+
1064+
/*
1065+
* Upstream can be NULL only if branch refers to HEAD and HEAD
1066+
* points to something different than a branch.
1067+
*/
1068+
if (!upstream)
1069+
die(_("HEAD does not point to a branch"));
1070+
if (!upstream->merge || !upstream->merge[0]->dst) {
1071+
if (!ref_exists(upstream->refname))
1072+
die(_("No such branch: '%s'"), branch);
1073+
if (!upstream->merge) {
1074+
die(_("No upstream configured for branch '%s'"),
1075+
upstream->name);
1076+
}
1077+
die(
1078+
_("Upstream branch '%s' not stored as a remote-tracking branch"),
1079+
upstream->merge[0]->src);
1080+
}
1081+
free(branch);
1082+
1083+
return upstream->merge[0]->dst;
1084+
}
1085+
1086+
static int interpret_upstream_mark(const char *name, int namelen,
1087+
int at, struct strbuf *buf)
1088+
{
1089+
int len;
1090+
1091+
len = upstream_mark(name + at, namelen - at);
1092+
if (!len)
1093+
return -1;
1094+
1095+
set_shortened_ref(buf, get_upstream_branch(name, at));
1096+
return len + at;
1097+
}
1098+
10511099
/*
10521100
* This reads short-hand syntax that not only evaluates to a commit
10531101
* object name, but also can act as if the end user spelled the name
@@ -1072,9 +1120,7 @@ static int reinterpret(const char *name, int namelen, int len, struct strbuf *bu
10721120
int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
10731121
{
10741122
char *cp;
1075-
struct branch *upstream;
10761123
int len = interpret_nth_prior_checkout(name, buf);
1077-
int tmp_len;
10781124

10791125
if (!namelen)
10801126
namelen = strlen(name);
@@ -1096,36 +1142,11 @@ int interpret_branch_name(const char *name, int namelen, struct strbuf *buf)
10961142
if (len > 0)
10971143
return reinterpret(name, namelen, len, buf);
10981144

1099-
tmp_len = upstream_mark(cp, namelen - (cp - name));
1100-
if (!tmp_len)
1101-
return -1;
1145+
len = interpret_upstream_mark(name, namelen, cp - name, buf);
1146+
if (len > 0)
1147+
return len;
11021148

1103-
len = cp + tmp_len - name;
1104-
cp = xstrndup(name, cp - name);
1105-
upstream = branch_get(*cp ? cp : NULL);
1106-
/*
1107-
* Upstream can be NULL only if cp refers to HEAD and HEAD
1108-
* points to something different than a branch.
1109-
*/
1110-
if (!upstream)
1111-
die(_("HEAD does not point to a branch"));
1112-
if (!upstream->merge || !upstream->merge[0]->dst) {
1113-
if (!ref_exists(upstream->refname))
1114-
die(_("No such branch: '%s'"), cp);
1115-
if (!upstream->merge) {
1116-
die(_("No upstream configured for branch '%s'"),
1117-
upstream->name);
1118-
}
1119-
die(
1120-
_("Upstream branch '%s' not stored as a remote-tracking branch"),
1121-
upstream->merge[0]->src);
1122-
}
1123-
free(cp);
1124-
cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
1125-
strbuf_reset(buf);
1126-
strbuf_addstr(buf, cp);
1127-
free(cp);
1128-
return len;
1149+
return -1;
11291150
}
11301151

11311152
int strbuf_branchname(struct strbuf *sb, const char *name)

0 commit comments

Comments
 (0)