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