status: disable display of '#' comment prefix by default

Historically, "git status" needed to prefix each output line with '#' so
that the output could be added as comment to the commit message. This
prefix comment has no real purpose when "git status" is ran from the
command-line, and this may distract users from the real content.

Disable this prefix comment by default, and make it re-activable for
users needing backward compatibility with status.displayCommentPrefix.

Obviously, "git commit" ignores status.displayCommentPrefix and keeps the
comment unconditionnaly when writing to COMMIT_EDITMSG (but not when
writing to stdout for an error message or with --dry-run).

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Matthieu Moy 2013-09-06 19:43:07 +02:00 committed by Junio C Hamano
parent 3ba7407b8b
commit 2556b9962e
8 changed files with 114 additions and 17 deletions

View File

@ -2118,6 +2118,13 @@ status.branch::
Set to true to enable --branch by default in linkgit:git-status[1]. Set to true to enable --branch by default in linkgit:git-status[1].
The option --no-branch takes precedence over this variable. The option --no-branch takes precedence over this variable.
status.displayCommentPrefix::
If set to true, linkgit:git-status[1] will insert a comment
prefix before each output line (starting with
`core.commentChar`, i.e. `#` by default). This was the
behavior of linkgit:git-status[1] in Git 1.8.4 and previous.
Defaults to false.
status.showUntrackedFiles:: status.showUntrackedFiles::
By default, linkgit:git-status[1] and linkgit:git-commit[1] show By default, linkgit:git-status[1] and linkgit:git-commit[1] show
files which are not currently tracked by Git. Directories which files which are not currently tracked by Git. Directories which

View File

@ -599,6 +599,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
const char *hook_arg2 = NULL; const char *hook_arg2 = NULL;
int ident_shown = 0; int ident_shown = 0;
int clean_message_contents = (cleanup_mode != CLEANUP_NONE); int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
int old_display_comment_prefix;
/* This checks and barfs if author is badly specified */ /* This checks and barfs if author is badly specified */
determine_author_info(author_ident); determine_author_info(author_ident);
@ -696,6 +697,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
if (s->fp == NULL) if (s->fp == NULL)
die_errno(_("could not open '%s'"), git_path(commit_editmsg)); die_errno(_("could not open '%s'"), git_path(commit_editmsg));
/* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
old_display_comment_prefix = s->display_comment_prefix;
s->display_comment_prefix = 1;
if (clean_message_contents) if (clean_message_contents)
stripspace(&sb, 0); stripspace(&sb, 0);
@ -821,6 +826,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
*/ */
if (!commitable && whence != FROM_MERGE && !allow_empty && if (!commitable && whence != FROM_MERGE && !allow_empty &&
!(amend && is_a_merge(current_head))) { !(amend && is_a_merge(current_head))) {
s->display_comment_prefix = old_display_comment_prefix;
run_status(stdout, index_file, prefix, 0, s); run_status(stdout, index_file, prefix, 0, s);
if (amend) if (amend)
fputs(_(empty_amend_advice), stderr); fputs(_(empty_amend_advice), stderr);
@ -1182,6 +1188,10 @@ static int git_status_config(const char *k, const char *v, void *cb)
s->use_color = git_config_colorbool(k, v); s->use_color = git_config_colorbool(k, v);
return 0; return 0;
} }
if (!strcmp(k, "status.displaycommentprefix")) {
s->display_comment_prefix = git_config_bool(k, v);
return 0;
}
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
int slot = parse_status_slot(k, 13); int slot = parse_status_slot(k, 13);
if (slot < 0) if (slot < 0)

View File

@ -115,7 +115,7 @@ EOF
git config core.excludesFile excludes-file git config core.excludesFile excludes-file
git status | grep "^# " > output git -c status.displayCommentPrefix=true status | grep "^# " > output
cat > expect << EOF cat > expect << EOF
# .gitignore # .gitignore

View File

@ -4,6 +4,10 @@ test_description='basic work tree status reporting'
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'use status.displayCommentPrefix by default ' '
git config --global status.displayCommentPrefix true
'
test_expect_success setup ' test_expect_success setup '
git config --global advice.statusuoption false && git config --global advice.statusuoption false &&
test_commit A && test_commit A &&

