Skip to content

Commit da15d84

Browse files
jrnavar
authored andcommitted
commit: add "#" characters to commit advice automatically
Let the text pass through gettext, but the functional part not do so. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
1 parent 394a9d1 commit da15d84

File tree

2 files changed

+112
-68
lines changed

2 files changed

+112
-68
lines changed

builtin/commit.c

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,47 @@ static char *cut_ident_timestamp_part(char *string)
559559
return ket;
560560
}
561561

562+
__attribute__((format(printf, 4, 5)))
563+
void fprintf_commented(FILE *fp, int mid_line, const char *color, const char *fmt, ...)
564+
{
565+
struct strbuf sb = STRBUF_INIT;
566+
struct strbuf linebuf = STRBUF_INIT;
567+
const char *line, *eol;
568+
va_list ap;
569+
570+
va_start(ap, fmt);
571+
strbuf_vaddf(&sb, fmt, ap);
572+
va_end(ap);
573+
574+
if (!*sb.buf) {
575+
color_fprintf(fp, color, "# ");
576+
return;
577+
}
578+
for (line = sb.buf; ; line = eol + 1) {
579+
eol = strchr(line, '\n');
580+
581+
if (!eol && !*line)
582+
break; /* "...\n" */
583+
strbuf_reset(&linebuf);
584+
if (!mid_line) {
585+
strbuf_addch(&linebuf, '#');
586+
if (*line && *line != '\t')
587+
strbuf_addch(&linebuf, ' ');
588+
}
589+
if (eol) {
590+
strbuf_add(&linebuf, line, eol - line);
591+
color_fprintf_ln(fp, color, "%s", linebuf.buf);
592+
} else {
593+
strbuf_addstr(&linebuf, line);
594+
color_fprintf(fp, color, "%s", linebuf.buf);
595+
break;
596+
}
597+
mid_line = 0;
598+
}
599+
strbuf_release(&linebuf);
600+
strbuf_release(&sb);
601+
}
602+
562603
static int prepare_to_commit(const char *index_file, const char *prefix,
563604
struct wt_status *s,
564605
struct strbuf *author_ident)
@@ -695,50 +736,50 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
695736
if (use_editor && include_status) {
696737
char *ai_tmp, *ci_tmp;
697738
if (in_merge)
698-
fprintf(fp,
699-
_("#\n"
700-
"# It looks like you may be committing a MERGE.\n"
701-
"# If this is not correct, please remove the file\n"
702-
"# %s\n"
703-
"# and try again.\n"
704-
"#\n"),
739+
fprintf_commented(fp, 0, "",
740+
_("\n"
741+
"It looks like you may be committing a MERGE.\n"
742+
"If this is not correct, please remove the file\n"
743+
" %s\n"
744+
"and try again.\n"
745+
"\n"),
705746
git_path("MERGE_HEAD"));
706747

707-
fprintf(fp,
708-
_("\n"
709-
"# Please enter the commit message for your changes."));
748+
fprintf(fp, "\n");
749+
fprintf_commented(fp, 0, "",
750+
_("Please enter the commit message for your changes."));
710751
if (cleanup_mode == CLEANUP_ALL)
711-
fprintf(fp,
752+
fprintf_commented(fp, 1, "",
712753
_(" Lines starting\n"
713-
"# with '#' will be ignored, and an empty"
754+
"with '#' will be ignored, and an empty"
714755
" message aborts the commit.\n"));
715756
else /* CLEANUP_SPACE, that is. */
716-
fprintf(fp,
757+
fprintf_commented(fp, 1, "",
717758
_(" Lines starting\n"
718-
"# with '#' will be kept; you may remove them"
759+
"with '#' will be kept; you may remove them"
719760
" yourself if you want to.\n"
720-
"# An empty message aborts the commit.\n"));
761+
"An empty message aborts the commit.\n"));
721762
if (only_include_assumed)
722-
fprintf(fp, "# %s\n", only_include_assumed);
763+
fprintf_commented(fp, 0, "", "%s\n", only_include_assumed);
723764

724765
ai_tmp = cut_ident_timestamp_part(author_ident->buf);
725766
ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
726767
if (strcmp(author_ident->buf, committer_ident.buf))
727-
fprintf(fp,
768+
fprintf_commented(fp, 0, "",
728769
_("%s"
729-
"# Author: %s\n"),
730-
ident_shown++ ? "" : "#\n",
770+
"Author: %s\n"),
771+
ident_shown++ ? "" : "\n",
731772
author_ident->buf);
732773

733774
if (!user_ident_sufficiently_given())
734-
fprintf(fp,
775+
fprintf_commented(fp, 0, "",
735776
_("%s"
736-
"# Committer: %s\n"),
737-
ident_shown++ ? "" : "#\n",
777+
"Committer: %s\n"),
778+
ident_shown++ ? "" : "\n",
738779
committer_ident.buf);
739780

740781
if (ident_shown)
741-
fprintf(fp, "#\n");
782+
fprintf_commented(fp, 0, "", "\n");
742783

743784
saved_color_setting = s->use_color;
744785
s->use_color = 0;

wt-status.c

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "refs.h"
1313
#include "submodule.h"
1414

15+
__attribute__((format(printf, 4, 5)))
16+
extern void fprintf_commented(FILE *fp, int mid_line, const char *color, const char *fmt, ...);
17+
1518
static char default_wt_status_colors[][COLOR_MAXLEN] = {
1619
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
1720
GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
@@ -57,33 +60,33 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
5760
{
5861
const char *c = color(WT_STATUS_HEADER, s);
5962

60-
color_fprintf_ln(s->fp, c, _("# Unmerged paths:"));
63+
fprintf_commented(s->fp, 0, c, _("Unmerged paths:\n"));
6164
if (!advice_status_hints)
6265
return;
6366
if (s->in_merge)
6467
;
6568
else if (!s->is_initial)
66-
color_fprintf_ln(s->fp, c, _("# (use \"git reset %s <file>...\" to unstage)"), s->reference);
69+
fprintf_commented(s->fp, 0, c, _(" (use \"git reset %s <file>...\" to unstage)\n"), s->reference);
6770
else
68-
color_fprintf_ln(s->fp, c, _("# (use \"git rm --cached <file>...\" to unstage)"));
69-
color_fprintf_ln(s->fp, c, _("# (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
70-
color_fprintf_ln(s->fp, c, "#");
71+
fprintf_commented(s->fp, 0, c, _(" (use \"git rm --cached <file>...\" to unstage)\n"));
72+
fprintf_commented(s->fp, 0, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)\n"));
73+
fprintf_commented(s->fp, 0, c, "\n");
7174
}
7275

7376
static void wt_status_print_cached_header(struct wt_status *s)
7477
{
7578
const char *c = color(WT_STATUS_HEADER, s);
7679

77-
color_fprintf_ln(s->fp, c, _("# Changes to be committed:"));
80+
fprintf_commented(s->fp, 0, c, _("Changes to be committed:\n"));
7881
if (!advice_status_hints)
7982
return;
8083
if (s->in_merge)
8184
; /* NEEDSWORK: use "git reset --unresolve"??? */
8285
else if (!s->is_initial)
83-
color_fprintf_ln(s->fp, c, _("# (use \"git reset %s <file>...\" to unstage)"), s->reference);
86+
fprintf_commented(s->fp, 0, c, _(" (use \"git reset %s <file>...\" to unstage)\n"), s->reference);
8487
else
85-
color_fprintf_ln(s->fp, c, _("# (use \"git rm --cached <file>...\" to unstage)"));
86-
color_fprintf_ln(s->fp, c, "#");
88+
fprintf_commented(s->fp, 0, c, _(" (use \"git rm --cached <file>...\" to unstage)\n"));
89+
fprintf_commented(s->fp, 0, c, "\n");
8790
}
8891

8992
static void wt_status_print_dirty_header(struct wt_status *s,
@@ -92,34 +95,34 @@ static void wt_status_print_dirty_header(struct wt_status *s,
9295
{
9396
const char *c = color(WT_STATUS_HEADER, s);
9497

95-
color_fprintf_ln(s->fp, c, _("# Changes not staged for commit:"));
98+
fprintf_commented(s->fp, 0, c, _("Changes not staged for commit:\n"));
9699
if (!advice_status_hints)
97100
return;
98101
if (!has_deleted)
99-
color_fprintf_ln(s->fp, c, _("# (use \"git add <file>...\" to update what will be committed)"));
102+
fprintf_commented(s->fp, 0, c, _(" (use \"git add <file>...\" to update what will be committed)\n"));
100103
else
101-
color_fprintf_ln(s->fp, c, _("# (use \"git add/rm <file>...\" to update what will be committed)"));
102-
color_fprintf_ln(s->fp, c, _("# (use \"git checkout -- <file>...\" to discard changes in working directory)"));
104+
fprintf_commented(s->fp, 0, c, _(" (use \"git add/rm <file>...\" to update what will be committed)\n"));
105+
fprintf_commented(s->fp, 0, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)\n"));
103106
if (has_dirty_submodules)
104-
color_fprintf_ln(s->fp, c, _("# (commit or discard the untracked or modified content in submodules)"));
105-
color_fprintf_ln(s->fp, c, "#");
107+
fprintf_commented(s->fp, 0, c, _(" (commit or discard the untracked or modified content in submodules)\n"));
108+
fprintf_commented(s->fp, 0, c, "\n");
106109
}
107110

108111
static void wt_status_print_other_header(struct wt_status *s,
109112
const char *what,
110113
const char *how)
111114
{
112115
const char *c = color(WT_STATUS_HEADER, s);
113-
color_fprintf_ln(s->fp, c, _("# %s files:"), what);
116+
fprintf_commented(s->fp, 0, c, _("%s files:\n"), what);
114117
if (!advice_status_hints)
115118
return;
116-
color_fprintf_ln(s->fp, c, _("# (use \"git %s <file>...\" to include in what will be committed)"), how);
117-
color_fprintf_ln(s->fp, c, "#");
119+
fprintf_commented(s->fp, 0, c, _(" (use \"git %s <file>...\" to include in what will be committed)\n"), how);
120+
fprintf_commented(s->fp, 0, c, "\n");
118121
}
119122

120123
static void wt_status_print_trailer(struct wt_status *s)
121124
{
122-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
125+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\n");
123126
}
124127

125128
#define quote_path quote_path_relative
@@ -133,7 +136,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s,
133136
const char *one, *how = _("bug");
134137

135138
one = quote_path(it->string, -1, &onebuf, s->prefix);
136-
color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
139+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\t");
137140
switch (d->stagemask) {
138141
case 1: how = _("both deleted:"); break;
139142
case 2: how = _("added by us:"); break;
@@ -143,7 +146,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s,
143146
case 6: how = _("both added:"); break;
144147
case 7: how = _("both modified:"); break;
145148
}
146-
color_fprintf(s->fp, c, "%-20s%s\n", how, one);
149+
fprintf_commented(s->fp, 1, c, "%-20s%s\n", how, one);
147150
strbuf_release(&onebuf);
148151
}
149152

@@ -186,40 +189,40 @@ static void wt_status_print_change_data(struct wt_status *s,
186189
one = quote_path(one_name, -1, &onebuf, s->prefix);
187190
two = quote_path(two_name, -1, &twobuf, s->prefix);
188191

189-
color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
192+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\t");
190193
switch (status) {
191194
case DIFF_STATUS_ADDED:
192-
color_fprintf(s->fp, c, _("new file: %s"), one);
195+
fprintf_commented(s->fp, 1, c, _("new file: %s"), one);
193196
break;
194197
case DIFF_STATUS_COPIED:
195-
color_fprintf(s->fp, c, _("copied: %s -> %s"), one, two);
198+
fprintf_commented(s->fp, 1, c, _("copied: %s -> %s"), one, two);
196199
break;
197200
case DIFF_STATUS_DELETED:
198-
color_fprintf(s->fp, c, _("deleted: %s"), one);
201+
fprintf_commented(s->fp, 1, c, _("deleted: %s"), one);
199202
break;
200203
case DIFF_STATUS_MODIFIED:
201-
color_fprintf(s->fp, c, _("modified: %s"), one);
204+
fprintf_commented(s->fp, 1, c, _("modified: %s"), one);
202205
break;
203206
case DIFF_STATUS_RENAMED:
204-
color_fprintf(s->fp, c, _("renamed: %s -> %s"), one, two);
207+
fprintf_commented(s->fp, 1, c, _("renamed: %s -> %s"), one, two);
205208
break;
206209
case DIFF_STATUS_TYPE_CHANGED:
207-
color_fprintf(s->fp, c, _("typechange: %s"), one);
210+
fprintf_commented(s->fp, 1, c, _("typechange: %s"), one);
208211
break;
209212
case DIFF_STATUS_UNKNOWN:
210-
color_fprintf(s->fp, c, _("unknown: %s"), one);
213+
fprintf_commented(s->fp, 1, c, _("unknown: %s"), one);
211214
break;
212215
case DIFF_STATUS_UNMERGED:
213-
color_fprintf(s->fp, c, _("unmerged: %s"), one);
216+
fprintf_commented(s->fp, 1, c, _("unmerged: %s"), one);
214217
break;
215218
default:
216219
die(_("bug: unhandled diff status %c"), status);
217220
}
218221
if (extra.len) {
219-
color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
222+
fprintf_commented(s->fp, 1, color(WT_STATUS_HEADER, s), "%s", extra.buf);
220223
strbuf_release(&extra);
221224
}
222-
fprintf(s->fp, "\n");
225+
fprintf_commented(s->fp, 1, "", "\n");
223226
strbuf_release(&onebuf);
224227
strbuf_release(&twobuf);
225228
}
@@ -573,8 +576,8 @@ static void wt_status_print_other(struct wt_status *s,
573576
for (i = 0; i < l->nr; i++) {
574577
struct string_list_item *it;
575578
it = &(l->items[i]);
576-
color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
577-
color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
579+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\t");
580+
fprintf_commented(s->fp, 1, color(WT_STATUS_UNTRACKED, s), "%s\n",
578581
quote_path(it->string, strlen(it->string),
579582
&buf, s->prefix));
580583
}
@@ -622,9 +625,9 @@ static void wt_status_print_tracking(struct wt_status *s)
622625
return;
623626

624627
for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
625-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
626-
"# %.*s", (int)(ep - cp), cp);
627-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
628+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s),
629+
"%.*s\n", (int)(ep - cp), cp);
630+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\n");
628631
}
629632

630633
void wt_status_print(struct wt_status *s)
@@ -642,17 +645,17 @@ void wt_status_print(struct wt_status *s)
642645
branch_status_color = color(WT_STATUS_NOBRANCH, s);
643646
on_what = _("Not currently on any branch.");
644647
}
645-
color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
646-
color_fprintf(s->fp, branch_status_color, "%s", on_what);
647-
color_fprintf_ln(s->fp, branch_color, "%s", branch_name);
648+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "");
649+
fprintf_commented(s->fp, 1, branch_status_color, "%s", on_what);
650+
fprintf_commented(s->fp, 1, branch_color, "%s\n", branch_name);
648651
if (!s->is_initial)
649652
wt_status_print_tracking(s);
650653
}
651654

652655
if (s->is_initial) {
653-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
654-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), _("# Initial commit"));
655-
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
656+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\n");
657+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), _("Initial commit\n"));
658+
fprintf_commented(s->fp, 0, color(WT_STATUS_HEADER, s), "\n");
656659
}
657660

658661
wt_status_print_updated(s);
@@ -669,15 +672,15 @@ void wt_status_print(struct wt_status *s)
669672
if (s->show_ignored_files)
670673
wt_status_print_other(s, &s->ignored, _("Ignored"), "add -f");
671674
} else if (s->commitable)
672-
fprintf(s->fp, _("# Untracked files not listed%s\n"),
675+
fprintf_commented(s->fp, 0, _("Untracked files not listed%s\n"),
673676
advice_status_hints
674677
? _(" (use -u option to show untracked files)") : "");
675678

676679
if (s->verbose)
677680
wt_status_print_verbose(s);
678681
if (!s->commitable) {
679682
if (s->amend)
680-
fprintf(s->fp, _("# No changes\n"));
683+
fprintf_commented(s->fp, 0, "", _("No changes\n"));
681684
else if (s->nowarn)
682685
; /* nothing */
683686
else if (s->workdir_dirty)

0 commit comments

Comments
 (0)