Skip to content

Commit db43891

Browse files
committed
Merge branch 'rs/wt-status-detached-branch-fix' into maint
"git status --branch --short" accessed beyond the constant string "HEAD", which has been corrected. * rs/wt-status-detached-branch-fix: wt-status: use skip_prefix() to get rid of magic string length constants wt-status: don't skip a magical number of characters blindly wt-status: avoid building bogus branch name with detached HEAD wt-status: exit early using goto in wt_shortstatus_print_tracking() t7060: add test for status --branch on a detached HEAD
2 parents f97aee1 + c72b49d commit db43891

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

t/t7060-wtstatus.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,19 @@ EOF
213213
git checkout master
214214
'
215215

216+
test_expect_success 'status --branch with detached HEAD' '
217+
git reset --hard &&
218+
git checkout master^0 &&
219+
git status --branch --porcelain >actual &&
220+
cat >expected <<-EOF &&
221+
## HEAD (no branch)
222+
?? .gitconfig
223+
?? actual
224+
?? expect
225+
?? expected
226+
?? mdconflict/
227+
EOF
228+
test_i18ncmp expected actual
229+
'
216230

217231
test_done

wt-status.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,15 @@ static void wt_status_print_verbose(struct wt_status *s)
897897
static void wt_status_print_tracking(struct wt_status *s)
898898
{
899899
struct strbuf sb = STRBUF_INIT;
900-
const char *cp, *ep;
900+
const char *cp, *ep, *branch_name;
901901
struct branch *branch;
902902
char comment_line_string[3];
903903
int i;
904904

905905
assert(s->branch && !s->is_initial);
906-
if (!starts_with(s->branch, "refs/heads/"))
906+
if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
907907
return;
908-
branch = branch_get(s->branch + 11);
908+
branch = branch_get(branch_name);
909909
if (!format_tracking_info(branch, &sb))
910910
return;
911911

@@ -1268,6 +1268,7 @@ static char *read_and_strip_branch(const char *path)
12681268
{
12691269
struct strbuf sb = STRBUF_INIT;
12701270
unsigned char sha1[20];
1271+
const char *branch_name;
12711272

12721273
if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
12731274
goto got_nothing;
@@ -1276,8 +1277,8 @@ static char *read_and_strip_branch(const char *path)
12761277
strbuf_setlen(&sb, sb.len - 1);
12771278
if (!sb.len)
12781279
goto got_nothing;
1279-
if (starts_with(sb.buf, "refs/heads/"))
1280-
strbuf_remove(&sb,0, strlen("refs/heads/"));
1280+
if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1281+
strbuf_remove(&sb, 0, branch_name - sb.buf);
12811282
else if (starts_with(sb.buf, "refs/"))
12821283
;
12831284
else if (!get_sha1_hex(sb.buf, sha1)) {
@@ -1308,9 +1309,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
13081309
struct grab_1st_switch_cbdata *cb = cb_data;
13091310
const char *target = NULL, *end;
13101311

1311-
if (!starts_with(message, "checkout: moving from "))
1312+
if (!skip_prefix(message, "checkout: moving from ", &message))
13121313
return 0;
1313-
message += strlen("checkout: moving from ");
13141314
target = strstr(message, " to ");
13151315
if (!target)
13161316
return 0;
@@ -1348,14 +1348,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
13481348
/* perhaps sha1 is a tag, try to dereference to a commit */
13491349
((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
13501350
!hashcmp(cb.nsha1, commit->object.sha1)))) {
1351-
int ofs;
1352-
if (starts_with(ref, "refs/tags/"))
1353-
ofs = strlen("refs/tags/");
1354-
else if (starts_with(ref, "refs/remotes/"))
1355-
ofs = strlen("refs/remotes/");
1356-
else
1357-
ofs = 0;
1358-
state->detached_from = xstrdup(ref + ofs);
1351+
const char *from = ref;
1352+
if (!skip_prefix(from, "refs/tags/", &from))
1353+
skip_prefix(from, "refs/remotes/", &from);
1354+
state->detached_from = xstrdup(from);
13591355
} else
13601356
state->detached_from =
13611357
xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
@@ -1442,9 +1438,7 @@ void wt_status_print(struct wt_status *s)
14421438
if (s->branch) {
14431439
const char *on_what = _("On branch ");
14441440
const char *branch_name = s->branch;
1445-
if (starts_with(branch_name, "refs/heads/"))
1446-
branch_name += 11;
1447-
else if (!strcmp(branch_name, "HEAD")) {
1441+
if (!strcmp(branch_name, "HEAD")) {
14481442
branch_status_color = color(WT_STATUS_NOBRANCH, s);
14491443
if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
14501444
if (state.rebase_interactive_in_progress)
@@ -1462,7 +1456,8 @@ void wt_status_print(struct wt_status *s)
14621456
branch_name = "";
14631457
on_what = _("Not currently on any branch.");
14641458
}
1465-
}
1459+
} else
1460+
skip_prefix(branch_name, "refs/heads/", &branch_name);
14661461
status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
14671462
status_printf_more(s, branch_status_color, "%s", on_what);
14681463
status_printf_more(s, branch_color, "%s\n", branch_name);
@@ -1644,24 +1639,24 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
16441639
return;
16451640
branch_name = s->branch;
16461641

1647-
if (starts_with(branch_name, "refs/heads/"))
1648-
branch_name += 11;
1649-
else if (!strcmp(branch_name, "HEAD")) {
1650-
branch_name = _("HEAD (no branch)");
1651-
branch_color_local = color(WT_STATUS_NOBRANCH, s);
1652-
}
1653-
1654-
branch = branch_get(s->branch + 11);
16551642
if (s->is_initial)
16561643
color_fprintf(s->fp, header_color, _("Initial commit on "));
16571644

1645+
if (!strcmp(s->branch, "HEAD")) {
1646+
color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1647+
_("HEAD (no branch)"));
1648+
goto conclude;
1649+
}
1650+
1651+
skip_prefix(branch_name, "refs/heads/", &branch_name);
1652+
1653+
branch = branch_get(branch_name);
1654+
16581655
color_fprintf(s->fp, branch_color_local, "%s", branch_name);
16591656

16601657
if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
1661-
if (!base) {
1662-
fputc(s->null_termination ? '\0' : '\n', s->fp);
1663-
return;
1664-
}
1658+
if (!base)
1659+
goto conclude;
16651660

16661661
upstream_is_gone = 1;
16671662
}
@@ -1671,10 +1666,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
16711666
color_fprintf(s->fp, branch_color_remote, "%s", base);
16721667
free((char *)base);
16731668

1674-
if (!upstream_is_gone && !num_ours && !num_theirs) {
1675-
fputc(s->null_termination ? '\0' : '\n', s->fp);
1676-
return;
1677-
}
1669+
if (!upstream_is_gone && !num_ours && !num_theirs)
1670+
goto conclude;
16781671

16791672
#define LABEL(string) (s->no_gettext ? (string) : _(string))
16801673

@@ -1695,6 +1688,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
16951688
}
16961689

16971690
color_fprintf(s->fp, header_color, "]");
1691+
conclude:
16981692
fputc(s->null_termination ? '\0' : '\n', s->fp);
16991693
}
17001694

0 commit comments

Comments
 (0)