~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/conflicts.py

  • Committer: Aaron Bentley
  • Date: 2006-03-10 17:05:47 UTC
  • mto: (2027.1.2 revert-subpath-56549)
  • mto: This revision was merged to the branch mainline in revision 1647.
  • Revision ID: abentley@panoramicfeedback.com-20060310170547-dee4d0542ffe7281
Resolve uses the new stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
import bzrlib.status
29
29
from bzrlib.commands import register_command
30
 
from bzrlib.errors import BzrCommandError, NotConflicted
 
30
from bzrlib.errors import BzrCommandError, NotConflicted, UnsupportedOperation
31
31
from bzrlib.option import Option
32
32
from bzrlib.osutils import rename
33
33
from bzrlib.rio import Stanza
81
81
            if not all:
82
82
                raise BzrCommandError(
83
83
                    "command 'resolve' needs one or more FILE, or --all")
84
 
            tree = WorkingTree.open_containing(u'.')[0]
85
 
            file_list = list(tree.abspath(f) for f in tree.iter_conflicts())
86
84
        else:
87
85
            if all:
88
86
                raise BzrCommandError(
89
87
                    "If --all is specified, no FILE may be provided")
90
 
        for filename in file_list:
91
 
            failures = 0
 
88
        tree = WorkingTree.open_containing(u'.')[0]
 
89
        resolve(tree, file_list)
 
90
 
 
91
 
 
92
def resolve(tree, paths=None):
 
93
    tree.lock_write()
 
94
    try:
 
95
        tree_conflicts = list(tree.conflict_lines())
 
96
        if paths is None:
 
97
            new_conflicts = []
 
98
            selected_conflicts = tree_conflicts
 
99
        else:
 
100
            new_conflicts, selected_conflicts = \
 
101
                select_conflicts(tree, paths, tree_conflicts)
 
102
        try:
 
103
            tree.set_conflict_lines(new_conflicts)
 
104
        except UnsupportedOperation:
 
105
            pass
 
106
        remove_conflict_files(tree, selected_conflicts)
 
107
    finally:
 
108
        tree.unlock()
 
109
 
 
110
 
 
111
def select_conflicts(tree, paths, tree_conflicts):
 
112
    path_set = set(paths)
 
113
    ids = {}
 
114
    selected_paths = set()
 
115
    new_conflicts = []
 
116
    selected_conflicts = []
 
117
    for path in paths:
 
118
        file_id = tree.path2id(path)
 
119
        if file_id is not None:
 
120
            ids[file_id] = path
 
121
 
 
122
    for conflict, stanza in zip(tree_conflicts, 
 
123
        conflict_stanzas(tree_conflicts)):
 
124
        selected = False
 
125
        for key in ('path', 'conflict_path'):
 
126
            try:
 
127
                cpath = stanza[key]
 
128
            except KeyError:
 
129
                continue
 
130
            if cpath in path_set:
 
131
                selected = True
 
132
                selected_paths.add(cpath)
 
133
        for key in ('file_id', 'conflict_file_id'):
 
134
            try:
 
135
                cfile_id = stanza[key]
 
136
            except KeyError:
 
137
                continue
 
138
            try:
 
139
                cpath = ids[cfile_id]
 
140
            except KeyError:
 
141
                continue
 
142
            selected = True
 
143
            selected_paths.add(cpath)
 
144
        if selected:
 
145
            selected_conflicts.append(conflict)
 
146
        else:
 
147
            new_conflicts.append(conflict)
 
148
    for path in [p for p in paths if p not in selected_paths]:
 
149
        if not os.path.exists(tree.abspath(filename)):
 
150
            print "%s does not exist" % path
 
151
        else:
 
152
            print "%s is not conflicted" % path
 
153
    return new_conflicts, selected_conflicts
 
154
 
 
155
def remove_conflict_files(tree, conflicts):
 
156
    for stanza in conflict_stanzas(conflicts):
 
157
        if stanza['type'] in ("text conflict", "contents conflict"):
92
158
            for suffix in CONFLICT_SUFFIXES:
93
159
                try:
94
 
                    os.unlink(filename+suffix)
 
160
                    os.unlink(stanza['path']+suffix)
95
161
                except OSError, e:
96
162
                    if e.errno != errno.ENOENT:
97
163
                        raise
98
164
                    else:
99
165
                        failures += 1
100
 
            if failures == len(CONFLICT_SUFFIXES):
101
 
                if not os.path.exists(filename):
102
 
                    print "%s does not exist" % filename
103
 
                else:
104
 
                    print "%s is not conflicted" % filename
 
166
    
105
167
 
106
168
def restore(filename):
107
169
    """\