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
23 from stgit
.config
import config
24 from stgit
.utils
import append_string
31 merger
= config
.get('gitmergeonefile', 'merger')
32 except Exception, err
:
33 print >> sys
.stderr
, 'Configuration error: %s' % err
36 if config
.has_option('gitmergeonefile', 'keeporig'):
37 keeporig
= config
.get('gitmergeonefile', 'keeporig')
45 if 'GIT_DIR' in os
.environ
:
46 base_dir
= os
.environ
['GIT_DIR']
61 f
= os
.popen(cmd
, 'r')
62 string
= f
.readline().strip()
64 print >> sys
.stderr
, 'Error: failed to execute "%s"' % cmd
68 def __checkout_files():
69 """Check out the files passed as arguments
71 global orig
, src1
, src2
74 orig
= '%s.older' % path
75 tmp
= __output('git-unpack-file %s' % orig_hash
)
76 os
.chmod(tmp
, int(orig_mode
, 8))
79 src1
= '%s.local' % path
80 tmp
= __output('git-unpack-file %s' % file
1_hash
)
81 os
.chmod(tmp
, int(file1_mode
, 8))
84 src2
= '%s.remote' % path
85 tmp
= __output('git-unpack-file %s' % file
2_hash
)
86 os
.chmod(tmp
, int(file2_mode
, 8))
90 """Remove any temporary files
101 """Write the conflict file for the 'path' variable and exit
103 append_string(os
.path
.join(base_dir
, 'conflicts'), path
)
107 # $1 - original file SHA1 (or empty)
108 # $2 - file in branch1 SHA1 (or empty)
109 # $3 - file in branch2 SHA1 (or empty)
110 # $4 - pathname in repository
111 # $5 - orignal file mode (or empty)
112 # $6 - file in branch1 mode (or empty)
113 # $7 - file in branch2 mode (or empty)
115 #print 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
116 # % tuple(sys.argv[1:8])
117 orig_hash
, file1_hash
, file2_hash
, path
, orig_mode
, file1_mode
, file2_mode
= \
118 [__str2none(x
) for x
in sys
.argv
[1:8]]
126 # file exists in origin
129 if file1_hash
and file2_hash
:
130 # if modes are the same (git-read-tree probably dealed with it)
131 if file1_hash
== file2_hash
:
132 if os
.system('git-update-cache --cacheinfo %s %s %s'
133 %
(file1_mode
, file1_hash
, path
)) != 0:
134 print >> sys
.stderr
, 'Error: git-update-cache failed'
136 if os
.system('git-checkout-cache -u -f -- %s' % path
):
137 print >> sys
.stderr
, 'Error: git-checkout-cache failed'
139 if file1_mode
!= file2_mode
:
140 print >> sys
.stderr
, \
141 'Error: File added in both, permissions conflict'
145 merge_ok
= os
.system(merger %
{'branch1': src1
,
148 'output': path
}) == 0
151 os
.system('git-update-cache -- %s' % path
)
155 print >> sys
.stderr
, \
156 'Error: three-way merge tool failed for file "%s"' % path
157 # reset the cache to the first branch
158 os
.system('git-update-cache --cacheinfo %s %s %s'
159 %
(file1_mode
, file1_hash
, path
))
160 if keeporig
!= 'yes':
163 # file deleted in both or deleted in one and unchanged in the other
164 elif not (file1_hash
or file2_hash
) \
165 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
166 if os
.path
.exists(path
):
169 sys
.exit(os
.system('git-update-cache --remove -- %s' % path
))
170 # file does not exist in origin
173 if file1_hash
and file2_hash
:
175 if file1_hash
== file2_hash
:
176 if os
.system('git-update-cache --add --cacheinfo %s %s %s'
177 %
(file1_mode
, file1_hash
, path
)) != 0:
178 print >> sys
.stderr
, 'Error: git-update-cache failed'
180 if os
.system('git-checkout-cache -u -f -- %s' % path
):
181 print >> sys
.stderr
, 'Error: git-checkout-cache failed'
183 if file1_mode
!= file2_mode
:
184 print >> sys
.stderr
, \
185 'Error: File "s" added in both, permissions conflict' \
188 # files are different
190 print >> sys
.stderr
, \
191 'Error: File "%s" added in branches but different' % path
194 elif file1_hash
or file2_hash
:
201 if os
.system('git-update-cache --add --cacheinfo %s %s %s'
202 %
(mode
, obj
, path
)) != 0:
203 print >> sys
.stderr
, 'Error: git-update-cache failed'
206 sys
.exit(os
.system('git-checkout-cache -u -f -- %s' % path
))
209 print >> sys
.stderr
, 'Error: Un-handled merge conflict'
210 print >> sys
.stderr
, 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
211 %
tuple(sys
.argv
[1:8])