def test_revision_tree(self): commit_id = self.simple_commit() revid = default_mapping.revision_id_foreign_to_bzr(commit_id) repo = Repository.open('.') tree = repo.revision_tree(revid) self.assertEquals(tree.get_revision_id(), revid) self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
def run(self, src_location, dest_location): from bzrlib.bzrdir import BzrDir, format_registry from bzrlib.errors import NoRepositoryPresent, NotBranchError from bzrlib.repository import Repository source_repo = Repository.open(src_location) format = format_registry.make_bzrdir('rich-root-pack') try: target_bzrdir = BzrDir.open(dest_location) except NotBranchError: target_bzrdir = BzrDir.create(dest_location, format=format) try: target_repo = target_bzrdir.open_repository() except NoRepositoryPresent: target_repo = target_bzrdir.create_repository(shared=True) target_repo.fetch(source_repo) for name, ref in source_repo._git.heads().iteritems(): head_loc = os.path.join(dest_location, name) try: head_bzrdir = BzrDir.open(head_loc) except NotBranchError: head_bzrdir = BzrDir.create(head_loc, format=format) try: head_branch = head_bzrdir.open_branch() except NotBranchError: head_branch = head_bzrdir.create_branch() head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
def __is_repository(self): try: r = Repository.open(self.directory) except: return False return r.is_shared()
def run(self, src_location, dest_location): from bzrlib.bzrdir import BzrDir, format_registry from bzrlib.errors import NoRepositoryPresent, NotBranchError from bzrlib.repository import Repository source_repo = Repository.open(src_location) format = format_registry.make_bzrdir('rich-root-pack') try: target_bzrdir = BzrDir.open(dest_location) except NotBranchError: target_bzrdir = BzrDir.create(dest_location, format=format) try: target_repo = target_bzrdir.open_repository() except NoRepositoryPresent: target_repo = target_bzrdir.create_repository(shared=True) target_repo.fetch(source_repo) for name, ref in source_repo._git.heads().iteritems(): head_loc = os.path.join(dest_location, name) try: head_bzrdir = BzrDir.open(head_loc) except NotBranchError: head_bzrdir = BzrDir.create(head_loc, format=format) try: head_branch = head_bzrdir.open_branch() except NotBranchError: head_branch = head_bzrdir.create_branch() head_branch.generate_revision_history( source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
def run(self): # Go through configured branches. for entry in self._config.get("General", "branches").split(","): (url, projectname) = entry.split(" ") self._db_lock.acquire() # Initialize the branch data at this URL if needed. if not url in self._db["branches"]: self._db["branches"][url] = { "revno": 0, } self._db_lock.release() # Open the branch. branch = Branch.open(url) # Get the latest revision number. revno = branch.revno() # Acquire lock, store old revision number, and update with new one. self._db_lock.acquire() old_revno = self._db["branches"][url]["revno"] self._db["branches"][url]["revno"] = revno self._db_lock.release() # If the previous revision number was not 0 and it is different from # the latest revision number, it's time to do notifications. if old_revno and old_revno != revno: # Get revision IDs. revids = [ branch.get_rev_id(old_revno + i + 1) for i in range(revno - old_revno) ] # Open the repository. repo = Repository.open(url) # Notify about all new revisions. for (i, revision) in enumerate(repo.get_revisions(revids)): # Get the revision's committer and remove the email part. committer = re.sub(" \<.+\>", "", revision.committer) # Construct the notify message. msg = "{0} committed r{1} to branch {2}: {3}".format( committer, old_revno + i + 1, revision.properties["branch-nick"], revision.message) for bot in self._bots: if bot.config.getboolean(bot.section, "dmsay_commits"): bot.command_queue_add( "/dmsay {0}".format(msg), bot.config.getfloat(bot.section, "delay")) if self._cia: self._cia.submit( self.generate_cia_xml(branch, old_revno + i + 1, revision, projectname))
def test_uses_lockdir(self): """repo format 7 actually locks on lockdir""" base_url = self.get_url() control = BzrDirMetaFormat1().initialize(base_url) repo = RepositoryFormat7().initialize(control, shared=True) t = control.get_repository_transport(None) repo.lock_write() repo.unlock() del repo # make sure the same lock is created by opening it repo = Repository.open(base_url) repo.lock_write() self.assertTrue(t.has('lock/held/info')) repo.unlock() self.assertFalse(t.has('lock/held/info'))
def apply_pack(self, refs, read): """ apply pack from client to current repository """ fd, path = tempfile.mkstemp(suffix=".pack") f = os.fdopen(fd, 'w') f.write(read()) f.close() p = PackData(path) entries = p.sorted_entries() write_pack_index_v2(path[:-5] + ".idx", entries, p.calculate_checksum()) def get_objects(): pack = Pack(path[:-5]) for obj in pack.iterobjects(): yield obj target = Repository.open(self.directory) target.lock_write() try: target.start_write_group() try: import_git_objects(target, self.mapping, iter(get_objects())) finally: target.commit_write_group() finally: target.unlock() for oldsha, sha, ref in refs: if ref[:11] == 'refs/heads/': branch_nick = ref[11:] try: target_dir = BzrDir.open(self.directory + "/" + branch_nick) except: target_dir = BzrDir.create(self.directory + "/" + branch_nick) try: target_branch = target_dir.open_branch() except: target_branch = target_dir.create_branch() rev_id = self.mapping.revision_id_foreign_to_bzr(sha) target_branch.generate_revision_history(rev_id)
def test_get_revision(self): # GitRepository.get_revision gives a Revision object. # Create a git repository with a revision. tests.run_git('init') builder = tests.GitBranchBuilder() builder.set_file('a', 'text for a\n', False) commit_handle = builder.commit('Joe Foo <*****@*****.**>', u'message') mapping = builder.finish() commit_id = mapping[commit_handle] # Get the corresponding Revision object. revid = default_mapping.revision_id_foreign_to_bzr(commit_id) repo = Repository.open('.') rev = repo.get_revision(revid) self.assertIsInstance(rev, revision.Revision)
def run(self): # Go through configured branches. for entry in self._config.get("General", "branches").split(","): (url, projectname) = entry.split(" ") self._db_lock.acquire() # Initialize the branch data at this URL if needed. if not url in self._db["branches"]: self._db["branches"][url] = { "revno": 0, } self._db_lock.release() # Open the branch. branch = Branch.open(url) # Get the latest revision number. revno = branch.revno() # Acquire lock, store old revision number, and update with new one. self._db_lock.acquire() old_revno = self._db["branches"][url]["revno"] self._db["branches"][url]["revno"] = revno self._db_lock.release() # If the previous revision number was not 0 and it is different from # the latest revision number, it's time to do notifications. if old_revno and old_revno != revno: # Get revision IDs. revids = [branch.get_rev_id(old_revno + i + 1) for i in range(revno - old_revno)] # Open the repository. repo = Repository.open(url) # Notify about all new revisions. for (i, revision) in enumerate(repo.get_revisions(revids)): # Get the revision's committer and remove the email part. committer = re.sub(" \<.+\>", "", revision.committer) # Construct the notify message. msg = "{0} committed r{1} to branch {2}: {3}".format(committer, old_revno + i + 1, revision.properties["branch-nick"], revision.message) for bot in self._bots: if bot.config.getboolean(bot.section, "dmsay_commits"): bot.command_queue_add("/dmsay {0}".format(msg), bot.config.getfloat(bot.section, "delay")) if self._cia: self._cia.submit(self.generate_cia_xml(branch, old_revno + i + 1, revision, projectname))
def __init__(self, directory=None, config=None, mr_compat=True): self.directory = directory or os.getcwd() self.control_dir = os.path.join(self.directory, '.bzr') self.config_file = config or os.path.join(self.directory, '.mrconfig') self.mr_compat = mr_compat if mr_compat: if self.__is_repository(): self.config = self.__read_cfg() self.bzr_dir = Repository.open(self.directory) else: self.config = ConfigParser.RawConfigParser() r = BzrDir.create(self.directory) self.bzr_dir = r.create_repository(shared=True) else: self.config = ConfigParser.RawConfigParser() self.bzr_dir = None
def fetch_objects(self, determine_wants, graph_walker, progress): """ yield git objects to send to client """ wants = determine_wants(self.get_refs()) commits_to_send = set( [self.mapping.revision_id_foreign_to_bzr(w) for w in wants]) rev_done = set() obj_sent = set() repo = Repository.open(self.directory) objects = set() repo.lock_read() try: have = graph_walker.next() while have: rev_done.add(have) if repo.has_revision( self.mapping.revision_id_foregin_to_bzr(sha)): graph_walker.ack(have) have = graph_walker.next() while commits_to_send: commit = commits_to_send.pop() if commit in rev_done: continue rev_done.add(commit) rev = repo.get_revision(commit) commits_to_send.update( [p for p in rev.parent_ids if not p in rev_done]) for sha, obj in inventory_to_tree_and_blobs( repo, self.mapping, commit): if sha not in obj_sent: obj_sent.add(sha) objects.add(obj) objects.add(revision_to_commit(rev, self.mapping, sha)) finally: repo.unlock() return (len(objects), iter(objects))
def apply_pack(self, refs, read): """ apply pack from client to current repository """ fd, path = tempfile.mkstemp(suffix=".pack") f = os.fdopen(fd, 'w') f.write(read()) f.close() p = PackData(path) entries = p.sorted_entries() write_pack_index_v2(path[:-5]+".idx", entries, p.calculate_checksum()) def get_objects(): pack = Pack(path[:-5]) for obj in pack.iterobjects(): yield obj target = Repository.open(self.directory) target.lock_write() try: target.start_write_group() try: import_git_objects(target, self.mapping, iter(get_objects())) finally: target.commit_write_group() finally: target.unlock() for oldsha, sha, ref in refs: if ref[:11] == 'refs/heads/': branch_nick = ref[11:] try: target_dir = BzrDir.open(self.directory + "/" + branch_nick) except: target_dir = BzrDir.create(self.directory + "/" + branch_nick) try: target_branch = target_dir.open_branch() except: target_branch = target_dir.create_branch() rev_id = self.mapping.revision_id_foreign_to_bzr(sha) target_branch.generate_revision_history(rev_id)
def fetch_objects(self, determine_wants, graph_walker, progress): """ yield git objects to send to client """ wants = determine_wants(self.get_refs()) commits_to_send = set([self.mapping.revision_id_foreign_to_bzr(w) for w in wants]) rev_done = set() obj_sent = set() repo = Repository.open(self.directory) objects = set() repo.lock_read() try: have = graph_walker.next() while have: rev_done.add(have) if repo.has_revision(self.mapping.revision_id_foregin_to_bzr(sha)): graph_walker.ack(have) have = graph_walker.next() while commits_to_send: commit = commits_to_send.pop() if commit in rev_done: continue rev_done.add(commit) rev = repo.get_revision(commit) commits_to_send.update([p for p in rev.parent_ids if not p in rev_done]) for sha, obj in inventory_to_tree_and_blobs(repo, self.mapping, commit): if sha not in obj_sent: obj_sent.add(sha) objects.add(obj) objects.add(revision_to_commit(rev, self.mapping, sha)) finally: repo.unlock() return (len(objects), iter(objects))
def test_get_inventory(self): # GitRepository.get_inventory gives a GitInventory object with # plausible entries for typical cases. commit_id = self.simple_commit() # Get the corresponding Inventory object. revid = default_mapping.revision_id_foreign_to_bzr(commit_id) repo = Repository.open('.') inv = repo.get_inventory(revid) self.assertIsInstance(inv, inventory.Inventory) printed_inv = '\n'.join( repr((path, entry.executable, entry)) for path, entry in inv.iter_entries()) self.assertEqualDiff( printed_inv, "('', False, InventoryDirectory('TREE_ROOT', u'', parent_id=None," " revision='" + default_mapping.revision_id_foreign_to_bzr( "69c39cfa65962f3cf16b9b3eb08a15954e9d8590") + "'))\n" "(u'data', False, InventoryFile('data', u'data'," " parent_id='TREE_ROOT'," " sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n" "(u'executable', True, InventoryFile('executable', u'executable'," " parent_id='TREE_ROOT'," " sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n" "(u'link', False, InventoryLink('link', u'link'," " parent_id='TREE_ROOT', revision='" + default_mapping.revision_id_foreign_to_bzr( "69c39cfa65962f3cf16b9b3eb08a15954e9d8590") + "'))\n" "(u'subdir', False, InventoryDirectory('subdir', u'subdir'," " parent_id='TREE_ROOT', revision='" + default_mapping.revision_id_foreign_to_bzr( "69c39cfa65962f3cf16b9b3eb08a15954e9d8590") + "'))\n" "(u'subdir/subfile', False, InventoryFile('subdir/subfile'," " u'subfile', parent_id='subdir'," " sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
def setUp(self): tests.TestCaseWithTransport.setUp(self) git.repo.Repo.create(self.test_dir) self.git_repo = Repository.open(self.test_dir)
def test_has_git_repo(self): tests.run_git('init') repo = Repository.open('.') self.assertIsInstance(repo._git, git.repo.Repo)
def test_open_existing(self): tests.run_git('init') repo = Repository.open('.') self.assertIsInstance(repo, repository.GitRepository)
def test_get_revision_unknown(self): tests.run_git('init') repo = Repository.open('.') self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")