Skip to content

Commit

Permalink
Merge branch 'jc/diff-irreversible-delete'
Browse files Browse the repository at this point in the history
* jc/diff-irreversible-delete:
  git diff -D: omit the preimage of deletes
  • Loading branch information
Junio C Hamano committed Apr 28, 2011
2 parents 76a89d6 + 467ddc1 commit 50d3062
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Documentation/diff-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,19 @@ endif::git-log[]
projects, so use it with caution. Giving more than one
`-C` option has the same effect.

-D::
--irreversible-delete::
Omit the preimage for deletes, i.e. print only the header but not
the diff between the preimage and `/dev/null`. The resulting patch
is not meant to be applied with `patch` nor `git apply`; this is
solely for people who want to just concentrate on reviewing the
text after the change. In addition, the output obviously lack
enough information to apply such a patch in reverse, even manually,
hence the name of the option.
+
When used together with `-B`, omit also the preimage in the deletion part
of a delete/create pair.

-l<num>::
The `-M` and `-C` options require O(n^2) processing time where n
is the number of potential rename/copy targets. This
Expand Down
19 changes: 14 additions & 5 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,14 @@ static void emit_rewrite_diff(const char *name_a,
line_prefix, metainfo, a_name.buf, name_a_tab, reset,
line_prefix, metainfo, b_name.buf, name_b_tab, reset,
line_prefix, fraginfo);
print_line_count(o->file, lc_a);
if (!o->irreversible_delete)
print_line_count(o->file, lc_a);
else
fprintf(o->file, "?,?");
fprintf(o->file, " +");
print_line_count(o->file, lc_b);
fprintf(o->file, " @@%s\n", reset);
if (lc_a)
if (lc_a && !o->irreversible_delete)
emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
if (lc_b)
emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Expand Down Expand Up @@ -1981,7 +1984,11 @@ static void builtin_diff(const char *name_a,
}
}

if (!DIFF_OPT_TST(o, TEXT) &&
if (o->irreversible_delete && lbl[1][0] == '/') {
fprintf(o->file, "%s", header.buf);
strbuf_reset(&header);
goto free_ab_and_return;
} else if (!DIFF_OPT_TST(o, TEXT) &&
( (!textconv_one && diff_filespec_is_binary(one)) ||
(!textconv_two && diff_filespec_is_binary(two)) )) {
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
Expand All @@ -2001,8 +2008,7 @@ static void builtin_diff(const char *name_a,
fprintf(o->file, "%sBinary files %s and %s differ\n",
line_prefix, lbl[0], lbl[1]);
o->found_changes = 1;
}
else {
} else {
/* Crazy xdl interfaces.. */
const char *diffopts = getenv("GIT_DIFF_OPTS");
xpparam_t xpp;
Expand Down Expand Up @@ -3200,6 +3206,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
return error("invalid argument to -M: %s", arg+2);
options->detect_rename = DIFF_DETECT_RENAME;
}
else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) {
options->irreversible_delete = 1;
}
else if (!prefixcmp(arg, "-C") || !prefixcmp(arg, "--find-copies=") ||
!strcmp(arg, "--find-copies")) {
if (options->detect_rename == DIFF_DETECT_COPY)
Expand Down
1 change: 1 addition & 0 deletions diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct diff_options {
int interhunkcontext;
int break_opt;
int detect_rename;
int irreversible_delete;
int skip_stat_unmatch;
int line_termination;
int output_format;
Expand Down
43 changes: 42 additions & 1 deletion t/t4022-diff-rewrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ test_expect_success setup '
tr \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM" \
<"$TEST_DIRECTORY"/../COPYING >test
<"$TEST_DIRECTORY"/../COPYING >test &&
echo "to be deleted" >test2 &&
git add test2
'

Expand All @@ -25,5 +27,44 @@ test_expect_success 'detect rewrite' '
'

cat >expect <<EOF
diff --git a/test2 b/test2
deleted file mode 100644
index 4202011..0000000
--- a/test2
+++ /dev/null
@@ -1 +0,0 @@
-to be deleted
EOF
test_expect_success 'show deletion diff without -D' '
rm test2 &&
git diff -- test2 >actual &&
test_cmp expect actual
'

cat >expect <<EOF
diff --git a/test2 b/test2
deleted file mode 100644
index 4202011..0000000
EOF
test_expect_success 'suppress deletion diff with -D' '
git diff -D -- test2 >actual &&
test_cmp expect actual
'

test_expect_success 'show deletion diff with -B' '
git diff -B -- test >actual &&
grep "Linus Torvalds" actual
'

test_expect_success 'suppress deletion diff with -B -D' '
git diff -B -D -- test >actual &&
grep -v "Linus Torvalds" actual
'

test_done

0 comments on commit 50d3062

Please sign in to comment.