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