-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcd
executable file
·80 lines (68 loc) · 2.62 KB
/
rcd
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
#!/usr/bin/env python
"""Open vimdiff with the modified and unmodified versions of a file.
"""
import os
import sys
import subprocess
import tempfile
def get_file_from_hg(filepath):
#print("Getting %s from hg" % filepath)
p = subprocess.Popen(("hg", "cat", filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def get_file_from_bzr(filepath):
#print("Getting %s from bzr" % filepath)
p = subprocess.Popen(("bzr", "cat", filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def get_file_from_git(filepath):
#print("Getting %s from git" % filepath)
p = subprocess.Popen(("git", "ls-files", "--full-name", filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
filepath = out.strip()
p = subprocess.Popen(("git", "show", "HEAD:" + filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def get_file_from_svn(filepath):
#print("Getting %s from svn" % filepath)
p = subprocess.Popen(("svn", "cat", filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def get_file_from_cvs(filepath):
#print("Getting %s from cvs" % filepath)
p = subprocess.Popen(("cvs", "update", "-p", filepath), stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def get_file_from_revcontrol(filepath):
thisdir = os.path.dirname(os.path.realpath(os.path.abspath(filepath)))
if os.path.exists(os.path.join(thisdir, '.svn')):
return get_file_from_svn(filepath)
if os.path.exists(os.path.join(thisdir, 'CVS')):
return get_file_from_cvs(filepath)
while True:
if os.path.exists(os.path.join(thisdir, '.hg')):
return get_file_from_hg(filepath)
if os.path.exists(os.path.join(thisdir, '.bzr')):
return get_file_from_bzr(filepath)
if os.path.exists(os.path.join(thisdir, '.git')):
return get_file_from_git(filepath)
parent = os.path.dirname(thisdir)
if parent == thisdir:
sys.exit("Couldn't find any known revision control system for %s" % filepath)
thisdir = parent
def open_diffs(filepath):
orig_contents = get_file_from_revcontrol(filepath)
orig_file = tempfile.NamedTemporaryFile()
try:
orig_file.write(orig_contents)
orig_file.flush()
os.chmod(orig_file.name, 0400)
subprocess.call(("vimdiff", orig_file.name, filepath))
finally:
orig_file.close()
if __name__ == '__main__':
if len(sys.argv) <= 1:
print "Usage: rcd <filepath> [<filepath> ...]"
sys.exit(1)
for filepath in sys.argv[1:]:
open_diffs(filepath)