-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirvimdiff
More file actions
executable file
·87 lines (76 loc) · 2.74 KB
/
dirvimdiff
File metadata and controls
executable file
·87 lines (76 loc) · 2.74 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
#!/usr/bin/env python3
""" diff two folders, show diffences per file in a tab """
import os
import sys
import re
import tempfile
import subprocess
import logging
import io
#
# Copyright (C) 2016,
# Thomas Langewouters <thomas.langewouters@thouters.be>
# Copyright (C) 2022,
# Thomas Langewouters <thomas.langewouters@thouters.be>
#
# This is free software, you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# KNOWN ISSUES
# This file's quality is 'QUICK HACK'
# - it expects LOCALE/ env LANG to be 'C'
# - parses output of 'diff', will fail if filenames contain 'Only in', ': ', etc
# -
NEWLINE = "\n"
HELP = """\
dirvimdiff - script to show two directories' file differences in vim tabs"
dirvimdiff [options] dir1 dir2"
Option:
-g Use gvimdiff (graphical mode) instead of vimdiff"
All other options are passed to diff
"""
def main():
""" main function """
vimdiff = "vimdiff"
if sys.argv[1] == "-g":
vimdiff = "gvimdiff -f"
del sys.argv[1]
if "--help" in sys.argv:
print(HELP)
sys.exit(1)
_scriptname = sys.argv.pop(0)
dir1 = sys.argv.pop(0)
dir2 = sys.argv.pop(0)
cmd = f"diff -r {dir1} {dir2} --brief --exclude '*.o' --exclude '*.pdf' --exclude '*.a'"
with tempfile.NamedTemporaryFile(prefix='dirvimdiff', encoding='utf-8', mode='w') as script, \
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) as proc:
script.write(":ex\n")
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
if line.startswith("Only in"):
only_in = line[len("Only in "):line.find(
": ")] + "/" + line[line.find(": ")+2:]
logging.debug("ONLY IN %s", only_in)
logging.debug("DIR1 %s", dir1)
logging.debug("DIR2 %s", dir2)
if only_in.startswith(dir1):
files = (only_in, "/dev/null")
else:
files = ("/dev/null", only_in)
elif line.startswith("Files "):
files = re.match("Files (.*) and (.*) differ", line).groups()
logging.debug("DIFFER %s", files)
else:
raise Exception(f"unexepcted {line}")
command = f":tabnew {files[0]}{NEWLINE}:vert diffsplit {files[1]}{NEWLINE}"
logging.debug("command: %s", command)
script.write(command)
script.write(":vi\n")
script.flush()
logging.debug("script is %s", script.name)
command = f"{vimdiff} -s {script.name}"
if os.path.isfile("commit.txt"):
command += " commit.txt"
os.system(command)
if __name__ == "__main__":
main()