Skip to content

Commit 475722d

Browse files
committed
commit -v: add --word-diff opt to commit template
`--word-diff` was available for the `diff` command, but not for the `commit` one, whereas it is very useful when updating column-based data-format like CSV for example. Only support the PORCELAIN mode for now, but if deemed necessary can add plain too, I don't think COLOR makes sense when outputting a file. Signed-off-by: kraktus <rinses-riotous8n@icloud.com>
1 parent e895506 commit 475722d

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

Documentation/git-commit.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ If specified twice, show in addition the unified diff between
371371
what would be committed and the worktree files, i.e. the unstaged
372372
changes to tracked files.
373373

374+
`--word-diff`::
375+
Show a word diff instead of a line diff in the verbose commit
376+
template (requires `-v`).
377+
374378
`-q`::
375379
`--quiet`::
376380
Suppress commit summary message.

builtin/commit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ static int all, also, interactive, patch_interactive, only, amend, signoff;
126126
static struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
127127
static int edit_flag = -1; /* unspecified */
128128
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
129+
static int commit_word_diff;
129130
static int config_commit_verbose = -1; /* unspecified */
130131
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
131132
static const char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
@@ -573,6 +574,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
573574
s->reference = "HEAD^1";
574575
}
575576
s->verbose = verbose;
577+
s->word_diff = commit_word_diff;
576578
s->index_file = index_file;
577579
s->fp = fp;
578580
s->nowarn = nowarn;
@@ -1705,6 +1707,8 @@ int cmd_commit(int argc,
17051707
static struct option builtin_commit_options[] = {
17061708
OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
17071709
OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1710+
OPT_BOOL(0, "word-diff", &commit_word_diff,
1711+
N_("show word diff in verbose commit template")),
17081712

17091713
OPT_GROUP(N_("Commit message options")),
17101714
OPT_FILENAME('F', "file", &logfile, N_("read message from file")),

t/t7507-commit-verbose.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,49 @@ test_expect_success "status ignores commit.verbose=true" '
166166
! grep "^diff --git actual"
167167
'
168168

169+
write_script "check-for-word-diff" <<\EOF
170+
cp "$1" template_out
171+
exit 0
172+
EOF
173+
test_set_editor "$PWD/check-for-word-diff"
174+
175+
test_expect_success 'setup for word-diff tests' '
176+
echo "the quick brown fox" >wordfile &&
177+
git add wordfile &&
178+
git commit -F message &&
179+
echo "the slow brown fox" >wordfile &&
180+
git add wordfile &&
181+
git commit -F message
182+
'
183+
184+
test_expect_success '--word-diff with -v shows word diff' '
185+
git commit --amend -v --word-diff &&
186+
grep "^-quick$" template_out &&
187+
grep "^+slow$" template_out
188+
'
189+
190+
test_expect_success '--word-diff without -v is a no-op' '
191+
git commit --amend --word-diff &&
192+
! grep "^~$" template_out
193+
'
194+
195+
test_expect_success '-v -v --word-diff shows word diff in both sections' '
196+
echo "the fast brown fox" >wordfile &&
197+
git commit --amend -v -v --word-diff &&
198+
grep "^-quick$" template_out &&
199+
grep "^+slow$" template_out &&
200+
grep "^-slow$" template_out &&
201+
grep "^+fast$" template_out
202+
'
203+
204+
test_expect_success 'word-diff markers stripped from saved commit message' '
205+
git commit --amend -v --word-diff &&
206+
check_message message
207+
'
208+
209+
test_expect_success 'no --word-diff produces line diff without markers' '
210+
git commit --amend -v &&
211+
! grep "^~$" template_out
212+
'
213+
169214
test_done

wt-status.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,8 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
11771177
rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
11781178
rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
11791179
rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1180+
if (s->word_diff)
1181+
rev.diffopt.word_diff = DIFF_WORDS_PORCELAIN;
11801182
rev.diffopt.file = s->fp;
11811183
rev.diffopt.close_file = 0;
11821184
/*

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct wt_status {
130130
int detect_rename;
131131
int rename_score;
132132
int rename_limit;
133+
int word_diff;
133134
enum wt_status_format status_format;
134135
unsigned char added_cut_line; /* boolean */
135136
struct wt_status_state state;

0 commit comments

Comments
 (0)