chiark / gitweb /
3b3175becd57f991e343ad7041e4c3b6bb21d696
[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.config import config
23 from stgit.utils import append_string
24
25
26 class GitMergeException(Exception):
27     pass
28
29
30 #
31 # Options
32 #
33 try:
34     merger = config.get('stgit', 'merger')
35     keeporig = config.get('stgit', 'keeporig')
36 except Exception, err:
37     raise GitMergeException, 'Configuration error: %s' % err
38
39
40 #
41 # Utility functions
42 #
43 def __str2none(x):
44     if x == '':
45         return None
46     else:
47         return x
48
49 def __output(cmd):
50     f = os.popen(cmd, 'r')
51     string = f.readline().rstrip()
52     if f.close():
53         raise GitMergeException, 'Error: failed to execute "%s"' % cmd
54     return string
55
56 def __checkout_files(orig_hash, file1_hash, file2_hash,
57                      path,
58                      orig_mode, file1_mode, file2_mode):
59     """Check out the files passed as arguments
60     """
61     global orig, src1, src2
62
63     if orig_hash:
64         orig = '%s.older' % path
65         tmp = __output('git-unpack-file %s' % orig_hash)
66         os.chmod(tmp, int(orig_mode, 8))
67         os.renames(tmp, orig)
68     if file1_hash:
69         src1 = '%s.local' % path
70         tmp = __output('git-unpack-file %s' % file1_hash)
71         os.chmod(tmp, int(file1_mode, 8))
72         os.renames(tmp, src1)
73     if file2_hash:
74         src2 = '%s.remote' % path
75         tmp = __output('git-unpack-file %s' % file2_hash)
76         os.chmod(tmp, int(file2_mode, 8))
77         os.renames(tmp, src2)
78
79 def __remove_files(orig_hash, file1_hash, file2_hash):
80     """Remove any temporary files
81     """
82     if orig_hash:
83         os.remove(orig)
84     if file1_hash:
85         os.remove(src1)
86     if file2_hash:
87         os.remove(src2)
88     pass
89
90 # GIT_DIR value cached
91 __base_dir = None
92
93 def __conflict(path):
94     """Write the conflict file for the 'path' variable and exit
95     """
96     global __base_dir
97
98     if not __base_dir:
99         if 'GIT_DIR' in os.environ:
100             __base_dir = os.environ['GIT_DIR']
101         else:
102             __base_dir = __output('git-rev-parse --git-dir')
103
104     append_string(os.path.join(__base_dir, 'conflicts'), path)
105
106
107 #
108 # Main algorithm
109 #
110 def merge(orig_hash, file1_hash, file2_hash,
111           path,
112           orig_mode, file1_mode, file2_mode):
113     """Three-way merge for one file algorithm
114     """
115     __checkout_files(orig_hash, file1_hash, file2_hash,
116                      path,
117                      orig_mode, file1_mode, file2_mode)
118
119     # file exists in origin
120     if orig_hash:
121         # modified in both
122         if file1_hash and file2_hash:
123             # if modes are the same (git-read-tree probably dealt with it)
124             if file1_hash == file2_hash:
125                 if os.system('git-update-index --cacheinfo %s %s %s'
126                              % (file1_mode, file1_hash, path)) != 0:
127                     print >> sys.stderr, 'Error: git-update-index failed'
128                     __conflict(path)
129                     return 1
130                 if os.system('git-checkout-index -u -f -- %s' % path):
131                     print >> sys.stderr, 'Error: git-checkout-index failed'
132                     __conflict(path)
133                     return 1
134                 if file1_mode != file2_mode:
135                     print >> sys.stderr, \
136                           'Error: File added in both, permissions conflict'
137                     __conflict(path)
138                     return 1
139             # 3-way merge
140             else:
141                 merge_ok = os.system(merger % {'branch1': src1,
142                                                'ancestor': orig,
143                                                'branch2': src2,
144                                                'output': path }) == 0
145
146                 if merge_ok:
147                     os.system('git-update-index -- %s' % path)
148                     __remove_files(orig_hash, file1_hash, file2_hash)
149                     return 0
150                 else:
151                     print >> sys.stderr, \
152                           'Error: three-way merge tool failed for file "%s"' \
153                           % path
154                     # reset the cache to the first branch
155                     os.system('git-update-index --cacheinfo %s %s %s'
156                               % (file1_mode, file1_hash, path))
157                     if keeporig != 'yes':
158                         __remove_files(orig_hash, file1_hash, file2_hash)
159                     __conflict(path)
160                     return 1
161         # file deleted in both or deleted in one and unchanged in the other
162         elif not (file1_hash or file2_hash) \
163                or file1_hash == orig_hash or file2_hash == orig_hash:
164             if os.path.exists(path):
165                 os.remove(path)
166             __remove_files(orig_hash, file1_hash, file2_hash)
167             return os.system('git-update-index --remove -- %s' % path)
168         # file deleted in one and changed in the other
169         else:
170             # Do something here - we must at least merge the entry in
171             # the cache, instead of leaving it in U(nmerged) state. In
172             # fact, stg resolved does not handle that.
173
174             # Do the same thing cogito does - remove the file in any case.
175             os.system('git-update-index --remove -- %s' % path)
176
177             #if file1_hash:
178                 ## file deleted upstream and changed in the patch. The
179                 ## patch is probably going to move the changes
180                 ## elsewhere.
181
182                 #os.system('git-update-index --remove -- %s' % path)
183             #else:
184                 ## file deleted in the patch and changed upstream. We
185                 ## could re-delete it, but for now leave it there -
186                 ## and let the user check if he still wants to remove
187                 ## the file.
188
189                 ## reset the cache to the first branch
190                 #os.system('git-update-index --cacheinfo %s %s %s'
191                 #          % (file1_mode, file1_hash, path))
192             __conflict(path)
193             return 1
194
195     # file does not exist in origin
196     else:
197         # file added in both
198         if file1_hash and file2_hash:
199             # files are the same
200             if file1_hash == file2_hash:
201                 if os.system('git-update-index --add --cacheinfo %s %s %s'
202                              % (file1_mode, file1_hash, path)) != 0:
203                     print >> sys.stderr, 'Error: git-update-index failed'
204                     __conflict(path)
205                     return 1
206                 if os.system('git-checkout-index -u -f -- %s' % path):
207                     print >> sys.stderr, 'Error: git-checkout-index failed'
208                     __conflict(path)
209                     return 1
210                 if file1_mode != file2_mode:
211                     print >> sys.stderr, \
212                           'Error: File "s" added in both, ' \
213                           'permissions conflict' % path
214                     __conflict(path)
215                     return 1
216             # files are different
217             else:
218                 print >> sys.stderr, \
219                       'Error: File "%s" added in branches but different' % path
220                 __conflict(path)
221                 return 1
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(path)
234                 return 1
235             __remove_files(orig_hash, file1_hash, file2_hash)
236             return os.system('git-checkout-index -u -f -- %s' % path)
237
238     # Unhandled case
239     print >> sys.stderr, 'Error: Unhandled merge conflict: ' \
240           '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
241           % (orig_hash, file1_hash, file2_hash,
242              path,
243              orig_mode, file1_mode, file2_mode)
244     __conflict(path)
245     return 1