/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/rename_map.py

  • Committer: Jelmer Vernooij
  • Date: 2018-03-10 13:18:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6893.
  • Revision ID: jelmer@jelmer.uk-20180310131810-iiblggbkb757eopm
Avoid call to has_id.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
 
from cStringIO import StringIO
19
 
 
20
 
from bzrlib import (
 
17
from __future__ import absolute_import
 
18
 
 
19
 
 
20
from . import (
21
21
    osutils,
22
22
    progress,
23
23
    trace,
24
 
)
25
 
from bzrlib.ui import ui_factory
26
 
 
 
24
    )
 
25
from .i18n import gettext
 
26
from .sixish import (
 
27
    BytesIO,
 
28
    viewitems,
 
29
    )
 
30
from .ui import ui_factory
27
31
 
28
32
class RenameMap(object):
29
33
    """Determine a mapping of renames."""
61
65
        :param file_ids: A list of file_ids to perform the updates for.
62
66
        """
63
67
        desired_files = [(f, f) for f in file_ids]
64
 
        task = ui_factory.nested_progress_bar()
65
 
        try:
 
68
        with ui_factory.nested_progress_bar() as task:
66
69
            for num, (file_id, contents) in enumerate(
67
70
                tree.iter_files_bytes(desired_files)):
68
 
                task.update('Calculating hashes', num, len(file_ids))
69
 
                s = StringIO()
 
71
                task.update(gettext('Calculating hashes'), num, len(file_ids))
 
72
                s = BytesIO()
70
73
                s.writelines(contents)
71
74
                s.seek(0)
72
75
                self.add_edge_hashes(s.readlines(), file_id)
73
 
        finally:
74
 
            task.finished()
75
76
 
76
77
    def hitcounts(self, lines):
77
78
        """Count the number of hash hits for each tag, for the given lines.
100
101
        :return: A list of tuples of count, path, file_id.
101
102
        """
102
103
        all_hits = []
103
 
        task = ui_factory.nested_progress_bar()
104
 
        try:
 
104
        with ui_factory.nested_progress_bar() as task:
105
105
            for num, path in enumerate(paths):
106
 
                task.update('Determining hash hits', num, len(paths))
107
 
                hits = self.hitcounts(self.tree.get_file_lines(None,
108
 
                                                               path=path))
109
 
                all_hits.extend((v, path, k) for k, v in hits.items())
110
 
        finally:
111
 
            task.finished()
 
106
                task.update(gettext('Determining hash hits'), num, len(paths))
 
107
                hits = self.hitcounts(self.tree.get_file_lines(path))
 
108
                all_hits.extend((v, path, k) for k, v in viewitems(hits))
112
109
        return all_hits
113
110
 
114
111
    def file_match(self, paths):
143
140
            while True:
144
141
                child = path
145
142
                path = osutils.dirname(path)
146
 
                if self.tree.path2id(path) is not None:
 
143
                if self.tree.is_versioned(path):
147
144
                    break
148
145
                required_parents.setdefault(path, []).append(child)
149
146
        require_ids = {}
150
 
        for parent, children in required_parents.iteritems():
 
147
        for parent, children in viewitems(required_parents):
151
148
            child_file_ids = set()
152
149
            for child in children:
153
150
                file_id = matches.get(child)
164
161
        parent directories.
165
162
        """
166
163
        all_hits = []
167
 
        for file_id, file_id_children in missing_parents.iteritems():
168
 
            for path, path_children in required_parents.iteritems():
 
164
        for file_id, file_id_children in viewitems(missing_parents):
 
165
            for path, path_children in viewitems(required_parents):
169
166
                hits = len(path_children.intersection(file_id_children))
170
167
                if hits > 0:
171
168
                    all_hits.append((hits, path, file_id))
175
172
        missing_files = set()
176
173
        missing_parents = {}
177
174
        candidate_files = set()
178
 
        task = ui_factory.nested_progress_bar()
179
 
        iterator = self.tree.iter_changes(basis, want_unversioned=True,
180
 
                                          pb=task)
181
 
        try:
 
175
        with ui_factory.nested_progress_bar() as task:
 
176
            iterator = self.tree.iter_changes(basis, want_unversioned=True,
 
177
                                              pb=task)
182
178
            for (file_id, paths, changed_content, versioned, parent, name,
183
179
                 kind, executable) in iterator:
184
180
                if kind[1] is None and versioned[1]:
185
 
                    missing_parents.setdefault(parent[0], set()).add(file_id)
 
181
                    if not self.tree.has_filename(self.tree.id2path(parent[0])):
 
182
                        missing_parents.setdefault(parent[0], set()).add(file_id)
186
183
                    if kind[0] == 'file':
187
184
                        missing_files.add(file_id)
188
185
                    else:
198
195
                            for child in children:
199
196
                                if child[2] == 'file':
200
197
                                    candidate_files.add(child[0])
201
 
        finally:
202
 
            task.finished()
203
198
        return missing_files, missing_parents, candidate_files
204
199
 
205
200
    @classmethod
212
207
        :param tree: A write-locked working tree.
213
208
        """
214
209
        required_parents = {}
215
 
        task = ui_factory.nested_progress_bar()
216
 
        try:
 
210
        with ui_factory.nested_progress_bar() as task:
217
211
            pp = progress.ProgressPhase('Guessing renames', 4, task)
218
212
            basis = tree.basis_tree()
219
213
            basis.lock_read()
238
232
            pp.next_phase()
239
233
            delta = rn._make_inventory_delta(matches)
240
234
            for old, new, file_id, entry in delta:
241
 
                trace.note("%s => %s", old, new)
 
235
                trace.note( gettext("{0} => {1}").format(old, new) )
242
236
            if not dry_run:
243
237
                tree.add(required_parents)
244
238
                tree.apply_inventory_delta(delta)
245
 
        finally:
246
 
            task.finished()
247
239
 
248
240
    def _make_inventory_delta(self, matches):
249
241
        delta = []
250
 
        file_id_matches = dict((f, p) for p, f in matches.items())
251
 
        for old_path, entry in self.tree.iter_entries_by_dir(matches.values()):
 
242
        file_id_matches = dict((f, p) for p, f in viewitems(matches))
 
243
        for old_path, entry in self.tree.iter_entries_by_dir(file_id_matches):
252
244
            new_path = file_id_matches[entry.file_id]
253
245
            parent_path, new_name = osutils.split(new_path)
254
246
            parent_id = matches.get(parent_path)
255
247
            if parent_id is None:
256
248
                parent_id = self.tree.path2id(parent_path)
 
249
                if parent_id is None:
 
250
                    added, ignored = self.tree.smart_add([parent_path], recurse=False)
 
251
                    if len(ignored) > 0 and ignored[0] == parent_path:
 
252
                        continue
 
253
                    else:
 
254
                        parent_id = self.tree.path2id(parent_path)
257
255
            if entry.name == new_name and entry.parent_id == parent_id:
258
256
                continue
259
257
            new_entry = entry.copy()