Skip to content

Commit 2556b99

Browse files
moygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ba7407 commit 2556b99

File tree

8 files changed

+114
-17
lines changed

8 files changed

+114
-17
lines changed

Documentation/config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,13 @@ status.branch::
21182118
Set to true to enable --branch by default in linkgit:git-status[1].
21192119
The option --no-branch takes precedence over this variable.
21202120

2121+
status.displayCommentPrefix::
2122+
If set to true, linkgit:git-status[1] will insert a comment
2123+
prefix before each output line (starting with
2124+
`core.commentChar`, i.e. `#` by default). This was the
2125+
behavior of linkgit:git-status[1] in Git 1.8.4 and previous.
2126+
Defaults to false.
2127+
21212128
status.showUntrackedFiles::
21222129
By default, linkgit:git-status[1] and linkgit:git-commit[1] show
21232130
files which are not currently tracked by Git. Directories which

builtin/commit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
599599
const char *hook_arg2 = NULL;
600600
int ident_shown = 0;
601601
int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
602+
int old_display_comment_prefix;
602603

603604
/* This checks and barfs if author is badly specified */
604605
determine_author_info(author_ident);
@@ -696,6 +697,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
696697
if (s->fp == NULL)
697698
die_errno(_("could not open '%s'"), git_path(commit_editmsg));
698699

700+
/* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
701+
old_display_comment_prefix = s->display_comment_prefix;
702+
s->display_comment_prefix = 1;
703+
699704
if (clean_message_contents)
700705
stripspace(&sb, 0);
701706

@@ -821,6 +826,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
821826
*/
822827
if (!commitable && whence != FROM_MERGE && !allow_empty &&
823828
!(amend && is_a_merge(current_head))) {
829+
s->display_comment_prefix = old_display_comment_prefix;
824830
run_status(stdout, index_file, prefix, 0, s);
825831
if (amend)
826832
fputs(_(empty_amend_advice), stderr);
@@ -1182,6 +1188,10 @@ static int git_status_config(const char *k, const char *v, void *cb)
11821188
s->use_color = git_config_colorbool(k, v);
11831189
return 0;
11841190
}
1191+
if (!strcmp(k, "status.displaycommentprefix")) {
1192+
s->display_comment_prefix = git_config_bool(k, v);
1193+
return 0;
1194+
}
11851195
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
11861196
int slot = parse_status_slot(k, 13);
11871197
if (slot < 0)

t/t3001-ls-files-others-exclude.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ EOF
115115

116116
git config core.excludesFile excludes-file
117117

118-
git status | grep "^# " > output
118+
git -c status.displayCommentPrefix=true status | grep "^# " > output
119119

120120
cat > expect << EOF
121121
# .gitignore

t/t7060-wtstatus.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ test_description='basic work tree status reporting'
44

55
. ./test-lib.sh
66

7+
test_expect_success 'use status.displayCommentPrefix by default ' '
8+
git config --global status.displayCommentPrefix true
9+
'
10+
711
test_expect_success setup '
812
git config --global advice.statusuoption false &&
913
test_commit A &&

t/t7508-status.sh

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ test_description='git status'
77

88
. ./test-lib.sh
99

10+
test_expect_success 'use status.displayCommentPrefix by default ' '
11+
git config --global status.displayCommentPrefix true
12+
'
13+
1014
test_expect_success 'status -h in broken repository' '
1115
git config --global advice.statusuoption false &&
1216
mkdir broken &&
@@ -60,8 +64,12 @@ test_expect_success 'status (1)' '
6064
test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output
6165
'
6266

67+
strip_comments () {
68+
sed "s/^\# //; s/^\#$//; s/^#\t/\t/" <"$1" >"$1".tmp &&
69+
rm "$1" && mv "$1".tmp "$1"
70+
}
71+
6372
test_expect_success 'status --column' '
64-
COLUMNS=50 git status --column="column dense" >output &&
6573
cat >expect <<\EOF &&
6674
# On branch master
6775
# Changes to be committed:
@@ -78,9 +86,16 @@ test_expect_success 'status --column' '
7886
# Untracked files:
7987
# (use "git add <file>..." to include in what will be committed)
8088
#
81-
# dir1/untracked dir2/untracked untracked
82-
# dir2/modified output
89+
# dir1/untracked dir2/untracked output
90+
# dir2/modified expect untracked
8391
EOF
92+
COLUMNS=50 git -c status.displayCommentPrefix=true status --column="column dense" >output &&
93+
test_i18ncmp expect output
94+
'
95+
96+
test_expect_success 'status --column status.displayCommentPrefix=false' '
97+
strip_comments expect &&
98+
COLUMNS=49 git -c status.displayCommentPrefix=false status --column="column dense" >output &&
8499
test_i18ncmp expect output
85100
'
86101

@@ -108,11 +123,39 @@ cat >expect <<\EOF
108123
# untracked
109124
EOF
110125

111-
test_expect_success 'status (2)' '
112-
git status >output &&
126+
test_expect_success 'status with status.displayCommentPrefix=true' '
127+
git -c status.displayCommentPrefix=true status >output &&
128+
test_i18ncmp expect output
129+
'
130+
131+
test_expect_success 'status with status.displayCommentPrefix=false' '
132+
strip_comments expect &&
133+
git -c status.displayCommentPrefix=false status >output &&
113134
test_i18ncmp expect output
114135
'
115136

