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