/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/hashcache.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# TODO: Perhaps use a Python pickle instead of a text file; might be faster.
29
29
 
30
30
 
 
31
 
31
32
CACHE_HEADER = b"### bzr hashcache v5\n"
32
33
 
33
34
import os
52
53
FP_MODE_COLUMN = 5
53
54
 
54
55
 
 
56
 
55
57
class HashCache(object):
56
58
    """Cache for looking up file SHA-1.
57
59
 
89
91
    needs_write = False
90
92
 
91
93
    def __init__(self, root, cache_file_name, mode=None,
92
 
                 content_filter_stack_provider=None):
 
94
            content_filter_stack_provider=None):
93
95
        """Create a hash cache in base dir, and set the file mode to mode.
94
96
 
95
97
        :param content_filter_stack_provider: a function that takes a
163
165
            cache_sha1, cache_fp = None, None
164
166
 
165
167
        if cache_fp == file_fp:
 
168
            ## mutter("hashcache hit for %s %r -> %s", path, file_fp, cache_sha1)
 
169
            ## mutter("now = %s", time.time())
166
170
            self.hit_count += 1
167
171
            return cache_sha1
168
172
 
173
177
            if self._filter_provider is None:
174
178
                filters = []
175
179
            else:
176
 
                filters = self._filter_provider(path=path)
 
180
                filters = self._filter_provider(path=path, file_id=None)
177
181
            digest = self._really_sha1_file(abspath, filters)
178
182
        elif stat.S_ISLNK(mode):
179
183
            target = osutils.readlink(abspath)
205
209
                self.needs_write = True
206
210
                del self._cache[path]
207
211
        else:
208
 
            # mutter('%r added to cache: now=%f, mtime=%d, ctime=%d',
 
212
            ## mutter('%r added to cache: now=%f, mtime=%d, ctime=%d',
209
213
            ##        path, time.time(), file_fp[FP_MTIME_COLUMN],
210
 
            # file_fp[FP_CTIME_COLUMN])
 
214
            ##        file_fp[FP_CTIME_COLUMN])
211
215
            self.update_count += 1
212
216
            self.needs_write = True
213
217
            self._cache[path] = (digest, file_fp)
219
223
 
220
224
    def write(self):
221
225
        """Write contents of cache to file."""
222
 
        with atomicfile.AtomicFile(self.cache_file_name(), 'wb',
223
 
                                   new_mode=self._mode) as outf:
 
226
        outf = atomicfile.AtomicFile(self.cache_file_name(), 'wb',
 
227
                                     new_mode=self._mode)
 
228
        try:
224
229
            outf.write(CACHE_HEADER)
225
230
 
226
 
            for path, c in viewitems(self._cache):
 
231
            for path, c  in viewitems(self._cache):
227
232
                line_info = [path.encode('utf-8'), b'// ', c[0], b' ']
228
233
                line_info.append(b'%d %d %d %d %d %d' % c[1])
229
234
                line_info.append(b'\n')
230
235
                outf.write(b''.join(line_info))
 
236
            outf.commit()
231
237
            self.needs_write = False
232
 
            # mutter("write hash cache: %s hits=%d misses=%d stat=%d recent=%d updates=%d",
233
 
            #        self.cache_file_name(), self.hit_count, self.miss_count,
234
 
            # self.stat_count,
235
 
            # self.danger_count, self.update_count)
 
238
            ## mutter("write hash cache: %s hits=%d misses=%d stat=%d recent=%d updates=%d",
 
239
            ##        self.cache_file_name(), self.hit_count, self.miss_count,
 
240
            ##        self.stat_count,
 
241
            ##        self.danger_count, self.update_count)
 
242
        finally:
 
243
            outf.close()
236
244
 
237
245
    def read(self):
238
246
        """Reinstate cache from file.