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
|
#!/bin/env python
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Wrapper around BeyondCompare or kdiff3 so it can be used as svn's diff3-cmd
tool.
The basic idea here is based heavily off of diffwrap.py at:
http://svnbook.red-bean.com/en/1.5/svn.advanced.externaldifftools.html#svn.advanced.externaldifftools.diff3.ex-1
"""
import optparse
import os
import subprocess
import sys
import traceback
CYGDRIVE = '/cygdrive/'
CYGLEN = len(CYGDRIVE)
# TODO(ojan): Eventually make this a cross-platform script that makes it easy
# to use different diffing tools (an enum of tools maybe?).
# The difficulty is that each tool has its own supported arguments.
def EnsureWindowsPath(path):
""" Returns a Windows-style path given a Windows path or a cygwin path.
"""
if path.startswith(CYGDRIVE):
path = path[CYGLEN:CYGLEN + 1] + ':' + path[CYGLEN + 1:]
path = path.replace('/', '\\')
return path
def GetPathToBinary(exe):
""" Try to find a copy of the binary that exists. Search for the full path
and then the basename.
"""
if not os.path.exists(exe):
exe = os.path.basename(exe)
return exe
def main(args):
""" Provides a wrapper around 3rd-party diffing tools so they can be used as
the diff3-cmd in svn merge.
args: The arguments passed by svn merge to its diff3 tool.
"""
# Grab the arguments from the end of the list since svn will add any other
# arguments provided before these.
# The titles of the files being diffed.
title_mine = EnsureWindowsPath(args[-8])
title_older = EnsureWindowsPath(args[-6])
title_yours = EnsureWindowsPath(args[-4])
# The paths to the files being diffed. These will be temp files.
mine = EnsureWindowsPath(args[-3])
older = EnsureWindowsPath(args[-2])
yours = EnsureWindowsPath(args[-1])
# The command for which diff3 tool to use.
diff_tool = args[1]
if diff_tool == "--use-beyondcompare":
exe = GetPathToBinary("c:/Progra~1/Beyond~1/BComp.exe")
if not os.path.exists(exe):
exe = GetPathToBinary("c:/Progra~2/Beyond~1/BComp.exe")
cmd = [exe,
mine,
yours,
older,
mine,
'/reviewconflicts',
'/automerge',
'/leftreadonly',
'/rightreadonly',
'/ignoreunimportant',
'/lefttitle', title_mine,
'/righttitle', title_yours,
'/centertitle', title_older,
'/outputtitle', 'merged']
elif diff_tool == "--use-kdiff3":
exe = GetPathToBinary("c:/Progra~1/KDiff3/kdiff3.exe")
if not os.path.exists(exe):
exe = GetPathToBinary("c:/Progra~2/KDiff3/kdiff3.exe")
cmd = [exe,
older,
mine,
yours,
"--merge",
"--auto",
"--output", mine]
else:
# TODO(ojan): Maybe fall back to diff3?
raise Exception, "Must specify a diff tool to use."
try:
# Work around http://bugs.python.org/issue3905
# by passing stderr and stdin as well since beyondcompare is a GUI app
return_code = subprocess.call(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=True)
except Exception,e:
print "Error running " + (" ".join(cmd))
traceback.print_exc(file=sys.stdout)
# After performing the merge, this script needs to print the contents
# of the merged file to stdout.
# Return an errorcode of 0 on successful merge, 1 if unresolved conflicts
# remain in the result. Any other errorcode will be treated as fatal.
merged_file_contents = open(mine).read()
# For reasons I don't understand, an extra line break gets added at the end
# of the file. Strip it.
merged_file_contents = merged_file_contents[:-1]
print merged_file_contents
return return_code
if '__main__' == __name__:
try:
return_code = main(sys.argv)
except Exception,e:
traceback.print_exc(file=sys.stdout)
# diff3 uses '1' to mean "conflict" and 2 to mean "I barfed".
sys.exit(2) # return "I barfed"
sys.exit(return_code)
|