View File

@ -7,6 +7,10 @@ test_description='git status'
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'use status.displayCommentPrefix by default ' '
git config --global status.displayCommentPrefix true
'
test_expect_success 'status -h in broken repository' ' test_expect_success 'status -h in broken repository' '
git config --global advice.statusuoption false && git config --global advice.statusuoption false &&
mkdir broken && mkdir broken &&
@ -60,8 +64,12 @@ test_expect_success 'status (1)' '
test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output
' '
strip_comments () {
sed "s/^\# //; s/^\#$//; s/^#\t/\t/" <"$1" >"$1".tmp &&
rm "$1" && mv "$1".tmp "$1"
}
test_expect_success 'status --column' ' test_expect_success 'status --column' '
COLUMNS=50 git status --column="column dense" >output &&
cat >expect <<\EOF && cat >expect <<\EOF &&
# On branch master # On branch master
# Changes to be committed: # Changes to be committed:
@ -78,9 +86,16 @@ test_expect_success 'status --column' '
# Untracked files: # Untracked files:
# (use "git add <file>..." to include in what will be committed) # (use "git add <file>..." to include in what will be committed)
# #
# dir1/untracked dir2/untracked untracked # dir1/untracked dir2/untracked output
# dir2/modified output # dir2/modified expect untracked
EOF EOF
COLUMNS=50 git -c status.displayCommentPrefix=true status --column="column dense" >output &&
test_i18ncmp expect output
'
test_expect_success 'status --column status.displayCommentPrefix=false' '
strip_comments expect &&
COLUMNS=49 git -c status.displayCommentPrefix=false status --column="column dense" >output &&
test_i18ncmp expect output test_i18ncmp expect output
' '
@ -108,11 +123,39 @@ cat >expect <<\EOF
# untracked # untracked
EOF EOF
test_expect_success 'status (2)' ' test_expect_success 'status with status.displayCommentPrefix=true' '
git status >output && git -c status.displayCommentPrefix=true status >output &&
test_i18ncmp expect output test_i18ncmp expect output
' '
test_expect_success 'status with status.displayCommentPrefix=false' '
strip_comments expect &&
git -c status.displayCommentPrefix=false status >output &&
test_i18ncmp expect output
'
test_expect_success 'setup fake editor' '
cat >.git/editor <<-\EOF &&
#! /bin/sh
cp "$1" output
EOF
chmod 755 .git/editor
'
commit_template_commented () {
(
EDITOR=.git/editor &&
export EDITOR &&
# Fails due to empty message
test_must_fail git commit
) &&
! grep '^[^#]' output
}
test_expect_success 'commit ignores status.displayCommentPrefix=false in COMMIT_EDITMSG' '
commit_template_commented
'
cat >expect <<\EOF cat >expect <<\EOF
# On branch master # On branch master
# Changes to be committed: # Changes to be committed:
@ -872,6 +915,16 @@ test_expect_success 'status submodule summary' '
test_i18ncmp expect output test_i18ncmp expect output
' '
test_expect_success 'status submodule summary with status.displayCommentPrefix=false' '
strip_comments expect &&
git -c status.displayCommentPrefix=false status >output &&
test_i18ncmp expect output
'
test_expect_success 'commit with submodule summary ignores status.displayCommentPrefix' '
commit_template_commented
'
cat >expect <<EOF cat >expect <<EOF
M dir1/modified M dir1/modified
A dir2/added A dir2/added

View File

@ -13,6 +13,10 @@ test_description='git status advice'
set_fake_editor set_fake_editor
test_expect_success 'use status.displayCommentPrefix by default ' '
git config --global status.displayCommentPrefix true
'
test_expect_success 'prepare for conflicts' ' test_expect_success 'prepare for conflicts' '
git config --global advice.statusuoption false && git config --global advice.statusuoption false &&
test_commit init main.txt init && test_commit init main.txt init &&

View File

