提交 db43891c 编写于 作者: J Junio C Hamano

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
...@@ -213,5 +213,19 @@ EOF ...@@ -213,5 +213,19 @@ EOF
git checkout master git checkout master
' '
test_expect_success 'status --branch with detached HEAD' '
git reset --hard &&
git checkout master^0 &&
git status --branch --porcelain >actual &&
cat >expected <<-EOF &&
## HEAD (no branch)
?? .gitconfig
?? actual
?? expect
?? expected
?? mdconflict/
EOF
test_i18ncmp expected actual
'
test_done test_done
...@@ -897,15 +897,15 @@ static void wt_status_print_verbose(struct wt_status *s) ...@@ -897,15 +897,15 @@ static void wt_status_print_verbose(struct wt_status *s)
static void wt_status_print_tracking(struct wt_status *s) static void wt_status_print_tracking(struct wt_status *s)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
const char *cp, *ep; const char *cp, *ep, *branch_name;
struct branch *branch; struct branch *branch;
char comment_line_string[3]; char comment_line_string[3];
int i; int i;
assert(s->branch && !s->is_initial); assert(s->branch && !s->is_initial);
if (!starts_with(s->branch, "refs/heads/")) if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
return; return;
branch = branch_get(s->branch + 11); branch = branch_get(branch_name);
if (!format_tracking_info(branch, &sb)) if (!format_tracking_info(branch, &sb))
return; return;
...@@ -1268,6 +1268,7 @@ static char *read_and_strip_branch(const char *path) ...@@ -1268,6 +1268,7 @@ static char *read_and_strip_branch(const char *path)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
unsigned char sha1[20]; unsigned char sha1[20];
const char *branch_name;
if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0) if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
goto got_nothing; goto got_nothing;
...@@ -1276,8 +1277,8 @@ static char *read_and_strip_branch(const char *path) ...@@ -1276,8 +1277,8 @@ static char *read_and_strip_branch(const char *path)
strbuf_setlen(&sb, sb.len - 1); strbuf_setlen(&sb, sb.len - 1);
if (!sb.len) if (!sb.len)
goto got_nothing; goto got_nothing;
if (starts_with(sb.buf, "refs/heads/")) if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
strbuf_remove(&sb,0, strlen("refs/heads/")); strbuf_remove(&sb, 0, branch_name - sb.buf);
else if (starts_with(sb.buf, "refs/")) else if (starts_with(sb.buf, "refs/"))
; ;
else if (!get_sha1_hex(sb.buf, sha1)) { else if (!get_sha1_hex(sb.buf, sha1)) {
...@@ -1308,9 +1309,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1, ...@@ -1308,9 +1309,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
struct grab_1st_switch_cbdata *cb = cb_data; struct grab_1st_switch_cbdata *cb = cb_data;
const char *target = NULL, *end; const char *target = NULL, *end;
if (!starts_with(message, "checkout: moving from ")) if (!skip_prefix(message, "checkout: moving from ", &message))
return 0; return 0;
message += strlen("checkout: moving from ");
target = strstr(message, " to "); target = strstr(message, " to ");
if (!target) if (!target)
return 0; return 0;
...@@ -1348,14 +1348,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state) ...@@ -1348,14 +1348,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
/* perhaps sha1 is a tag, try to dereference to a commit */ /* perhaps sha1 is a tag, try to dereference to a commit */
((commit = lookup_commit_reference_gently(sha1, 1)) != NULL && ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
!hashcmp(cb.nsha1, commit->object.sha1)))) { !hashcmp(cb.nsha1, commit->object.sha1)))) {
int ofs; const char *from = ref;
if (starts_with(ref, "refs/tags/")) if (!skip_prefix(from, "refs/tags/", &from))
ofs = strlen("refs/tags/"); skip_prefix(from, "refs/remotes/", &from);
else if (starts_with(ref, "refs/remotes/")) state->detached_from = xstrdup(from);
ofs = strlen("refs/remotes/");
else
ofs = 0;
state->detached_from = xstrdup(ref + ofs);
} else } else
state->detached_from = state->detached_from =
xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV)); xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
...@@ -1442,9 +1438,7 @@ void wt_status_print(struct wt_status *s) ...@@ -1442,9 +1438,7 @@ void wt_status_print(struct wt_status *s)
if (s->branch) { if (s->branch) {
const char *on_what = _("On branch "); const char *on_what = _("On branch ");
const char *branch_name = s->branch; const char *branch_name = s->branch;
if (starts_with(branch_name, "refs/heads/")) if (!strcmp(branch_name, "HEAD")) {
branch_name += 11;
else if (!strcmp(branch_name, "HEAD")) {
branch_status_color = color(WT_STATUS_NOBRANCH, s); branch_status_color = color(WT_STATUS_NOBRANCH, s);
if (state.rebase_in_progress || state.rebase_interactive_in_progress) { if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
if (state.rebase_interactive_in_progress) if (state.rebase_interactive_in_progress)
...@@ -1462,7 +1456,8 @@ void wt_status_print(struct wt_status *s) ...@@ -1462,7 +1456,8 @@ void wt_status_print(struct wt_status *s)
branch_name = ""; branch_name = "";
on_what = _("Not currently on any branch."); on_what = _("Not currently on any branch.");
} }
} } else
skip_prefix(branch_name, "refs/heads/", &branch_name);
status_printf(s, color(WT_STATUS_HEADER, s), "%s", ""); status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
status_printf_more(s, branch_status_color, "%s", on_what); status_printf_more(s, branch_status_color, "%s", on_what);
status_printf_more(s, branch_color, "%s\n", branch_name); status_printf_more(s, branch_color, "%s\n", branch_name);
...@@ -1644,24 +1639,24 @@ static void wt_shortstatus_print_tracking(struct wt_status *s) ...@@ -1644,24 +1639,24 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
return; return;
branch_name = s->branch; branch_name = s->branch;
if (starts_with(branch_name, "refs/heads/"))
branch_name += 11;
else if (!strcmp(branch_name, "HEAD")) {
branch_name = _("HEAD (no branch)");
branch_color_local = color(WT_STATUS_NOBRANCH, s);
}
branch = branch_get(s->branch + 11);
if (s->is_initial) if (s->is_initial)
color_fprintf(s->fp, header_color, _("Initial commit on ")); color_fprintf(s->fp, header_color, _("Initial commit on "));
if (!strcmp(s->branch, "HEAD")) {
color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
_("HEAD (no branch)"));
goto conclude;
}
skip_prefix(branch_name, "refs/heads/", &branch_name);
branch = branch_get(branch_name);
color_fprintf(s->fp, branch_color_local, "%s", branch_name); color_fprintf(s->fp, branch_color_local, "%s", branch_name);
if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) { if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
if (!base) { if (!base)
fputc(s->null_termination ? '\0' : '\n', s->fp); goto conclude;
return;
}
upstream_is_gone = 1; upstream_is_gone = 1;
} }
...@@ -1671,10 +1666,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s) ...@@ -1671,10 +1666,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
color_fprintf(s->fp, branch_color_remote, "%s", base); color_fprintf(s->fp, branch_color_remote, "%s", base);
free((char *)base); free((char *)base);
if (!upstream_is_gone && !num_ours && !num_theirs) { if (!upstream_is_gone && !num_ours && !num_theirs)
fputc(s->null_termination ? '\0' : '\n', s->fp); goto conclude;
return;
}
#define LABEL(string) (s->no_gettext ? (string) : _(string)) #define LABEL(string) (s->no_gettext ? (string) : _(string))
...@@ -1695,6 +1688,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s) ...@@ -1695,6 +1688,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
} }
color_fprintf(s->fp, header_color, "]"); color_fprintf(s->fp, header_color, "]");
conclude:
fputc(s->null_termination ? '\0' : '\n', s->fp); fputc(s->null_termination ? '\0' : '\n', s->fp);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册