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