README text moved to the wiki page
[stgit] / gitmergeonefile.py
CommitLineData
41a6d859
CM
1#!/usr/bin/env python
2"""Performs a 3-way merge for GIT files
3"""
4
5__copyright__ = """
6Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License version 2 as
10published by the Free Software Foundation.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20"""
21
22import sys, os
7ab9ac47
CM
23
24# Try to detect where it is run from and set prefix and the search path.
25# It is assumed that the user installed StGIT using the --prefix= option
26prefix, bin = os.path.split(sys.path[0])
27
28if bin == 'bin' and prefix != sys.prefix:
29 major, minor = sys.version_info[0:2]
30 sys.path += [os.path.join(prefix, 'lib', 'python'),
31 os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor)),
32 os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor),
33 'site-packages')]
34
41a6d859
CM
35from stgit.config import config
36from stgit.utils import append_string
37
38
39#
40# Options
41#
42try:
43 merger = config.get('gitmergeonefile', 'merger')
44except Exception, err:
45 print >> sys.stderr, 'Configuration error: %s' % err
46 sys.exit(1)
47
48if config.has_option('gitmergeonefile', 'keeporig'):
49 keeporig = config.get('gitmergeonefile', 'keeporig')
50else:
51 keeporig = 'yes'
52
53
54#
55# Global variables
56#
57if 'GIT_DIR' in os.environ:
58 base_dir = os.environ['GIT_DIR']
59else:
60 base_dir = '.git'
61
62
63#
64# Utility functions
65#
66def __str2none(x):
67 if x == '':
68 return None
69 else:
70 return x
71
72def __output(cmd):
73 f = os.popen(cmd, 'r')
74 string = f.readline().strip()
75 if f.close():
76 print >> sys.stderr, 'Error: failed to execute "%s"' % cmd
77 sys.exit(1)
78 return string
79
80def __checkout_files():
81 """Check out the files passed as arguments
82 """
83 global orig, src1, src2
84
85 if orig_hash:
86 orig = '%s.older' % path
87 tmp = __output('git-unpack-file %s' % orig_hash)
88 os.chmod(tmp, int(orig_mode, 8))
8c737ba4 89 os.renames(tmp, orig)
41a6d859
CM
90 if file1_hash:
91 src1 = '%s.local' % path
92 tmp = __output('git-unpack-file %s' % file1_hash)
93 os.chmod(tmp, int(file1_mode, 8))
8c737ba4 94 os.renames(tmp, src1)
41a6d859
CM
95 if file2_hash:
96 src2 = '%s.remote' % path
97 tmp = __output('git-unpack-file %s' % file2_hash)
98 os.chmod(tmp, int(file2_mode, 8))
8c737ba4 99 os.renames(tmp, src2)
41a6d859
CM
100
101def __remove_files():
102 """Remove any temporary files
103 """
104 if orig_hash:
105 os.remove(orig)
106 if file1_hash:
107 os.remove(src1)
108 if file2_hash:
109 os.remove(src2)
110 pass
111
112def __conflict():
113 """Write the conflict file for the 'path' variable and exit
114 """
115 append_string(os.path.join(base_dir, 'conflicts'), path)
116 sys.exit(1)
117
118
119# $1 - original file SHA1 (or empty)
120# $2 - file in branch1 SHA1 (or empty)
121# $3 - file in branch2 SHA1 (or empty)
122# $4 - pathname in repository
123# $5 - orignal file mode (or empty)
124# $6 - file in branch1 mode (or empty)
125# $7 - file in branch2 mode (or empty)
8c737ba4
CM
126#
127#print 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
128# % tuple(sys.argv[1:8])
41a6d859
CM
129orig_hash, file1_hash, file2_hash, path, orig_mode, file1_mode, file2_mode = \
130 [__str2none(x) for x in sys.argv[1:8]]
131
132
133#
134# Main algorithm
135#
136__checkout_files()
137
138# file exists in origin
139if orig_hash:
140 # modified in both
141 if file1_hash and file2_hash:
142 # if modes are the same (git-read-tree probably dealed with it)
143 if file1_hash == file2_hash:
144 if os.system('git-update-cache --cacheinfo %s %s %s'
145 % (file1_mode, file1_hash, path)) != 0:
146 print >> sys.stderr, 'Error: git-update-cache failed'
147 __conflict()
148 if os.system('git-checkout-cache -u -f -- %s' % path):
149 print >> sys.stderr, 'Error: git-checkout-cache failed'
150 __conflict()
151 if file1_mode != file2_mode:
152 print >> sys.stderr, \
153 'Error: File added in both, permissions conflict'
154 __conflict()
155 # 3-way merge
156 else:
157 merge_ok = os.system(merger % {'branch1': src1,
158 'ancestor': orig,
159 'branch2': src2,
160 'output': path }) == 0
161
162 if merge_ok:
8c737ba4 163 os.system('git-update-cache -- %s' % path)
41a6d859
CM
164 __remove_files()
165 sys.exit(0)
166 else:
167 print >> sys.stderr, \
168 'Error: three-way merge tool failed for file "%s"' % path
169 # reset the cache to the first branch
170 os.system('git-update-cache --cacheinfo %s %s %s'
171 % (file1_mode, file1_hash, path))
172 if keeporig != 'yes':
173 __remove_files()
174 __conflict()
175 # file deleted in both or deleted in one and unchanged in the other
176 elif not (file1_hash or file2_hash) \
177 or file1_hash == orig_hash or file2_hash == orig_hash:
178 if os.path.exists(path):
179 os.remove(path)
180 __remove_files()
8c737ba4 181 sys.exit(os.system('git-update-cache --remove -- %s' % path))
41a6d859
CM
182# file does not exist in origin
183else:
184 # file added in both
185 if file1_hash and file2_hash:
186 # files are the same
187 if file1_hash == file2_hash:
188 if os.system('git-update-cache --add --cacheinfo %s %s %s'
189 % (file1_mode, file1_hash, path)) != 0:
190 print >> sys.stderr, 'Error: git-update-cache failed'
191 __conflict()
192 if os.system('git-checkout-cache -u -f -- %s' % path):
193 print >> sys.stderr, 'Error: git-checkout-cache failed'
194 __conflict()
195 if file1_mode != file2_mode:
196 print >> sys.stderr, \
197 'Error: File "s" added in both, permissions conflict' \
198 % path
199 __conflict()
200 # files are different
201 else:
202 print >> sys.stderr, \
203 'Error: File "%s" added in branches but different' % path
204 __conflict()
205 # file added in one
206 elif file1_hash or file2_hash:
207 if file1_hash:
208 mode = file1_mode
209 obj = file1_hash
210 else:
211 mode = file2_mode
212 obj = file2_hash
213 if os.system('git-update-cache --add --cacheinfo %s %s %s'
214 % (mode, obj, path)) != 0:
215 print >> sys.stderr, 'Error: git-update-cache failed'
216 __conflict()
217 __remove_files()
218 sys.exit(os.system('git-checkout-cache -u -f -- %s' % path))
219
220# Un-handled case
221print >> sys.stderr, 'Error: Un-handled merge conflict'
8c737ba4 222print >> sys.stderr, 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
41a6d859
CM
223 % tuple(sys.argv[1:8])
224__conflict()