137+
test_expect_success 'setup fake editor' '
138+
cat >.git/editor <<-\EOF &&
139+
#! /bin/sh
140+
cp "$1" output
141+
EOF
142+
chmod 755 .git/editor
143+
'
144+
145+
commit_template_commented () {
146+
(
147+
EDITOR=.git/editor &&
148+
export EDITOR &&
149+
# Fails due to empty message
150+
test_must_fail git commit
151+
) &&
152+
! grep '^[^#]' output
153+
}
154+
155+
test_expect_success 'commit ignores status.displayCommentPrefix=false in COMMIT_EDITMSG' '
156+
commit_template_commented
157+
'
158+
116159
cat >expect <<\EOF
117160
# On branch master
118161
# Changes to be committed:
@@ -872,6 +915,16 @@ test_expect_success 'status submodule summary' '
872915
test_i18ncmp expect output
873916
'
874917

918+
test_expect_success 'status submodule summary with status.displayCommentPrefix=false' '
919+
strip_comments expect &&
920+
git -c status.displayCommentPrefix=false status >output &&
921+
test_i18ncmp expect output
922+
'
923+
924+
test_expect_success 'commit with submodule summary ignores status.displayCommentPrefix' '
925+
commit_template_commented
926+
'
927+
875928
cat >expect <<EOF
876929
M dir1/modified
877930
A dir2/added

t/t7512-status-help.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ test_description='git status advice'
1313

1414
set_fake_editor
1515

16+
test_expect_success 'use status.displayCommentPrefix by default ' '
17+
git config --global status.displayCommentPrefix true
18+
'
19+
1620
test_expect_success 'prepare for conflicts' '
1721
git config --global advice.statusuoption false &&
1822
test_commit init main.txt init &&

wt-status.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
4646

4747
strbuf_vaddf(&sb, fmt, ap);
4848
if (!sb.len) {
49-
strbuf_addch(&sb, comment_line_char);
50-
if (!trail)
51-
strbuf_addch(&sb, ' ');
49+
if (s->display_comment_prefix) {
50+
strbuf_addch(&sb, comment_line_char);
51+
if (!trail)
52+
strbuf_addch(&sb, ' ');
53+
}
5254
color_print_strbuf(s->fp, color, &sb);
5355
if (trail)
5456
fprintf(s->fp, "%s", trail);
@@ -59,7 +61,7 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
5961
eol = strchr(line, '\n');
6062

6163
strbuf_reset(&linebuf);
62-
if (at_bol) {
64+
if (at_bol && s->display_comment_prefix) {
6365
strbuf_addch(&linebuf, comment_line_char);
6466
if (*line != '\n' && *line != '\t')
6567
strbuf_addch(&linebuf, ' ');
@@ -129,6 +131,7 @@ void wt_status_prepare(struct wt_status *s)
129131
s->untracked.strdup_strings = 1;
130132
s->ignored.strdup_strings = 1;
131133
s->show_branch = -1; /* unspecified */
134+
s->display_comment_prefix = 0;
132135
}
133136

134137
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
707710
strbuf_addbuf(&summary, &cmd_stdout);
708711
strbuf_release(&cmd_stdout);
709712

710-
summary_content = strbuf_detach(&summary, &len);
711-
strbuf_add_commented_lines(&summary, summary_content, len);
712-
free(summary_content);
713+
if (s->display_comment_prefix) {
714+
summary_content = strbuf_detach(&summary, &len);
715+
strbuf_add_commented_lines(&summary, summary_content, len);
716+
free(summary_content);
717+
}
713718

714719
fputs(summary.buf, s->fp);
715720
strbuf_release(&summary);
@@ -748,8 +753,9 @@ static void wt_status_print_other(struct wt_status *s,
748753
if (!column_active(s->colopts))
749754
return;
750755

751-
strbuf_addf(&buf, "%s#\t%s",
756+
strbuf_addf(&buf, "%s%s\t%s",
752757
color(WT_STATUS_HEADER, s),
758+
s->display_comment_prefix ? "#" : "",
753759
color(WT_STATUS_UNTRACKED, s));
754760
memset(&copts, 0, sizeof(copts));
755761
copts.padding = 1;
@@ -793,6 +799,8 @@ static void wt_status_print_tracking(struct wt_status *s)
793799
struct strbuf sb = STRBUF_INIT;
794800
const char *cp, *ep;
795801
struct branch *branch;
802+
char comment_line_string[3];
803+
int i;
796804

797805
assert(s->branch && !s->is_initial);
798806
if (prefixcmp(s->branch, "refs/heads/"))
@@ -801,12 +809,22 @@ static void wt_status_print_tracking(struct wt_status *s)
801809
if (!format_tracking_info(branch, &sb))
802810
return;
803811

812+
i = 0;
813+
if (s->display_comment_prefix) {
814+
comment_line_string[i++] = comment_line_char;
815+
comment_line_string[i++] = ' ';
816+
}
817+
comment_line_string[i] = '\0';
818+
804819
for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
805820
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
806-
"%c %.*s", comment_line_char,
821+
"%s%.*s", comment_line_string,
807822
(int)(ep - cp), cp);
808-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
809-
comment_line_char);
823+
if (s->display_comment_prefix)
824+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
825+
comment_line_char);
826+
else
827+
fprintf_ln(s->fp, "");
810828
}
811829

812830
static int has_unmerged(struct wt_status *s)

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct wt_status {
5050
enum commit_whence whence;
5151
int nowarn;
5252
int use_color;
53+
int display_comment_prefix;
5354
int relative_paths;
5455
int submodule_summary;
5556
int show_ignored_files;

0 commit comments

Comments
 (0)