##// END OF EJS Templates
merge: directly use repo.vfs.join...
Pierre-Yves David -
r31323:102d3a30 default
parent child Browse files
Show More
@@ -1,1729 +1,1729 b''
1 # merge.py - directory-level update/merge handling for Mercurial
1 # merge.py - directory-level update/merge handling for Mercurial
2 #
2 #
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import hashlib
11 import hashlib
12 import os
12 import os
13 import shutil
13 import shutil
14 import struct
14 import struct
15
15
16 from .i18n import _
16 from .i18n import _
17 from .node import (
17 from .node import (
18 addednodeid,
18 addednodeid,
19 bin,
19 bin,
20 hex,
20 hex,
21 modifiednodeid,
21 modifiednodeid,
22 nullhex,
22 nullhex,
23 nullid,
23 nullid,
24 nullrev,
24 nullrev,
25 )
25 )
26 from . import (
26 from . import (
27 copies,
27 copies,
28 error,
28 error,
29 filemerge,
29 filemerge,
30 match as matchmod,
30 match as matchmod,
31 obsolete,
31 obsolete,
32 pycompat,
32 pycompat,
33 scmutil,
33 scmutil,
34 subrepo,
34 subrepo,
35 util,
35 util,
36 worker,
36 worker,
37 )
37 )
38
38
39 _pack = struct.pack
39 _pack = struct.pack
40 _unpack = struct.unpack
40 _unpack = struct.unpack
41
41
42 def _droponode(data):
42 def _droponode(data):
43 # used for compatibility for v1
43 # used for compatibility for v1
44 bits = data.split('\0')
44 bits = data.split('\0')
45 bits = bits[:-2] + bits[-1:]
45 bits = bits[:-2] + bits[-1:]
46 return '\0'.join(bits)
46 return '\0'.join(bits)
47
47
48 class mergestate(object):
48 class mergestate(object):
49 '''track 3-way merge state of individual files
49 '''track 3-way merge state of individual files
50
50
51 The merge state is stored on disk when needed. Two files are used: one with
51 The merge state is stored on disk when needed. Two files are used: one with
52 an old format (version 1), and one with a new format (version 2). Version 2
52 an old format (version 1), and one with a new format (version 2). Version 2
53 stores a superset of the data in version 1, including new kinds of records
53 stores a superset of the data in version 1, including new kinds of records
54 in the future. For more about the new format, see the documentation for
54 in the future. For more about the new format, see the documentation for
55 `_readrecordsv2`.
55 `_readrecordsv2`.
56
56
57 Each record can contain arbitrary content, and has an associated type. This
57 Each record can contain arbitrary content, and has an associated type. This
58 `type` should be a letter. If `type` is uppercase, the record is mandatory:
58 `type` should be a letter. If `type` is uppercase, the record is mandatory:
59 versions of Mercurial that don't support it should abort. If `type` is
59 versions of Mercurial that don't support it should abort. If `type` is
60 lowercase, the record can be safely ignored.
60 lowercase, the record can be safely ignored.
61
61
62 Currently known records:
62 Currently known records:
63
63
64 L: the node of the "local" part of the merge (hexified version)
64 L: the node of the "local" part of the merge (hexified version)
65 O: the node of the "other" part of the merge (hexified version)
65 O: the node of the "other" part of the merge (hexified version)
66 F: a file to be merged entry
66 F: a file to be merged entry
67 C: a change/delete or delete/change conflict
67 C: a change/delete or delete/change conflict
68 D: a file that the external merge driver will merge internally
68 D: a file that the external merge driver will merge internally
69 (experimental)
69 (experimental)
70 m: the external merge driver defined for this merge plus its run state
70 m: the external merge driver defined for this merge plus its run state
71 (experimental)
71 (experimental)
72 f: a (filename, dictionary) tuple of optional values for a given file
72 f: a (filename, dictionary) tuple of optional values for a given file
73 X: unsupported mandatory record type (used in tests)
73 X: unsupported mandatory record type (used in tests)
74 x: unsupported advisory record type (used in tests)
74 x: unsupported advisory record type (used in tests)
75 l: the labels for the parts of the merge.
75 l: the labels for the parts of the merge.
76
76
77 Merge driver run states (experimental):
77 Merge driver run states (experimental):
78 u: driver-resolved files unmarked -- needs to be run next time we're about
78 u: driver-resolved files unmarked -- needs to be run next time we're about
79 to resolve or commit
79 to resolve or commit
80 m: driver-resolved files marked -- only needs to be run before commit
80 m: driver-resolved files marked -- only needs to be run before commit
81 s: success/skipped -- does not need to be run any more
81 s: success/skipped -- does not need to be run any more
82
82
83 '''
83 '''
84 statepathv1 = 'merge/state'
84 statepathv1 = 'merge/state'
85 statepathv2 = 'merge/state2'
85 statepathv2 = 'merge/state2'
86
86
87 @staticmethod
87 @staticmethod
88 def clean(repo, node=None, other=None, labels=None):
88 def clean(repo, node=None, other=None, labels=None):
89 """Initialize a brand new merge state, removing any existing state on
89 """Initialize a brand new merge state, removing any existing state on
90 disk."""
90 disk."""
91 ms = mergestate(repo)
91 ms = mergestate(repo)
92 ms.reset(node, other, labels)
92 ms.reset(node, other, labels)
93 return ms
93 return ms
94
94
95 @staticmethod
95 @staticmethod
96 def read(repo):
96 def read(repo):
97 """Initialize the merge state, reading it from disk."""
97 """Initialize the merge state, reading it from disk."""
98 ms = mergestate(repo)
98 ms = mergestate(repo)
99 ms._read()
99 ms._read()
100 return ms
100 return ms
101
101
102 def __init__(self, repo):
102 def __init__(self, repo):
103 """Initialize the merge state.
103 """Initialize the merge state.
104
104
105 Do not use this directly! Instead call read() or clean()."""
105 Do not use this directly! Instead call read() or clean()."""
106 self._repo = repo
106 self._repo = repo
107 self._dirty = False
107 self._dirty = False
108 self._labels = None
108 self._labels = None
109
109
110 def reset(self, node=None, other=None, labels=None):
110 def reset(self, node=None, other=None, labels=None):
111 self._state = {}
111 self._state = {}
112 self._stateextras = {}
112 self._stateextras = {}
113 self._local = None
113 self._local = None
114 self._other = None
114 self._other = None
115 self._labels = labels
115 self._labels = labels
116 for var in ('localctx', 'otherctx'):
116 for var in ('localctx', 'otherctx'):
117 if var in vars(self):
117 if var in vars(self):
118 delattr(self, var)
118 delattr(self, var)
119 if node:
119 if node:
120 self._local = node
120 self._local = node
121 self._other = other
121 self._other = other
122 self._readmergedriver = None
122 self._readmergedriver = None
123 if self.mergedriver:
123 if self.mergedriver:
124 self._mdstate = 's'
124 self._mdstate = 's'
125 else:
125 else:
126 self._mdstate = 'u'
126 self._mdstate = 'u'
127 shutil.rmtree(self._repo.join('merge'), True)
127 shutil.rmtree(self._repo.vfs.join('merge'), True)
128 self._results = {}
128 self._results = {}
129 self._dirty = False
129 self._dirty = False
130
130
131 def _read(self):
131 def _read(self):
132 """Analyse each record content to restore a serialized state from disk
132 """Analyse each record content to restore a serialized state from disk
133
133
134 This function process "record" entry produced by the de-serialization
134 This function process "record" entry produced by the de-serialization
135 of on disk file.
135 of on disk file.
136 """
136 """
137 self._state = {}
137 self._state = {}
138 self._stateextras = {}
138 self._stateextras = {}
139 self._local = None
139 self._local = None
140 self._other = None
140 self._other = None
141 for var in ('localctx', 'otherctx'):
141 for var in ('localctx', 'otherctx'):
142 if var in vars(self):
142 if var in vars(self):
143 delattr(self, var)
143 delattr(self, var)
144 self._readmergedriver = None
144 self._readmergedriver = None
145 self._mdstate = 's'
145 self._mdstate = 's'
146 unsupported = set()
146 unsupported = set()
147 records = self._readrecords()
147 records = self._readrecords()
148 for rtype, record in records:
148 for rtype, record in records:
149 if rtype == 'L':
149 if rtype == 'L':
150 self._local = bin(record)
150 self._local = bin(record)
151 elif rtype == 'O':
151 elif rtype == 'O':
152 self._other = bin(record)
152 self._other = bin(record)
153 elif rtype == 'm':
153 elif rtype == 'm':
154 bits = record.split('\0', 1)
154 bits = record.split('\0', 1)
155 mdstate = bits[1]
155 mdstate = bits[1]
156 if len(mdstate) != 1 or mdstate not in 'ums':
156 if len(mdstate) != 1 or mdstate not in 'ums':
157 # the merge driver should be idempotent, so just rerun it
157 # the merge driver should be idempotent, so just rerun it
158 mdstate = 'u'
158 mdstate = 'u'
159
159
160 self._readmergedriver = bits[0]
160 self._readmergedriver = bits[0]
161 self._mdstate = mdstate
161 self._mdstate = mdstate
162 elif rtype in 'FDC':
162 elif rtype in 'FDC':
163 bits = record.split('\0')
163 bits = record.split('\0')
164 self._state[bits[0]] = bits[1:]
164 self._state[bits[0]] = bits[1:]
165 elif rtype == 'f':
165 elif rtype == 'f':
166 filename, rawextras = record.split('\0', 1)
166 filename, rawextras = record.split('\0', 1)
167 extraparts = rawextras.split('\0')
167 extraparts = rawextras.split('\0')
168 extras = {}
168 extras = {}
169 i = 0
169 i = 0
170 while i < len(extraparts):
170 while i < len(extraparts):
171 extras[extraparts[i]] = extraparts[i + 1]
171 extras[extraparts[i]] = extraparts[i + 1]
172 i += 2
172 i += 2
173
173
174 self._stateextras[filename] = extras
174 self._stateextras[filename] = extras
175 elif rtype == 'l':
175 elif rtype == 'l':
176 labels = record.split('\0', 2)
176 labels = record.split('\0', 2)
177 self._labels = [l for l in labels if len(l) > 0]
177 self._labels = [l for l in labels if len(l) > 0]
178 elif not rtype.islower():
178 elif not rtype.islower():
179 unsupported.add(rtype)
179 unsupported.add(rtype)
180 self._results = {}
180 self._results = {}
181 self._dirty = False
181 self._dirty = False
182
182
183 if unsupported:
183 if unsupported:
184 raise error.UnsupportedMergeRecords(unsupported)
184 raise error.UnsupportedMergeRecords(unsupported)
185
185
186 def _readrecords(self):
186 def _readrecords(self):
187 """Read merge state from disk and return a list of record (TYPE, data)
187 """Read merge state from disk and return a list of record (TYPE, data)
188
188
189 We read data from both v1 and v2 files and decide which one to use.
189 We read data from both v1 and v2 files and decide which one to use.
190
190
191 V1 has been used by version prior to 2.9.1 and contains less data than
191 V1 has been used by version prior to 2.9.1 and contains less data than
192 v2. We read both versions and check if no data in v2 contradicts
192 v2. We read both versions and check if no data in v2 contradicts
193 v1. If there is not contradiction we can safely assume that both v1
193 v1. If there is not contradiction we can safely assume that both v1
194 and v2 were written at the same time and use the extract data in v2. If
194 and v2 were written at the same time and use the extract data in v2. If
195 there is contradiction we ignore v2 content as we assume an old version
195 there is contradiction we ignore v2 content as we assume an old version
196 of Mercurial has overwritten the mergestate file and left an old v2
196 of Mercurial has overwritten the mergestate file and left an old v2
197 file around.
197 file around.
198
198
199 returns list of record [(TYPE, data), ...]"""
199 returns list of record [(TYPE, data), ...]"""
200 v1records = self._readrecordsv1()
200 v1records = self._readrecordsv1()
201 v2records = self._readrecordsv2()
201 v2records = self._readrecordsv2()
202 if self._v1v2match(v1records, v2records):
202 if self._v1v2match(v1records, v2records):
203 return v2records
203 return v2records
204 else:
204 else:
205 # v1 file is newer than v2 file, use it
205 # v1 file is newer than v2 file, use it
206 # we have to infer the "other" changeset of the merge
206 # we have to infer the "other" changeset of the merge
207 # we cannot do better than that with v1 of the format
207 # we cannot do better than that with v1 of the format
208 mctx = self._repo[None].parents()[-1]
208 mctx = self._repo[None].parents()[-1]
209 v1records.append(('O', mctx.hex()))
209 v1records.append(('O', mctx.hex()))
210 # add place holder "other" file node information
210 # add place holder "other" file node information
211 # nobody is using it yet so we do no need to fetch the data
211 # nobody is using it yet so we do no need to fetch the data
212 # if mctx was wrong `mctx[bits[-2]]` may fails.
212 # if mctx was wrong `mctx[bits[-2]]` may fails.
213 for idx, r in enumerate(v1records):
213 for idx, r in enumerate(v1records):
214 if r[0] == 'F':
214 if r[0] == 'F':
215 bits = r[1].split('\0')
215 bits = r[1].split('\0')
216 bits.insert(-2, '')
216 bits.insert(-2, '')
217 v1records[idx] = (r[0], '\0'.join(bits))
217 v1records[idx] = (r[0], '\0'.join(bits))
218 return v1records
218 return v1records
219
219
220 def _v1v2match(self, v1records, v2records):
220 def _v1v2match(self, v1records, v2records):
221 oldv2 = set() # old format version of v2 record
221 oldv2 = set() # old format version of v2 record
222 for rec in v2records:
222 for rec in v2records:
223 if rec[0] == 'L':
223 if rec[0] == 'L':
224 oldv2.add(rec)
224 oldv2.add(rec)
225 elif rec[0] == 'F':
225 elif rec[0] == 'F':
226 # drop the onode data (not contained in v1)
226 # drop the onode data (not contained in v1)
227 oldv2.add(('F', _droponode(rec[1])))
227 oldv2.add(('F', _droponode(rec[1])))
228 for rec in v1records:
228 for rec in v1records:
229 if rec not in oldv2:
229 if rec not in oldv2:
230 return False
230 return False
231 else:
231 else:
232 return True
232 return True
233
233
234 def _readrecordsv1(self):
234 def _readrecordsv1(self):
235 """read on disk merge state for version 1 file
235 """read on disk merge state for version 1 file
236
236
237 returns list of record [(TYPE, data), ...]
237 returns list of record [(TYPE, data), ...]
238
238
239 Note: the "F" data from this file are one entry short
239 Note: the "F" data from this file are one entry short
240 (no "other file node" entry)
240 (no "other file node" entry)
241 """
241 """
242 records = []
242 records = []
243 try:
243 try:
244 f = self._repo.vfs(self.statepathv1)
244 f = self._repo.vfs(self.statepathv1)
245 for i, l in enumerate(f):
245 for i, l in enumerate(f):
246 if i == 0:
246 if i == 0:
247 records.append(('L', l[:-1]))
247 records.append(('L', l[:-1]))
248 else:
248 else:
249 records.append(('F', l[:-1]))
249 records.append(('F', l[:-1]))
250 f.close()
250 f.close()
251 except IOError as err:
251 except IOError as err:
252 if err.errno != errno.ENOENT:
252 if err.errno != errno.ENOENT:
253 raise
253 raise
254 return records
254 return records
255
255
256 def _readrecordsv2(self):
256 def _readrecordsv2(self):
257 """read on disk merge state for version 2 file
257 """read on disk merge state for version 2 file
258
258
259 This format is a list of arbitrary records of the form:
259 This format is a list of arbitrary records of the form:
260
260
261 [type][length][content]
261 [type][length][content]
262
262
263 `type` is a single character, `length` is a 4 byte integer, and
263 `type` is a single character, `length` is a 4 byte integer, and
264 `content` is an arbitrary byte sequence of length `length`.
264 `content` is an arbitrary byte sequence of length `length`.
265
265
266 Mercurial versions prior to 3.7 have a bug where if there are
266 Mercurial versions prior to 3.7 have a bug where if there are
267 unsupported mandatory merge records, attempting to clear out the merge
267 unsupported mandatory merge records, attempting to clear out the merge
268 state with hg update --clean or similar aborts. The 't' record type
268 state with hg update --clean or similar aborts. The 't' record type
269 works around that by writing out what those versions treat as an
269 works around that by writing out what those versions treat as an
270 advisory record, but later versions interpret as special: the first
270 advisory record, but later versions interpret as special: the first
271 character is the 'real' record type and everything onwards is the data.
271 character is the 'real' record type and everything onwards is the data.
272
272
273 Returns list of records [(TYPE, data), ...]."""
273 Returns list of records [(TYPE, data), ...]."""
274 records = []
274 records = []
275 try:
275 try:
276 f = self._repo.vfs(self.statepathv2)
276 f = self._repo.vfs(self.statepathv2)
277 data = f.read()
277 data = f.read()
278 off = 0
278 off = 0
279 end = len(data)
279 end = len(data)
280 while off < end:
280 while off < end:
281 rtype = data[off]
281 rtype = data[off]
282 off += 1
282 off += 1
283 length = _unpack('>I', data[off:(off + 4)])[0]
283 length = _unpack('>I', data[off:(off + 4)])[0]
284 off += 4
284 off += 4
285 record = data[off:(off + length)]
285 record = data[off:(off + length)]
286 off += length
286 off += length
287 if rtype == 't':
287 if rtype == 't':
288 rtype, record = record[0], record[1:]
288 rtype, record = record[0], record[1:]
289 records.append((rtype, record))
289 records.append((rtype, record))
290 f.close()
290 f.close()
291 except IOError as err:
291 except IOError as err:
292 if err.errno != errno.ENOENT:
292 if err.errno != errno.ENOENT:
293 raise
293 raise
294 return records
294 return records
295
295
296 @util.propertycache
296 @util.propertycache
297 def mergedriver(self):
297 def mergedriver(self):
298 # protect against the following:
298 # protect against the following:
299 # - A configures a malicious merge driver in their hgrc, then
299 # - A configures a malicious merge driver in their hgrc, then
300 # pauses the merge
300 # pauses the merge
301 # - A edits their hgrc to remove references to the merge driver
301 # - A edits their hgrc to remove references to the merge driver
302 # - A gives a copy of their entire repo, including .hg, to B
302 # - A gives a copy of their entire repo, including .hg, to B
303 # - B inspects .hgrc and finds it to be clean
303 # - B inspects .hgrc and finds it to be clean
304 # - B then continues the merge and the malicious merge driver
304 # - B then continues the merge and the malicious merge driver
305 # gets invoked
305 # gets invoked
306 configmergedriver = self._repo.ui.config('experimental', 'mergedriver')
306 configmergedriver = self._repo.ui.config('experimental', 'mergedriver')
307 if (self._readmergedriver is not None
307 if (self._readmergedriver is not None
308 and self._readmergedriver != configmergedriver):
308 and self._readmergedriver != configmergedriver):
309 raise error.ConfigError(
309 raise error.ConfigError(
310 _("merge driver changed since merge started"),
310 _("merge driver changed since merge started"),
311 hint=_("revert merge driver change or abort merge"))
311 hint=_("revert merge driver change or abort merge"))
312
312
313 return configmergedriver
313 return configmergedriver
314
314
315 @util.propertycache
315 @util.propertycache
316 def localctx(self):
316 def localctx(self):
317 if self._local is None:
317 if self._local is None:
318 raise RuntimeError("localctx accessed but self._local isn't set")
318 raise RuntimeError("localctx accessed but self._local isn't set")
319 return self._repo[self._local]
319 return self._repo[self._local]
320
320
321 @util.propertycache
321 @util.propertycache
322 def otherctx(self):
322 def otherctx(self):
323 if self._other is None:
323 if self._other is None:
324 raise RuntimeError("otherctx accessed but self._other isn't set")
324 raise RuntimeError("otherctx accessed but self._other isn't set")
325 return self._repo[self._other]
325 return self._repo[self._other]
326
326
327 def active(self):
327 def active(self):
328 """Whether mergestate is active.
328 """Whether mergestate is active.
329
329
330 Returns True if there appears to be mergestate. This is a rough proxy
330 Returns True if there appears to be mergestate. This is a rough proxy
331 for "is a merge in progress."
331 for "is a merge in progress."
332 """
332 """
333 # Check local variables before looking at filesystem for performance
333 # Check local variables before looking at filesystem for performance
334 # reasons.
334 # reasons.
335 return bool(self._local) or bool(self._state) or \
335 return bool(self._local) or bool(self._state) or \
336 self._repo.vfs.exists(self.statepathv1) or \
336 self._repo.vfs.exists(self.statepathv1) or \
337 self._repo.vfs.exists(self.statepathv2)
337 self._repo.vfs.exists(self.statepathv2)
338
338
339 def commit(self):
339 def commit(self):
340 """Write current state on disk (if necessary)"""
340 """Write current state on disk (if necessary)"""
341 if self._dirty:
341 if self._dirty:
342 records = self._makerecords()
342 records = self._makerecords()
343 self._writerecords(records)
343 self._writerecords(records)
344 self._dirty = False
344 self._dirty = False
345
345
346 def _makerecords(self):
346 def _makerecords(self):
347 records = []
347 records = []
348 records.append(('L', hex(self._local)))
348 records.append(('L', hex(self._local)))
349 records.append(('O', hex(self._other)))
349 records.append(('O', hex(self._other)))
350 if self.mergedriver:
350 if self.mergedriver:
351 records.append(('m', '\0'.join([
351 records.append(('m', '\0'.join([
352 self.mergedriver, self._mdstate])))
352 self.mergedriver, self._mdstate])))
353 for d, v in self._state.iteritems():
353 for d, v in self._state.iteritems():
354 if v[0] == 'd':
354 if v[0] == 'd':
355 records.append(('D', '\0'.join([d] + v)))
355 records.append(('D', '\0'.join([d] + v)))
356 # v[1] == local ('cd'), v[6] == other ('dc') -- not supported by
356 # v[1] == local ('cd'), v[6] == other ('dc') -- not supported by
357 # older versions of Mercurial
357 # older versions of Mercurial
358 elif v[1] == nullhex or v[6] == nullhex:
358 elif v[1] == nullhex or v[6] == nullhex:
359 records.append(('C', '\0'.join([d] + v)))
359 records.append(('C', '\0'.join([d] + v)))
360 else:
360 else:
361 records.append(('F', '\0'.join([d] + v)))
361 records.append(('F', '\0'.join([d] + v)))
362 for filename, extras in sorted(self._stateextras.iteritems()):
362 for filename, extras in sorted(self._stateextras.iteritems()):
363 rawextras = '\0'.join('%s\0%s' % (k, v) for k, v in
363 rawextras = '\0'.join('%s\0%s' % (k, v) for k, v in
364 extras.iteritems())
364 extras.iteritems())
365 records.append(('f', '%s\0%s' % (filename, rawextras)))
365 records.append(('f', '%s\0%s' % (filename, rawextras)))
366 if self._labels is not None:
366 if self._labels is not None:
367 labels = '\0'.join(self._labels)
367 labels = '\0'.join(self._labels)
368 records.append(('l', labels))
368 records.append(('l', labels))
369 return records
369 return records
370
370
371 def _writerecords(self, records):
371 def _writerecords(self, records):
372 """Write current state on disk (both v1 and v2)"""
372 """Write current state on disk (both v1 and v2)"""
373 self._writerecordsv1(records)
373 self._writerecordsv1(records)
374 self._writerecordsv2(records)
374 self._writerecordsv2(records)
375
375
376 def _writerecordsv1(self, records):
376 def _writerecordsv1(self, records):
377 """Write current state on disk in a version 1 file"""
377 """Write current state on disk in a version 1 file"""
378 f = self._repo.vfs(self.statepathv1, 'w')
378 f = self._repo.vfs(self.statepathv1, 'w')
379 irecords = iter(records)
379 irecords = iter(records)
380 lrecords = next(irecords)
380 lrecords = next(irecords)
381 assert lrecords[0] == 'L'
381 assert lrecords[0] == 'L'
382 f.write(hex(self._local) + '\n')
382 f.write(hex(self._local) + '\n')
383 for rtype, data in irecords:
383 for rtype, data in irecords:
384 if rtype == 'F':
384 if rtype == 'F':
385 f.write('%s\n' % _droponode(data))
385 f.write('%s\n' % _droponode(data))
386 f.close()
386 f.close()
387
387
388 def _writerecordsv2(self, records):
388 def _writerecordsv2(self, records):
389 """Write current state on disk in a version 2 file
389 """Write current state on disk in a version 2 file
390
390
391 See the docstring for _readrecordsv2 for why we use 't'."""
391 See the docstring for _readrecordsv2 for why we use 't'."""
392 # these are the records that all version 2 clients can read
392 # these are the records that all version 2 clients can read
393 whitelist = 'LOF'
393 whitelist = 'LOF'
394 f = self._repo.vfs(self.statepathv2, 'w')
394 f = self._repo.vfs(self.statepathv2, 'w')
395 for key, data in records:
395 for key, data in records:
396 assert len(key) == 1
396 assert len(key) == 1
397 if key not in whitelist:
397 if key not in whitelist:
398 key, data = 't', '%s%s' % (key, data)
398 key, data = 't', '%s%s' % (key, data)
399 format = '>sI%is' % len(data)
399 format = '>sI%is' % len(data)
400 f.write(_pack(format, key, len(data), data))
400 f.write(_pack(format, key, len(data), data))
401 f.close()
401 f.close()
402
402
403 def add(self, fcl, fco, fca, fd):
403 def add(self, fcl, fco, fca, fd):
404 """add a new (potentially?) conflicting file the merge state
404 """add a new (potentially?) conflicting file the merge state
405 fcl: file context for local,
405 fcl: file context for local,
406 fco: file context for remote,
406 fco: file context for remote,
407 fca: file context for ancestors,
407 fca: file context for ancestors,
408 fd: file path of the resulting merge.
408 fd: file path of the resulting merge.
409
409
410 note: also write the local version to the `.hg/merge` directory.
410 note: also write the local version to the `.hg/merge` directory.
411 """
411 """
412 if fcl.isabsent():
412 if fcl.isabsent():
413 hash = nullhex
413 hash = nullhex
414 else:
414 else:
415 hash = hashlib.sha1(fcl.path()).hexdigest()
415 hash = hashlib.sha1(fcl.path()).hexdigest()
416 self._repo.vfs.write('merge/' + hash, fcl.data())
416 self._repo.vfs.write('merge/' + hash, fcl.data())
417 self._state[fd] = ['u', hash, fcl.path(),
417 self._state[fd] = ['u', hash, fcl.path(),
418 fca.path(), hex(fca.filenode()),
418 fca.path(), hex(fca.filenode()),
419 fco.path(), hex(fco.filenode()),
419 fco.path(), hex(fco.filenode()),
420 fcl.flags()]
420 fcl.flags()]
421 self._stateextras[fd] = { 'ancestorlinknode' : hex(fca.node()) }
421 self._stateextras[fd] = { 'ancestorlinknode' : hex(fca.node()) }
422 self._dirty = True
422 self._dirty = True
423
423
424 def __contains__(self, dfile):
424 def __contains__(self, dfile):
425 return dfile in self._state
425 return dfile in self._state
426
426
427 def __getitem__(self, dfile):
427 def __getitem__(self, dfile):
428 return self._state[dfile][0]
428 return self._state[dfile][0]
429
429
430 def __iter__(self):
430 def __iter__(self):
431 return iter(sorted(self._state))
431 return iter(sorted(self._state))
432
432
433 def files(self):
433 def files(self):
434 return self._state.keys()
434 return self._state.keys()
435
435
436 def mark(self, dfile, state):
436 def mark(self, dfile, state):
437 self._state[dfile][0] = state
437 self._state[dfile][0] = state
438 self._dirty = True
438 self._dirty = True
439
439
440 def mdstate(self):
440 def mdstate(self):
441 return self._mdstate
441 return self._mdstate
442
442
443 def unresolved(self):
443 def unresolved(self):
444 """Obtain the paths of unresolved files."""
444 """Obtain the paths of unresolved files."""
445
445
446 for f, entry in self._state.items():
446 for f, entry in self._state.items():
447 if entry[0] == 'u':
447 if entry[0] == 'u':
448 yield f
448 yield f
449
449
450 def driverresolved(self):
450 def driverresolved(self):
451 """Obtain the paths of driver-resolved files."""
451 """Obtain the paths of driver-resolved files."""
452
452
453 for f, entry in self._state.items():
453 for f, entry in self._state.items():
454 if entry[0] == 'd':
454 if entry[0] == 'd':
455 yield f
455 yield f
456
456
457 def extras(self, filename):
457 def extras(self, filename):
458 return self._stateextras.setdefault(filename, {})
458 return self._stateextras.setdefault(filename, {})
459
459
460 def _resolve(self, preresolve, dfile, wctx):
460 def _resolve(self, preresolve, dfile, wctx):
461 """rerun merge process for file path `dfile`"""
461 """rerun merge process for file path `dfile`"""
462 if self[dfile] in 'rd':
462 if self[dfile] in 'rd':
463 return True, 0
463 return True, 0
464 stateentry = self._state[dfile]
464 stateentry = self._state[dfile]
465 state, hash, lfile, afile, anode, ofile, onode, flags = stateentry
465 state, hash, lfile, afile, anode, ofile, onode, flags = stateentry
466 octx = self._repo[self._other]
466 octx = self._repo[self._other]
467 extras = self.extras(dfile)
467 extras = self.extras(dfile)
468 anccommitnode = extras.get('ancestorlinknode')
468 anccommitnode = extras.get('ancestorlinknode')
469 if anccommitnode:
469 if anccommitnode:
470 actx = self._repo[anccommitnode]
470 actx = self._repo[anccommitnode]
471 else:
471 else:
472 actx = None
472 actx = None
473 fcd = self._filectxorabsent(hash, wctx, dfile)
473 fcd = self._filectxorabsent(hash, wctx, dfile)
474 fco = self._filectxorabsent(onode, octx, ofile)
474 fco = self._filectxorabsent(onode, octx, ofile)
475 # TODO: move this to filectxorabsent
475 # TODO: move this to filectxorabsent
476 fca = self._repo.filectx(afile, fileid=anode, changeid=actx)
476 fca = self._repo.filectx(afile, fileid=anode, changeid=actx)
477 # "premerge" x flags
477 # "premerge" x flags
478 flo = fco.flags()
478 flo = fco.flags()
479 fla = fca.flags()
479 fla = fca.flags()
480 if 'x' in flags + flo + fla and 'l' not in flags + flo + fla:
480 if 'x' in flags + flo + fla and 'l' not in flags + flo + fla:
481 if fca.node() == nullid and flags != flo:
481 if fca.node() == nullid and flags != flo:
482 if preresolve:
482 if preresolve:
483 self._repo.ui.warn(
483 self._repo.ui.warn(
484 _('warning: cannot merge flags for %s '
484 _('warning: cannot merge flags for %s '
485 'without common ancestor - keeping local flags\n')
485 'without common ancestor - keeping local flags\n')
486 % afile)
486 % afile)
487 elif flags == fla:
487 elif flags == fla:
488 flags = flo
488 flags = flo
489 if preresolve:
489 if preresolve:
490 # restore local
490 # restore local
491 if hash != nullhex:
491 if hash != nullhex:
492 f = self._repo.vfs('merge/' + hash)
492 f = self._repo.vfs('merge/' + hash)
493 self._repo.wwrite(dfile, f.read(), flags)
493 self._repo.wwrite(dfile, f.read(), flags)
494 f.close()
494 f.close()
495 else:
495 else:
496 self._repo.wvfs.unlinkpath(dfile, ignoremissing=True)
496 self._repo.wvfs.unlinkpath(dfile, ignoremissing=True)
497 complete, r, deleted = filemerge.premerge(self._repo, self._local,
497 complete, r, deleted = filemerge.premerge(self._repo, self._local,
498 lfile, fcd, fco, fca,
498 lfile, fcd, fco, fca,
499 labels=self._labels)
499 labels=self._labels)
500 else:
500 else:
501 complete, r, deleted = filemerge.filemerge(self._repo, self._local,
501 complete, r, deleted = filemerge.filemerge(self._repo, self._local,
502 lfile, fcd, fco, fca,
502 lfile, fcd, fco, fca,
503 labels=self._labels)
503 labels=self._labels)
504 if r is None:
504 if r is None:
505 # no real conflict
505 # no real conflict
506 del self._state[dfile]
506 del self._state[dfile]
507 self._stateextras.pop(dfile, None)
507 self._stateextras.pop(dfile, None)
508 self._dirty = True
508 self._dirty = True
509 elif not r:
509 elif not r:
510 self.mark(dfile, 'r')
510 self.mark(dfile, 'r')
511
511
512 if complete:
512 if complete:
513 action = None
513 action = None
514 if deleted:
514 if deleted:
515 if fcd.isabsent():
515 if fcd.isabsent():
516 # dc: local picked. Need to drop if present, which may
516 # dc: local picked. Need to drop if present, which may
517 # happen on re-resolves.
517 # happen on re-resolves.
518 action = 'f'
518 action = 'f'
519 else:
519 else:
520 # cd: remote picked (or otherwise deleted)
520 # cd: remote picked (or otherwise deleted)
521 action = 'r'
521 action = 'r'
522 else:
522 else:
523 if fcd.isabsent(): # dc: remote picked
523 if fcd.isabsent(): # dc: remote picked
524 action = 'g'
524 action = 'g'
525 elif fco.isabsent(): # cd: local picked
525 elif fco.isabsent(): # cd: local picked
526 if dfile in self.localctx:
526 if dfile in self.localctx:
527 action = 'am'
527 action = 'am'
528 else:
528 else:
529 action = 'a'
529 action = 'a'
530 # else: regular merges (no action necessary)
530 # else: regular merges (no action necessary)
531 self._results[dfile] = r, action
531 self._results[dfile] = r, action
532
532
533 return complete, r
533 return complete, r
534
534
535 def _filectxorabsent(self, hexnode, ctx, f):
535 def _filectxorabsent(self, hexnode, ctx, f):
536 if hexnode == nullhex:
536 if hexnode == nullhex:
537 return filemerge.absentfilectx(ctx, f)
537 return filemerge.absentfilectx(ctx, f)
538 else:
538 else:
539 return ctx[f]
539 return ctx[f]
540
540
541 def preresolve(self, dfile, wctx):
541 def preresolve(self, dfile, wctx):
542 """run premerge process for dfile
542 """run premerge process for dfile
543
543
544 Returns whether the merge is complete, and the exit code."""
544 Returns whether the merge is complete, and the exit code."""
545 return self._resolve(True, dfile, wctx)
545 return self._resolve(True, dfile, wctx)
546
546
547 def resolve(self, dfile, wctx):
547 def resolve(self, dfile, wctx):
548 """run merge process (assuming premerge was run) for dfile
548 """run merge process (assuming premerge was run) for dfile
549
549
550 Returns the exit code of the merge."""
550 Returns the exit code of the merge."""
551 return self._resolve(False, dfile, wctx)[1]
551 return self._resolve(False, dfile, wctx)[1]
552
552
553 def counts(self):
553 def counts(self):
554 """return counts for updated, merged and removed files in this
554 """return counts for updated, merged and removed files in this
555 session"""
555 session"""
556 updated, merged, removed = 0, 0, 0
556 updated, merged, removed = 0, 0, 0
557 for r, action in self._results.itervalues():
557 for r, action in self._results.itervalues():
558 if r is None:
558 if r is None:
559 updated += 1
559 updated += 1
560 elif r == 0:
560 elif r == 0:
561 if action == 'r':
561 if action == 'r':
562 removed += 1
562 removed += 1
563 else:
563 else:
564 merged += 1
564 merged += 1
565 return updated, merged, removed
565 return updated, merged, removed
566
566
567 def unresolvedcount(self):
567 def unresolvedcount(self):
568 """get unresolved count for this merge (persistent)"""
568 """get unresolved count for this merge (persistent)"""
569 return len([True for f, entry in self._state.iteritems()
569 return len([True for f, entry in self._state.iteritems()
570 if entry[0] == 'u'])
570 if entry[0] == 'u'])
571
571
572 def actions(self):
572 def actions(self):
573 """return lists of actions to perform on the dirstate"""
573 """return lists of actions to perform on the dirstate"""
574 actions = {'r': [], 'f': [], 'a': [], 'am': [], 'g': []}
574 actions = {'r': [], 'f': [], 'a': [], 'am': [], 'g': []}
575 for f, (r, action) in self._results.iteritems():
575 for f, (r, action) in self._results.iteritems():
576 if action is not None:
576 if action is not None:
577 actions[action].append((f, None, "merge result"))
577 actions[action].append((f, None, "merge result"))
578 return actions
578 return actions
579
579
580 def recordactions(self):
580 def recordactions(self):
581 """record remove/add/get actions in the dirstate"""
581 """record remove/add/get actions in the dirstate"""
582 branchmerge = self._repo.dirstate.p2() != nullid
582 branchmerge = self._repo.dirstate.p2() != nullid
583 recordupdates(self._repo, self.actions(), branchmerge)
583 recordupdates(self._repo, self.actions(), branchmerge)
584
584
585 def queueremove(self, f):
585 def queueremove(self, f):
586 """queues a file to be removed from the dirstate
586 """queues a file to be removed from the dirstate
587
587
588 Meant for use by custom merge drivers."""
588 Meant for use by custom merge drivers."""
589 self._results[f] = 0, 'r'
589 self._results[f] = 0, 'r'
590
590
591 def queueadd(self, f):
591 def queueadd(self, f):
592 """queues a file to be added to the dirstate
592 """queues a file to be added to the dirstate
593
593
594 Meant for use by custom merge drivers."""
594 Meant for use by custom merge drivers."""
595 self._results[f] = 0, 'a'
595 self._results[f] = 0, 'a'
596
596
597 def queueget(self, f):
597 def queueget(self, f):
598 """queues a file to be marked modified in the dirstate
598 """queues a file to be marked modified in the dirstate
599
599
600 Meant for use by custom merge drivers."""
600 Meant for use by custom merge drivers."""
601 self._results[f] = 0, 'g'
601 self._results[f] = 0, 'g'
602
602
603 def _getcheckunknownconfig(repo, section, name):
603 def _getcheckunknownconfig(repo, section, name):
604 config = repo.ui.config(section, name, default='abort')
604 config = repo.ui.config(section, name, default='abort')
605 valid = ['abort', 'ignore', 'warn']
605 valid = ['abort', 'ignore', 'warn']
606 if config not in valid:
606 if config not in valid:
607 validstr = ', '.join(["'" + v + "'" for v in valid])
607 validstr = ', '.join(["'" + v + "'" for v in valid])
608 raise error.ConfigError(_("%s.%s not valid "
608 raise error.ConfigError(_("%s.%s not valid "
609 "('%s' is none of %s)")
609 "('%s' is none of %s)")
610 % (section, name, config, validstr))
610 % (section, name, config, validstr))
611 return config
611 return config
612
612
613 def _checkunknownfile(repo, wctx, mctx, f, f2=None):
613 def _checkunknownfile(repo, wctx, mctx, f, f2=None):
614 if f2 is None:
614 if f2 is None:
615 f2 = f
615 f2 = f
616 return (repo.wvfs.audit.check(f)
616 return (repo.wvfs.audit.check(f)
617 and repo.wvfs.isfileorlink(f)
617 and repo.wvfs.isfileorlink(f)
618 and repo.dirstate.normalize(f) not in repo.dirstate
618 and repo.dirstate.normalize(f) not in repo.dirstate
619 and mctx[f2].cmp(wctx[f]))
619 and mctx[f2].cmp(wctx[f]))
620
620
621 def _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce):
621 def _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce):
622 """
622 """
623 Considers any actions that care about the presence of conflicting unknown
623 Considers any actions that care about the presence of conflicting unknown
624 files. For some actions, the result is to abort; for others, it is to
624 files. For some actions, the result is to abort; for others, it is to
625 choose a different action.
625 choose a different action.
626 """
626 """
627 conflicts = set()
627 conflicts = set()
628 warnconflicts = set()
628 warnconflicts = set()
629 abortconflicts = set()
629 abortconflicts = set()
630 unknownconfig = _getcheckunknownconfig(repo, 'merge', 'checkunknown')
630 unknownconfig = _getcheckunknownconfig(repo, 'merge', 'checkunknown')
631 ignoredconfig = _getcheckunknownconfig(repo, 'merge', 'checkignored')
631 ignoredconfig = _getcheckunknownconfig(repo, 'merge', 'checkignored')
632 if not force:
632 if not force:
633 def collectconflicts(conflicts, config):
633 def collectconflicts(conflicts, config):
634 if config == 'abort':
634 if config == 'abort':
635 abortconflicts.update(conflicts)
635 abortconflicts.update(conflicts)
636 elif config == 'warn':
636 elif config == 'warn':
637 warnconflicts.update(conflicts)
637 warnconflicts.update(conflicts)
638
638
639 for f, (m, args, msg) in actions.iteritems():
639 for f, (m, args, msg) in actions.iteritems():
640 if m in ('c', 'dc'):
640 if m in ('c', 'dc'):
641 if _checkunknownfile(repo, wctx, mctx, f):
641 if _checkunknownfile(repo, wctx, mctx, f):
642 conflicts.add(f)
642 conflicts.add(f)
643 elif m == 'dg':
643 elif m == 'dg':
644 if _checkunknownfile(repo, wctx, mctx, f, args[0]):
644 if _checkunknownfile(repo, wctx, mctx, f, args[0]):
645 conflicts.add(f)
645 conflicts.add(f)
646
646
647 ignoredconflicts = set([c for c in conflicts
647 ignoredconflicts = set([c for c in conflicts
648 if repo.dirstate._ignore(c)])
648 if repo.dirstate._ignore(c)])
649 unknownconflicts = conflicts - ignoredconflicts
649 unknownconflicts = conflicts - ignoredconflicts
650 collectconflicts(ignoredconflicts, ignoredconfig)
650 collectconflicts(ignoredconflicts, ignoredconfig)
651 collectconflicts(unknownconflicts, unknownconfig)
651 collectconflicts(unknownconflicts, unknownconfig)
652 else:
652 else:
653 for f, (m, args, msg) in actions.iteritems():
653 for f, (m, args, msg) in actions.iteritems():
654 if m == 'cm':
654 if m == 'cm':
655 fl2, anc = args
655 fl2, anc = args
656 different = _checkunknownfile(repo, wctx, mctx, f)
656 different = _checkunknownfile(repo, wctx, mctx, f)
657 if repo.dirstate._ignore(f):
657 if repo.dirstate._ignore(f):
658 config = ignoredconfig
658 config = ignoredconfig
659 else:
659 else:
660 config = unknownconfig
660 config = unknownconfig
661
661
662 # The behavior when force is True is described by this table:
662 # The behavior when force is True is described by this table:
663 # config different mergeforce | action backup
663 # config different mergeforce | action backup
664 # * n * | get n
664 # * n * | get n
665 # * y y | merge -
665 # * y y | merge -
666 # abort y n | merge - (1)
666 # abort y n | merge - (1)
667 # warn y n | warn + get y
667 # warn y n | warn + get y
668 # ignore y n | get y
668 # ignore y n | get y
669 #
669 #
670 # (1) this is probably the wrong behavior here -- we should
670 # (1) this is probably the wrong behavior here -- we should
671 # probably abort, but some actions like rebases currently
671 # probably abort, but some actions like rebases currently
672 # don't like an abort happening in the middle of
672 # don't like an abort happening in the middle of
673 # merge.update.
673 # merge.update.
674 if not different:
674 if not different:
675 actions[f] = ('g', (fl2, False), "remote created")
675 actions[f] = ('g', (fl2, False), "remote created")
676 elif mergeforce or config == 'abort':
676 elif mergeforce or config == 'abort':
677 actions[f] = ('m', (f, f, None, False, anc),
677 actions[f] = ('m', (f, f, None, False, anc),
678 "remote differs from untracked local")
678 "remote differs from untracked local")
679 elif config == 'abort':
679 elif config == 'abort':
680 abortconflicts.add(f)
680 abortconflicts.add(f)
681 else:
681 else:
682 if config == 'warn':
682 if config == 'warn':
683 warnconflicts.add(f)
683 warnconflicts.add(f)
684 actions[f] = ('g', (fl2, True), "remote created")
684 actions[f] = ('g', (fl2, True), "remote created")
685
685
686 for f in sorted(abortconflicts):
686 for f in sorted(abortconflicts):
687 repo.ui.warn(_("%s: untracked file differs\n") % f)
687 repo.ui.warn(_("%s: untracked file differs\n") % f)
688 if abortconflicts:
688 if abortconflicts:
689 raise error.Abort(_("untracked files in working directory "
689 raise error.Abort(_("untracked files in working directory "
690 "differ from files in requested revision"))
690 "differ from files in requested revision"))
691
691
692 for f in sorted(warnconflicts):
692 for f in sorted(warnconflicts):
693 repo.ui.warn(_("%s: replacing untracked file\n") % f)
693 repo.ui.warn(_("%s: replacing untracked file\n") % f)
694
694
695 for f, (m, args, msg) in actions.iteritems():
695 for f, (m, args, msg) in actions.iteritems():
696 backup = f in conflicts
696 backup = f in conflicts
697 if m == 'c':
697 if m == 'c':
698 flags, = args
698 flags, = args
699 actions[f] = ('g', (flags, backup), msg)
699 actions[f] = ('g', (flags, backup), msg)
700
700
701 def _forgetremoved(wctx, mctx, branchmerge):
701 def _forgetremoved(wctx, mctx, branchmerge):
702 """
702 """
703 Forget removed files
703 Forget removed files
704
704
705 If we're jumping between revisions (as opposed to merging), and if
705 If we're jumping between revisions (as opposed to merging), and if
706 neither the working directory nor the target rev has the file,
706 neither the working directory nor the target rev has the file,
707 then we need to remove it from the dirstate, to prevent the
707 then we need to remove it from the dirstate, to prevent the
708 dirstate from listing the file when it is no longer in the
708 dirstate from listing the file when it is no longer in the
709 manifest.
709 manifest.
710
710
711 If we're merging, and the other revision has removed a file
711 If we're merging, and the other revision has removed a file
712 that is not present in the working directory, we need to mark it
712 that is not present in the working directory, we need to mark it
713 as removed.
713 as removed.
714 """
714 """
715
715
716 actions = {}
716 actions = {}
717 m = 'f'
717 m = 'f'
718 if branchmerge:
718 if branchmerge:
719 m = 'r'
719 m = 'r'
720 for f in wctx.deleted():
720 for f in wctx.deleted():
721 if f not in mctx:
721 if f not in mctx:
722 actions[f] = m, None, "forget deleted"
722 actions[f] = m, None, "forget deleted"
723
723
724 if not branchmerge:
724 if not branchmerge:
725 for f in wctx.removed():
725 for f in wctx.removed():
726 if f not in mctx:
726 if f not in mctx:
727 actions[f] = 'f', None, "forget removed"
727 actions[f] = 'f', None, "forget removed"
728
728
729 return actions
729 return actions
730
730
731 def _checkcollision(repo, wmf, actions):
731 def _checkcollision(repo, wmf, actions):
732 # build provisional merged manifest up
732 # build provisional merged manifest up
733 pmmf = set(wmf)
733 pmmf = set(wmf)
734
734
735 if actions:
735 if actions:
736 # k, dr, e and rd are no-op
736 # k, dr, e and rd are no-op
737 for m in 'a', 'am', 'f', 'g', 'cd', 'dc':
737 for m in 'a', 'am', 'f', 'g', 'cd', 'dc':
738 for f, args, msg in actions[m]:
738 for f, args, msg in actions[m]:
739 pmmf.add(f)
739 pmmf.add(f)
740 for f, args, msg in actions['r']:
740 for f, args, msg in actions['r']:
741 pmmf.discard(f)
741 pmmf.discard(f)
742 for f, args, msg in actions['dm']:
742 for f, args, msg in actions['dm']:
743 f2, flags = args
743 f2, flags = args
744 pmmf.discard(f2)
744 pmmf.discard(f2)
745 pmmf.add(f)
745 pmmf.add(f)
746 for f, args, msg in actions['dg']:
746 for f, args, msg in actions['dg']:
747 pmmf.add(f)
747 pmmf.add(f)
748 for f, args, msg in actions['m']:
748 for f, args, msg in actions['m']:
749 f1, f2, fa, move, anc = args
749 f1, f2, fa, move, anc = args
750 if move:
750 if move:
751 pmmf.discard(f1)
751 pmmf.discard(f1)
752 pmmf.add(f)
752 pmmf.add(f)
753
753
754 # check case-folding collision in provisional merged manifest
754 # check case-folding collision in provisional merged manifest
755 foldmap = {}
755 foldmap = {}
756 for f in sorted(pmmf):
756 for f in sorted(pmmf):
757 fold = util.normcase(f)
757 fold = util.normcase(f)
758 if fold in foldmap:
758 if fold in foldmap:
759 raise error.Abort(_("case-folding collision between %s and %s")
759 raise error.Abort(_("case-folding collision between %s and %s")
760 % (f, foldmap[fold]))
760 % (f, foldmap[fold]))
761 foldmap[fold] = f
761 foldmap[fold] = f
762
762
763 # check case-folding of directories
763 # check case-folding of directories
764 foldprefix = unfoldprefix = lastfull = ''
764 foldprefix = unfoldprefix = lastfull = ''
765 for fold, f in sorted(foldmap.items()):
765 for fold, f in sorted(foldmap.items()):
766 if fold.startswith(foldprefix) and not f.startswith(unfoldprefix):
766 if fold.startswith(foldprefix) and not f.startswith(unfoldprefix):
767 # the folded prefix matches but actual casing is different
767 # the folded prefix matches but actual casing is different
768 raise error.Abort(_("case-folding collision between "
768 raise error.Abort(_("case-folding collision between "
769 "%s and directory of %s") % (lastfull, f))
769 "%s and directory of %s") % (lastfull, f))
770 foldprefix = fold + '/'
770 foldprefix = fold + '/'
771 unfoldprefix = f + '/'
771 unfoldprefix = f + '/'
772 lastfull = f
772 lastfull = f
773
773
774 def driverpreprocess(repo, ms, wctx, labels=None):
774 def driverpreprocess(repo, ms, wctx, labels=None):
775 """run the preprocess step of the merge driver, if any
775 """run the preprocess step of the merge driver, if any
776
776
777 This is currently not implemented -- it's an extension point."""
777 This is currently not implemented -- it's an extension point."""
778 return True
778 return True
779
779
780 def driverconclude(repo, ms, wctx, labels=None):
780 def driverconclude(repo, ms, wctx, labels=None):
781 """run the conclude step of the merge driver, if any
781 """run the conclude step of the merge driver, if any
782
782
783 This is currently not implemented -- it's an extension point."""
783 This is currently not implemented -- it's an extension point."""
784 return True
784 return True
785
785
786 def manifestmerge(repo, wctx, p2, pa, branchmerge, force, matcher,
786 def manifestmerge(repo, wctx, p2, pa, branchmerge, force, matcher,
787 acceptremote, followcopies):
787 acceptremote, followcopies):
788 """
788 """
789 Merge wctx and p2 with ancestor pa and generate merge action list
789 Merge wctx and p2 with ancestor pa and generate merge action list
790
790
791 branchmerge and force are as passed in to update
791 branchmerge and force are as passed in to update
792 matcher = matcher to filter file lists
792 matcher = matcher to filter file lists
793 acceptremote = accept the incoming changes without prompting
793 acceptremote = accept the incoming changes without prompting
794 """
794 """
795 if matcher is not None and matcher.always():
795 if matcher is not None and matcher.always():
796 matcher = None
796 matcher = None
797
797
798 copy, movewithdir, diverge, renamedelete, dirmove = {}, {}, {}, {}, {}
798 copy, movewithdir, diverge, renamedelete, dirmove = {}, {}, {}, {}, {}
799
799
800 # manifests fetched in order are going to be faster, so prime the caches
800 # manifests fetched in order are going to be faster, so prime the caches
801 [x.manifest() for x in
801 [x.manifest() for x in
802 sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
802 sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
803
803
804 if followcopies:
804 if followcopies:
805 ret = copies.mergecopies(repo, wctx, p2, pa)
805 ret = copies.mergecopies(repo, wctx, p2, pa)
806 copy, movewithdir, diverge, renamedelete, dirmove = ret
806 copy, movewithdir, diverge, renamedelete, dirmove = ret
807
807
808 repo.ui.note(_("resolving manifests\n"))
808 repo.ui.note(_("resolving manifests\n"))
809 repo.ui.debug(" branchmerge: %s, force: %s, partial: %s\n"
809 repo.ui.debug(" branchmerge: %s, force: %s, partial: %s\n"
810 % (bool(branchmerge), bool(force), bool(matcher)))
810 % (bool(branchmerge), bool(force), bool(matcher)))
811 repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, wctx, p2))
811 repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, wctx, p2))
812
812
813 m1, m2, ma = wctx.manifest(), p2.manifest(), pa.manifest()
813 m1, m2, ma = wctx.manifest(), p2.manifest(), pa.manifest()
814 copied = set(copy.values())
814 copied = set(copy.values())
815 copied.update(movewithdir.values())
815 copied.update(movewithdir.values())
816
816
817 if '.hgsubstate' in m1:
817 if '.hgsubstate' in m1:
818 # check whether sub state is modified
818 # check whether sub state is modified
819 if any(wctx.sub(s).dirty() for s in wctx.substate):
819 if any(wctx.sub(s).dirty() for s in wctx.substate):
820 m1['.hgsubstate'] = modifiednodeid
820 m1['.hgsubstate'] = modifiednodeid
821
821
822 diff = m1.diff(m2, match=matcher)
822 diff = m1.diff(m2, match=matcher)
823
823
824 if matcher is None:
824 if matcher is None:
825 matcher = matchmod.always('', '')
825 matcher = matchmod.always('', '')
826
826
827 actions = {}
827 actions = {}
828 for f, ((n1, fl1), (n2, fl2)) in diff.iteritems():
828 for f, ((n1, fl1), (n2, fl2)) in diff.iteritems():
829 if n1 and n2: # file exists on both local and remote side
829 if n1 and n2: # file exists on both local and remote side
830 if f not in ma:
830 if f not in ma:
831 fa = copy.get(f, None)
831 fa = copy.get(f, None)
832 if fa is not None:
832 if fa is not None:
833 actions[f] = ('m', (f, f, fa, False, pa.node()),
833 actions[f] = ('m', (f, f, fa, False, pa.node()),
834 "both renamed from " + fa)
834 "both renamed from " + fa)
835 else:
835 else:
836 actions[f] = ('m', (f, f, None, False, pa.node()),
836 actions[f] = ('m', (f, f, None, False, pa.node()),
837 "both created")
837 "both created")
838 else:
838 else:
839 a = ma[f]
839 a = ma[f]
840 fla = ma.flags(f)
840 fla = ma.flags(f)
841 nol = 'l' not in fl1 + fl2 + fla
841 nol = 'l' not in fl1 + fl2 + fla
842 if n2 == a and fl2 == fla:
842 if n2 == a and fl2 == fla:
843 actions[f] = ('k' , (), "remote unchanged")
843 actions[f] = ('k' , (), "remote unchanged")
844 elif n1 == a and fl1 == fla: # local unchanged - use remote
844 elif n1 == a and fl1 == fla: # local unchanged - use remote
845 if n1 == n2: # optimization: keep local content
845 if n1 == n2: # optimization: keep local content
846 actions[f] = ('e', (fl2,), "update permissions")
846 actions[f] = ('e', (fl2,), "update permissions")
847 else:
847 else:
848 actions[f] = ('g', (fl2, False), "remote is newer")
848 actions[f] = ('g', (fl2, False), "remote is newer")
849 elif nol and n2 == a: # remote only changed 'x'
849 elif nol and n2 == a: # remote only changed 'x'
850 actions[f] = ('e', (fl2,), "update permissions")
850 actions[f] = ('e', (fl2,), "update permissions")
851 elif nol and n1 == a: # local only changed 'x'
851 elif nol and n1 == a: # local only changed 'x'
852 actions[f] = ('g', (fl1, False), "remote is newer")
852 actions[f] = ('g', (fl1, False), "remote is newer")
853 else: # both changed something
853 else: # both changed something
854 actions[f] = ('m', (f, f, f, False, pa.node()),
854 actions[f] = ('m', (f, f, f, False, pa.node()),
855 "versions differ")
855 "versions differ")
856 elif n1: # file exists only on local side
856 elif n1: # file exists only on local side
857 if f in copied:
857 if f in copied:
858 pass # we'll deal with it on m2 side
858 pass # we'll deal with it on m2 side
859 elif f in movewithdir: # directory rename, move local
859 elif f in movewithdir: # directory rename, move local
860 f2 = movewithdir[f]
860 f2 = movewithdir[f]
861 if matcher(f2) and f2 in m2:
861 if matcher(f2) and f2 in m2:
862 actions[f2] = ('m', (f, f2, None, True, pa.node()),
862 actions[f2] = ('m', (f, f2, None, True, pa.node()),
863 "remote directory rename, both created")
863 "remote directory rename, both created")
864 else:
864 else:
865 actions[f2] = ('dm', (f, fl1),
865 actions[f2] = ('dm', (f, fl1),
866 "remote directory rename - move from " + f)
866 "remote directory rename - move from " + f)
867 elif f in copy:
867 elif f in copy:
868 f2 = copy[f]
868 f2 = copy[f]
869 actions[f] = ('m', (f, f2, f2, False, pa.node()),
869 actions[f] = ('m', (f, f2, f2, False, pa.node()),
870 "local copied/moved from " + f2)
870 "local copied/moved from " + f2)
871 elif f in ma: # clean, a different, no remote
871 elif f in ma: # clean, a different, no remote
872 if n1 != ma[f]:
872 if n1 != ma[f]:
873 if acceptremote:
873 if acceptremote:
874 actions[f] = ('r', None, "remote delete")
874 actions[f] = ('r', None, "remote delete")
875 else:
875 else:
876 actions[f] = ('cd', (f, None, f, False, pa.node()),
876 actions[f] = ('cd', (f, None, f, False, pa.node()),
877 "prompt changed/deleted")
877 "prompt changed/deleted")
878 elif n1 == addednodeid:
878 elif n1 == addednodeid:
879 # This extra 'a' is added by working copy manifest to mark
879 # This extra 'a' is added by working copy manifest to mark
880 # the file as locally added. We should forget it instead of
880 # the file as locally added. We should forget it instead of
881 # deleting it.
881 # deleting it.
882 actions[f] = ('f', None, "remote deleted")
882 actions[f] = ('f', None, "remote deleted")
883 else:
883 else:
884 actions[f] = ('r', None, "other deleted")
884 actions[f] = ('r', None, "other deleted")
885 elif n2: # file exists only on remote side
885 elif n2: # file exists only on remote side
886 if f in copied:
886 if f in copied:
887 pass # we'll deal with it on m1 side
887 pass # we'll deal with it on m1 side
888 elif f in movewithdir:
888 elif f in movewithdir:
889 f2 = movewithdir[f]
889 f2 = movewithdir[f]
890 if matcher(f2) and f2 in m1:
890 if matcher(f2) and f2 in m1:
891 actions[f2] = ('m', (f2, f, None, False, pa.node()),
891 actions[f2] = ('m', (f2, f, None, False, pa.node()),
892 "local directory rename, both created")
892 "local directory rename, both created")
893 else:
893 else:
894 actions[f2] = ('dg', (f, fl2),
894 actions[f2] = ('dg', (f, fl2),
895 "local directory rename - get from " + f)
895 "local directory rename - get from " + f)
896 elif f in copy:
896 elif f in copy:
897 f2 = copy[f]
897 f2 = copy[f]
898 if matcher(f2) and f2 in m2:
898 if matcher(f2) and f2 in m2:
899 actions[f] = ('m', (f2, f, f2, False, pa.node()),
899 actions[f] = ('m', (f2, f, f2, False, pa.node()),
900 "remote copied from " + f2)
900 "remote copied from " + f2)
901 else:
901 else:
902 actions[f] = ('m', (f2, f, f2, True, pa.node()),
902 actions[f] = ('m', (f2, f, f2, True, pa.node()),
903 "remote moved from " + f2)
903 "remote moved from " + f2)
904 elif f not in ma:
904 elif f not in ma:
905 # local unknown, remote created: the logic is described by the
905 # local unknown, remote created: the logic is described by the
906 # following table:
906 # following table:
907 #
907 #
908 # force branchmerge different | action
908 # force branchmerge different | action
909 # n * * | create
909 # n * * | create
910 # y n * | create
910 # y n * | create
911 # y y n | create
911 # y y n | create
912 # y y y | merge
912 # y y y | merge
913 #
913 #
914 # Checking whether the files are different is expensive, so we
914 # Checking whether the files are different is expensive, so we
915 # don't do that when we can avoid it.
915 # don't do that when we can avoid it.
916 if not force:
916 if not force:
917 actions[f] = ('c', (fl2,), "remote created")
917 actions[f] = ('c', (fl2,), "remote created")
918 elif not branchmerge:
918 elif not branchmerge:
919 actions[f] = ('c', (fl2,), "remote created")
919 actions[f] = ('c', (fl2,), "remote created")
920 else:
920 else:
921 actions[f] = ('cm', (fl2, pa.node()),
921 actions[f] = ('cm', (fl2, pa.node()),
922 "remote created, get or merge")
922 "remote created, get or merge")
923 elif n2 != ma[f]:
923 elif n2 != ma[f]:
924 df = None
924 df = None
925 for d in dirmove:
925 for d in dirmove:
926 if f.startswith(d):
926 if f.startswith(d):
927 # new file added in a directory that was moved
927 # new file added in a directory that was moved
928 df = dirmove[d] + f[len(d):]
928 df = dirmove[d] + f[len(d):]
929 break
929 break
930 if matcher(df) and df in m1:
930 if matcher(df) and df in m1:
931 actions[df] = ('m', (df, f, f, False, pa.node()),
931 actions[df] = ('m', (df, f, f, False, pa.node()),
932 "local directory rename - respect move from " + f)
932 "local directory rename - respect move from " + f)
933 elif acceptremote:
933 elif acceptremote:
934 actions[f] = ('c', (fl2,), "remote recreating")
934 actions[f] = ('c', (fl2,), "remote recreating")
935 else:
935 else:
936 actions[f] = ('dc', (None, f, f, False, pa.node()),
936 actions[f] = ('dc', (None, f, f, False, pa.node()),
937 "prompt deleted/changed")
937 "prompt deleted/changed")
938
938
939 return actions, diverge, renamedelete
939 return actions, diverge, renamedelete
940
940
941 def _resolvetrivial(repo, wctx, mctx, ancestor, actions):
941 def _resolvetrivial(repo, wctx, mctx, ancestor, actions):
942 """Resolves false conflicts where the nodeid changed but the content
942 """Resolves false conflicts where the nodeid changed but the content
943 remained the same."""
943 remained the same."""
944
944
945 for f, (m, args, msg) in actions.items():
945 for f, (m, args, msg) in actions.items():
946 if m == 'cd' and f in ancestor and not wctx[f].cmp(ancestor[f]):
946 if m == 'cd' and f in ancestor and not wctx[f].cmp(ancestor[f]):
947 # local did change but ended up with same content
947 # local did change but ended up with same content
948 actions[f] = 'r', None, "prompt same"
948 actions[f] = 'r', None, "prompt same"
949 elif m == 'dc' and f in ancestor and not mctx[f].cmp(ancestor[f]):
949 elif m == 'dc' and f in ancestor and not mctx[f].cmp(ancestor[f]):
950 # remote did change but ended up with same content
950 # remote did change but ended up with same content
951 del actions[f] # don't get = keep local deleted
951 del actions[f] # don't get = keep local deleted
952
952
953 def calculateupdates(repo, wctx, mctx, ancestors, branchmerge, force,
953 def calculateupdates(repo, wctx, mctx, ancestors, branchmerge, force,
954 acceptremote, followcopies, matcher=None,
954 acceptremote, followcopies, matcher=None,
955 mergeforce=False):
955 mergeforce=False):
956 "Calculate the actions needed to merge mctx into wctx using ancestors"
956 "Calculate the actions needed to merge mctx into wctx using ancestors"
957 if len(ancestors) == 1: # default
957 if len(ancestors) == 1: # default
958 actions, diverge, renamedelete = manifestmerge(
958 actions, diverge, renamedelete = manifestmerge(
959 repo, wctx, mctx, ancestors[0], branchmerge, force, matcher,
959 repo, wctx, mctx, ancestors[0], branchmerge, force, matcher,
960 acceptremote, followcopies)
960 acceptremote, followcopies)
961 _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce)
961 _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce)
962
962
963 else: # only when merge.preferancestor=* - the default
963 else: # only when merge.preferancestor=* - the default
964 repo.ui.note(
964 repo.ui.note(
965 _("note: merging %s and %s using bids from ancestors %s\n") %
965 _("note: merging %s and %s using bids from ancestors %s\n") %
966 (wctx, mctx, _(' and ').join(str(anc) for anc in ancestors)))
966 (wctx, mctx, _(' and ').join(str(anc) for anc in ancestors)))
967
967
968 # Call for bids
968 # Call for bids
969 fbids = {} # mapping filename to bids (action method to list af actions)
969 fbids = {} # mapping filename to bids (action method to list af actions)
970 diverge, renamedelete = None, None
970 diverge, renamedelete = None, None
971 for ancestor in ancestors:
971 for ancestor in ancestors:
972 repo.ui.note(_('\ncalculating bids for ancestor %s\n') % ancestor)
972 repo.ui.note(_('\ncalculating bids for ancestor %s\n') % ancestor)
973 actions, diverge1, renamedelete1 = manifestmerge(
973 actions, diverge1, renamedelete1 = manifestmerge(
974 repo, wctx, mctx, ancestor, branchmerge, force, matcher,
974 repo, wctx, mctx, ancestor, branchmerge, force, matcher,
975 acceptremote, followcopies)
975 acceptremote, followcopies)
976 _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce)
976 _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce)
977
977
978 # Track the shortest set of warning on the theory that bid
978 # Track the shortest set of warning on the theory that bid
979 # merge will correctly incorporate more information
979 # merge will correctly incorporate more information
980 if diverge is None or len(diverge1) < len(diverge):
980 if diverge is None or len(diverge1) < len(diverge):
981 diverge = diverge1
981 diverge = diverge1
982 if renamedelete is None or len(renamedelete) < len(renamedelete1):
982 if renamedelete is None or len(renamedelete) < len(renamedelete1):
983 renamedelete = renamedelete1
983 renamedelete = renamedelete1
984
984
985 for f, a in sorted(actions.iteritems()):
985 for f, a in sorted(actions.iteritems()):
986 m, args, msg = a
986 m, args, msg = a
987 repo.ui.debug(' %s: %s -> %s\n' % (f, msg, m))
987 repo.ui.debug(' %s: %s -> %s\n' % (f, msg, m))
988 if f in fbids:
988 if f in fbids:
989 d = fbids[f]
989 d = fbids[f]
990 if m in d:
990 if m in d:
991 d[m].append(a)
991 d[m].append(a)
992 else:
992 else:
993 d[m] = [a]
993 d[m] = [a]
994 else:
994 else:
995 fbids[f] = {m: [a]}
995 fbids[f] = {m: [a]}
996
996
997 # Pick the best bid for each file
997 # Pick the best bid for each file
998 repo.ui.note(_('\nauction for merging merge bids\n'))
998 repo.ui.note(_('\nauction for merging merge bids\n'))
999 actions = {}
999 actions = {}
1000 dms = [] # filenames that have dm actions
1000 dms = [] # filenames that have dm actions
1001 for f, bids in sorted(fbids.items()):
1001 for f, bids in sorted(fbids.items()):
1002 # bids is a mapping from action method to list af actions
1002 # bids is a mapping from action method to list af actions
1003 # Consensus?
1003 # Consensus?
1004 if len(bids) == 1: # all bids are the same kind of method
1004 if len(bids) == 1: # all bids are the same kind of method
1005 m, l = bids.items()[0]
1005 m, l = bids.items()[0]
1006 if all(a == l[0] for a in l[1:]): # len(bids) is > 1
1006 if all(a == l[0] for a in l[1:]): # len(bids) is > 1
1007 repo.ui.note(_(" %s: consensus for %s\n") % (f, m))
1007 repo.ui.note(_(" %s: consensus for %s\n") % (f, m))
1008 actions[f] = l[0]
1008 actions[f] = l[0]
1009 if m == 'dm':
1009 if m == 'dm':
1010 dms.append(f)
1010 dms.append(f)
1011 continue
1011 continue
1012 # If keep is an option, just do it.
1012 # If keep is an option, just do it.
1013 if 'k' in bids:
1013 if 'k' in bids:
1014 repo.ui.note(_(" %s: picking 'keep' action\n") % f)
1014 repo.ui.note(_(" %s: picking 'keep' action\n") % f)
1015 actions[f] = bids['k'][0]
1015 actions[f] = bids['k'][0]
1016 continue
1016 continue
1017 # If there are gets and they all agree [how could they not?], do it.
1017 # If there are gets and they all agree [how could they not?], do it.
1018 if 'g' in bids:
1018 if 'g' in bids:
1019 ga0 = bids['g'][0]
1019 ga0 = bids['g'][0]
1020 if all(a == ga0 for a in bids['g'][1:]):
1020 if all(a == ga0 for a in bids['g'][1:]):
1021 repo.ui.note(_(" %s: picking 'get' action\n") % f)
1021 repo.ui.note(_(" %s: picking 'get' action\n") % f)
1022 actions[f] = ga0
1022 actions[f] = ga0
1023 continue
1023 continue
1024 # TODO: Consider other simple actions such as mode changes
1024 # TODO: Consider other simple actions such as mode changes
1025 # Handle inefficient democrazy.
1025 # Handle inefficient democrazy.
1026 repo.ui.note(_(' %s: multiple bids for merge action:\n') % f)
1026 repo.ui.note(_(' %s: multiple bids for merge action:\n') % f)
1027 for m, l in sorted(bids.items()):
1027 for m, l in sorted(bids.items()):
1028 for _f, args, msg in l:
1028 for _f, args, msg in l:
1029 repo.ui.note(' %s -> %s\n' % (msg, m))
1029 repo.ui.note(' %s -> %s\n' % (msg, m))
1030 # Pick random action. TODO: Instead, prompt user when resolving
1030 # Pick random action. TODO: Instead, prompt user when resolving
1031 m, l = bids.items()[0]
1031 m, l = bids.items()[0]
1032 repo.ui.warn(_(' %s: ambiguous merge - picked %s action\n') %
1032 repo.ui.warn(_(' %s: ambiguous merge - picked %s action\n') %
1033 (f, m))
1033 (f, m))
1034 actions[f] = l[0]
1034 actions[f] = l[0]
1035 if m == 'dm':
1035 if m == 'dm':
1036 dms.append(f)
1036 dms.append(f)
1037 continue
1037 continue
1038 # Work around 'dm' that can cause multiple actions for the same file
1038 # Work around 'dm' that can cause multiple actions for the same file
1039 for f in dms:
1039 for f in dms:
1040 dm, (f0, flags), msg = actions[f]
1040 dm, (f0, flags), msg = actions[f]
1041 assert dm == 'dm', dm
1041 assert dm == 'dm', dm
1042 if f0 in actions and actions[f0][0] == 'r':
1042 if f0 in actions and actions[f0][0] == 'r':
1043 # We have one bid for removing a file and another for moving it.
1043 # We have one bid for removing a file and another for moving it.
1044 # These two could be merged as first move and then delete ...
1044 # These two could be merged as first move and then delete ...
1045 # but instead drop moving and just delete.
1045 # but instead drop moving and just delete.
1046 del actions[f]
1046 del actions[f]
1047 repo.ui.note(_('end of auction\n\n'))
1047 repo.ui.note(_('end of auction\n\n'))
1048
1048
1049 _resolvetrivial(repo, wctx, mctx, ancestors[0], actions)
1049 _resolvetrivial(repo, wctx, mctx, ancestors[0], actions)
1050
1050
1051 if wctx.rev() is None:
1051 if wctx.rev() is None:
1052 fractions = _forgetremoved(wctx, mctx, branchmerge)
1052 fractions = _forgetremoved(wctx, mctx, branchmerge)
1053 actions.update(fractions)
1053 actions.update(fractions)
1054
1054
1055 return actions, diverge, renamedelete
1055 return actions, diverge, renamedelete
1056
1056
1057 def batchremove(repo, actions):
1057 def batchremove(repo, actions):
1058 """apply removes to the working directory
1058 """apply removes to the working directory
1059
1059
1060 yields tuples for progress updates
1060 yields tuples for progress updates
1061 """
1061 """
1062 verbose = repo.ui.verbose
1062 verbose = repo.ui.verbose
1063 unlinkpath = repo.wvfs.unlinkpath
1063 unlinkpath = repo.wvfs.unlinkpath
1064 audit = repo.wvfs.audit
1064 audit = repo.wvfs.audit
1065 try:
1065 try:
1066 cwd = pycompat.getcwd()
1066 cwd = pycompat.getcwd()
1067 except OSError as err:
1067 except OSError as err:
1068 if err.errno != errno.ENOENT:
1068 if err.errno != errno.ENOENT:
1069 raise
1069 raise
1070 cwd = None
1070 cwd = None
1071 i = 0
1071 i = 0
1072 for f, args, msg in actions:
1072 for f, args, msg in actions:
1073 repo.ui.debug(" %s: %s -> r\n" % (f, msg))
1073 repo.ui.debug(" %s: %s -> r\n" % (f, msg))
1074 if verbose:
1074 if verbose:
1075 repo.ui.note(_("removing %s\n") % f)
1075 repo.ui.note(_("removing %s\n") % f)
1076 audit(f)
1076 audit(f)
1077 try:
1077 try:
1078 unlinkpath(f, ignoremissing=True)
1078 unlinkpath(f, ignoremissing=True)
1079 except OSError as inst:
1079 except OSError as inst:
1080 repo.ui.warn(_("update failed to remove %s: %s!\n") %
1080 repo.ui.warn(_("update failed to remove %s: %s!\n") %
1081 (f, inst.strerror))
1081 (f, inst.strerror))
1082 if i == 100:
1082 if i == 100:
1083 yield i, f
1083 yield i, f
1084 i = 0
1084 i = 0
1085 i += 1
1085 i += 1
1086 if i > 0:
1086 if i > 0:
1087 yield i, f
1087 yield i, f
1088 if cwd:
1088 if cwd:
1089 # cwd was present before we started to remove files
1089 # cwd was present before we started to remove files
1090 # let's check if it is present after we removed them
1090 # let's check if it is present after we removed them
1091 try:
1091 try:
1092 pycompat.getcwd()
1092 pycompat.getcwd()
1093 except OSError as err:
1093 except OSError as err:
1094 if err.errno != errno.ENOENT:
1094 if err.errno != errno.ENOENT:
1095 raise
1095 raise
1096 # Print a warning if cwd was deleted
1096 # Print a warning if cwd was deleted
1097 repo.ui.warn(_("current directory was removed\n"
1097 repo.ui.warn(_("current directory was removed\n"
1098 "(consider changing to repo root: %s)\n") %
1098 "(consider changing to repo root: %s)\n") %
1099 repo.root)
1099 repo.root)
1100
1100
1101 def batchget(repo, mctx, actions):
1101 def batchget(repo, mctx, actions):
1102 """apply gets to the working directory
1102 """apply gets to the working directory
1103
1103
1104 mctx is the context to get from
1104 mctx is the context to get from
1105
1105
1106 yields tuples for progress updates
1106 yields tuples for progress updates
1107 """
1107 """
1108 verbose = repo.ui.verbose
1108 verbose = repo.ui.verbose
1109 fctx = mctx.filectx
1109 fctx = mctx.filectx
1110 wwrite = repo.wwrite
1110 wwrite = repo.wwrite
1111 ui = repo.ui
1111 ui = repo.ui
1112 i = 0
1112 i = 0
1113 with repo.wvfs.backgroundclosing(ui, expectedcount=len(actions)):
1113 with repo.wvfs.backgroundclosing(ui, expectedcount=len(actions)):
1114 for f, (flags, backup), msg in actions:
1114 for f, (flags, backup), msg in actions:
1115 repo.ui.debug(" %s: %s -> g\n" % (f, msg))
1115 repo.ui.debug(" %s: %s -> g\n" % (f, msg))
1116 if verbose:
1116 if verbose:
1117 repo.ui.note(_("getting %s\n") % f)
1117 repo.ui.note(_("getting %s\n") % f)
1118
1118
1119 if backup:
1119 if backup:
1120 absf = repo.wjoin(f)
1120 absf = repo.wjoin(f)
1121 orig = scmutil.origpath(ui, repo, absf)
1121 orig = scmutil.origpath(ui, repo, absf)
1122 try:
1122 try:
1123 if repo.wvfs.isfileorlink(f):
1123 if repo.wvfs.isfileorlink(f):
1124 util.rename(absf, orig)
1124 util.rename(absf, orig)
1125 except OSError as e:
1125 except OSError as e:
1126 if e.errno != errno.ENOENT:
1126 if e.errno != errno.ENOENT:
1127 raise
1127 raise
1128
1128
1129 if repo.wvfs.isdir(f) and not repo.wvfs.islink(f):
1129 if repo.wvfs.isdir(f) and not repo.wvfs.islink(f):
1130 repo.wvfs.removedirs(f)
1130 repo.wvfs.removedirs(f)
1131 wwrite(f, fctx(f).data(), flags, backgroundclose=True)
1131 wwrite(f, fctx(f).data(), flags, backgroundclose=True)
1132 if i == 100:
1132 if i == 100:
1133 yield i, f
1133 yield i, f
1134 i = 0
1134 i = 0
1135 i += 1
1135 i += 1
1136 if i > 0:
1136 if i > 0:
1137 yield i, f
1137 yield i, f
1138
1138
1139 def applyupdates(repo, actions, wctx, mctx, overwrite, labels=None):
1139 def applyupdates(repo, actions, wctx, mctx, overwrite, labels=None):
1140 """apply the merge action list to the working directory
1140 """apply the merge action list to the working directory
1141
1141
1142 wctx is the working copy context
1142 wctx is the working copy context
1143 mctx is the context to be merged into the working copy
1143 mctx is the context to be merged into the working copy
1144
1144
1145 Return a tuple of counts (updated, merged, removed, unresolved) that
1145 Return a tuple of counts (updated, merged, removed, unresolved) that
1146 describes how many files were affected by the update.
1146 describes how many files were affected by the update.
1147 """
1147 """
1148
1148
1149 updated, merged, removed = 0, 0, 0
1149 updated, merged, removed = 0, 0, 0
1150 ms = mergestate.clean(repo, wctx.p1().node(), mctx.node(), labels)
1150 ms = mergestate.clean(repo, wctx.p1().node(), mctx.node(), labels)
1151 moves = []
1151 moves = []
1152 for m, l in actions.items():
1152 for m, l in actions.items():
1153 l.sort()
1153 l.sort()
1154
1154
1155 # 'cd' and 'dc' actions are treated like other merge conflicts
1155 # 'cd' and 'dc' actions are treated like other merge conflicts
1156 mergeactions = sorted(actions['cd'])
1156 mergeactions = sorted(actions['cd'])
1157 mergeactions.extend(sorted(actions['dc']))
1157 mergeactions.extend(sorted(actions['dc']))
1158 mergeactions.extend(actions['m'])
1158 mergeactions.extend(actions['m'])
1159 for f, args, msg in mergeactions:
1159 for f, args, msg in mergeactions:
1160 f1, f2, fa, move, anc = args
1160 f1, f2, fa, move, anc = args
1161 if f == '.hgsubstate': # merged internally
1161 if f == '.hgsubstate': # merged internally
1162 continue
1162 continue
1163 if f1 is None:
1163 if f1 is None:
1164 fcl = filemerge.absentfilectx(wctx, fa)
1164 fcl = filemerge.absentfilectx(wctx, fa)
1165 else:
1165 else:
1166 repo.ui.debug(" preserving %s for resolve of %s\n" % (f1, f))
1166 repo.ui.debug(" preserving %s for resolve of %s\n" % (f1, f))
1167 fcl = wctx[f1]
1167 fcl = wctx[f1]
1168 if f2 is None:
1168 if f2 is None:
1169 fco = filemerge.absentfilectx(mctx, fa)
1169 fco = filemerge.absentfilectx(mctx, fa)
1170 else:
1170 else:
1171 fco = mctx[f2]
1171 fco = mctx[f2]
1172 actx = repo[anc]
1172 actx = repo[anc]
1173 if fa in actx:
1173 if fa in actx:
1174 fca = actx[fa]
1174 fca = actx[fa]
1175 else:
1175 else:
1176 # TODO: move to absentfilectx
1176 # TODO: move to absentfilectx
1177 fca = repo.filectx(f1, fileid=nullrev)
1177 fca = repo.filectx(f1, fileid=nullrev)
1178 ms.add(fcl, fco, fca, f)
1178 ms.add(fcl, fco, fca, f)
1179 if f1 != f and move:
1179 if f1 != f and move:
1180 moves.append(f1)
1180 moves.append(f1)
1181
1181
1182 audit = repo.wvfs.audit
1182 audit = repo.wvfs.audit
1183 _updating = _('updating')
1183 _updating = _('updating')
1184 _files = _('files')
1184 _files = _('files')
1185 progress = repo.ui.progress
1185 progress = repo.ui.progress
1186
1186
1187 # remove renamed files after safely stored
1187 # remove renamed files after safely stored
1188 for f in moves:
1188 for f in moves:
1189 if os.path.lexists(repo.wjoin(f)):
1189 if os.path.lexists(repo.wjoin(f)):
1190 repo.ui.debug("removing %s\n" % f)
1190 repo.ui.debug("removing %s\n" % f)
1191 audit(f)
1191 audit(f)
1192 repo.wvfs.unlinkpath(f)
1192 repo.wvfs.unlinkpath(f)
1193
1193
1194 numupdates = sum(len(l) for m, l in actions.items() if m != 'k')
1194 numupdates = sum(len(l) for m, l in actions.items() if m != 'k')
1195
1195
1196 if [a for a in actions['r'] if a[0] == '.hgsubstate']:
1196 if [a for a in actions['r'] if a[0] == '.hgsubstate']:
1197 subrepo.submerge(repo, wctx, mctx, wctx, overwrite, labels)
1197 subrepo.submerge(repo, wctx, mctx, wctx, overwrite, labels)
1198
1198
1199 # remove in parallel (must come first)
1199 # remove in parallel (must come first)
1200 z = 0
1200 z = 0
1201 prog = worker.worker(repo.ui, 0.001, batchremove, (repo,), actions['r'])
1201 prog = worker.worker(repo.ui, 0.001, batchremove, (repo,), actions['r'])
1202 for i, item in prog:
1202 for i, item in prog:
1203 z += i
1203 z += i
1204 progress(_updating, z, item=item, total=numupdates, unit=_files)
1204 progress(_updating, z, item=item, total=numupdates, unit=_files)
1205 removed = len(actions['r'])
1205 removed = len(actions['r'])
1206
1206
1207 # get in parallel
1207 # get in parallel
1208 prog = worker.worker(repo.ui, 0.001, batchget, (repo, mctx), actions['g'])
1208 prog = worker.worker(repo.ui, 0.001, batchget, (repo, mctx), actions['g'])
1209 for i, item in prog:
1209 for i, item in prog:
1210 z += i
1210 z += i
1211 progress(_updating, z, item=item, total=numupdates, unit=_files)
1211 progress(_updating, z, item=item, total=numupdates, unit=_files)
1212 updated = len(actions['g'])
1212 updated = len(actions['g'])
1213
1213
1214 if [a for a in actions['g'] if a[0] == '.hgsubstate']:
1214 if [a for a in actions['g'] if a[0] == '.hgsubstate']:
1215 subrepo.submerge(repo, wctx, mctx, wctx, overwrite, labels)
1215 subrepo.submerge(repo, wctx, mctx, wctx, overwrite, labels)
1216
1216
1217 # forget (manifest only, just log it) (must come first)
1217 # forget (manifest only, just log it) (must come first)
1218 for f, args, msg in actions['f']:
1218 for f, args, msg in actions['f']:
1219 repo.ui.debug(" %s: %s -> f\n" % (f, msg))
1219 repo.ui.debug(" %s: %s -> f\n" % (f, msg))
1220 z += 1
1220 z += 1
1221 progress(_updating, z, item=f, total=numupdates, unit=_files)
1221 progress(_updating, z, item=f, total=numupdates, unit=_files)
1222
1222
1223 # re-add (manifest only, just log it)
1223 # re-add (manifest only, just log it)
1224 for f, args, msg in actions['a']:
1224 for f, args, msg in actions['a']:
1225 repo.ui.debug(" %s: %s -> a\n" % (f, msg))
1225 repo.ui.debug(" %s: %s -> a\n" % (f, msg))
1226 z += 1
1226 z += 1
1227 progress(_updating, z, item=f, total=numupdates, unit=_files)
1227 progress(_updating, z, item=f, total=numupdates, unit=_files)
1228
1228
1229 # re-add/mark as modified (manifest only, just log it)
1229 # re-add/mark as modified (manifest only, just log it)
1230 for f, args, msg in actions['am']:
1230 for f, args, msg in actions['am']:
1231 repo.ui.debug(" %s: %s -> am\n" % (f, msg))
1231 repo.ui.debug(" %s: %s -> am\n" % (f, msg))
1232 z += 1
1232 z += 1
1233 progress(_updating, z, item=f, total=numupdates, unit=_files)
1233 progress(_updating, z, item=f, total=numupdates, unit=_files)
1234
1234
1235 # keep (noop, just log it)
1235 # keep (noop, just log it)
1236 for f, args, msg in actions['k']:
1236 for f, args, msg in actions['k']:
1237 repo.ui.debug(" %s: %s -> k\n" % (f, msg))
1237 repo.ui.debug(" %s: %s -> k\n" % (f, msg))
1238 # no progress
1238 # no progress
1239
1239
1240 # directory rename, move local
1240 # directory rename, move local
1241 for f, args, msg in actions['dm']:
1241 for f, args, msg in actions['dm']:
1242 repo.ui.debug(" %s: %s -> dm\n" % (f, msg))
1242 repo.ui.debug(" %s: %s -> dm\n" % (f, msg))
1243 z += 1
1243 z += 1
1244 progress(_updating, z, item=f, total=numupdates, unit=_files)
1244 progress(_updating, z, item=f, total=numupdates, unit=_files)
1245 f0, flags = args
1245 f0, flags = args
1246 repo.ui.note(_("moving %s to %s\n") % (f0, f))
1246 repo.ui.note(_("moving %s to %s\n") % (f0, f))
1247 audit(f)
1247 audit(f)
1248 repo.wwrite(f, wctx.filectx(f0).data(), flags)
1248 repo.wwrite(f, wctx.filectx(f0).data(), flags)
1249 repo.wvfs.unlinkpath(f0)
1249 repo.wvfs.unlinkpath(f0)
1250 updated += 1
1250 updated += 1
1251
1251
1252 # local directory rename, get
1252 # local directory rename, get
1253 for f, args, msg in actions['dg']:
1253 for f, args, msg in actions['dg']:
1254 repo.ui.debug(" %s: %s -> dg\n" % (f, msg))
1254 repo.ui.debug(" %s: %s -> dg\n" % (f, msg))
1255 z += 1
1255 z += 1
1256 progress(_updating, z, item=f, total=numupdates, unit=_files)
1256 progress(_updating, z, item=f, total=numupdates, unit=_files)
1257 f0, flags = args
1257 f0, flags = args
1258 repo.ui.note(_("getting %s to %s\n") % (f0, f))
1258 repo.ui.note(_("getting %s to %s\n") % (f0, f))
1259 repo.wwrite(f, mctx.filectx(f0).data(), flags)
1259 repo.wwrite(f, mctx.filectx(f0).data(), flags)
1260 updated += 1
1260 updated += 1
1261
1261
1262 # exec
1262 # exec
1263 for f, args, msg in actions['e']:
1263 for f, args, msg in actions['e']:
1264 repo.ui.debug(" %s: %s -> e\n" % (f, msg))
1264 repo.ui.debug(" %s: %s -> e\n" % (f, msg))
1265 z += 1
1265 z += 1
1266 progress(_updating, z, item=f, total=numupdates, unit=_files)
1266 progress(_updating, z, item=f, total=numupdates, unit=_files)
1267 flags, = args
1267 flags, = args
1268 audit(f)
1268 audit(f)
1269 util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags)
1269 util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags)
1270 updated += 1
1270 updated += 1
1271
1271
1272 # the ordering is important here -- ms.mergedriver will raise if the merge
1272 # the ordering is important here -- ms.mergedriver will raise if the merge
1273 # driver has changed, and we want to be able to bypass it when overwrite is
1273 # driver has changed, and we want to be able to bypass it when overwrite is
1274 # True
1274 # True
1275 usemergedriver = not overwrite and mergeactions and ms.mergedriver
1275 usemergedriver = not overwrite and mergeactions and ms.mergedriver
1276
1276
1277 if usemergedriver:
1277 if usemergedriver:
1278 ms.commit()
1278 ms.commit()
1279 proceed = driverpreprocess(repo, ms, wctx, labels=labels)
1279 proceed = driverpreprocess(repo, ms, wctx, labels=labels)
1280 # the driver might leave some files unresolved
1280 # the driver might leave some files unresolved
1281 unresolvedf = set(ms.unresolved())
1281 unresolvedf = set(ms.unresolved())
1282 if not proceed:
1282 if not proceed:
1283 # XXX setting unresolved to at least 1 is a hack to make sure we
1283 # XXX setting unresolved to at least 1 is a hack to make sure we
1284 # error out
1284 # error out
1285 return updated, merged, removed, max(len(unresolvedf), 1)
1285 return updated, merged, removed, max(len(unresolvedf), 1)
1286 newactions = []
1286 newactions = []
1287 for f, args, msg in mergeactions:
1287 for f, args, msg in mergeactions:
1288 if f in unresolvedf:
1288 if f in unresolvedf:
1289 newactions.append((f, args, msg))
1289 newactions.append((f, args, msg))
1290 mergeactions = newactions
1290 mergeactions = newactions
1291
1291
1292 # premerge
1292 # premerge
1293 tocomplete = []
1293 tocomplete = []
1294 for f, args, msg in mergeactions:
1294 for f, args, msg in mergeactions:
1295 repo.ui.debug(" %s: %s -> m (premerge)\n" % (f, msg))
1295 repo.ui.debug(" %s: %s -> m (premerge)\n" % (f, msg))
1296 z += 1
1296 z += 1
1297 progress(_updating, z, item=f, total=numupdates, unit=_files)
1297 progress(_updating, z, item=f, total=numupdates, unit=_files)
1298 if f == '.hgsubstate': # subrepo states need updating
1298 if f == '.hgsubstate': # subrepo states need updating
1299 subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx),
1299 subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx),
1300 overwrite, labels)
1300 overwrite, labels)
1301 continue
1301 continue
1302 audit(f)
1302 audit(f)
1303 complete, r = ms.preresolve(f, wctx)
1303 complete, r = ms.preresolve(f, wctx)
1304 if not complete:
1304 if not complete:
1305 numupdates += 1
1305 numupdates += 1
1306 tocomplete.append((f, args, msg))
1306 tocomplete.append((f, args, msg))
1307
1307
1308 # merge
1308 # merge
1309 for f, args, msg in tocomplete:
1309 for f, args, msg in tocomplete:
1310 repo.ui.debug(" %s: %s -> m (merge)\n" % (f, msg))
1310 repo.ui.debug(" %s: %s -> m (merge)\n" % (f, msg))
1311 z += 1
1311 z += 1
1312 progress(_updating, z, item=f, total=numupdates, unit=_files)
1312 progress(_updating, z, item=f, total=numupdates, unit=_files)
1313 ms.resolve(f, wctx)
1313 ms.resolve(f, wctx)
1314
1314
1315 ms.commit()
1315 ms.commit()
1316
1316
1317 unresolved = ms.unresolvedcount()
1317 unresolved = ms.unresolvedcount()
1318
1318
1319 if usemergedriver and not unresolved and ms.mdstate() != 's':
1319 if usemergedriver and not unresolved and ms.mdstate() != 's':
1320 if not driverconclude(repo, ms, wctx, labels=labels):
1320 if not driverconclude(repo, ms, wctx, labels=labels):
1321 # XXX setting unresolved to at least 1 is a hack to make sure we
1321 # XXX setting unresolved to at least 1 is a hack to make sure we
1322 # error out
1322 # error out
1323 unresolved = max(unresolved, 1)
1323 unresolved = max(unresolved, 1)
1324
1324
1325 ms.commit()
1325 ms.commit()
1326
1326
1327 msupdated, msmerged, msremoved = ms.counts()
1327 msupdated, msmerged, msremoved = ms.counts()
1328 updated += msupdated
1328 updated += msupdated
1329 merged += msmerged
1329 merged += msmerged
1330 removed += msremoved
1330 removed += msremoved
1331
1331
1332 extraactions = ms.actions()
1332 extraactions = ms.actions()
1333 if extraactions:
1333 if extraactions:
1334 mfiles = set(a[0] for a in actions['m'])
1334 mfiles = set(a[0] for a in actions['m'])
1335 for k, acts in extraactions.iteritems():
1335 for k, acts in extraactions.iteritems():
1336 actions[k].extend(acts)
1336 actions[k].extend(acts)
1337 # Remove these files from actions['m'] as well. This is important
1337 # Remove these files from actions['m'] as well. This is important
1338 # because in recordupdates, files in actions['m'] are processed
1338 # because in recordupdates, files in actions['m'] are processed
1339 # after files in other actions, and the merge driver might add
1339 # after files in other actions, and the merge driver might add
1340 # files to those actions via extraactions above. This can lead to a
1340 # files to those actions via extraactions above. This can lead to a
1341 # file being recorded twice, with poor results. This is especially
1341 # file being recorded twice, with poor results. This is especially
1342 # problematic for actions['r'] (currently only possible with the
1342 # problematic for actions['r'] (currently only possible with the
1343 # merge driver in the initial merge process; interrupted merges
1343 # merge driver in the initial merge process; interrupted merges
1344 # don't go through this flow).
1344 # don't go through this flow).
1345 #
1345 #
1346 # The real fix here is to have indexes by both file and action so
1346 # The real fix here is to have indexes by both file and action so
1347 # that when the action for a file is changed it is automatically
1347 # that when the action for a file is changed it is automatically
1348 # reflected in the other action lists. But that involves a more
1348 # reflected in the other action lists. But that involves a more
1349 # complex data structure, so this will do for now.
1349 # complex data structure, so this will do for now.
1350 #
1350 #
1351 # We don't need to do the same operation for 'dc' and 'cd' because
1351 # We don't need to do the same operation for 'dc' and 'cd' because
1352 # those lists aren't consulted again.
1352 # those lists aren't consulted again.
1353 mfiles.difference_update(a[0] for a in acts)
1353 mfiles.difference_update(a[0] for a in acts)
1354
1354
1355 actions['m'] = [a for a in actions['m'] if a[0] in mfiles]
1355 actions['m'] = [a for a in actions['m'] if a[0] in mfiles]
1356
1356
1357 progress(_updating, None, total=numupdates, unit=_files)
1357 progress(_updating, None, total=numupdates, unit=_files)
1358
1358
1359 return updated, merged, removed, unresolved
1359 return updated, merged, removed, unresolved
1360
1360
1361 def recordupdates(repo, actions, branchmerge):
1361 def recordupdates(repo, actions, branchmerge):
1362 "record merge actions to the dirstate"
1362 "record merge actions to the dirstate"
1363 # remove (must come first)
1363 # remove (must come first)
1364 for f, args, msg in actions.get('r', []):
1364 for f, args, msg in actions.get('r', []):
1365 if branchmerge:
1365 if branchmerge:
1366 repo.dirstate.remove(f)
1366 repo.dirstate.remove(f)
1367 else:
1367 else:
1368 repo.dirstate.drop(f)
1368 repo.dirstate.drop(f)
1369
1369
1370 # forget (must come first)
1370 # forget (must come first)
1371 for f, args, msg in actions.get('f', []):
1371 for f, args, msg in actions.get('f', []):
1372 repo.dirstate.drop(f)
1372 repo.dirstate.drop(f)
1373
1373
1374 # re-add
1374 # re-add
1375 for f, args, msg in actions.get('a', []):
1375 for f, args, msg in actions.get('a', []):
1376 repo.dirstate.add(f)
1376 repo.dirstate.add(f)
1377
1377
1378 # re-add/mark as modified
1378 # re-add/mark as modified
1379 for f, args, msg in actions.get('am', []):
1379 for f, args, msg in actions.get('am', []):
1380 if branchmerge:
1380 if branchmerge:
1381 repo.dirstate.normallookup(f)
1381 repo.dirstate.normallookup(f)
1382 else:
1382 else:
1383 repo.dirstate.add(f)
1383 repo.dirstate.add(f)
1384
1384
1385 # exec change
1385 # exec change
1386 for f, args, msg in actions.get('e', []):
1386 for f, args, msg in actions.get('e', []):
1387 repo.dirstate.normallookup(f)
1387 repo.dirstate.normallookup(f)
1388
1388
1389 # keep
1389 # keep
1390 for f, args, msg in actions.get('k', []):
1390 for f, args, msg in actions.get('k', []):
1391 pass
1391 pass
1392
1392
1393 # get
1393 # get
1394 for f, args, msg in actions.get('g', []):
1394 for f, args, msg in actions.get('g', []):
1395 if branchmerge:
1395 if branchmerge:
1396 repo.dirstate.otherparent(f)
1396 repo.dirstate.otherparent(f)
1397 else:
1397 else:
1398 repo.dirstate.normal(f)
1398 repo.dirstate.normal(f)
1399
1399
1400 # merge
1400 # merge
1401 for f, args, msg in actions.get('m', []):
1401 for f, args, msg in actions.get('m', []):
1402 f1, f2, fa, move, anc = args
1402 f1, f2, fa, move, anc = args
1403 if branchmerge:
1403 if branchmerge:
1404 # We've done a branch merge, mark this file as merged
1404 # We've done a branch merge, mark this file as merged
1405 # so that we properly record the merger later
1405 # so that we properly record the merger later
1406 repo.dirstate.merge(f)
1406 repo.dirstate.merge(f)
1407 if f1 != f2: # copy/rename
1407 if f1 != f2: # copy/rename
1408 if move:
1408 if move:
1409 repo.dirstate.remove(f1)
1409 repo.dirstate.remove(f1)
1410 if f1 != f:
1410 if f1 != f:
1411 repo.dirstate.copy(f1, f)
1411 repo.dirstate.copy(f1, f)
1412 else:
1412 else:
1413 repo.dirstate.copy(f2, f)
1413 repo.dirstate.copy(f2, f)
1414 else:
1414 else:
1415 # We've update-merged a locally modified file, so
1415 # We've update-merged a locally modified file, so
1416 # we set the dirstate to emulate a normal checkout
1416 # we set the dirstate to emulate a normal checkout
1417 # of that file some time in the past. Thus our
1417 # of that file some time in the past. Thus our
1418 # merge will appear as a normal local file
1418 # merge will appear as a normal local file
1419 # modification.
1419 # modification.
1420 if f2 == f: # file not locally copied/moved
1420 if f2 == f: # file not locally copied/moved
1421 repo.dirstate.normallookup(f)
1421 repo.dirstate.normallookup(f)
1422 if move:
1422 if move:
1423 repo.dirstate.drop(f1)
1423 repo.dirstate.drop(f1)
1424
1424
1425 # directory rename, move local
1425 # directory rename, move local
1426 for f, args, msg in actions.get('dm', []):
1426 for f, args, msg in actions.get('dm', []):
1427 f0, flag = args
1427 f0, flag = args
1428 if branchmerge:
1428 if branchmerge:
1429 repo.dirstate.add(f)
1429 repo.dirstate.add(f)
1430 repo.dirstate.remove(f0)
1430 repo.dirstate.remove(f0)
1431 repo.dirstate.copy(f0, f)
1431 repo.dirstate.copy(f0, f)
1432 else:
1432 else:
1433 repo.dirstate.normal(f)
1433 repo.dirstate.normal(f)
1434 repo.dirstate.drop(f0)
1434 repo.dirstate.drop(f0)
1435
1435
1436 # directory rename, get
1436 # directory rename, get
1437 for f, args, msg in actions.get('dg', []):
1437 for f, args, msg in actions.get('dg', []):
1438 f0, flag = args
1438 f0, flag = args
1439 if branchmerge:
1439 if branchmerge:
1440 repo.dirstate.add(f)
1440 repo.dirstate.add(f)
1441 repo.dirstate.copy(f0, f)
1441 repo.dirstate.copy(f0, f)
1442 else:
1442 else:
1443 repo.dirstate.normal(f)
1443 repo.dirstate.normal(f)
1444
1444
1445 def update(repo, node, branchmerge, force, ancestor=None,
1445 def update(repo, node, branchmerge, force, ancestor=None,
1446 mergeancestor=False, labels=None, matcher=None, mergeforce=False,
1446 mergeancestor=False, labels=None, matcher=None, mergeforce=False,
1447 updatecheck=None):
1447 updatecheck=None):
1448 """
1448 """
1449 Perform a merge between the working directory and the given node
1449 Perform a merge between the working directory and the given node
1450
1450
1451 node = the node to update to
1451 node = the node to update to
1452 branchmerge = whether to merge between branches
1452 branchmerge = whether to merge between branches
1453 force = whether to force branch merging or file overwriting
1453 force = whether to force branch merging or file overwriting
1454 matcher = a matcher to filter file lists (dirstate not updated)
1454 matcher = a matcher to filter file lists (dirstate not updated)
1455 mergeancestor = whether it is merging with an ancestor. If true,
1455 mergeancestor = whether it is merging with an ancestor. If true,
1456 we should accept the incoming changes for any prompts that occur.
1456 we should accept the incoming changes for any prompts that occur.
1457 If false, merging with an ancestor (fast-forward) is only allowed
1457 If false, merging with an ancestor (fast-forward) is only allowed
1458 between different named branches. This flag is used by rebase extension
1458 between different named branches. This flag is used by rebase extension
1459 as a temporary fix and should be avoided in general.
1459 as a temporary fix and should be avoided in general.
1460 labels = labels to use for base, local and other
1460 labels = labels to use for base, local and other
1461 mergeforce = whether the merge was run with 'merge --force' (deprecated): if
1461 mergeforce = whether the merge was run with 'merge --force' (deprecated): if
1462 this is True, then 'force' should be True as well.
1462 this is True, then 'force' should be True as well.
1463
1463
1464 The table below shows all the behaviors of the update command
1464 The table below shows all the behaviors of the update command
1465 given the -c and -C or no options, whether the working directory
1465 given the -c and -C or no options, whether the working directory
1466 is dirty, whether a revision is specified, and the relationship of
1466 is dirty, whether a revision is specified, and the relationship of
1467 the parent rev to the target rev (linear or not). Match from top first. The
1467 the parent rev to the target rev (linear or not). Match from top first. The
1468 -n option doesn't exist on the command line, but represents the
1468 -n option doesn't exist on the command line, but represents the
1469 experimental.updatecheck=noconflict option.
1469 experimental.updatecheck=noconflict option.
1470
1470
1471 This logic is tested by test-update-branches.t.
1471 This logic is tested by test-update-branches.t.
1472
1472
1473 -c -C -n -m dirty rev linear | result
1473 -c -C -n -m dirty rev linear | result
1474 y y * * * * * | (1)
1474 y y * * * * * | (1)
1475 y * y * * * * | (1)
1475 y * y * * * * | (1)
1476 y * * y * * * | (1)
1476 y * * y * * * | (1)
1477 * y y * * * * | (1)
1477 * y y * * * * | (1)
1478 * y * y * * * | (1)
1478 * y * y * * * | (1)
1479 * * y y * * * | (1)
1479 * * y y * * * | (1)
1480 * * * * * n n | x
1480 * * * * * n n | x
1481 * * * * n * * | ok
1481 * * * * n * * | ok
1482 n n n n y * y | merge
1482 n n n n y * y | merge
1483 n n n n y y n | (2)
1483 n n n n y y n | (2)
1484 n n n y y * * | merge
1484 n n n y y * * | merge
1485 n n y n y * * | merge if no conflict
1485 n n y n y * * | merge if no conflict
1486 n y n n y * * | discard
1486 n y n n y * * | discard
1487 y n n n y * * | (3)
1487 y n n n y * * | (3)
1488
1488
1489 x = can't happen
1489 x = can't happen
1490 * = don't-care
1490 * = don't-care
1491 1 = incompatible options (checked in commands.py)
1491 1 = incompatible options (checked in commands.py)
1492 2 = abort: uncommitted changes (commit or update --clean to discard changes)
1492 2 = abort: uncommitted changes (commit or update --clean to discard changes)
1493 3 = abort: uncommitted changes (checked in commands.py)
1493 3 = abort: uncommitted changes (checked in commands.py)
1494
1494
1495 Return the same tuple as applyupdates().
1495 Return the same tuple as applyupdates().
1496 """
1496 """
1497
1497
1498 # This function used to find the default destination if node was None, but
1498 # This function used to find the default destination if node was None, but
1499 # that's now in destutil.py.
1499 # that's now in destutil.py.
1500 assert node is not None
1500 assert node is not None
1501 if not branchmerge and not force:
1501 if not branchmerge and not force:
1502 # TODO: remove the default once all callers that pass branchmerge=False
1502 # TODO: remove the default once all callers that pass branchmerge=False
1503 # and force=False pass a value for updatecheck. We may want to allow
1503 # and force=False pass a value for updatecheck. We may want to allow
1504 # updatecheck='abort' to better suppport some of these callers.
1504 # updatecheck='abort' to better suppport some of these callers.
1505 if updatecheck is None:
1505 if updatecheck is None:
1506 updatecheck = 'linear'
1506 updatecheck = 'linear'
1507 assert updatecheck in ('none', 'linear', 'noconflict')
1507 assert updatecheck in ('none', 'linear', 'noconflict')
1508 # If we're doing a partial update, we need to skip updating
1508 # If we're doing a partial update, we need to skip updating
1509 # the dirstate, so make a note of any partial-ness to the
1509 # the dirstate, so make a note of any partial-ness to the
1510 # update here.
1510 # update here.
1511 if matcher is None or matcher.always():
1511 if matcher is None or matcher.always():
1512 partial = False
1512 partial = False
1513 else:
1513 else:
1514 partial = True
1514 partial = True
1515 with repo.wlock():
1515 with repo.wlock():
1516 wc = repo[None]
1516 wc = repo[None]
1517 pl = wc.parents()
1517 pl = wc.parents()
1518 p1 = pl[0]
1518 p1 = pl[0]
1519 pas = [None]
1519 pas = [None]
1520 if ancestor is not None:
1520 if ancestor is not None:
1521 pas = [repo[ancestor]]
1521 pas = [repo[ancestor]]
1522
1522
1523 overwrite = force and not branchmerge
1523 overwrite = force and not branchmerge
1524
1524
1525 p2 = repo[node]
1525 p2 = repo[node]
1526 if pas[0] is None:
1526 if pas[0] is None:
1527 if repo.ui.configlist('merge', 'preferancestor', ['*']) == ['*']:
1527 if repo.ui.configlist('merge', 'preferancestor', ['*']) == ['*']:
1528 cahs = repo.changelog.commonancestorsheads(p1.node(), p2.node())
1528 cahs = repo.changelog.commonancestorsheads(p1.node(), p2.node())
1529 pas = [repo[anc] for anc in (sorted(cahs) or [nullid])]
1529 pas = [repo[anc] for anc in (sorted(cahs) or [nullid])]
1530 else:
1530 else:
1531 pas = [p1.ancestor(p2, warn=branchmerge)]
1531 pas = [p1.ancestor(p2, warn=branchmerge)]
1532
1532
1533 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
1533 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
1534
1534
1535 ### check phase
1535 ### check phase
1536 if not overwrite:
1536 if not overwrite:
1537 if len(pl) > 1:
1537 if len(pl) > 1:
1538 raise error.Abort(_("outstanding uncommitted merge"))
1538 raise error.Abort(_("outstanding uncommitted merge"))
1539 ms = mergestate.read(repo)
1539 ms = mergestate.read(repo)
1540 if list(ms.unresolved()):
1540 if list(ms.unresolved()):
1541 raise error.Abort(_("outstanding merge conflicts"))
1541 raise error.Abort(_("outstanding merge conflicts"))
1542 if branchmerge:
1542 if branchmerge:
1543 if pas == [p2]:
1543 if pas == [p2]:
1544 raise error.Abort(_("merging with a working directory ancestor"
1544 raise error.Abort(_("merging with a working directory ancestor"
1545 " has no effect"))
1545 " has no effect"))
1546 elif pas == [p1]:
1546 elif pas == [p1]:
1547 if not mergeancestor and p1.branch() == p2.branch():
1547 if not mergeancestor and p1.branch() == p2.branch():
1548 raise error.Abort(_("nothing to merge"),
1548 raise error.Abort(_("nothing to merge"),
1549 hint=_("use 'hg update' "
1549 hint=_("use 'hg update' "
1550 "or check 'hg heads'"))
1550 "or check 'hg heads'"))
1551 if not force and (wc.files() or wc.deleted()):
1551 if not force and (wc.files() or wc.deleted()):
1552 raise error.Abort(_("uncommitted changes"),
1552 raise error.Abort(_("uncommitted changes"),
1553 hint=_("use 'hg status' to list changes"))
1553 hint=_("use 'hg status' to list changes"))
1554 for s in sorted(wc.substate):
1554 for s in sorted(wc.substate):
1555 wc.sub(s).bailifchanged()
1555 wc.sub(s).bailifchanged()
1556
1556
1557 elif not overwrite:
1557 elif not overwrite:
1558 if p1 == p2: # no-op update
1558 if p1 == p2: # no-op update
1559 # call the hooks and exit early
1559 # call the hooks and exit early
1560 repo.hook('preupdate', throw=True, parent1=xp2, parent2='')
1560 repo.hook('preupdate', throw=True, parent1=xp2, parent2='')
1561 repo.hook('update', parent1=xp2, parent2='', error=0)
1561 repo.hook('update', parent1=xp2, parent2='', error=0)
1562 return 0, 0, 0, 0
1562 return 0, 0, 0, 0
1563
1563
1564 if (updatecheck == 'linear' and
1564 if (updatecheck == 'linear' and
1565 pas not in ([p1], [p2])): # nonlinear
1565 pas not in ([p1], [p2])): # nonlinear
1566 dirty = wc.dirty(missing=True)
1566 dirty = wc.dirty(missing=True)
1567 if dirty:
1567 if dirty:
1568 # Branching is a bit strange to ensure we do the minimal
1568 # Branching is a bit strange to ensure we do the minimal
1569 # amount of call to obsolete.foreground.
1569 # amount of call to obsolete.foreground.
1570 foreground = obsolete.foreground(repo, [p1.node()])
1570 foreground = obsolete.foreground(repo, [p1.node()])
1571 # note: the <node> variable contains a random identifier
1571 # note: the <node> variable contains a random identifier
1572 if repo[node].node() in foreground:
1572 if repo[node].node() in foreground:
1573 pass # allow updating to successors
1573 pass # allow updating to successors
1574 else:
1574 else:
1575 msg = _("uncommitted changes")
1575 msg = _("uncommitted changes")
1576 hint = _("commit or update --clean to discard changes")
1576 hint = _("commit or update --clean to discard changes")
1577 raise error.UpdateAbort(msg, hint=hint)
1577 raise error.UpdateAbort(msg, hint=hint)
1578 else:
1578 else:
1579 # Allow jumping branches if clean and specific rev given
1579 # Allow jumping branches if clean and specific rev given
1580 pass
1580 pass
1581
1581
1582 if overwrite:
1582 if overwrite:
1583 pas = [wc]
1583 pas = [wc]
1584 elif not branchmerge:
1584 elif not branchmerge:
1585 pas = [p1]
1585 pas = [p1]
1586
1586
1587 # deprecated config: merge.followcopies
1587 # deprecated config: merge.followcopies
1588 followcopies = repo.ui.configbool('merge', 'followcopies', True)
1588 followcopies = repo.ui.configbool('merge', 'followcopies', True)
1589 if overwrite:
1589 if overwrite:
1590 followcopies = False
1590 followcopies = False
1591 elif not pas[0]:
1591 elif not pas[0]:
1592 followcopies = False
1592 followcopies = False
1593 if not branchmerge and not wc.dirty(missing=True):
1593 if not branchmerge and not wc.dirty(missing=True):
1594 followcopies = False
1594 followcopies = False
1595
1595
1596 ### calculate phase
1596 ### calculate phase
1597 actionbyfile, diverge, renamedelete = calculateupdates(
1597 actionbyfile, diverge, renamedelete = calculateupdates(
1598 repo, wc, p2, pas, branchmerge, force, mergeancestor,
1598 repo, wc, p2, pas, branchmerge, force, mergeancestor,
1599 followcopies, matcher=matcher, mergeforce=mergeforce)
1599 followcopies, matcher=matcher, mergeforce=mergeforce)
1600
1600
1601 if updatecheck == 'noconflict':
1601 if updatecheck == 'noconflict':
1602 for f, (m, args, msg) in actionbyfile.iteritems():
1602 for f, (m, args, msg) in actionbyfile.iteritems():
1603 if m not in ('g', 'k', 'r'):
1603 if m not in ('g', 'k', 'r'):
1604 msg = _("conflicting changes")
1604 msg = _("conflicting changes")
1605 hint = _("commit or update --clean to discard changes")
1605 hint = _("commit or update --clean to discard changes")
1606 raise error.Abort(msg, hint=hint)
1606 raise error.Abort(msg, hint=hint)
1607
1607
1608 # Prompt and create actions. Most of this is in the resolve phase
1608 # Prompt and create actions. Most of this is in the resolve phase
1609 # already, but we can't handle .hgsubstate in filemerge or
1609 # already, but we can't handle .hgsubstate in filemerge or
1610 # subrepo.submerge yet so we have to keep prompting for it.
1610 # subrepo.submerge yet so we have to keep prompting for it.
1611 if '.hgsubstate' in actionbyfile:
1611 if '.hgsubstate' in actionbyfile:
1612 f = '.hgsubstate'
1612 f = '.hgsubstate'
1613 m, args, msg = actionbyfile[f]
1613 m, args, msg = actionbyfile[f]
1614 prompts = filemerge.partextras(labels)
1614 prompts = filemerge.partextras(labels)
1615 prompts['f'] = f
1615 prompts['f'] = f
1616 if m == 'cd':
1616 if m == 'cd':
1617 if repo.ui.promptchoice(
1617 if repo.ui.promptchoice(
1618 _("local%(l)s changed %(f)s which other%(o)s deleted\n"
1618 _("local%(l)s changed %(f)s which other%(o)s deleted\n"
1619 "use (c)hanged version or (d)elete?"
1619 "use (c)hanged version or (d)elete?"
1620 "$$ &Changed $$ &Delete") % prompts, 0):
1620 "$$ &Changed $$ &Delete") % prompts, 0):
1621 actionbyfile[f] = ('r', None, "prompt delete")
1621 actionbyfile[f] = ('r', None, "prompt delete")
1622 elif f in p1:
1622 elif f in p1:
1623 actionbyfile[f] = ('am', None, "prompt keep")
1623 actionbyfile[f] = ('am', None, "prompt keep")
1624 else:
1624 else:
1625 actionbyfile[f] = ('a', None, "prompt keep")
1625 actionbyfile[f] = ('a', None, "prompt keep")
1626 elif m == 'dc':
1626 elif m == 'dc':
1627 f1, f2, fa, move, anc = args
1627 f1, f2, fa, move, anc = args
1628 flags = p2[f2].flags()
1628 flags = p2[f2].flags()
1629 if repo.ui.promptchoice(
1629 if repo.ui.promptchoice(
1630 _("other%(o)s changed %(f)s which local%(l)s deleted\n"
1630 _("other%(o)s changed %(f)s which local%(l)s deleted\n"
1631 "use (c)hanged version or leave (d)eleted?"
1631 "use (c)hanged version or leave (d)eleted?"
1632 "$$ &Changed $$ &Deleted") % prompts, 0) == 0:
1632 "$$ &Changed $$ &Deleted") % prompts, 0) == 0:
1633 actionbyfile[f] = ('g', (flags, False), "prompt recreating")
1633 actionbyfile[f] = ('g', (flags, False), "prompt recreating")
1634 else:
1634 else:
1635 del actionbyfile[f]
1635 del actionbyfile[f]
1636
1636
1637 # Convert to dictionary-of-lists format
1637 # Convert to dictionary-of-lists format
1638 actions = dict((m, []) for m in 'a am f g cd dc r dm dg m e k'.split())
1638 actions = dict((m, []) for m in 'a am f g cd dc r dm dg m e k'.split())
1639 for f, (m, args, msg) in actionbyfile.iteritems():
1639 for f, (m, args, msg) in actionbyfile.iteritems():
1640 if m not in actions:
1640 if m not in actions:
1641 actions[m] = []
1641 actions[m] = []
1642 actions[m].append((f, args, msg))
1642 actions[m].append((f, args, msg))
1643
1643
1644 if not util.fscasesensitive(repo.path):
1644 if not util.fscasesensitive(repo.path):
1645 # check collision between files only in p2 for clean update
1645 # check collision between files only in p2 for clean update
1646 if (not branchmerge and
1646 if (not branchmerge and
1647 (force or not wc.dirty(missing=True, branch=False))):
1647 (force or not wc.dirty(missing=True, branch=False))):
1648 _checkcollision(repo, p2.manifest(), None)
1648 _checkcollision(repo, p2.manifest(), None)
1649 else:
1649 else:
1650 _checkcollision(repo, wc.manifest(), actions)
1650 _checkcollision(repo, wc.manifest(), actions)
1651
1651
1652 # divergent renames
1652 # divergent renames
1653 for f, fl in sorted(diverge.iteritems()):
1653 for f, fl in sorted(diverge.iteritems()):
1654 repo.ui.warn(_("note: possible conflict - %s was renamed "
1654 repo.ui.warn(_("note: possible conflict - %s was renamed "
1655 "multiple times to:\n") % f)
1655 "multiple times to:\n") % f)
1656 for nf in fl:
1656 for nf in fl:
1657 repo.ui.warn(" %s\n" % nf)
1657 repo.ui.warn(" %s\n" % nf)
1658
1658
1659 # rename and delete
1659 # rename and delete
1660 for f, fl in sorted(renamedelete.iteritems()):
1660 for f, fl in sorted(renamedelete.iteritems()):
1661 repo.ui.warn(_("note: possible conflict - %s was deleted "
1661 repo.ui.warn(_("note: possible conflict - %s was deleted "
1662 "and renamed to:\n") % f)
1662 "and renamed to:\n") % f)
1663 for nf in fl:
1663 for nf in fl:
1664 repo.ui.warn(" %s\n" % nf)
1664 repo.ui.warn(" %s\n" % nf)
1665
1665
1666 ### apply phase
1666 ### apply phase
1667 if not branchmerge: # just jump to the new rev
1667 if not branchmerge: # just jump to the new rev
1668 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
1668 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
1669 if not partial:
1669 if not partial:
1670 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
1670 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
1671 # note that we're in the middle of an update
1671 # note that we're in the middle of an update
1672 repo.vfs.write('updatestate', p2.hex())
1672 repo.vfs.write('updatestate', p2.hex())
1673
1673
1674 stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels)
1674 stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels)
1675
1675
1676 if not partial:
1676 if not partial:
1677 repo.dirstate.beginparentchange()
1677 repo.dirstate.beginparentchange()
1678 repo.setparents(fp1, fp2)
1678 repo.setparents(fp1, fp2)
1679 recordupdates(repo, actions, branchmerge)
1679 recordupdates(repo, actions, branchmerge)
1680 # update completed, clear state
1680 # update completed, clear state
1681 util.unlink(repo.join('updatestate'))
1681 util.unlink(repo.vfs.join('updatestate'))
1682
1682
1683 if not branchmerge:
1683 if not branchmerge:
1684 repo.dirstate.setbranch(p2.branch())
1684 repo.dirstate.setbranch(p2.branch())
1685 repo.dirstate.endparentchange()
1685 repo.dirstate.endparentchange()
1686
1686
1687 if not partial:
1687 if not partial:
1688 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
1688 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
1689 return stats
1689 return stats
1690
1690
1691 def graft(repo, ctx, pctx, labels, keepparent=False):
1691 def graft(repo, ctx, pctx, labels, keepparent=False):
1692 """Do a graft-like merge.
1692 """Do a graft-like merge.
1693
1693
1694 This is a merge where the merge ancestor is chosen such that one
1694 This is a merge where the merge ancestor is chosen such that one
1695 or more changesets are grafted onto the current changeset. In
1695 or more changesets are grafted onto the current changeset. In
1696 addition to the merge, this fixes up the dirstate to include only
1696 addition to the merge, this fixes up the dirstate to include only
1697 a single parent (if keepparent is False) and tries to duplicate any
1697 a single parent (if keepparent is False) and tries to duplicate any
1698 renames/copies appropriately.
1698 renames/copies appropriately.
1699
1699
1700 ctx - changeset to rebase
1700 ctx - changeset to rebase
1701 pctx - merge base, usually ctx.p1()
1701 pctx - merge base, usually ctx.p1()
1702 labels - merge labels eg ['local', 'graft']
1702 labels - merge labels eg ['local', 'graft']
1703 keepparent - keep second parent if any
1703 keepparent - keep second parent if any
1704
1704
1705 """
1705 """
1706 # If we're grafting a descendant onto an ancestor, be sure to pass
1706 # If we're grafting a descendant onto an ancestor, be sure to pass
1707 # mergeancestor=True to update. This does two things: 1) allows the merge if
1707 # mergeancestor=True to update. This does two things: 1) allows the merge if
1708 # the destination is the same as the parent of the ctx (so we can use graft
1708 # the destination is the same as the parent of the ctx (so we can use graft
1709 # to copy commits), and 2) informs update that the incoming changes are
1709 # to copy commits), and 2) informs update that the incoming changes are
1710 # newer than the destination so it doesn't prompt about "remote changed foo
1710 # newer than the destination so it doesn't prompt about "remote changed foo
1711 # which local deleted".
1711 # which local deleted".
1712 mergeancestor = repo.changelog.isancestor(repo['.'].node(), ctx.node())
1712 mergeancestor = repo.changelog.isancestor(repo['.'].node(), ctx.node())
1713
1713
1714 stats = update(repo, ctx.node(), True, True, pctx.node(),
1714 stats = update(repo, ctx.node(), True, True, pctx.node(),
1715 mergeancestor=mergeancestor, labels=labels)
1715 mergeancestor=mergeancestor, labels=labels)
1716
1716
1717 pother = nullid
1717 pother = nullid
1718 parents = ctx.parents()
1718 parents = ctx.parents()
1719 if keepparent and len(parents) == 2 and pctx in parents:
1719 if keepparent and len(parents) == 2 and pctx in parents:
1720 parents.remove(pctx)
1720 parents.remove(pctx)
1721 pother = parents[0].node()
1721 pother = parents[0].node()
1722
1722
1723 repo.dirstate.beginparentchange()
1723 repo.dirstate.beginparentchange()
1724 repo.setparents(repo['.'].node(), pother)
1724 repo.setparents(repo['.'].node(), pother)
1725 repo.dirstate.write(repo.currenttransaction())
1725 repo.dirstate.write(repo.currenttransaction())
1726 # fix up dirstate for copies and renames
1726 # fix up dirstate for copies and renames
1727 copies.duplicatecopies(repo, ctx.rev(), pctx.rev())
1727 copies.duplicatecopies(repo, ctx.rev(), pctx.rev())
1728 repo.dirstate.endparentchange()
1728 repo.dirstate.endparentchange()
1729 return stats
1729 return stats
General Comments 0
You need to be logged in to leave comments. Login now