/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 bzrlib/transform.py

Change the create_file api to allow it to take a sha1 of the content.

That way we don't have to compute the sha1 value while creating the content.
Since we normally already have the sha1. build_tree needs to be updated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from stat import S_ISREG, S_IEXEC
20
20
import time
21
21
 
22
 
import bzrlib
23
22
from bzrlib import (
24
23
    errors,
25
24
    lazy_import,
1251
1250
            descendants.update(self._limbo_descendants(descendant))
1252
1251
        return descendants
1253
1252
 
1254
 
    def create_file(self, contents, trans_id, mode_id=None):
 
1253
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1255
1254
        """Schedule creation of a new file.
1256
1255
 
1257
 
        See also new_file.
1258
 
 
1259
 
        Contents is an iterator of strings, all of which will be written
1260
 
        to the target destination.
1261
 
 
1262
 
        New file takes the permissions of any existing file with that id,
1263
 
        unless mode_id is specified.
 
1256
        :seealso: new_file.
 
1257
 
 
1258
        :param contents: an iterator of strings, all of which will be written
 
1259
            to the target destination.
 
1260
        :param trans_id: TreeTransform handle
 
1261
        :param mode_id: If not None, force the mode of the target file to match
 
1262
            the mode of the object referenced by mode_id.
 
1263
            Otherwise, we will try to preserve mode bits of an existing file.
 
1264
        :param sha1: If the sha1 of this content is already known, pass it in.
 
1265
            We can use it to prevent future sha1 computations.
1264
1266
        """
1265
1267
        name = self._limbo_name(trans_id)
1266
1268
        f = open(name, 'wb')
1273
1275
                f.close()
1274
1276
                os.unlink(name)
1275
1277
                raise
1276
 
            if contents.__class__ is list:
1277
 
                sha_digest = osutils.sha_strings(contents)
1278
 
                f.writelines(contents)
1279
 
            else:
1280
 
                sha_value = osutils.sha()
1281
 
                def observe_sha1(contents):
1282
 
                    sha_value_update = sha_value.update
1283
 
                    for content in contents:
1284
 
                        sha_value_update(content)
1285
 
                        yield content
1286
 
                f.writelines(observe_sha1(contents))
1287
 
                sha_digest = sha_value.hexdigest()
 
1278
            f.writelines(contents)
1288
1279
        finally:
1289
1280
            f.close()
1290
1281
        self._set_mtime(name)
1292
1283
        # It is unfortunate we have to use lstat instead of fstat, but we just
1293
1284
        # used utime and chmod on the file, so we need the accurate final
1294
1285
        # details.
1295
 
        self._observed_sha1s[trans_id] = (sha_digest, osutils.lstat(name))
 
1286
        if sha1 is not None:
 
1287
            self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1296
1288
 
1297
1289
    def _read_file_chunks(self, trans_id):
1298
1290
        cur_file = open(self._limbo_name(trans_id), 'rb')
1855
1847
                        modified_paths.append(full_path)
1856
1848
                if trans_id in self._new_executability:
1857
1849
                    self._set_executability(path, trans_id)
 
1850
                if trans_id in self._observed_sha1s:
 
1851
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
 
1852
                    st = osutils.lstat(full_path)
 
1853
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1858
1854
        finally:
1859
1855
            child_pb.finished()
1860
1856
        self._new_contents.clear()
2547
2543
                    executable = tree.is_executable(file_id, tree_path)
2548
2544
                    if executable:
2549
2545
                        tt.set_executability(executable, trans_id)
2550
 
                    trans_data = (trans_id, tree_path)
 
2546
                    trans_data = (trans_id, tree_path, entry.text_sha1)
2551
2547
                    deferred_contents.append((file_id, trans_data))
2552
2548
                else:
2553
2549
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2598
2594
        unchanged = dict(unchanged)
2599
2595
        new_desired_files = []
2600
2596
        count = 0
2601
 
        for file_id, (trans_id, tree_path) in desired_files:
 
2597
        for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2602
2598
            accelerator_path = unchanged.get(file_id)
2603
2599
            if accelerator_path is None:
2604
 
                new_desired_files.append((file_id, (trans_id, tree_path)))
 
2600
                new_desired_files.append((file_id,
 
2601
                    (trans_id, tree_path, text_sha1)))
2605
2602
                continue
2606
2603
            pb.update('Adding file contents', count + offset, total)
2607
2604
            if hardlink:
2623
2620
                        pass
2624
2621
            count += 1
2625
2622
        offset += count
2626
 
    for count, ((trans_id, tree_path), contents) in enumerate(
 
2623
    for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2627
2624
            tree.iter_files_bytes(new_desired_files)):
2628
2625
        if wt.supports_content_filtering():
2629
2626
            filters = wt._content_filter_stack(tree_path)