def update_file(self, path, content, author, parent_revision, commit_message=None): commit_message = get_commit_message(commit_message) self.branch.lock_write() try: file_id = self.tree.path2id(path) if file_id is not None: f = File(self, path, file_id) content = self._get_final_text(content, f, parent_revision) else: content = normalize_content(content) with TransformPreview(self.tree) as tt: trans_id = tt.trans_id_tree_path(path) if tt.tree_kind(trans_id) is not None: tt.delete_contents(trans_id) else: name = splitpath(path)[-1] tt.version_file(gen_file_id(name), trans_id) create_parents(tt, path, trans_id) tt.create_file(content, trans_id) try: tt.commit(self.branch, commit_message, authors=[author]) except MalformedTransform, e: for conflict in e.conflicts: if conflict[0] == 'non-directory parent': path = FinalPaths(tt).get_path(trans_id) raise FileExists( '%s exists and is not a directory' % conflict[1]) raise self.tree = self.branch.basis_tree()
def create_entries(base, layout): if not layout: return num_dirs, num_files = layout[0] for dnum in xrange(num_dirs): if base: path = '%s/%02d_directory' % (base, dnum) else: path = '%02d_directory' % (dnum,) dir_id = generate_ids.gen_file_id(path) state.add(path, dir_id, 'directory', dir_stat, '') for fnum in xrange(num_files): fname = '%s/%02d_filename' % (path, fnum) file_id = generate_ids.gen_file_id(fname) state.add(fname, file_id, 'file', file_stat, file_sha1) create_entries(path, layout[1:])
def writeFile(self, path, contents): """Write file to branch; may be an update or a new file. If you write a file multiple times, the first one is used and the rest ignored. """ assert self.is_open, "Writing file to closed DirectBranchCommit." if path in self.files: # We already have this file. Ignore second write. return file_id = self.revision_tree.path2id(path) if file_id is None: parent_path, name = os.path.split(path) parent_id = self._getDir(parent_path) file_id = gen_file_id(name) self.transform_preview.new_file( name, parent_id, [contents], file_id) else: trans_id = self.transform_preview.trans_id_tree_path(path) # Delete old contents. It doesn't actually matter whether # we do this before creating the new contents. self.transform_preview.delete_contents(trans_id) self.transform_preview.create_file([contents], trans_id) self.files.add(path)
def writeFile(self, path, contents): """Write file to branch; may be an update or a new file. If you write a file multiple times, the first one is used and the rest ignored. """ assert self.is_open, "Writing file to closed DirectBranchCommit." if path in self.files: # We already have this file. Ignore second write. return file_id = self.revision_tree.path2id(path) if file_id is None: parent_path, name = os.path.split(path) parent_id = self._getDir(parent_path) file_id = gen_file_id(name) self.transform_preview.new_file(name, parent_id, [contents], file_id) else: trans_id = self.transform_preview.trans_id_tree_path(path) # Delete old contents. It doesn't actually matter whether # we do this before creating the new contents. self.transform_preview.delete_contents(trans_id) self.transform_preview.create_file([contents], trans_id) self.files.add(path)
def bzr_file_id_and_new(self, path): """Get a Bazaar file identifier and new flag for a path. :return: file_id, is_new where is_new = True if the file_id is newly created """ if path not in self._paths_deleted_this_commit: # Try file-ids renamed in this commit id = self._modified_file_ids.get(path) if id is not None: return id, False # Try the basis inventory id = self.basis_inventory.path2id(path) if id is not None: return id, False # Try the other inventories if len(self.parents) > 1: for inv in self.parent_invs[1:]: id = self.basis_inventory.path2id(path) if id is not None: return id, False # Doesn't exist yet so create it dirname, basename = osutils.split(path) id = generate_ids.gen_file_id(basename) self.debug("Generated new file id %s for '%s' in revision-id '%s'", id, path, self.revision_id) self._new_file_ids[path] = id return id, True
def create_parents(tt, path, trans_id): prev_trans_id = trans_id for parent_path, tail in iter_paths(path): trans_id = tt.trans_id_tree_path(parent_path) if tt.tree_kind(trans_id) is not None: break tt.adjust_path(tail, trans_id, prev_trans_id) tt.create_directory(trans_id) tt.version_file(gen_file_id(tail), trans_id) prev_trans_id = trans_id
def assertGenFileId(self, regex, filename): """gen_file_id should create a file id matching the regex. The file id should be ascii, and should be an 8-bit string """ file_id = generate_ids.gen_file_id(filename) self.assertContainsRe(file_id, '^'+regex+'$') # It should be a utf8 file_id, not a unicode one self.assertIsInstance(file_id, str) # gen_file_id should always return ascii file ids. file_id.decode('ascii')
def assertGenFileId(self, regex, filename): """gen_file_id should create a file id matching the regex. The file id should be ascii, and should be an 8-bit string """ file_id = generate_ids.gen_file_id(filename) self.assertContainsRe(file_id, '^' + regex + '$') # It should be a utf8 file_id, not a unicode one self.assertIsInstance(file_id, str) # gen_file_id should always return ascii file ids. file_id.decode('ascii')
def _fix_other_tree(self, this_tree, other_tree): """We need to pretend that other_tree's root is actually not at ''.""" parent_dir, name = osutils.split(self._target_subdir) parent_id = this_tree.path2id(parent_dir) root_ie = other_tree.inventory.root root_ie.parent_id = parent_id root_ie.name = name new_file_id = generate_ids.gen_file_id(name) trace.mutter('munging root_ie.file_id: %s => %s', root_ie.file_id, new_file_id) del other_tree.inventory._byid[root_ie.file_id] root_ie.file_id = new_file_id other_tree.inventory._byid[new_file_id] = root_ie # We need to fake a new id for root_ie for child_ie in root_ie.children.itervalues(): child_ie.parent_id = new_file_id
def _getDir(self, path): """Get trans_id for directory "path." Create if necessary.""" path_id = self.path_ids.get(path) if path_id: # This is a path we've just created in the branch. return path_id if self.revision_tree.path2id(path): # This is a path that was already in the branch. return self.transform_preview.trans_id_tree_path(path) # Look up (or create) parent directory. parent_dir, dirname = os.path.split(path) if dirname: parent_id = self._getDir(parent_dir) else: parent_id = ROOT_PARENT # Create new directory. dirfile_id = gen_file_id(path) path_id = self.transform_preview.new_directory(dirname, parent_id, dirfile_id) self.path_ids[path] = path_id return path_id
def _getDir(self, path): """Get trans_id for directory "path." Create if necessary.""" path_id = self.path_ids.get(path) if path_id: # This is a path we've just created in the branch. return path_id if self.revision_tree.path2id(path): # This is a path that was already in the branch. return self.transform_preview.trans_id_tree_path(path) # Look up (or create) parent directory. parent_dir, dirname = os.path.split(path) if dirname: parent_id = self._getDir(parent_dir) else: parent_id = ROOT_PARENT # Create new directory. dirfile_id = gen_file_id(path) path_id = self.transform_preview.new_directory( dirname, parent_id, dirfile_id) self.path_ids[path] = path_id return path_id