2 """Performs a 3-way merge for GIT files
6 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
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])
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
),
34 sys
.path
= local_path
+ sys
.path
36 from stgit
.config
import config
37 from stgit
.utils
import append_string
38 from stgit
.git
import get_base_dir
45 merger
= config
.get('gitmergeonefile', 'merger')
46 except Exception, err
:
47 print >> sys
.stderr
, 'Configuration error: %s' % err
50 if config
.has_option('gitmergeonefile', 'keeporig'):
51 keeporig
= config
.get('gitmergeonefile', 'keeporig')
66 f
= os
.popen(cmd
, 'r')
67 string
= f
.readline().strip()
69 print >> sys
.stderr
, 'Error: failed to execute "%s"' % cmd
73 def __checkout_files():
74 """Check out the files passed as arguments
76 global orig
, src1
, src2
79 orig
= '%s.older' % path
80 tmp
= __output('git-unpack-file %s' % orig_hash
)
81 os
.chmod(tmp
, int(orig_mode
, 8))
84 src1
= '%s.local' % path
85 tmp
= __output('git-unpack-file %s' % file1_hash
)
86 os
.chmod(tmp
, int(file1_mode
, 8))
89 src2
= '%s.remote' % path
90 tmp
= __output('git-unpack-file %s' % file2_hash
)
91 os
.chmod(tmp
, int(file2_mode
, 8))
95 """Remove any temporary files
106 """Write the conflict file for the 'path' variable and exit
108 append_string(os
.path
.join(get_base_dir(), 'conflicts'), path
)
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)
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]]
131 # file exists in origin
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'
141 if os
.system('git-checkout-index -u -f -- %s' % path
):
142 print >> sys
.stderr
, 'Error: git-checkout-index failed'
144 if file1_mode
!= file2_mode
:
145 print >> sys
.stderr
, \
146 'Error: File added in both, permissions conflict'
150 merge_ok
= os
.system(merger
% {'branch1': src1
,
153 'output': path
}) == 0
156 os
.system('git-update-index -- %s' % path
)
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':
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
):
174 sys
.exit(os
.system('git-update-index --remove -- %s' % path
))
175 # file deleted in one and changed in the other
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.
181 # Do the same thing cogito does - remove the file in any case.
182 os
.system('git-update-index --remove -- %s' % path
)
185 ## file deleted upstream and changed in the patch. The patch is
186 ## probably going to move the changes elsewhere.
188 #os.system('git-update-index --remove -- %s' % path)
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.
194 ## reset the cache to the first branch
195 #os.system('git-update-index --cacheinfo %s %s %s'
196 #% (file1_mode, file1_hash, path))
199 # file does not exist in origin
202 if file1_hash
and file2_hash
:
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'
209 if os
.system('git-checkout-index -u -f -- %s' % path
):
210 print >> sys
.stderr
, 'Error: git-checkout-index failed'
212 if file1_mode
!= file2_mode
:
213 print >> sys
.stderr
, \
214 'Error: File "s" added in both, permissions conflict' \
217 # files are different
219 print >> sys
.stderr
, \
220 'Error: File "%s" added in branches but different' % path
223 elif file1_hash
or 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'
235 sys
.exit(os
.system('git-checkout-index -u -f -- %s' % path
))
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])