-
Notifications
You must be signed in to change notification settings - Fork 6
/
diff-lazy.py
108 lines (101 loc) · 1.7 KB
/
diff-lazy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python3
# diff-lazy.py
# produce a more 'lazy' output than gnu diff -u
# aka lazy diff, or less-greedy diff
#todo implement -p flag in difflib = show C function name
#todo add a 'lazy' flag to gnu diff
# license = Unlicense = public domain
# author = milahu
'''
example
f1.c:
bool foo_function()
{
#ifdef FOO_CONFIG
return true;
#else
return false;
#endif
}
f2.c:
#ifdef FOO_CONFIG
bool foo_variable = true;
#else
bool foo_variable = false;
#endif
bool foo_function()
{
return foo_variable;
}
gnu diff:
diff -u --label f1.c --label f2.c f1.c f2.c
--- f1.c
+++ f2.c
@@ -1,8 +1,9 @@
-bool foo_function()
-{
#ifdef FOO_CONFIG
- return true;
+bool foo_variable = true;
#else
- return false;
+bool foo_variable = false;
#endif
+bool foo_function()
+{
+ return foo_variable;
}
python diff:
difflib.unified_diff
--- f1.c
+++ f2.c
@@ -1,8 +1,9 @@
+#ifdef FOO_CONFIG
+bool foo_variable = true;
+#else
+bool foo_variable = false;
+#endif
bool foo_function()
{
-#ifdef FOO_CONFIG
- return true;
-#else
- return false;
-#endif
+ return foo_variable;
}
'''
import sys
import difflib
def my_diff(f, f2=None):
if not f2:
# guess original file
f1 = f + '.orig'
f2 = f
# git diff naming
n1 = 'a/' + f
n2 = 'b/' + f
else:
f1 = f
#f2 = f2
n1 = f1
n2 = f2
return ''.join(
difflib.unified_diff(
open(f1, 'r').readlines(),
open(f2, 'r').readlines(),
fromfile=n1, # label
tofile=n2, # label
lineterm='\n'
)
)
if __name__ == '__main__':
argc = len(sys.argv) - 1
if argc == 1:
print(my_diff(sys.argv[1]))
elif argc == 2:
print(my_diff(sys.argv[1], sys.argv[2]))
else:
print('usage: ' + sys.argv[0] + ' f [f2]')
print('f --> f.orig vs f')
print('f + f2 --> f vs f2')