@ -46,9 +46,11 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
strbuf_vaddf(&sb, fmt, ap); strbuf_vaddf(&sb, fmt, ap);
if (!sb.len) { if (!sb.len) {
strbuf_addch(&sb, comment_line_char); if (s->display_comment_prefix) {
if (!trail) strbuf_addch(&sb, comment_line_char);
strbuf_addch(&sb, ' '); if (!trail)
strbuf_addch(&sb, ' ');
}
color_print_strbuf(s->fp, color, &sb); color_print_strbuf(s->fp, color, &sb);
if (trail) if (trail)
fprintf(s->fp, "%s", trail); fprintf(s->fp, "%s", trail);
@ -59,7 +61,7 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
eol = strchr(line, '\n'); eol = strchr(line, '\n');
strbuf_reset(&linebuf); strbuf_reset(&linebuf);
if (at_bol) { if (at_bol && s->display_comment_prefix) {
strbuf_addch(&linebuf, comment_line_char); strbuf_addch(&linebuf, comment_line_char);
if (*line != '\n' && *line != '\t') if (*line != '\n' && *line != '\t')
strbuf_addch(&linebuf, ' '); strbuf_addch(&linebuf, ' ');
@ -129,6 +131,7 @@ void wt_status_prepare(struct wt_status *s)
s->untracked.strdup_strings = 1; s->untracked.strdup_strings = 1;
s->ignored.strdup_strings = 1; s->ignored.strdup_strings = 1;
s->show_branch = -1; /* unspecified */ s->show_branch = -1; /* unspecified */
s->display_comment_prefix = 0;
} }
static void wt_status_print_unmerged_header(struct wt_status *s) static void wt_status_print_unmerged_header(struct wt_status *s)
@ -707,9 +710,11 @@ static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitt
strbuf_addbuf(&summary, &cmd_stdout); strbuf_addbuf(&summary, &cmd_stdout);
strbuf_release(&cmd_stdout); strbuf_release(&cmd_stdout);
summary_content = strbuf_detach(&summary, &len); if (s->display_comment_prefix) {
strbuf_add_commented_lines(&summary, summary_content, len); summary_content = strbuf_detach(&summary, &len);
free(summary_content); strbuf_add_commented_lines(&summary, summary_content, len);
free(summary_content);
}
fputs(summary.buf, s->fp); fputs(summary.buf, s->fp);
strbuf_release(&summary); strbuf_release(&summary);
@ -748,8 +753,9 @@ static void wt_status_print_other(struct wt_status *s,
if (!column_active(s->colopts)) if (!column_active(s->colopts))
return; return;
strbuf_addf(&buf, "%s#\t%s", strbuf_addf(&buf, "%s%s\t%s",
color(WT_STATUS_HEADER, s), color(WT_STATUS_HEADER, s),
s->display_comment_prefix ? "#" : "",
color(WT_STATUS_UNTRACKED, s)); color(WT_STATUS_UNTRACKED, s));
memset(&copts, 0, sizeof(copts)); memset(&copts, 0, sizeof(copts));
copts.padding = 1; copts.padding = 1;
@ -793,6 +799,8 @@ 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;
struct branch *branch; struct branch *branch;
char comment_line_string[3];
int i;
assert(s->branch && !s->is_initial); assert(s->branch && !s->is_initial);
if (prefixcmp(s->branch, "refs/heads/")) if (prefixcmp(s->branch, "refs/heads/"))
@ -801,12 +809,22 @@ static void wt_status_print_tracking(struct wt_status *s)
if (!format_tracking_info(branch, &sb)) if (!format_tracking_info(branch, &sb))
return; return;
i = 0;
if (s->display_comment_prefix) {
comment_line_string[i++] = comment_line_char;
comment_line_string[i++] = ' ';
}
comment_line_string[i] = '\0';
for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
"%c %.*s", comment_line_char, "%s%.*s", comment_line_string,
(int)(ep - cp), cp); (int)(ep - cp), cp);
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c", if (s->display_comment_prefix)
comment_line_char); color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
comment_line_char);
else
fprintf_ln(s->fp, "");
} }
static int has_unmerged(struct wt_status *s) static int has_unmerged(struct wt_status *s)

View File

@ -50,6 +50,7 @@ struct wt_status {
enum commit_whence whence; enum commit_whence whence;
int nowarn; int nowarn;
int use_color; int use_color;
int display_comment_prefix;
int relative_paths; int relative_paths;
int submodule_summary; int submodule_summary;
int show_ignored_files; int show_ignored_files;