-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.py
More file actions
372 lines (307 loc) · 12.7 KB
/
diff.py
File metadata and controls
372 lines (307 loc) · 12.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import json
import hashlib
import fnmatch
from pathlib import Path
import difflib
def get_hash(path):
try:
with open(path, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
except Exception:
return ""
def get_content(path):
try:
with open(path, "r", encoding="utf-8") as f:
return f.read()
except Exception:
return ""
def load_gitignore_patterns(folder):
"""Load patterns from .gitignore file"""
gitignore_path = Path(folder) / ".gitignore"
patterns = []
if gitignore_path.exists():
try:
with open(gitignore_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if line and not line.startswith("#"):
patterns.append(line)
except Exception:
pass
return patterns
def is_ignored(file_path, patterns):
"""Check if a file path matches any gitignore pattern"""
file_path_str = str(file_path)
for pattern in patterns:
# Handle directory patterns (ending with /)
if pattern.endswith("/"):
if file_path_str.startswith(pattern) or ("/" + pattern) in file_path_str:
return True
# Handle negation patterns (starting with !)
elif pattern.startswith("!"):
# This is a negation pattern - would need more complex logic
# For now, we'll skip negation patterns
continue
# Handle glob patterns
else:
if fnmatch.fnmatch(file_path_str, pattern) or fnmatch.fnmatch(
file_path_str, "*/" + pattern
):
return True
# Also check if any parent directory matches
parts = file_path_str.split("/")
for i in range(len(parts)):
partial_path = "/".join(parts[: i + 1])
if fnmatch.fnmatch(partial_path, pattern):
return True
return False
def scan(folder):
folder = Path(folder)
files = {}
gitignore_patterns = load_gitignore_patterns(folder)
for f in folder.rglob("*"):
if (
f.is_file()
and not f.name.startswith(".DS_Store")
and ".git" not in str(f)
and f.name != ".state.json"
):
relative_path = f.relative_to(folder)
# Check if file should be ignored based on gitignore patterns
if not is_ignored(relative_path, gitignore_patterns):
files[str(relative_path)] = {
"hash": get_hash(f),
"content": get_content(f),
}
return files
def save(data, fname):
with open(fname, "w") as f:
json.dump(data, f, indent=2)
def load(fname):
if Path(fname).exists():
try:
with open(fname) as f:
return json.load(f) or {}
except Exception:
return {}
return {}
def is_json_file(filename):
"""Check if a file is likely a JSON file based on extension"""
return filename.lower().endswith((".json", ".ipynb"))
def format_content_for_diff(content, filename):
"""Format content for better diff display"""
if is_json_file(filename):
try:
# Try to parse and pretty-print JSON
parsed = json.loads(content)
return json.dumps(parsed, indent=2, sort_keys=True)
except (json.JSONDecodeError, ValueError):
# If it's not valid JSON, return as-is
pass
return content
def show_diff(added, removed, modified, context_lines=2):
"""Display changes in a clean, simple format - returns string instead of printing"""
output = []
# New files
if added:
output.append("NEW FILES:")
for f in sorted(added):
output.append(f" + {f}")
output.append("")
# Deleted files
if removed:
output.append("DELETED FILES:")
for f in sorted(removed):
output.append(f" - {f}")
output.append("")
# Modified files
if modified:
output.append("Modified files:")
output.append("")
for f in modified:
filename = f["file"]
output.append(f"File: {filename}")
output.append("-" * 70)
old_content = f["old_content"]
new_content = f["new_content"]
# Format content for better diffing (pretty-print JSON, etc.)
old_formatted = format_content_for_diff(old_content, filename)
new_formatted = format_content_for_diff(new_content, filename)
# Split content into lines
old_lines = old_formatted.splitlines()
new_lines = new_formatted.splitlines()
# Use SequenceMatcher for comparison
matcher = difflib.SequenceMatcher(None, old_lines, new_lines)
opcodes = list(matcher.get_opcodes())
# Check if there are any changes
has_changes = any(tag != "equal" for tag, _, _, _, _ in opcodes)
if not has_changes:
output.append(" (No textual differences found)")
output.append("")
continue
# Build hunks: regions to display (changes + context)
hunks = []
for tag, i1, i2, j1, j2 in opcodes:
if tag != "equal":
# Include context around changes
# For inserts where i1==i2, ensure we still create a valid hunk
hunk_start = max(0, i1 - context_lines)
hunk_end = min(len(old_lines), max(i1, i2) + context_lines)
hunks.append((hunk_start, hunk_end, tag, i1, i2, j1, j2))
# Merge overlapping hunks and track which opcodes belong to each
if hunks:
merged_hunks = [(hunks[0][0], hunks[0][1], [hunks[0][2:]])]
for hunk_start, hunk_end, tag, i1, i2, j1, j2 in hunks[1:]:
last_start, last_end, last_ops = merged_hunks[-1]
if hunk_start <= last_end + 1:
# Overlapping or adjacent, merge them
merged_hunks[-1] = (
last_start,
max(last_end, hunk_end),
last_ops + [(tag, i1, i2, j1, j2)],
)
else:
merged_hunks.append(
(hunk_start, hunk_end, [(tag, i1, i2, j1, j2)])
)
# Start markdown diff block
output.append("```diff")
# Display each hunk
for hunk_idx, (hunk_start, hunk_end, hunk_ops) in enumerate(
merged_hunks
):
# Show ellipsis if this isn't the first hunk
if hunk_idx > 0:
output.append("...")
# Track current line numbers for old and new files
old_line_num = hunk_start + 1
new_line_num = hunk_start + 1
# Calculate new_line_num offset by counting changes before this hunk
for tag, i1, i2, j1, j2 in opcodes:
if i1 >= hunk_start:
break
if tag == "insert":
new_line_num += j2 - j1
elif tag == "delete":
new_line_num -= i2 - i1
elif tag == "replace":
new_line_num += (j2 - j1) - (i2 - i1)
# Process all opcodes, showing context and changes
for tag, i1, i2, j1, j2 in opcodes:
# Check if this opcode overlaps with current hunk
if tag == "equal":
# For equal sections, only show if within hunk range
start_idx = max(i1, hunk_start)
end_idx = min(i2, hunk_end)
if start_idx < end_idx:
for idx in range(start_idx, end_idx):
output.append(
f" {old_line_num:4d} {old_lines[idx]}"
)
old_line_num += 1
new_line_num += 1
else:
# For changes, check if this change is part of current hunk
change_in_hunk = False
for change_tag, ci1, ci2, cj1, cj2 in hunk_ops:
if (
tag == change_tag
and i1 == ci1
and i2 == ci2
and j1 == cj1
and j2 == cj2
):
change_in_hunk = True
break
if not change_in_hunk:
# Update line numbers for skipped changes
if tag == "insert":
new_line_num += j2 - j1
elif tag == "delete":
old_line_num += i2 - i1
elif tag == "replace":
old_line_num += i2 - i1
new_line_num += j2 - j1
continue
if tag == "replace":
# Show removed lines
for idx in range(i1, i2):
output.append(
f"- {old_line_num:4d} {old_lines[idx]}"
)
old_line_num += 1
# Show added lines
for idx in range(j1, j2):
output.append(
f"+ {new_line_num:4d} {new_lines[idx]}"
)
new_line_num += 1
elif tag == "delete":
for idx in range(i1, i2):
output.append(
f"- {old_line_num:4d} {old_lines[idx]}"
)
old_line_num += 1
elif tag == "insert":
# For inserts, show the new line numbers
for idx in range(j1, j2):
output.append(
f"+ {new_line_num:4d} {new_lines[idx]}"
)
new_line_num += 1
# End markdown diff block
output.append("```")
output.append("")
return "\n".join(output)
def main(folder=".", print_output=True):
"""
Main diff function - returns diff as string.
Args:
folder: Folder to scan for changes
print_output: If True, prints output to console. If False, only returns string.
Returns:
String containing the diff output
"""
statefile = ".state.json"
curr = scan(folder)
prev = load(statefile)
if not prev:
result = "First run - establishing baseline"
if print_output:
print(result)
save(curr, statefile)
return result
added = set(curr) - set(prev)
removed = set(prev) - set(curr)
modified = []
for f in curr:
if f in prev and curr[f]["hash"] != prev[f]["hash"]:
modified.append(
{
"file": f,
"old_content": prev[f]["content"],
"new_content": curr[f]["content"],
}
)
output = []
if added or removed or modified:
output.append("Changes:")
for f in sorted(added):
output.append(f"+ {f}")
for f in sorted(removed):
output.append(f"- {f}")
for f in modified:
output.append(f"~ {f['file']}")
diff_details = show_diff(added, removed, modified)
if diff_details:
output.append(diff_details)
else:
output.append("No changes")
result = "\n".join(output)
if print_output:
print(result)
save(curr, statefile)
return result
if __name__ == "__main__":
main()