def test_mkdir_w_nested_trees(self): """'bzr mkdir' with nested trees""" self.run_bzr('init') os.mkdir('a') os.chdir('a') self.run_bzr('init') os.mkdir('b') os.chdir('b') self.run_bzr('init') os.chdir('../..') self.run_bzr('mkdir dir a/dir a/b/dir') self.failUnless(os.path.isdir('dir')) self.failUnless(os.path.isdir('a/dir')) self.failUnless(os.path.isdir('a/b/dir')) wt = WorkingTree.open('.') wt_a = WorkingTree.open('a') wt_b = WorkingTree.open('a/b') delta = wt.changes_from(wt.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.failIf(delta.modified) delta = wt_a.changes_from(wt_a.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.failIf(delta.modified) delta = wt_b.changes_from(wt_b.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.failIf(delta.modified)
def test_init_branch(self): out, err = self.run_bzr('init') self.assertEqual('', out) self.assertEqual('', err) # Can it handle subdirectories of branches too ? out, err = self.run_bzr('init subdir1') self.assertEqual('', out) self.assertEqual('', err) WorkingTree.open('subdir1') self.run_bzr_error( ['Parent directory of subdir2/nothere does not exist'], 'init subdir2/nothere') out, err = self.run_bzr('init subdir2/nothere', retcode=3) self.assertEqual('', out) os.mkdir('subdir2') out, err = self.run_bzr('init subdir2') self.assertEqual('', out) self.assertEqual('', err) # init an existing branch. out, err = self.run_bzr('init subdir2', retcode=3) self.assertEqual('', out) self.failUnless(err.startswith('bzr: ERROR: Already a branch:'))
def test_init_branch(self): out, err = self.run_bzr('init') self.assertEqual( "Created a standalone tree (format: %s)\n" % (self._default_label, ), out) self.assertEqual('', err) # Can it handle subdirectories of branches too ? out, err = self.run_bzr('init subdir1') self.assertEqual( "Created a standalone tree (format: %s)\n" % (self._default_label, ), out) self.assertEqual('', err) WorkingTree.open('subdir1') self.run_bzr_error( ['Parent directory of subdir2/nothere does not exist'], 'init subdir2/nothere') out, err = self.run_bzr('init subdir2/nothere', retcode=3) self.assertEqual('', out) os.mkdir('subdir2') out, err = self.run_bzr('init subdir2') self.assertEqual( "Created a standalone tree (format: %s)\n" % (self._default_label, ), out) self.assertEqual('', err) # init an existing branch. out, err = self.run_bzr('init subdir2', retcode=3) self.assertEqual('', out) self.assertTrue(err.startswith('bzr: ERROR: Already a branch:'))
def test_mkdir_w_nested_trees(self): """'bzr mkdir' with nested trees""" self.make_branch_and_tree('.') self.make_branch_and_tree('a') self.make_branch_and_tree('a/b') self.run_bzr(['mkdir', 'dir', 'a/dir', 'a/b/dir']) self.assertTrue(os.path.isdir('dir')) self.assertTrue(os.path.isdir('a/dir')) self.assertTrue(os.path.isdir('a/b/dir')) wt = WorkingTree.open('.') wt_a = WorkingTree.open('a') wt_b = WorkingTree.open('a/b') delta = wt.changes_from(wt.basis_tree()) self.assertEqual(len(delta.added), 1) self.assertEqual(delta.added[0][0], 'dir') self.assertFalse(delta.modified) delta = wt_a.changes_from(wt_a.basis_tree()) self.assertEqual(len(delta.added), 1) self.assertEqual(delta.added[0][0], 'dir') self.assertFalse(delta.modified) delta = wt_b.changes_from(wt_b.basis_tree()) self.assertEqual(len(delta.added), 1) self.assertEqual(delta.added[0][0], 'dir') self.assertFalse(delta.modified)
def test_init_branch(self): out, err = self.run_bzr('init') self.assertEqual("Created a standalone tree (format: %s)\n" % ( self._default_label,), out) self.assertEqual('', err) # Can it handle subdirectories of branches too ? out, err = self.run_bzr('init subdir1') self.assertEqual("Created a standalone tree (format: %s)\n" % ( self._default_label,), out) self.assertEqual('', err) WorkingTree.open('subdir1') self.run_bzr_error(['Parent directory of subdir2/nothere does not exist'], 'init subdir2/nothere') out, err = self.run_bzr('init subdir2/nothere', retcode=3) self.assertEqual('', out) os.mkdir('subdir2') out, err = self.run_bzr('init subdir2') self.assertEqual("Created a standalone tree (format: %s)\n" % ( self._default_label,), out) self.assertEqual('', err) # init an existing branch. out, err = self.run_bzr('init subdir2', retcode=3) self.assertEqual('', out) self.assertTrue(err.startswith('bzr: ERROR: Already a branch:'))
def test_mkdir_w_nested_trees(self): """'bzr mkdir' with nested trees""" self.make_branch_and_tree('.') self.make_branch_and_tree('a') self.make_branch_and_tree('a/b') self.run_bzr(['mkdir', 'dir', 'a/dir', 'a/b/dir']) self.assertTrue(os.path.isdir('dir')) self.assertTrue(os.path.isdir('a/dir')) self.assertTrue(os.path.isdir('a/b/dir')) wt = WorkingTree.open('.') wt_a = WorkingTree.open('a') wt_b = WorkingTree.open('a/b') delta = wt.changes_from(wt.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.assertFalse(delta.modified) delta = wt_a.changes_from(wt_a.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.assertFalse(delta.modified) delta = wt_b.changes_from(wt_b.basis_tree()) self.assertEquals(len(delta.added), 1) self.assertEquals(delta.added[0][0], 'dir') self.assertFalse(delta.modified)
def update_branches(sourcecode_directory, update_branches, possible_transports=None, tip=False, quiet=False): """Update the existing branches in sourcecode.""" if possible_transports is None: possible_transports = [] # XXX: JonathanLange 2009-11-09: Rather than updating one branch after # another, we could instead try to get them in parallel. for project, (branch_url, revision, optional) in (update_branches.iteritems()): # Update project from branch_url. destination = os.path.join(sourcecode_directory, project) if not quiet: print 'Updating %s to %s' % (project, _format_revision_name(revision, tip)) local_tree = WorkingTree.open(destination) try: remote_branch = Branch.open( branch_url, possible_transports=possible_transports) except BzrError: if optional: report_exception(sys.exc_info(), sys.stderr) continue else: raise possible_transports.append(remote_branch.bzrdir.root_transport) revision_id = get_revision_id(revision, remote_branch, tip) try: result = local_tree.pull(remote_branch, stop_revision=revision_id, overwrite=True, possible_transports=possible_transports) except IncompatibleRepositories: # XXX JRV 20100407: Ideally remote_branch.bzrdir._format # should be passed into upgrade() to ensure the format is the same # locally and remotely. Unfortunately smart server branches # have their _format set to RemoteFormat rather than an actual # format instance. upgrade(destination) # Upgraded, repoen working tree local_tree = WorkingTree.open(destination) result = local_tree.pull(remote_branch, stop_revision=revision_id, overwrite=True, possible_transports=possible_transports) if result.old_revid == result.new_revid: if not quiet: print ' (No change)' else: if result.old_revno < result.new_revno: change = 'Updated' else: change = 'Reverted' if not quiet: print ' (%s from %s to %s)' % (change, result.old_revno, result.new_revno)
def test_new_files(self): if sys.platform == 'win32': raise TestSkipped('chmod has no effect on win32') t = self.make_branch_and_tree('.') b = t.branch with open('a', 'wb') as f: f.write('foo\n') # ensure check_mode_r works with capital-letter file-ids like TREE_ROOT t.add('a', 'CAPS-ID') t.commit('foo') chmod_r('.bzr', 0644, 0755) check_mode_r(self, '.bzr', 0644, 0755) # although we are modifying the filesystem # underneath the objects, they are not locked, and thus it must # be safe for most operations. But here we want to observe a # mode change in the control bits, which current do not refresh # when a new lock is taken out. t = WorkingTree.open('.') b = t.branch self.assertEqualMode(0755, b.control_files._dir_mode) self.assertEqualMode(0644, b.control_files._file_mode) self.assertEqualMode(0755, b.bzrdir._get_dir_mode()) self.assertEqualMode(0644, b.bzrdir._get_file_mode()) # Modifying a file shouldn't break the permissions with open('a', 'wb') as f: f.write('foo2\n') t.commit('foo2') # The mode should be maintained after commit check_mode_r(self, '.bzr', 0644, 0755) # Adding a new file should maintain the permissions with open('b', 'wb') as f: f.write('new b\n') t.add('b') t.commit('new b') check_mode_r(self, '.bzr', 0644, 0755) # Recursively update the modes of all files chmod_r('.bzr', 0664, 0775) check_mode_r(self, '.bzr', 0664, 0775) t = WorkingTree.open('.') b = t.branch self.assertEqualMode(0775, b.control_files._dir_mode) self.assertEqualMode(0664, b.control_files._file_mode) self.assertEqualMode(0775, b.bzrdir._get_dir_mode()) self.assertEqualMode(0664, b.bzrdir._get_file_mode()) with open('a', 'wb') as f: f.write('foo3\n') t.commit('foo3') check_mode_r(self, '.bzr', 0664, 0775) with open('c', 'wb') as f: f.write('new c\n') t.add('c') t.commit('new c') check_mode_r(self, '.bzr', 0664, 0775)
def test_conflicts(self): self.create_conflicts() self.run_bzr('merge ../other', retcode=1) wt = WorkingTree.open('.') self.assertEqual(2, len(wt.conflicts())) self.run_bzr('remerge', retcode=1) wt = WorkingTree.open('.') self.assertEqual(2, len(wt.conflicts())) self.run_bzr('remerge hello', retcode=1) self.assertEqual(2, len(wt.conflicts()))
def update_branches(sourcecode_directory, update_branches, possible_transports=None, tip=False, quiet=False): """Update the existing branches in sourcecode.""" if possible_transports is None: possible_transports = [] # XXX: JonathanLange 2009-11-09: Rather than updating one branch after # another, we could instead try to get them in parallel. for project, (branch_url, revision, optional) in ( update_branches.iteritems()): # Update project from branch_url. destination = os.path.join(sourcecode_directory, project) if not quiet: print 'Updating %s to %s' % ( project, _format_revision_name(revision, tip)) local_tree = WorkingTree.open(destination) try: remote_branch = Branch.open( branch_url, possible_transports=possible_transports) except BzrError: if optional: report_exception(sys.exc_info(), sys.stderr) continue else: raise possible_transports.append( remote_branch.bzrdir.root_transport) revision_id = get_revision_id(revision, remote_branch, tip) try: result = local_tree.pull( remote_branch, stop_revision=revision_id, overwrite=True, possible_transports=possible_transports) except IncompatibleRepositories: # XXX JRV 20100407: Ideally remote_branch.bzrdir._format # should be passed into upgrade() to ensure the format is the same # locally and remotely. Unfortunately smart server branches # have their _format set to RemoteFormat rather than an actual # format instance. upgrade(destination) # Upgraded, repoen working tree local_tree = WorkingTree.open(destination) result = local_tree.pull( remote_branch, stop_revision=revision_id, overwrite=True, possible_transports=possible_transports) if result.old_revid == result.new_revid: if not quiet: print ' (No change)' else: if result.old_revno < result.new_revno: change = 'Updated' else: change = 'Reverted' if not quiet: print ' (%s from %s to %s)' % ( change, result.old_revno, result.new_revno)
def test_merge_delete_before_rename_to_temp(self): """delete before rename to temp This case requires that you must not do move-out-of-the-way before deletes: $ touch foo $ touch bar $ bzr add foo bar $ bzr commit $ rm foo $ bzr rm foo $ bzr mv bar foo $ bzr commit """ a_wt = self.make_branch_and_tree("a") with file("a/foo", "wb") as f: f.write("A/FOO") with file("a/bar", "wb") as f: f.write("A/BAR") a_wt.add("foo") a_wt.add("bar") a_wt.commit("added foo and bar") self.run_bzr("branch a b") b_wt = WorkingTree.open("b") os.unlink("b/foo") b_wt.remove("foo") b_wt.rename_one("bar", "foo") b_wt.commit("deleted foo, renamed bar to foo") a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def __init__(self, path=None): WorkingCopy.__init__(self) self.path = path or os.getcwd() self.workingtree = WorkingTree.open(self.path) self.repository = BazaarRepository(self.workingtree.branch.user_url) #self.repository.working_copy = self self._current_version = self.repository._repository.revno()
def test_branch_stacked_branch_stacked(self): """Asking to stack on a stacked branch does work""" # We have a mainline trunk_tree = self.make_branch_and_tree('target', format='1.9') trunk_revid = trunk_tree.commit('mainline') # and a branch from it which is stacked branch_tree = self.make_branch_and_tree('branch', format='1.9') branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base) # with some work on it work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree() branch_revid = work_tree.commit('moar work plz') work_tree.branch.push(branch_tree.branch) # you can chain branches on from there out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2']) self.assertEqual('', out) self.assertEqual('Created new stacked branch referring to %s.\n' % branch_tree.branch.base, err) self.assertEqual(branch_tree.branch.base, branch.Branch.open('branch2').get_stacked_on_url()) branch2_tree = WorkingTree.open('branch2') branch2_revid = work_tree.commit('work on second stacked branch') work_tree.branch.push(branch2_tree.branch) self.assertRevisionInRepository('branch2', branch2_revid) self.assertRevisionsInBranchRepository( [trunk_revid, branch_revid, branch2_revid], 'branch2')
def test_set_and_get_view_info(self): wt = self.make_branch_and_tree('wt') view_current = 'view-name' view_dict = { view_current: ['dir-1'], 'other-name': ['dir-2']} wt.views.set_view_info(view_current, view_dict) current, views = wt.views.get_view_info() self.assertEquals(view_current, current) self.assertEquals(view_dict, views) # then reopen the tree and see they're still there wt = WorkingTree.open('wt') current, views = wt.views.get_view_info() self.assertEquals(view_current, current) self.assertEquals(view_dict, views) # test setting a current view which does not exist self.assertRaises(errors.NoSuchView, wt.views.set_view_info, 'yet-another', view_dict) current, views = wt.views.get_view_info() self.assertEquals(view_current, current) self.assertEquals(view_dict, views) # test clearing the current view wt.views.set_view_info(None, view_dict) current, views = wt.views.get_view_info() self.assertEquals(None, current) self.assertEquals(view_dict, views)
def test_new_files_group_sticky_bit(self): if sys.platform == 'win32': raise TestSkipped('chmod has no effect on win32') elif sys.platform == 'darwin' or 'freebsd' in sys.platform: # FreeBSD-based platforms create temp dirs with the 'wheel' group, # which users are not likely to be in, and this prevents us from # setting the sgid bit os.chown(self.test_dir, os.getuid(), os.getgid()) t = self.make_branch_and_tree('.') b = t.branch # Test the group sticky bit # Recursively update the modes of all files chmod_r('.bzr', 0664, 02775) check_mode_r(self, '.bzr', 0664, 02775) t = WorkingTree.open('.') b = t.branch self.assertEqualMode(02775, b.control_files._dir_mode) self.assertEqualMode(0664, b.control_files._file_mode) self.assertEqualMode(02775, b.bzrdir._get_dir_mode()) self.assertEqualMode(0664, b.bzrdir._get_file_mode()) with open('a', 'wb') as f: f.write('foo4\n') t.commit('foo4') check_mode_r(self, '.bzr', 0664, 02775) with open('d', 'wb') as f: f.write('new d\n') t.add('d') t.commit('new d') check_mode_r(self, '.bzr', 0664, 02775)
def test_create_branch(self): branch = self.make_branch('branch') tree = branch.create_checkout('tree', lightweight=True) tree.commit('one', rev_id='rev-1') self.run_bzr('switch --create-branch ../branch2', working_dir='tree') tree = WorkingTree.open('tree') self.assertEndsWith(tree.branch.base, '/branch2/')
def runUpdate(filename): branch = "versioning.git" REMOTE_URL="bzr+ssh://bazaar.launchpad.net/~freecad-maintainers/freecad/%s" % (branch) PUSHTO_URL="bzr+ssh://bazaar.launchpad.net/~freecad-maintainers/freecad/%s" % (branch) LOCAL_BRANCH=path.join(gettempdir(),branch) # Location of branch on Launchpad remote_branch = Branch.open(REMOTE_URL) # Location of branch on local system local_branch = remote_branch.bzrdir.sprout(LOCAL_BRANCH).open_branch() # Change a file in the local branch try: wf = open(LOCAL_BRANCH + "/src/Build/Version.h", 'w') rf = open(filename, 'r') except IOError as error: raise error else: wf.write(rf.read()) wf.close() # Commit the change tree = WorkingTree.open(LOCAL_BRANCH) tree.commit("Update version number")
def test_branch_push_pull_merge_copies_tags(self): t = self.make_branch_and_tree('branch1') t.commit(allow_pointless=True, message='initial commit', rev_id='first-revid') b1 = t.branch b1.tags.set_tag('tag1', 'first-revid') # branching copies the tag across self.run_bzr('branch branch1 branch2') b2 = Branch.open('branch2') self.assertEqual(b2.tags.lookup_tag('tag1'), 'first-revid') # make a new tag and pull it b1.tags.set_tag('tag2', 'twa') self.run_bzr('pull -d branch2 branch1') self.assertEqual(b2.tags.lookup_tag('tag2'), 'twa') # make a new tag and push it b1.tags.set_tag('tag3', 'san') self.run_bzr('push -d branch1 branch2') self.assertEqual(b2.tags.lookup_tag('tag3'), 'san') # make a new tag and merge it t.commit(allow_pointless=True, message='second commit', rev_id='second-revid') t2 = WorkingTree.open('branch2') t2.commit(allow_pointless=True, message='commit in second') b1.tags.set_tag('tag4', 'second-revid') self.run_bzr('merge -d branch2 branch1') self.assertEqual(b2.tags.lookup_tag('tag4'), 'second-revid') # pushing to a new location copies the tag across self.run_bzr('push -d branch1 branch3') b3 = Branch.open('branch3') self.assertEqual(b3.tags.lookup_tag('tag1'), 'first-revid')
def create_tree(self): '''Create the dir and working tree.''' try: self.logger.debug( 'Using tree in %(tree_dir)s' % { 'tree_dir': self.config.tree_dir}) if os.path.exists(self.config.tree_dir): self.tree = WorkingTree.open(self.config.tree_dir) if self.tree.branch.user_url != self.bzr_branch.user_url: self.logger.debug('Tree URLs do not match: %s - %s' % ( self.bzr_branch.user_url, self.tree.branch.user_url)) raise InvalidWorkingTree( 'The `tree_dir` option for the target branch is not a ' 'lightweight checkout. Please ask a project ' 'administrator to resolve the issue, and try again.') else: self.logger.debug('Tree does not exist. Creating dir') # Create the path up to but not including tree_dir if it does # not exist. parent_dir = os.path.dirname(self.config.tree_dir) if not os.path.exists(parent_dir): os.makedirs(parent_dir) self.tree = self.bzr_branch.create_checkout( self.config.tree_dir, lightweight=True) except AttributeError: # Store this so we can rmtree later self.temp_tree_dir = tempfile.mkdtemp() self.logger.debug( 'Using temp dir at %(tree_dir)s' % { 'tree_dir': self.temp_tree_dir}) self.tree = self.bzr_branch.create_checkout(self.temp_tree_dir) self.cleanup()
def test_content_filtering_applied_on_merge(self): # Create a source branch with two revisions source, fileid_1, fileid_2, fileid_3, fileid_4 = \ self.create_cf_tree_with_two_revisions(txt_reader=None, txt_writer=None, dir='source') if not source.supports_content_filtering(): return self.assert_basis_content("Foo ROCKS!", source, fileid_1) self.assertFileEqual("Foo ROCKS!", 'source/file1.txt') self.assert_basis_content("Foo Bin", source, fileid_2) self.assert_basis_content("Hello World", source, fileid_4) self.assertFileEqual("Hello World", 'source/file4.txt') # Now patch in content filtering and branch from revision 1 self.patch_in_content_filter() self.run_bzr('branch -r1 source target') target = WorkingTree.open('target') self.assert_basis_content("Foo Txt", target, fileid_1) self.assertFileEqual("fOO tXT", 'target/file1.txt') self.assertFileEqual("Foo Bin", 'target/file2.bin') self.assertFileEqual("bAR tXT", 'target/file3.txt') # Merge the latter change and check the target tree is updated self.run_bzr('merge -d target source') self.assertFileEqual("fOO rocks!", 'target/file1.txt') self.assertFileEqual("hELLO wORLD", 'target/file4.txt') # Commit the merge and check the right content is stored target.commit("merge file1.txt changes from source") self.assert_basis_content("Foo ROCKS!", target, fileid_1) self.assert_basis_content("Hello World", target, fileid_4)
def test_branch_stacked_branch_stacked(self): """Asking to stack on a stacked branch does work""" # We have a mainline trunk_tree = self.make_branch_and_tree('target', format='development') trunk_revid = trunk_tree.commit('mainline') # and a branch from it which is stacked branch_tree = self.make_branch_and_tree('branch', format='development') branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base) # with some work on it branch_revid = branch_tree.commit('moar work plz') # you can chain branches on from there out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2']) self.assertEqual('', out) self.assertEqual('Created new stacked branch referring to %s.\n' % branch_tree.branch.base, err) self.assertEqual(branch_tree.branch.base, branch.Branch.open('branch2').get_stacked_on_url()) branch2_tree = WorkingTree.open('branch2') branch2_revid = branch2_tree.commit('work on second stacked branch') self.assertRevisionInRepository('branch2', branch2_revid) self.assertRevisionsInBranchRepository( [trunk_revid, branch_revid, branch2_revid], 'branch2')
def test_merge_delete_before_rename_to_temp(self): """delete before rename to temp This case requires that you must not do move-out-of-the-way before deletes: $ touch foo $ touch bar $ bzr add foo bar $ bzr commit $ rm foo $ bzr rm foo $ bzr mv bar foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') file('a/foo', 'wb').write('A/FOO') file('a/bar', 'wb').write('A/BAR') a_wt.add('foo') a_wt.add('bar') a_wt.commit('added foo and bar') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') os.unlink('b/foo') b_wt.remove('foo') b_wt.rename_one('bar', 'foo') b_wt.commit('deleted foo, renamed bar to foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def _update(self, top_level_elem, elems, location, before_commit_msg, commit_msg, after_commit_msg): """ A common utility method for updating a YAML file in the server's Bazaar repo. """ # TODO: Commit message could be a tad smarter and might include some # hints as to what gets committed to repo. # TODO: Move it elsewhere, to a separate 'init' method (or investigage # why creating the 'tree' object in 'ensure_repo_consistency is not # enough - possibly because we're using subprocesses and our own process # is not the same that ensure_repo_consistency has been called in). if not hasattr(self, "tree"): self.tree = WorkingTree.open(self.repo_location) data = {} data[top_level_elem] = elems data_pprinted = pprint(data) output = dump(data, Dumper=Dumper, default_flow_style=False) logger.debug(before_commit_msg) logger.debug("data_pprinted=[%s], output=[%s], location=[%s]" % (data_pprinted, output, location)) open(location, "w").write(output) self.tree.commit(commit_msg) logger.debug(after_commit_msg)
def commit(self, mutable_tree): # BUG: not run recursively if in above branch not have changes if disable_hooks or not self.read_config(): return from bzrlib.workingtree import WorkingTree snapshot = [] for arg in self.config: # url directory [revisionspec] wt = WorkingTree.open(pathjoin(self.root, arg[1])) if wt.has_changes(wt.basis_tree()): cmd = ['ci'] os.chdir(wt.basedir) try: run_bzr_catch_user_errors(cmd) finally: os.chdir(self.cwd) if len(arg) < 3: arg.append('') arg[2] = 'revid:' + wt.last_revision() arg[1] = self._quoted_if_need(arg[1]) snapshot.append(' '.join(arg).encode('utf-8')) path = pathjoin(self.root, SNAPSHOT_PATH) f = open(path, 'w') try: f.write('\n'.join(snapshot)) finally: f.close()
def test_trivial_star_merge(self): """Test that merges in a star shape Just Work.""" # John starts a branch self.build_tree(("original/", "original/file1", "original/file2")) tree = self.make_branch_and_tree("original") branch = tree.branch tree.smart_add(["original"]) tree.commit("start branch.", verbose=False) # Mary branches it. self.build_tree(("mary/",)) branch.bzrdir.clone("mary") # Now John commits a change file = open("original/file1", "wt") file.write("John\n") file.close() tree.commit("change file1") # Mary does too mary_tree = WorkingTree.open("mary") mary_branch = mary_tree.branch file = open("mary/file2", "wt") file.write("Mary\n") file.close() mary_tree.commit("change file2") # john should be able to merge with no conflicts. base = [None, None] other = ("mary", -1) tree.merge_from_branch(mary_tree.branch) self.assertEqual("John\n", open("original/file1", "rt").read()) self.assertEqual("Mary\n", open("original/file2", "rt").read())
def test_merge_rename_to_temp_before_delete(self): """rename to temp before delete, source children before parents This case requires that you must not do deletes before move-out-of-the-way, and that you must not do children after parents: $ mkdir foo $ touch foo/bar $ bzr add foo/bar $ bzr commit $ bzr mv foo/bar bar $ rmdir foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') os.mkdir('a/foo') file('a/foo/bar', 'wb').write('A/FOO/BAR') a_wt.add('foo') a_wt.add('foo/bar') a_wt.commit('added foo/bar') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') b_wt.rename_one('foo/bar', 'bar') os.rmdir('b/foo') b_wt.remove('foo') b_wt.commit('moved foo/bar to bar, deleted foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_create_before_rename(self): """create before rename, target parents before children This case requires that you must not do move-into-place before creates, and that you must not do children after parents: $ touch foo $ bzr add foo $ bzr commit $ bzr mkdir bar $ bzr add bar $ bzr mv foo bar/foo $ bzr commit """ os.mkdir('a') a_wt = self.make_branch_and_tree('a') with file('a/foo', 'wb') as f: f.write('A/FOO') a_wt.add('foo') a_wt.commit('added foo') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') os.mkdir('b/bar') b_wt.add('bar') b_wt.rename_one('foo', 'bar/foo') b_wt.commit('created bar dir, moved foo into bar') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_rename_to_temp_before_delete(self): """rename to temp before delete, source children before parents This case requires that you must not do deletes before move-out-of-the-way, and that you must not do children after parents: $ mkdir foo $ touch foo/bar $ bzr add foo/bar $ bzr commit $ bzr mv foo/bar bar $ rmdir foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') os.mkdir('a/foo') with file('a/foo/bar', 'wb') as f: f.write('A/FOO/BAR') a_wt.add('foo') a_wt.add('foo/bar') a_wt.commit('added foo/bar') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') b_wt.rename_one('foo/bar', 'bar') os.rmdir('b/foo') b_wt.remove('foo') b_wt.commit('moved foo/bar to bar, deleted foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_rename_before_create(self): """rename before create This case requires that you must not do creates before move-into-place: $ touch foo $ bzr add foo $ bzr commit $ bzr mv foo bar $ touch foo $ bzr add foo $ bzr commit """ a_wt = self.make_branch_and_tree("a") with file("a/foo", "wb") as f: f.write("A/FOO") a_wt.add("foo") a_wt.commit("added foo") self.run_bzr("branch a b") b_wt = WorkingTree.open("b") b_wt.rename_one("foo", "bar") with file("b/foo", "wb") as f: f.write("B/FOO") b_wt.add("foo") b_wt.commit("moved foo to bar, added new foo") a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_delete_before_rename_to_temp(self): """delete before rename to temp This case requires that you must not do move-out-of-the-way before deletes: $ touch foo $ touch bar $ bzr add foo bar $ bzr commit $ rm foo $ bzr rm foo $ bzr mv bar foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') with file('a/foo', 'wb') as f: f.write('A/FOO') with file('a/bar', 'wb') as f: f.write('A/BAR') a_wt.add('foo') a_wt.add('bar') a_wt.commit('added foo and bar') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') os.unlink('b/foo') b_wt.remove('foo') b_wt.rename_one('bar', 'foo') b_wt.commit('deleted foo, renamed bar to foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_pull(self): self.pullable_branch() os.chdir('a') (out, err) = self.run_bzr('merge --pull ../b') self.assertContainsRe(out, 'Now on revision 2\\.') tree_a = WorkingTree.open('.') self.assertEqual([self.id2], tree_a.get_parent_ids())
def test_merge_rename_before_create(self): """rename before create This case requires that you must not do creates before move-into-place: $ touch foo $ bzr add foo $ bzr commit $ bzr mv foo bar $ touch foo $ bzr add foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') file('a/foo', 'wb').write('A/FOO') a_wt.add('foo') a_wt.commit('added foo') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') b_wt.rename_one('foo', 'bar') file('b/foo', 'wb').write('B/FOO') b_wt.add('foo') b_wt.commit('moved foo to bar, added new foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_branch_push_pull_merge_copies_tags(self): t = self.make_branch_and_tree('branch1') t.commit(allow_pointless=True, message='initial commit', rev_id='first-revid') b1 = t.branch b1.tags.set_tag('tag1', 'first-revid') # branching copies the tag across self.run_bzr('branch branch1 branch2') b2 = Branch.open('branch2') self.assertEquals(b2.tags.lookup_tag('tag1'), 'first-revid') # make a new tag and pull it b1.tags.set_tag('tag2', 'twa') self.run_bzr('pull -d branch2 branch1') self.assertEquals(b2.tags.lookup_tag('tag2'), 'twa') # make a new tag and push it b1.tags.set_tag('tag3', 'san') self.run_bzr('push -d branch1 branch2') self.assertEquals(b2.tags.lookup_tag('tag3'), 'san') # make a new tag and merge it t.commit(allow_pointless=True, message='second commit', rev_id='second-revid') t2 = WorkingTree.open('branch2') t2.commit(allow_pointless=True, message='commit in second') b1.tags.set_tag('tag4', 'second-revid') self.run_bzr('merge -d branch2 branch1') self.assertEquals(b2.tags.lookup_tag('tag4'), 'second-revid') # pushing to a new location copies the tag across self.run_bzr('push -d branch1 branch3') b3 = Branch.open('branch3') self.assertEquals(b3.tags.lookup_tag('tag1'), 'first-revid')
def test_merge_create_before_rename(self): """create before rename, target parents before children This case requires that you must not do move-into-place before creates, and that you must not do children after parents: $ touch foo $ bzr add foo $ bzr commit $ bzr mkdir bar $ bzr add bar $ bzr mv foo bar/foo $ bzr commit """ os.mkdir("a") a_wt = self.make_branch_and_tree("a") with file("a/foo", "wb") as f: f.write("A/FOO") a_wt.add("foo") a_wt.commit("added foo") self.run_bzr("branch a b") b_wt = WorkingTree.open("b") os.mkdir("b/bar") b_wt.add("bar") b_wt.rename_one("foo", "bar/foo") b_wt.commit("created bar dir, moved foo into bar") a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_merge_rename_to_temp_before_delete(self): """rename to temp before delete, source children before parents This case requires that you must not do deletes before move-out-of-the-way, and that you must not do children after parents: $ mkdir foo $ touch foo/bar $ bzr add foo/bar $ bzr commit $ bzr mv foo/bar bar $ rmdir foo $ bzr commit """ a_wt = self.make_branch_and_tree("a") os.mkdir("a/foo") with file("a/foo/bar", "wb") as f: f.write("A/FOO/BAR") a_wt.add("foo") a_wt.add("foo/bar") a_wt.commit("added foo/bar") self.run_bzr("branch a b") b_wt = WorkingTree.open("b") b_wt.rename_one("foo/bar", "bar") os.rmdir("b/foo") b_wt.remove("foo") b_wt.commit("moved foo/bar to bar, deleted foo") a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_walkdirs_kernel_like_tree_unknown(self): """Walking a kernel sized tree is fast!(150ms).""" self.make_kernel_like_tree() tree = WorkingTree.open('.') # on roberts machine: this originally took: 157ms/4177ms # plain os.walk takes 213ms on this tree self.time(list, tree.walkdirs())
def test_create_branch(self): branch = self.make_branch('branch') tree = branch.create_checkout('tree', lightweight=True) tree.commit('one', rev_id='rev-1') self.run_bzr('switch --create-branch ../branch2', working_dir='tree') tree = WorkingTree.open('tree') self.assertEndsWith(tree.branch.base, '/branch2/')
def test_merge_rename_before_create(self): """rename before create This case requires that you must not do creates before move-into-place: $ touch foo $ bzr add foo $ bzr commit $ bzr mv foo bar $ touch foo $ bzr add foo $ bzr commit """ a_wt = self.make_branch_and_tree('a') with file('a/foo', 'wb') as f: f.write('A/FOO') a_wt.add('foo') a_wt.commit('added foo') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') b_wt.rename_one('foo', 'bar') with file('b/foo', 'wb') as f: f.write('B/FOO') b_wt.add('foo') b_wt.commit('moved foo to bar, added new foo') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_trivial_star_merge(self): """Test that merges in a star shape Just Work.""" # John starts a branch self.build_tree(("original/", "original/file1", "original/file2")) tree = self.make_branch_and_tree('original') branch = tree.branch tree.smart_add(["original"]) tree.commit("start branch.", verbose=False) # Mary branches it. self.build_tree(("mary/", )) branch.bzrdir.clone("mary") # Now John commits a change file = open("original/file1", "wt") file.write("John\n") file.close() tree.commit("change file1") # Mary does too mary_tree = WorkingTree.open('mary') mary_branch = mary_tree.branch file = open("mary/file2", "wt") file.write("Mary\n") file.close() mary_tree.commit("change file2") # john should be able to merge with no conflicts. base = [None, None] other = ("mary", -1) tree.merge_from_branch(mary_tree.branch) self.assertEqual("John\n", open("original/file1", "rt").read()) self.assertEqual("Mary\n", open("original/file2", "rt").read())
def test_merge_create_before_rename(self): """create before rename, target parents before children This case requires that you must not do move-into-place before creates, and that you must not do children after parents: $ touch foo $ bzr add foo $ bzr commit $ bzr mkdir bar $ bzr add bar $ bzr mv foo bar/foo $ bzr commit """ os.mkdir('a') a_wt = self.make_branch_and_tree('a') file('a/foo', 'wb').write('A/FOO') a_wt.add('foo') a_wt.commit('added foo') self.run_bzr('branch a b') b_wt = WorkingTree.open('b') os.mkdir('b/bar') b_wt.add('bar') b_wt.rename_one('foo', 'bar/foo') b_wt.commit('created bar dir, moved foo into bar') a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1))
def test_initialize(self): # initialize should create a working tree and branch in an existing dir t = self.make_branch_and_tree('.') b = branch.Branch.open('.') self.assertEqual(t.branch.base, b.base) t2 = WorkingTree.open('.') self.assertEqual(t.basedir, t2.basedir) self.assertEqual(b.base, t2.branch.base)
def test_initialize(self): # initialize should create a working tree and branch in an existing dir t = self.make_branch_and_tree('.') b = branch.Branch.open('.') self.assertEqual(t.branch.base, b.base) t2 = WorkingTree.open('.') self.assertEqual(t.basedir, t2.basedir) self.assertEqual(b.base, t2.branch.base)
def test_remove_with_new_in_dir1(self): tree = self._make_tree_and_add(files) self.run_bzr('remove --new --keep b b/c', error_regexes=["removed b", "removed b/c"]) tree = WorkingTree.open('.') self.assertInWorkingTree(a) self.assertEqual(tree.path2id(a), a + _id) self.assertFilesUnversioned([b, c])
def test_remove_with_new_in_dir1(self): tree = self._make_add_and_assert_tree(files) self.run_bzr('remove --new --keep b b/c', error_regexes=["removed b", "removed b/c"]) tree = WorkingTree.open('.') self.assertInWorkingTree(a) self.assertEqual(tree.path2id(a), a + _id) self.assertFilesUnversioned([b,c])
def test_smoke_update_checkout_bound_branch_local_commits(self): # smoke test for doing an update of a checkout of a bound # branch with local commits. master = self.make_branch_and_tree('master') # make a bound branch self.run_bzr('checkout master child') # get an object form of child child = WorkingTree.open('child') # check that out self.run_bzr('checkout --lightweight child checkout') # get an object form of the checkout to manipulate wt = WorkingTree.open('checkout') # change master a_file = file('master/file', 'wt') a_file.write('Foo') a_file.close() master.add(['file']) master_tip = master.commit('add file') # change child a_file = file('child/file_b', 'wt') a_file.write('Foo') a_file.close() child.add(['file_b']) child_tip = child.commit('add file_b', local=True) # check checkout a_file = file('checkout/file_c', 'wt') a_file.write('Foo') a_file.close() wt.add(['file_c']) # now, update checkout -> # get all three files and a pending merge. out, err = self.run_bzr('update checkout') self.assertEqual('', out) self.assertContainsRe(err, '\+N file') self.assertContainsRe(err, '\+N file_b') self.assertContainsRe( err, 'Updated to revision 1.\n' 'Your local commits will now show as' ' pending merges') self.assertEqual([master_tip, child_tip], wt.get_parent_ids()) self.failUnlessExists('checkout/file') self.failUnlessExists('checkout/file_b') self.failUnlessExists('checkout/file_c') self.assertTrue(wt.has_filename('file_c'))
def test_create_branch_short_name(self): branch = self.make_branch('branch') tree = branch.create_checkout('tree', lightweight=True) tree.commit('one', rev_id='rev-1') self.run_bzr('switch -b branch2', working_dir='tree') tree = WorkingTree.open('tree') # The new branch should have been created at the same level as # 'branch', because we did not have a '/' segment self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
def test_create_branch_short_name(self): branch = self.make_branch('branch') tree = branch.create_checkout('tree', lightweight=True) tree.commit('one', rev_id='rev-1') self.run_bzr('switch -b branch2', working_dir='tree') tree = WorkingTree.open('tree') # The new branch should have been created at the same level as # 'branch', because we did not have a '/' segment self.assertEqual(branch.base[:-1] + '2/', tree.branch.base)
def __init__(self, path=None): WorkingCopy.__init__(self, path) try: self.workingtree = WorkingTree.open(self.path) except NotBranchError: pass else: self.repository = BazaarRepository(self.workingtree.branch.user_url) # self.repository.working_copy = self self._current_version = self.repository._repository.revno()
def test_cant_write_to_readonly_branch(self): # We can't write to a read-only branch. self.createBazaarBranch('mark', '+junk', 'ro-branch') # Create a new revision on the local branch. tree = WorkingTree.open(self.local_branch.base) tree.commit('Empty commit', rev_id='rev2') # Push the local branch to the remote url remote_url = self.getTransportURL('~mark/+junk/ro-branch') self.assertCantPush(self.local_branch_path, remote_url)
def test_push_create_prefix(self): """'bzr push --create-prefix' will create leading directories.""" tree = self.create_simple_tree() self.run_bzr_error(['Parent directory of ../new/tree does not exist'], 'push ../new/tree', working_dir='tree') self.run_bzr('push ../new/tree --create-prefix', working_dir='tree') new_tree = WorkingTree.open('new/tree') self.assertEqual(tree.last_revision(), new_tree.last_revision()) self.failUnlessExists('new/tree/a')
def __init__(self, path=None): WorkingCopy.__init__(self, path) try: self.workingtree = WorkingTree.open(self.path) except NotBranchError: pass else: self.repository = BazaarRepository( self.workingtree.branch.user_url) #self.repository.working_copy = self self._current_version = self.repository._repository.revno()
def test_cant_write_to_readonly_branch(self): # We can't write to a read-only branch. self.createBazaarBranch('mark', '+junk', 'ro-branch') # Create a new revision on the local branch. tree = WorkingTree.open(self.local_branch.base) tree.commit('Empty commit', rev_id='rev2') # Push the local branch to the remote url remote_url = self.getTransportURL('~mark/+junk/ro-branch') self.assertCantPush(self.local_branch_path, remote_url)
def get_revision(self): """Read Bazaar revision information for the Bcfg2 repository.""" try: working_tree = WorkingTree.open(self.datastore) revision = str(working_tree.branch.revno()) if working_tree.has_changes(working_tree.basis_tree()) or working_tree.unknowns(): revision += "+" except errors.NotBranchError: logger.error("Failed to read Bazaar branch; disabling Bazaar support") raise Bcfg2.Server.Plugin.PluginInitError return revision
def test_create_branch_directory_services(self): branch = self.make_branch('branch') tree = branch.create_checkout('tree', lightweight=True) class FooLookup(object): def look_up(self, name, url): return 'foo-'+name directories.register('foo:', FooLookup, 'Create branches named foo-') self.addCleanup(directories.remove, 'foo:') self.run_bzr('switch -b foo:branch2', working_dir='tree') tree = WorkingTree.open('tree') self.assertEndsWith(tree.branch.base, 'foo-branch2/')
def test_merge(self): from bzrlib.branch import Branch a_tree = self.example_branch('a') ancestor = a_tree.branch.revno() b_tree = a_tree.bzrdir.sprout('b').open_workingtree() self.build_tree_contents([('b/goodbye', 'quux')]) b_tree.commit(message="more u's are always good") self.build_tree_contents([('a/hello', 'quuux')]) # We can't merge when there are in-tree changes os.chdir('a') self.run_bzr('merge ../b', retcode=3) a = WorkingTree.open('.') a_tip = a.commit("Like an epidemic of u's") self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof', retcode=3) self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3') a_tree.revert(backups=False) self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave') a_tree.revert(backups=False) self.run_bzr('merge ../b -r last:1..last:1 --merge-type lca') a_tree.revert(backups=False) self.run_bzr_error(['Show-base is not supported for this merge type'], 'merge ../b -r last:1..last:1 --merge-type weave' ' --show-base') a_tree.revert(backups=False) self.run_bzr('merge ../b -r last:1..last:1 --reprocess') a_tree.revert(backups=False) self.run_bzr('merge ../b -r last:1') self.check_file_contents('goodbye', 'quux') # Merging a branch pulls its revision into the tree b = Branch.open('../b') b_tip = b.last_revision() self.failUnless(a.branch.repository.has_revision(b_tip)) self.assertEqual([a_tip, b_tip], a.get_parent_ids()) a_tree.revert(backups=False) out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3) self.assertTrue("Not a branch" in err) self.run_bzr('merge -r revno:%d:./..revno:%d:../b' % (ancestor, b.revno())) self.assertEquals(a.get_parent_ids(), [a.branch.last_revision(), b.last_revision()]) self.check_file_contents('goodbye', 'quux') a_tree.revert(backups=False) self.run_bzr('merge -r revno:%d:../b' % b.revno()) self.assertEquals(a.get_parent_ids(), [a.branch.last_revision(), b.last_revision()]) a_tip = a.commit('merged') self.run_bzr('merge ../b -r last:1') self.assertEqual([a_tip], a.get_parent_ids())
def test_pending_with_null(self): """When base is forced to revno 0, parent_ids are set""" wt2 = self.test_unrelated() wt1 = WorkingTree.open('.') br1 = wt1.branch br1.fetch(wt2.branch) # merge all of branch 2 into branch 1 even though they # are not related. wt1.merge_from_branch(wt2.branch, wt2.last_revision(), 'null:') self.assertEqual([br1.last_revision(), wt2.branch.last_revision()], wt1.get_parent_ids()) return (wt1, wt2.branch)
def get_revision(self): """Read Bazaar revision information for the Bcfg2 repository.""" try: working_tree = WorkingTree.open(self.datastore) revision = str(working_tree.branch.revno()) if working_tree.has_changes( working_tree.basis_tree()) or working_tree.unknowns(): revision += "+" except errors.NotBranchError: logger.error( "Failed to read Bazaar branch; disabling Bazaar support") raise Bcfg2.Server.Plugin.PluginInitError return revision
def _prepare_rename_mod_tree(self): self.build_tree( ['a/', 'a/b', 'a/c', 'a/d/', 'a/d/e', 'f/', 'f/g', 'f/h', 'f/i']) self.run_bzr('init') self.run_bzr('add') self.run_bzr('commit -m 1') wt = WorkingTree.open('.') wt.rename_one('a/b', 'f/b') wt.rename_one('a/d/e', 'f/e') wt.rename_one('a/d', 'f/d') wt.rename_one('f/g', 'a/g') wt.rename_one('f/h', 'h') wt.rename_one('f', 'j')
def get_revision(self): """Read Bazaar revision information for the Bcfg2 repository.""" try: working_tree = WorkingTree.open(Bcfg2.Options.setup.vcs_root) revision = str(working_tree.branch.revno()) if (working_tree.has_changes(working_tree.basis_tree()) or working_tree.unknowns()): revision += "+" except errors.NotBranchError: msg = "Failed to read Bazaar branch" self.logger.error(msg) raise Bcfg2.Server.Plugin.PluginExecutionError(msg) return revision