Skip to content

Commit 5dc1308

Browse files
committed
Merge branch 'js/patience-diff'
* js/patience-diff: bash completions: Add the --patience option Introduce the diff option '--patience' Implement the patience diff algorithm Conflicts: contrib/completion/git-completion.bash
2 parents f3d6073 + cc54570 commit 5dc1308

File tree

10 files changed

+564
-2
lines changed

10 files changed

+564
-2
lines changed

Documentation/diff-options.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ endif::git-format-patch[]
3636
--patch-with-raw::
3737
Synonym for "-p --raw".
3838

39+
--patience:
40+
Generate a diff using the "patience diff" algorithm.
41+
3942
--stat[=width[,name-width]]::
4043
Generate a diffstat. You can override the default
4144
output width for 80-column terminal by "--stat=width".

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ $(LIB_FILE): $(LIB_OBJS)
12871287
$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIB_OBJS)
12881288

12891289
XDIFF_OBJS=xdiff/xdiffi.o xdiff/xprepare.o xdiff/xutils.o xdiff/xemit.o \
1290-
xdiff/xmerge.o
1290+
xdiff/xmerge.o xdiff/xpatience.o
12911291
$(XDIFF_OBJS): xdiff/xinclude.h xdiff/xmacros.h xdiff/xdiff.h xdiff/xtypes.h \
12921292
xdiff/xutils.h xdiff/xprepare.h xdiff/xdiffi.h xdiff/xemit.h
12931293

contrib/completion/git-completion.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ __git_diff_common_options="--stat --numstat --shortstat --summary
783783
--no-ext-diff
784784
--no-prefix --src-prefix= --dst-prefix=
785785
--inter-hunk-context=
786+
--patience
786787
--raw
787788
"
788789

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
24742474
options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
24752475
else if (!strcmp(arg, "--ignore-space-at-eol"))
24762476
options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2477+
else if (!strcmp(arg, "--patience"))
2478+
options->xdl_opts |= XDF_PATIENCE_DIFF;
24772479

24782480
/* flags options */
24792481
else if (!strcmp(arg, "--binary")) {

t/t4033-diff-patience.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/bin/sh
2+
3+
test_description='patience diff algorithm'
4+
5+
. ./test-lib.sh
6+
7+
cat >file1 <<\EOF
8+
#include <stdio.h>
9+
10+
// Frobs foo heartily
11+
int frobnitz(int foo)
12+
{
13+
int i;
14+
for(i = 0; i < 10; i++)
15+
{
16+
printf("Your answer is: ");
17+
printf("%d\n", foo);
18+
}
19+
}
20+
21+
int fact(int n)
22+
{
23+
if(n > 1)
24+
{
25+
return fact(n-1) * n;
26+
}
27+
return 1;
28+
}
29+
30+
int main(int argc, char **argv)
31+
{
32+
frobnitz(fact(10));
33+
}
34+
EOF
35+
36+
cat >file2 <<\EOF
37+
#include <stdio.h>
38+
39+
int fib(int n)
40+
{
41+
if(n > 2)
42+
{
43+
return fib(n-1) + fib(n-2);
44+
}
45+
return 1;
46+
}
47+
48+
// Frobs foo heartily
49+
int frobnitz(int foo)
50+
{
51+
int i;
52+
for(i = 0; i < 10; i++)
53+
{
54+
printf("%d\n", foo);
55+
}
56+
}
57+
58+
int main(int argc, char **argv)
59+
{
60+
frobnitz(fib(10));
61+
}
62+
EOF
63+
64+
cat >expect <<\EOF
65+
diff --git a/file1 b/file2
66+
index 6faa5a3..e3af329 100644
67+
--- a/file1
68+
+++ b/file2
69+
@@ -1,26 +1,25 @@
70+
#include <stdio.h>
71+
72+
+int fib(int n)
73+
+{
74+
+ if(n > 2)
75+
+ {
76+
+ return fib(n-1) + fib(n-2);
77+
+ }
78+
+ return 1;
79+
+}
80+
+
81+
// Frobs foo heartily
82+
int frobnitz(int foo)
83+
{
84+
int i;
85+
for(i = 0; i < 10; i++)
86+
{
87+
- printf("Your answer is: ");
88+
printf("%d\n", foo);
89+
}
90+
}
91+
92+
-int fact(int n)
93+
-{
94+
- if(n > 1)
95+
- {
96+
- return fact(n-1) * n;
97+
- }
98+
- return 1;
99+
-}
100+
-
101+
int main(int argc, char **argv)
102+
{
103+
- frobnitz(fact(10));
104+
+ frobnitz(fib(10));
105+
}
106+
EOF
107+
108+
test_expect_success 'patience diff' '
109+
110+
test_must_fail git diff --no-index --patience file1 file2 > output &&
111+
test_cmp expect output
112+
113+
'
114+
115+
test_expect_success 'patience diff output is valid' '
116+
117+
mv file2 expect &&
118+
git apply < output &&
119+
test_cmp expect file2
120+
121+
'
122+
123+
cat >uniq1 <<\EOF
124+
1
125+
2
126+
3
127+
4
128+
5
129+
6
130+
EOF
131+
132+
cat >uniq2 <<\EOF
133+
a
134+
b
135+
c
136+
d
137+
e
138+
f
139+
EOF
140+
141+
cat >expect <<\EOF
142+
diff --git a/uniq1 b/uniq2
143+
index b414108..0fdf397 100644
144+
--- a/uniq1
145+
+++ b/uniq2
146+
@@ -1,6 +1,6 @@
147+
-1
148+
-2
149+
-3
150+
-4
151+
-5
152+
-6
153+
+a
154+
+b
155+
+c
156+
+d
157+
+e
158+
+f
159+
EOF
160+
161+
test_expect_success 'completely different files' '
162+
163+
test_must_fail git diff --no-index --patience uniq1 uniq2 > output &&
164+
test_cmp expect output
165+
166+
'
167+
168+
test_done

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
#define XDF_IGNORE_WHITESPACE (1 << 2)
3333
#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
3434
#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
35+
#define XDF_PATIENCE_DIFF (1 << 5)
3536
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
3637

3738
#define XDL_PATCH_NORMAL '-'

xdiff/xdiffi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
329329
xdalgoenv_t xenv;
330330
diffdata_t dd1, dd2;
331331

332+
if (xpp->flags & XDF_PATIENCE_DIFF)
333+
return xdl_do_patience_diff(mf1, mf2, xpp, xe);
334+
332335
if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
333336

334337
return -1;

xdiff/xdiffi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr);
5555
void xdl_free_script(xdchange_t *xscr);
5656
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
5757
xdemitconf_t const *xecfg);
58+
int xdl_do_patience_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
59+
xdfenv_t *env);
5860

5961
#endif /* #if !defined(XDIFFI_H) */

0 commit comments

Comments
 (0)