1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
|
# Copyright (C) 2005, 2007, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# TODO: 'bzr resolve' should accept a directory name and work from that
# point down
import os
import re
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import errno
from bzrlib import (
builtins,
commands,
errors,
osutils,
rio,
trace,
transform,
workingtree,
)
""")
from bzrlib import (
option,
registry,
)
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
class cmd_conflicts(commands.Command):
"""List files with conflicts.
Merge will do its best to combine the changes in two branches, but there
are some kinds of problems only a human can fix. When it encounters those,
it will mark a conflict. A conflict means that you need to fix something,
before you should commit.
Conflicts normally are listed as short, human-readable messages. If --text
is supplied, the pathnames of files with text conflicts are listed,
instead. (This is useful for editing all files with text conflicts.)
Use bzr resolve when you have fixed a problem.
"""
takes_options = [
option.Option('text',
help='List paths of files with text conflicts.'),
]
_see_also = ['resolve', 'conflict-types']
def run(self, text=False):
wt = workingtree.WorkingTree.open_containing(u'.')[0]
for conflict in wt.conflicts():
if text:
if conflict.typestring != 'text conflict':
continue
self.outf.write(conflict.path + '\n')
else:
self.outf.write(str(conflict) + '\n')
resolve_action_registry = registry.Registry()
resolve_action_registry.register(
'done', 'done', 'Marks the conflict as resolved' )
resolve_action_registry.register(
'take-this', 'take_this',
'Resolve the conflict preserving the version in the working tree' )
resolve_action_registry.register(
'take-other', 'take_other',
'Resolve the conflict taking the merged version into account' )
resolve_action_registry.default_key = 'done'
class ResolveActionOption(option.RegistryOption):
def __init__(self):
super(ResolveActionOption, self).__init__(
'action', 'How to resolve the conflict.',
value_switches=True,
registry=resolve_action_registry)
class cmd_resolve(commands.Command):
"""Mark a conflict as resolved.
Merge will do its best to combine the changes in two branches, but there
are some kinds of problems only a human can fix. When it encounters those,
it will mark a conflict. A conflict means that you need to fix something,
before you should commit.
Once you have fixed a problem, use "bzr resolve" to automatically mark
text conflicts as fixed, "bzr resolve FILE" to mark a specific conflict as
resolved, or "bzr resolve --all" to mark all conflicts as resolved.
"""
aliases = ['resolved']
takes_args = ['file*']
takes_options = [
option.Option('all', help='Resolve all conflicts in this tree.'),
ResolveActionOption(),
]
_see_also = ['conflicts']
def run(self, file_list=None, all=False, action=None):
if all:
if file_list:
raise errors.BzrCommandError("If --all is specified,"
" no FILE may be provided")
tree = workingtree.WorkingTree.open_containing('.')[0]
if action is None:
action = 'done'
else:
tree, file_list = builtins.tree_files(file_list)
if file_list is None:
if action is None:
# FIXME: There is a special case here related to the option
# handling that could be clearer and easier to discover by
# providing an --auto action (bug #344013 and #383396) and
# make it mandatory instead of implicit and active only
# when no file_list is provided -- vila 091229
action = 'auto'
else:
if action is None:
action = 'done'
if action == 'auto':
if file_list is None:
un_resolved, resolved = tree.auto_resolve()
if len(un_resolved) > 0:
trace.note('%d conflict(s) auto-resolved.', len(resolved))
trace.note('Remaining conflicts:')
for conflict in un_resolved:
trace.note(conflict)
return 1
else:
trace.note('All conflicts resolved.')
return 0
else:
# FIXME: This can never occur but the block above needs some
# refactoring to transfer tree.auto_resolve() to
# conflict.auto(tree) --vila 091242
pass
else:
resolve(tree, file_list, action=action)
def resolve(tree, paths=None, ignore_misses=False, recursive=False,
action='done'):
"""Resolve some or all of the conflicts in a working tree.
:param paths: If None, resolve all conflicts. Otherwise, select only
specified conflicts.
:param recursive: If True, then elements of paths which are directories
have all their children resolved, etc. When invoked as part of
recursive commands like revert, this should be True. For commands
or applications wishing finer-grained control, like the resolve
command, this should be False.
:param ignore_misses: If False, warnings will be printed if the supplied
paths do not have conflicts.
:param action: How the conflict should be resolved,
"""
tree.lock_tree_write()
try:
tree_conflicts = tree.conflicts()
if paths is None:
new_conflicts = ConflictList()
to_process = tree_conflicts
else:
new_conflicts, to_process = tree_conflicts.select_conflicts(
tree, paths, ignore_misses, recursive)
for conflict in to_process:
try:
conflict._do(action, tree)
conflict.cleanup(tree)
except NotImplementedError:
new_conflicts.append(conflict)
try:
tree.set_conflicts(new_conflicts)
except errors.UnsupportedOperation:
pass
finally:
tree.unlock()
def restore(filename):
"""Restore a conflicted file to the state it was in before merging.
Only text restoration is supported at present.
"""
conflicted = False
try:
osutils.rename(filename + ".THIS", filename)
conflicted = True
except OSError, e:
if e.errno != errno.ENOENT:
raise
try:
os.unlink(filename + ".BASE")
conflicted = True
except OSError, e:
if e.errno != errno.ENOENT:
raise
try:
os.unlink(filename + ".OTHER")
conflicted = True
except OSError, e:
if e.errno != errno.ENOENT:
raise
if not conflicted:
raise errors.NotConflicted(filename)
class ConflictList(object):
"""List of conflicts.
Typically obtained from WorkingTree.conflicts()
Can be instantiated from stanzas or from Conflict subclasses.
"""
def __init__(self, conflicts=None):
object.__init__(self)
if conflicts is None:
self.__list = []
else:
self.__list = conflicts
def is_empty(self):
return len(self.__list) == 0
def __len__(self):
return len(self.__list)
def __iter__(self):
return iter(self.__list)
def __getitem__(self, key):
return self.__list[key]
def append(self, conflict):
return self.__list.append(conflict)
def __eq__(self, other_list):
return list(self) == list(other_list)
def __ne__(self, other_list):
return not (self == other_list)
def __repr__(self):
return "ConflictList(%r)" % self.__list
@staticmethod
def from_stanzas(stanzas):
"""Produce a new ConflictList from an iterable of stanzas"""
conflicts = ConflictList()
for stanza in stanzas:
conflicts.append(Conflict.factory(**stanza.as_dict()))
return conflicts
def to_stanzas(self):
"""Generator of stanzas"""
for conflict in self:
yield conflict.as_stanza()
def to_strings(self):
"""Generate strings for the provided conflicts"""
for conflict in self:
yield str(conflict)
def remove_files(self, tree):
"""Remove the THIS, BASE and OTHER files for listed conflicts"""
for conflict in self:
if not conflict.has_files:
continue
conflict.cleanup(tree)
def select_conflicts(self, tree, paths, ignore_misses=False,
recurse=False):
"""Select the conflicts associated with paths in a tree.
File-ids are also used for this.
:return: a pair of ConflictLists: (not_selected, selected)
"""
path_set = set(paths)
ids = {}
selected_paths = set()
new_conflicts = ConflictList()
selected_conflicts = ConflictList()
for path in paths:
file_id = tree.path2id(path)
if file_id is not None:
ids[file_id] = path
for conflict in self:
selected = False
for key in ('path', 'conflict_path'):
cpath = getattr(conflict, key, None)
if cpath is None:
continue
if cpath in path_set:
selected = True
selected_paths.add(cpath)
if recurse:
if osutils.is_inside_any(path_set, cpath):
selected = True
selected_paths.add(cpath)
for key in ('file_id', 'conflict_file_id'):
cfile_id = getattr(conflict, key, None)
if cfile_id is None:
continue
try:
cpath = ids[cfile_id]
except KeyError:
continue
selected = True
selected_paths.add(cpath)
if selected:
selected_conflicts.append(conflict)
else:
new_conflicts.append(conflict)
if ignore_misses is not True:
for path in [p for p in paths if p not in selected_paths]:
if not os.path.exists(tree.abspath(path)):
print "%s does not exist" % path
else:
print "%s is not conflicted" % path
return new_conflicts, selected_conflicts
class Conflict(object):
"""Base class for all types of conflict"""
# FIXME: cleanup should take care of that ? -- vila 091229
has_files = False
def __init__(self, path, file_id=None):
self.path = path
# warn turned off, because the factory blindly transfers the Stanza
# values to __init__ and Stanza is purely a Unicode api.
self.file_id = osutils.safe_file_id(file_id, warn=False)
def as_stanza(self):
s = rio.Stanza(type=self.typestring, path=self.path)
if self.file_id is not None:
# Stanza requires Unicode apis
s.add('file_id', self.file_id.decode('utf8'))
return s
def _cmp_list(self):
return [type(self), self.path, self.file_id]
def __cmp__(self, other):
if getattr(other, "_cmp_list", None) is None:
return -1
return cmp(self._cmp_list(), other._cmp_list())
def __hash__(self):
return hash((type(self), self.path, self.file_id))
def __eq__(self, other):
return self.__cmp__(other) == 0
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return self.format % self.__dict__
def __repr__(self):
rdict = dict(self.__dict__)
rdict['class'] = self.__class__.__name__
return self.rformat % rdict
@staticmethod
def factory(type, **kwargs):
global ctype
return ctype[type](**kwargs)
@staticmethod
def sort_key(conflict):
if conflict.path is not None:
return conflict.path, conflict.typestring
elif getattr(conflict, "conflict_path", None) is not None:
return conflict.conflict_path, conflict.typestring
else:
return None, conflict.typestring
def _do(self, action, tree):
"""Apply the specified action to the conflict.
:param action: The method name to call.
:param tree: The tree passed as a parameter to the method.
"""
meth = getattr(self, 'action_%s' % action, None)
if meth is None:
raise NotImplementedError(self.__class__.__name__ + '.' + action)
meth(tree)
def cleanup(self, tree):
raise NotImplementedError(self.cleanup)
def action_done(self, tree):
"""Mark the conflict as solved once it has been handled."""
# This method does nothing but simplifies the design of upper levels.
pass
def action_take_this(self, tree):
raise NotImplementedError(self.action_take_this)
def action_take_other(self, tree):
raise NotImplementedError(self.action_take_other)
class PathConflict(Conflict):
"""A conflict was encountered merging file paths"""
typestring = 'path conflict'
format = 'Path conflict: %(path)s / %(conflict_path)s'
rformat = '%(class)s(%(path)r, %(conflict_path)r, %(file_id)r)'
def __init__(self, path, conflict_path=None, file_id=None):
Conflict.__init__(self, path, file_id)
self.conflict_path = conflict_path
def as_stanza(self):
s = Conflict.as_stanza(self)
if self.conflict_path is not None:
s.add('conflict_path', self.conflict_path)
return s
def cleanup(self, tree):
# No additional files have been generated here
pass
def action_take_this(self, tree):
tree.rename_one(self.conflict_path, self.path)
def action_take_other(self, tree):
# just acccept bzr proposal
pass
class ContentsConflict(PathConflict):
"""The files are of different types, or not present"""
has_files = True
typestring = 'contents conflict'
format = 'Contents conflict in %(path)s'
def cleanup(self, tree):
for suffix in ('.BASE', '.OTHER'):
try:
osutils.delete_any(tree.abspath(self.path + suffix))
except OSError, e:
if e.errno != errno.ENOENT:
raise
# FIXME: I smell something weird here and it seems we should be able to be
# more coherent with some other conflict ? bzr *did* a choice there but
# neither action_take_this nor action_take_other reflect that...
# -- vila 20091224
def action_take_this(self, tree):
tree.remove([self.path + '.OTHER'], force=True, keep_files=False)
def action_take_other(self, tree):
tree.remove([self.path], force=True, keep_files=False)
# FIXME: TextConflict is about a single file-id, there never is a conflict_path
# attribute so we shouldn't inherit from PathConflict but simply from Conflict
# TODO: There should be a base revid attribute to better inform the user about
# how the conflicts were generated.
class TextConflict(PathConflict):
"""The merge algorithm could not resolve all differences encountered."""
has_files = True
typestring = 'text conflict'
format = 'Text conflict in %(path)s'
def cleanup(self, tree):
for suffix in CONFLICT_SUFFIXES:
try:
osutils.delete_any(tree.abspath(self.path+suffix))
except OSError, e:
if e.errno != errno.ENOENT:
raise
class HandledConflict(Conflict):
"""A path problem that has been provisionally resolved.
This is intended to be a base class.
"""
rformat = "%(class)s(%(action)r, %(path)r, %(file_id)r)"
def __init__(self, action, path, file_id=None):
Conflict.__init__(self, path, file_id)
self.action = action
def _cmp_list(self):
return Conflict._cmp_list(self) + [self.action]
def as_stanza(self):
s = Conflict.as_stanza(self)
s.add('action', self.action)
return s
def cleanup(self, tree):
"""Nothing to cleanup."""
pass
class HandledPathConflict(HandledConflict):
"""A provisionally-resolved path problem involving two paths.
This is intended to be a base class.
"""
rformat = "%(class)s(%(action)r, %(path)r, %(conflict_path)r,"\
" %(file_id)r, %(conflict_file_id)r)"
def __init__(self, action, path, conflict_path, file_id=None,
conflict_file_id=None):
HandledConflict.__init__(self, action, path, file_id)
self.conflict_path = conflict_path
# warn turned off, because the factory blindly transfers the Stanza
# values to __init__.
self.conflict_file_id = osutils.safe_file_id(conflict_file_id,
warn=False)
def _cmp_list(self):
return HandledConflict._cmp_list(self) + [self.conflict_path,
self.conflict_file_id]
def as_stanza(self):
s = HandledConflict.as_stanza(self)
s.add('conflict_path', self.conflict_path)
if self.conflict_file_id is not None:
s.add('conflict_file_id', self.conflict_file_id.decode('utf8'))
return s
class DuplicateID(HandledPathConflict):
"""Two files want the same file_id."""
typestring = 'duplicate id'
format = 'Conflict adding id to %(conflict_path)s. %(action)s %(path)s.'
class DuplicateEntry(HandledPathConflict):
"""Two directory entries want to have the same name."""
typestring = 'duplicate'
format = 'Conflict adding file %(conflict_path)s. %(action)s %(path)s.'
def action_take_this(self, tree):
tree.remove([self.conflict_path], force=True, keep_files=False)
tree.rename_one(self.path, self.conflict_path)
def action_take_other(self, tree):
tree.remove([self.path], force=True, keep_files=False)
class ParentLoop(HandledPathConflict):
"""An attempt to create an infinitely-looping directory structure.
This is rare, but can be produced like so:
tree A:
mv foo bar
tree B:
mv bar foo
merge A and B
"""
typestring = 'parent loop'
format = 'Conflict moving %(conflict_path)s into %(path)s. %(action)s.'
def action_take_this(self, tree):
# just acccept bzr proposal
pass
def action_take_other(self, tree):
# FIXME: We shouldn't have to manipulate so many paths here (and there
# is probably a bug or two...)
base_path = osutils.basename(self.path)
conflict_base_path = osutils.basename(self.conflict_path)
tt = transform.TreeTransform(tree)
try:
p_tid = tt.trans_id_file_id(self.file_id)
parent_tid = tt.get_tree_parent(p_tid)
cp_tid = tt.trans_id_file_id(self.conflict_file_id)
cparent_tid = tt.get_tree_parent(cp_tid)
tt.adjust_path(base_path, cparent_tid, cp_tid)
tt.adjust_path(conflict_base_path, parent_tid, p_tid)
tt.apply()
finally:
tt.finalize()
class UnversionedParent(HandledConflict):
"""An attempt to version a file whose parent directory is not versioned.
Typically, the result of a merge where one tree unversioned the directory
and the other added a versioned file to it.
"""
typestring = 'unversioned parent'
format = 'Conflict because %(path)s is not versioned, but has versioned'\
' children. %(action)s.'
# FIXME: We silently do nothing to make tests pass, but most probably the
# conflict shouldn't exist (the long story is that the conflict is
# generated with another one that can be resolved properly) -- vila 091224
def action_take_this(self, tree):
pass
def action_take_other(self, tree):
pass
class MissingParent(HandledConflict):
"""An attempt to add files to a directory that is not present.
Typically, the result of a merge where THIS deleted the directory and
the OTHER added a file to it.
See also: DeletingParent (same situation, THIS and OTHER reversed)
"""
typestring = 'missing parent'
format = 'Conflict adding files to %(path)s. %(action)s.'
def action_take_this(self, tree):
tree.remove([self.path], force=True, keep_files=False)
def action_take_other(self, tree):
# just acccept bzr proposal
pass
class DeletingParent(HandledConflict):
"""An attempt to add files to a directory that is not present.
Typically, the result of a merge where one OTHER deleted the directory and
the THIS added a file to it.
"""
typestring = 'deleting parent'
format = "Conflict: can't delete %(path)s because it is not empty. "\
"%(action)s."
# FIXME: It's a bit strange that the default action is not coherent with
# MissingParent from the *user* pov.
def action_take_this(self, tree):
# just acccept bzr proposal
pass
def action_take_other(self, tree):
tree.remove([self.path], force=True, keep_files=False)
class NonDirectoryParent(HandledConflict):
"""An attempt to add files to a directory that is not a directory or
an attempt to change the kind of a directory with files.
"""
typestring = 'non-directory parent'
format = "Conflict: %(path)s is not a directory, but has files in it."\
" %(action)s."
# FIXME: .OTHER should be used instead of .new when the conflict is created
def action_take_this(self, tree):
# FIXME: we should preserve that path when the conflict is generated !
if self.path.endswith('.new'):
conflict_path = self.path[:-(len('.new'))]
tree.remove([self.path], force=True, keep_files=False)
tree.add(conflict_path)
else:
raise NotImplementedError(self.action_take_this)
def action_take_other(self, tree):
# FIXME: we should preserve that path when the conflict is generated !
if self.path.endswith('.new'):
conflict_path = self.path[:-(len('.new'))]
tree.remove([conflict_path], force=True, keep_files=False)
tree.rename_one(self.path, conflict_path)
else:
raise NotImplementedError(self.action_take_other)
ctype = {}
def register_types(*conflict_types):
"""Register a Conflict subclass for serialization purposes"""
global ctype
for conflict_type in conflict_types:
ctype[conflict_type.typestring] = conflict_type
register_types(ContentsConflict, TextConflict, PathConflict, DuplicateID,
DuplicateEntry, ParentLoop, UnversionedParent, MissingParent,
DeletingParent, NonDirectoryParent)
|