Skip to content

Commit 50d3062

Browse files
committed
Merge branch 'jc/diff-irreversible-delete'
* jc/diff-irreversible-delete: git diff -D: omit the preimage of deletes
2 parents 76a89d6 + 467ddc1 commit 50d3062

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

Documentation/diff-options.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ endif::git-log[]
263263
projects, so use it with caution. Giving more than one
264264
`-C` option has the same effect.
265265

266+
-D::
267+
--irreversible-delete::
268+
Omit the preimage for deletes, i.e. print only the header but not
269+
the diff between the preimage and `/dev/null`. The resulting patch
270+
is not meant to be applied with `patch` nor `git apply`; this is
271+
solely for people who want to just concentrate on reviewing the
272+
text after the change. In addition, the output obviously lack
273+
enough information to apply such a patch in reverse, even manually,
274+
hence the name of the option.
275+
+
276+
When used together with `-B`, omit also the preimage in the deletion part
277+
of a delete/create pair.
278+
266279
-l<num>::
267280
The `-M` and `-C` options require O(n^2) processing time where n
268281
is the number of potential rename/copy targets. This

diff.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,14 @@ static void emit_rewrite_diff(const char *name_a,
581581
line_prefix, metainfo, a_name.buf, name_a_tab, reset,
582582
line_prefix, metainfo, b_name.buf, name_b_tab, reset,
583583
line_prefix, fraginfo);
584-
print_line_count(o->file, lc_a);
584+
if (!o->irreversible_delete)
585+
print_line_count(o->file, lc_a);
586+
else
587+
fprintf(o->file, "?,?");
585588
fprintf(o->file, " +");
586589
print_line_count(o->file, lc_b);
587590
fprintf(o->file, " @@%s\n", reset);
588-
if (lc_a)
591+
if (lc_a && !o->irreversible_delete)
589592
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
590593
if (lc_b)
591594
emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
@@ -1981,7 +1984,11 @@ static void builtin_diff(const char *name_a,
19811984
}
19821985
}
19831986

1984-
if (!DIFF_OPT_TST(o, TEXT) &&
1987+
if (o->irreversible_delete && lbl[1][0] == '/') {
1988+
fprintf(o->file, "%s", header.buf);
1989+
strbuf_reset(&header);
1990+
goto free_ab_and_return;
1991+
} else if (!DIFF_OPT_TST(o, TEXT) &&
19851992
( (!textconv_one && diff_filespec_is_binary(one)) ||
19861993
(!textconv_two && diff_filespec_is_binary(two)) )) {
19871994
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
@@ -2001,8 +2008,7 @@ static void builtin_diff(const char *name_a,
20012008
fprintf(o->file, "%sBinary files %s and %s differ\n",
20022009
line_prefix, lbl[0], lbl[1]);
20032010
o->found_changes = 1;
2004-
}
2005-
else {
2011+
} else {
20062012
/* Crazy xdl interfaces.. */
20072013
const char *diffopts = getenv("GIT_DIFF_OPTS");
20082014
xpparam_t xpp;
@@ -3200,6 +3206,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
32003206
return error("invalid argument to -M: %s", arg+2);
32013207
options->detect_rename = DIFF_DETECT_RENAME;
32023208
}
3209+
else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) {
3210+
options->irreversible_delete = 1;
3211+
}
32033212
else if (!prefixcmp(arg, "-C") || !prefixcmp(arg, "--find-copies=") ||
32043213
!strcmp(arg, "--find-copies")) {
32053214
if (options->detect_rename == DIFF_DETECT_COPY)

diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct diff_options {
104104
int interhunkcontext;
105105
int break_opt;
106106
int detect_rename;
107+
int irreversible_delete;
107108
int skip_stat_unmatch;
108109
int line_termination;
109110
int output_format;

t/t4022-diff-rewrite.sh

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ test_expect_success setup '
1111
tr \
1212
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \
1313
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM" \
14-
<"$TEST_DIRECTORY"/../COPYING >test
14+
<"$TEST_DIRECTORY"/../COPYING >test &&
15+
echo "to be deleted" >test2 &&
16+
git add test2
1517
1618
'
1719

@@ -25,5 +27,44 @@ test_expect_success 'detect rewrite' '
2527
2628
'
2729

30+
cat >expect <<EOF
31+
diff --git a/test2 b/test2
32+
deleted file mode 100644
33+
index 4202011..0000000
34+
--- a/test2
35+
+++ /dev/null
36+
@@ -1 +0,0 @@
37+
-to be deleted
38+
EOF
39+
test_expect_success 'show deletion diff without -D' '
40+
41+
rm test2 &&
42+
git diff -- test2 >actual &&
43+
test_cmp expect actual
44+
'
45+
46+
cat >expect <<EOF
47+
diff --git a/test2 b/test2
48+
deleted file mode 100644
49+
index 4202011..0000000
50+
EOF
51+
test_expect_success 'suppress deletion diff with -D' '
52+
53+
git diff -D -- test2 >actual &&
54+
test_cmp expect actual
55+
'
56+
57+
test_expect_success 'show deletion diff with -B' '
58+
59+
git diff -B -- test >actual &&
60+
grep "Linus Torvalds" actual
61+
'
62+
63+
test_expect_success 'suppress deletion diff with -B -D' '
64+
65+
git diff -B -D -- test >actual &&
66+
grep -v "Linus Torvalds" actual
67+
'
68+
2869
test_done
2970

0 commit comments

Comments
 (0)