def close(self, checksum=None): if self.file_stream: self.file_stream.seek(0) file_data = self.file_stream.read() else: file_data = "" if self.is_special is not None: self.is_symlink = (self.is_special and file_data.startswith("link ")) if self.is_symlink: ie = self.tree._bzr_inventory.add_path(self.path, 'symlink', self.file_id) else: ie = self.tree._bzr_inventory.add_path(self.path, 'file', self.file_id) ie.revision = self.revision_id actual_checksum = md5(file_data).hexdigest() assert checksum is None or checksum == actual_checksum, \ "checksum mismatch: %r != %r" % (checksum, actual_checksum) if self.is_symlink: ie.symlink_target = file_data[len("link "):] else: ie.text_sha1 = osutils.sha_string(file_data) ie.text_size = len(file_data) ie.executable = self.is_executable self.file_stream = None
def make_file(self, file_id, name, parent_id, content=b'content\n', revision=b'new-test-rev'): ie = InventoryFile(file_id, name, parent_id) ie.text_sha1 = osutils.sha_string(content) ie.text_size = len(content) ie.revision = revision return ie
def test_get_entry_by_path_partial(self): inv = inventory.Inventory(b'TREE_ROOT') inv.root.revision = b'revision' for args in [('src', 'directory', b'src-id'), ('doc', 'directory', b'doc-id'), ('src/hello.c', 'file'), ('src/bye.c', 'file', b'bye-id'), ('Makefile', 'file'), ('external', 'tree-reference', b'other-root')]: ie = inv.add_path(*args) ie.revision = b'revision' if args[1] == 'file': ie.text_sha1 = osutils.sha_string(b'content\n') ie.text_size = len(b'content\n') if args[1] == 'tree-reference': ie.reference_revision = b'reference' inv = self.inv_to_test_inv(inv) # Standard lookups ie, resolved, remaining = inv.get_entry_by_path_partial('') self.assertEqual((ie.file_id, resolved, remaining), (b'TREE_ROOT', [], [])) ie, resolved, remaining = inv.get_entry_by_path_partial('src') self.assertEqual((ie.file_id, resolved, remaining), (b'src-id', ['src'], [])) ie, resolved, remaining = inv.get_entry_by_path_partial('src/bye.c') self.assertEqual((ie.file_id, resolved, remaining), (b'bye-id', ['src', 'bye.c'], [])) # Paths in the external tree ie, resolved, remaining = inv.get_entry_by_path_partial('external') self.assertEqual((ie.file_id, resolved, remaining), (b'other-root', ['external'], [])) ie, resolved, remaining = inv.get_entry_by_path_partial('external/blah') self.assertEqual((ie.file_id, resolved, remaining), (b'other-root', ['external'], ['blah'])) # Nonexistant paths ie, resolved, remaining = inv.get_entry_by_path_partial('foo.c') self.assertEqual((ie, resolved, remaining), (None, None, None))
def test_get_file_sha1(self): work_tree = self.make_branch_and_tree('tree') self.build_tree_contents([('tree/file', b'file content')]) work_tree.add('file') tree = self._convert_tree(work_tree) tree.lock_read() self.addCleanup(tree.unlock) expected = osutils.sha_string(b'file content') self.assertEqual(expected, tree.get_file_sha1('file'))
def entry_sha1(entry): """Calculate the full text sha1 for an inventory entry. :param entry: Inventory entry :return: SHA1 hex string """ if entry.kind == 'symlink': return osutils.sha_string(entry.symlink_target) else: return entry.text_sha1
def _create_text_record(self, fileid, revision, parents, kind, fulltext): key = (fileid, revision) if kind == "symlink": self._symlink_targets[key] = fulltext bzr_fulltext = "" else: (meta, bzr_fulltext) = deserialize_file_text(str(fulltext)) return FulltextContentFactory(key, [(fileid, p) for p in parents], osutils.sha_string(bzr_fulltext), bzr_fulltext)
def test_fetch_inconsistent_last_changed_entries(self): """If an inventory has odd data we should still get what it references. This test tests that we do fetch a file text created in a revision not being fetched, but referenced from the revision we are fetching when the adjacent revisions to the one being fetched do not reference that text. """ if not self.repository_format.supports_full_versioned_files: raise TestNotApplicable('Need full versioned files') tree = self.make_branch_and_tree('source') revid = tree.commit('old') to_repo = self.make_to_repository('to_repo') try: to_repo.fetch(tree.branch.repository, revid) except errors.NoRoundtrippingSupport: raise TestNotApplicable('roundtripping not supported') # Make a broken revision and fetch it. source = tree.branch.repository source.lock_write() self.addCleanup(source.unlock) source.start_write_group() try: # We need two revisions: OLD and NEW. NEW will claim to need a file # 'FOO' changed in 'OLD'. OLD will not have that file at all. source.texts.insert_record_stream([ versionedfile.FulltextContentFactory((b'foo', revid), (), None, b'contents')]) basis = source.revision_tree(revid) parent_id = basis.path2id('') entry = inventory.make_entry('file', 'foo-path', parent_id, b'foo') entry.revision = revid entry.text_size = len('contents') entry.text_sha1 = osutils.sha_string(b'contents') inv_sha1, _ = source.add_inventory_by_delta(revid, [ (None, 'foo-path', b'foo', entry)], b'new', [revid]) rev = Revision(timestamp=0, timezone=None, committer="Foo Bar <*****@*****.**>", message="Message", inventory_sha1=inv_sha1, revision_id=b'new', parent_ids=[revid]) source.add_revision(rev.revision_id, rev) except: source.abort_write_group() raise else: source.commit_write_group() to_repo.fetch(source, b'new') to_repo.lock_read() self.addCleanup(to_repo.unlock) self.assertEqual(b'contents', next(to_repo.texts.get_record_stream([(b'foo', revid)], 'unordered', True)).get_bytes_as('fulltext'))
def _get_file_ie(self, path, flags): file_id = self.path2id(path) parent_id = self.path2id(posixpath.dirname(path)) if 'l' in flags: ie = HgTreeLink(file_id, posixpath.basename(path), parent_id) ie.symlink_target = self.get_symlink_target(path) else: ie = HgTreeFile(file_id, posixpath.basename(path), parent_id) text = self.get_file_text(path) ie.text_sha1 = osutils.sha_string(text) ie.text_size = len(text) ie.executable = ('x' in flags) ie.revision = self.get_file_revision(path) return ie
def test_get_file_verifier(self): work_tree = self.make_branch_and_tree('tree') self.build_tree_contents([('tree/file1', b'file content'), ('tree/file2', b'file content')]) work_tree.add(['file1', 'file2']) tree = self._convert_tree(work_tree) tree.lock_read() self.addCleanup(tree.unlock) (kind, data) = tree.get_file_verifier('file1') self.assertEqual(tree.get_file_verifier('file1'), tree.get_file_verifier('file2')) if kind == "SHA1": expected = osutils.sha_string(b'file content') self.assertEqual(expected, data)
def test_ids(self): """Test detection of files within selected directories.""" inv = inventory.Inventory(b'TREE_ROOT') inv.root.revision = b'revision' for args in [('src', 'directory', b'src-id'), ('doc', 'directory', b'doc-id'), ('src/hello.c', 'file'), ('src/bye.c', 'file', b'bye-id'), ('Makefile', 'file')]: ie = inv.add_path(*args) ie.revision = b'revision' if args[1] == 'file': ie.text_sha1 = osutils.sha_string(b'content\n') ie.text_size = len(b'content\n') inv = self.inv_to_test_inv(inv) self.assertEqual(inv.path2id('src'), b'src-id') self.assertEqual(inv.path2id('src/bye.c'), b'bye-id')
def prepare_inv_with_nested_dirs(self): inv = inventory.Inventory(b'tree-root') inv.root.revision = b'revision' for args in [('src', 'directory', b'src-id'), ('doc', 'directory', b'doc-id'), ('src/hello.c', 'file', b'hello-id'), ('src/bye.c', 'file', b'bye-id'), ('zz', 'file', b'zz-id'), ('src/sub/', 'directory', b'sub-id'), ('src/zz.c', 'file', b'zzc-id'), ('src/sub/a', 'file', b'a-id'), ('Makefile', 'file', b'makefile-id')]: ie = inv.add_path(*args) ie.revision = b'revision' if args[1] == 'file': ie.text_sha1 = osutils.sha_string(b'content\n') ie.text_size = len(b'content\n') return self.inv_to_test_inv(inv)
def make_one_file_inventory(self, repo, revision, parents, inv_revision=None, root_revision=None, file_contents=None, make_file_version=True): """Make an inventory containing a version of a file with ID 'a-file'. The file's ID will be 'a-file', and its filename will be 'a file name', stored at the tree root. :param repo: a repository to add the new file version to. :param revision: the revision ID of the new inventory. :param parents: the parents for this revision of 'a-file'. :param inv_revision: if not None, the revision ID to store in the inventory entry. Otherwise, this defaults to revision. :param root_revision: if not None, the inventory's root.revision will be set to this. :param file_contents: if not None, the contents of this file version. Otherwise a unique default (based on revision ID) will be generated. """ inv = Inventory(revision_id=revision) if root_revision is not None: inv.root.revision = root_revision file_id = b'a-file-id' entry = InventoryFile(file_id, 'a file name', b'TREE_ROOT') if inv_revision is not None: entry.revision = inv_revision else: entry.revision = revision entry.text_size = 0 if file_contents is None: file_contents = b'%sline\n' % entry.revision entry.text_sha1 = osutils.sha_string(file_contents) inv.add(entry) if make_file_version: repo.texts.add_lines((file_id, revision), [(file_id, parent) for parent in parents], [file_contents]) return inv
def test_fetch_revision_hash(self): """Ensure that inventory hashes are updated by fetch""" if not self.repository_format_to.supports_full_versioned_files: raise TestNotApplicable('Need full versioned files') from_tree = self.make_branch_and_tree('tree') revid = from_tree.commit('foo') to_repo = self.make_to_repository('to') try: to_repo.fetch(from_tree.branch.repository) except errors.NoRoundtrippingSupport: raise TestNotApplicable('roundtripping not supported') recorded_inv_sha1 = to_repo.get_revision(revid).inventory_sha1 to_repo.lock_read() self.addCleanup(to_repo.unlock) stream = to_repo.inventories.get_record_stream([(revid, )], 'unordered', True) bytes = next(stream).get_bytes_as('fulltext') computed_inv_sha1 = osutils.sha_string(bytes) self.assertEqual(computed_inv_sha1, recorded_inv_sha1)
parents: message: initial null commit inventory: directory . TREE_ROT test@user-1 no properties: branch-nick: test branch """ REV_1_SHORT = b"""\ bazaar-ng testament short form 1 revision-id: test@user-1 sha1: %s """ % osutils.sha_string(REV_1_TESTAMENT) REV_1_SHORT_STRICT = b"""\ bazaar-ng testament short form 2.1 revision-id: test@user-1 sha1: %s """ % osutils.sha_string(REV_1_STRICT_TESTAMENT) REV_1_SHORT_STRICT3 = b"""\ bazaar testament short form 3 strict revision-id: test@user-1 sha1: %s """ % osutils.sha_string(REV_1_STRICT_TESTAMENT3)
def get_file_sha1(self, path, stat_value=None): return osutils.sha_string(self.get_file_text(path))