def open_workingtree(self, recommend_upgrade=True): if (not self._git.bare and os.path.exists( os.path.join(self._git.controldir(), "index"))): return workingtree.GitWorkingTree(self, self.open_repository(), self.open_branch()) loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii') raise errors.bzr_errors.NoWorkingTree(loc)
def open_workingtree(self, recommend_upgrade=True): if (not self._git.bare and os.path.exists(os.path.join(self._git.controldir(), "index"))): return workingtree.GitWorkingTree(self, self.open_repository(), self.open_branch()) loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii') raise errors.bzr_errors.NoWorkingTree(loc)
def _convert_items(items, format, clean_up, dry_run, label=None): """Convert a sequence of control directories to the given format. :param items: the control directories to upgrade :param format: the format to convert to or None for the best default :param clean-up: if True, the backup.bzr directory is removed if the upgrade succeeded for a given repo/branch/tree :param dry_run: show what would happen but don't actually do any upgrades :param label: the label for these items or None to calculate one :return: items successfully upgraded, exceptions """ succeeded = [] exceptions = [] child_pb = ui.ui_factory.nested_progress_bar() child_pb.update(gettext('Upgrading bzrdirs'), 0, len(items)) for i, control_dir in enumerate(items): # Do the conversion location = control_dir.root_transport.base bzr_object, bzr_label = _get_object_and_label(control_dir) type_label = label or bzr_label child_pb.update(gettext("Upgrading %s") % (type_label), i+1, len(items)) ui.ui_factory.note(gettext('Upgrading {0} {1} ...').format(type_label, urlutils.unescape_for_display(location, 'utf-8'),)) try: if not dry_run: cv = Convert(control_dir=control_dir, format=format) except errors.UpToDateFormat, ex: ui.ui_factory.note(str(ex)) succeeded.append(control_dir) continue except Exception, ex: trace.warning('conversion error: %s' % ex) exceptions.append(ex) continue
def run(self, location=None, remember=False, directory=None, no_rebase=False, strict=None): from bzrlib import urlutils from bzrlib.controldir import ControlDir from bzrlib.errors import BzrCommandError, NoWorkingTree from bzrlib.workingtree import WorkingTree if directory is None: directory = "." try: source_wt = WorkingTree.open_containing(directory)[0] source_branch = source_wt.branch except NoWorkingTree: source_branch = Branch.open(directory) source_wt = None if source_wt is not None: source_wt.check_changed_or_out_of_date( strict, 'dpush_strict', more_error='Use --no-strict to force the push.', more_warning='Uncommitted changes will not be pushed.') stored_loc = source_branch.get_push_location() if location is None: if stored_loc is None: raise BzrCommandError(gettext("No push location known or specified.")) else: display_url = urlutils.unescape_for_display(stored_loc, self.outf.encoding) self.outf.write( gettext("Using saved location: %s\n") % display_url) location = stored_loc controldir = ControlDir.open(location) target_branch = controldir.open_branch() target_branch.lock_write() try: try: push_result = source_branch.push(target_branch, lossy=True) except errors.LossyPushToSameVCS: raise BzrCommandError(gettext("{0!r} and {1!r} are in the same VCS, lossy " "push not necessary. Please use regular push.").format( source_branch, target_branch)) # We successfully created the target, remember it if source_branch.get_push_location() is None or remember: # FIXME: Should be done only if we succeed ? -- vila 2012-01-18 source_branch.set_push_location(target_branch.base) if not no_rebase: old_last_revid = source_branch.last_revision() source_branch.pull(target_branch, overwrite=True) new_last_revid = source_branch.last_revision() if source_wt is not None and old_last_revid != new_last_revid: source_wt.lock_write() try: target = source_wt.branch.repository.revision_tree( new_last_revid) update_workingtree_fileids(source_wt, target) finally: source_wt.unlock() push_result.report(self.outf) finally: target_branch.unlock()
def run(self, location=None, remember=False, directory=None, no_rebase=False): from bzrlib import urlutils from bzrlib.bzrdir import BzrDir from bzrlib.errors import BzrCommandError, NoWorkingTree from bzrlib.trace import info from bzrlib.workingtree import WorkingTree from upgrade import update_workingtree_fileids if directory is None: directory = "." try: source_wt = WorkingTree.open_containing(directory)[0] source_branch = source_wt.branch except NoWorkingTree: source_branch = Branch.open_containing(directory)[0] source_wt = None stored_loc = source_branch.get_push_location() if location is None: if stored_loc is None: raise BzrCommandError("No push location known or specified.") else: display_url = urlutils.unescape_for_display(stored_loc, self.outf.encoding) self.outf.write("Using saved location: %s\n" % display_url) location = stored_loc bzrdir = BzrDir.open(location) target_branch = bzrdir.open_branch() target_branch.lock_write() try: if not isinstance(target_branch, ForeignBranch): info("target branch is not a foreign branch, using regular push.") target_branch.pull(source_branch) no_rebase = True else: revid_map = target_branch.dpull(source_branch) # We successfully created the target, remember it if source_branch.get_push_location() is None or remember: source_branch.set_push_location(target_branch.base) if not no_rebase: _, old_last_revid = source_branch.last_revision_info() new_last_revid = revid_map[old_last_revid] if source_wt is not None: source_wt.pull(target_branch, overwrite=True, stop_revision=new_last_revid) source_wt.lock_write() try: update_workingtree_fileids(source_wt, source_wt.branch.repository.revision_tree(old_last_revid), source_wt.branch.repository.revision_tree(new_last_revid)) finally: source_wt.unlock() else: source_branch.pull(target_branch, overwrite=True, stop_revision=new_last_revid) finally: target_branch.unlock()
def started(self, revno, rev_id, location=None): if location is not None: location = ' to: ' + unescape_for_display(location, 'utf-8') else: # When started was added, location was only made optional by # accident. Matt Nordhoff 20071129 symbol_versioning.warn("As of bzr 1.0 you must pass a location " "to started.", DeprecationWarning, stacklevel=2) location = '' self._note('Committing%s', location)
def convert(self): try: branch = self.bzrdir.open_branch() if branch.user_url != self.bzrdir.user_url: ui.ui_factory.note(gettext( 'This is a checkout. The branch (%s) needs to be upgraded' ' separately.') % (urlutils.unescape_for_display( branch.user_url, 'utf-8'))) del branch except (errors.NotBranchError, errors.IncompatibleRepositories): # might not be a format we can open without upgrading; see e.g. # https://bugs.launchpad.net/bzr/+bug/253891 pass if self.format is None: try: rich_root = self.bzrdir.find_repository()._format.rich_root_data except errors.NoRepositoryPresent: rich_root = False # assume no rich roots if rich_root: format_name = "default-rich-root" else: format_name = "default" format = format_registry.make_bzrdir(format_name) else: format = self.format if not self.bzrdir.needs_format_conversion(format): raise errors.UpToDateFormat(self.bzrdir._format) if not self.bzrdir.can_convert_format(): raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") % self.bzrdir._format) self.bzrdir.check_conversion_target(format) ui.ui_factory.note(gettext('starting upgrade of %s') % urlutils.unescape_for_display(self.transport.base, 'utf-8')) self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir() while self.bzrdir.needs_format_conversion(format): converter = self.bzrdir._format.get_converter(format) self.bzrdir = converter.convert(self.bzrdir, None) ui.ui_factory.note(gettext('finished'))
def test_upgrade_url(self): self.run_bzr('init --format=pack-0.92') t = self.get_transport() url = t.base display_url = urlutils.unescape_for_display(url, 'utf-8') out, err = self.run_bzr(['upgrade', '--format=2a', url]) backup_dir = 'backup.bzr.~1~' self.assertEqualDiff("""Upgrading branch %s ... starting upgrade of %s making backup of %s.bzr to %s%s starting repository conversion repository converted finished """ % (display_url, display_url, display_url, display_url, backup_dir), out) self.assertEqual('', err)
def auto_upload_hook(params, quiet=False): source_branch = params.branch destination = get_upload_location(source_branch) if destination is None: return auto_upload = get_upload_auto(source_branch) if not auto_upload: return if not quiet: display_url = urlutils.unescape_for_display(destination, sys.stdout.encoding) print "Automatically uploading to %s" % display_url to_transport = transport.get_transport(destination) last_revision = source_branch.last_revision() last_tree = source_branch.repository.revision_tree(last_revision) uploader = BzrUploader(source_branch, to_transport, sys.stdout, last_tree, last_revision, quiet=quiet) uploader.upload_tree()
def test_push_suggests_parent_alias(self): """Push suggests using :parent if there is a known parent branch.""" tree_a = self.make_branch_and_tree('a') tree_a.commit('this is a commit') tree_b = self.make_branch_and_tree('b') # If there is no parent location set, :parent isn't mentioned. out = self.run_bzr('push', working_dir='a', retcode=3) self.assertEquals(out, ('','bzr: ERROR: No push location known or specified.\n')) # If there is a parent location set, the error suggests :parent. tree_a.branch.set_parent(tree_b.branch.base) out = self.run_bzr('push', working_dir='a', retcode=3) self.assertEquals(out, ('','bzr: ERROR: No push location known or specified. ' 'To push to the parent branch ' '(at %s), use \'bzr push :parent\'.\n' % urlutils.unescape_for_display(tree_b.branch.base, 'utf-8')))
def test_push_suggests_parent_alias(self): """Push suggests using :parent if there is a known parent branch.""" tree_a = self.make_branch_and_tree('a') tree_a.commit('this is a commit') tree_b = self.make_branch_and_tree('b') # If there is no parent location set, :parent isn't mentioned. out = self.run_bzr('push', working_dir='a', retcode=3) self.assertEqual( out, ('', 'bzr: ERROR: No push location known or specified.\n')) # If there is a parent location set, the error suggests :parent. tree_a.branch.set_parent(tree_b.branch.base) out = self.run_bzr('push', working_dir='a', retcode=3) self.assertEqual( out, ('', 'bzr: ERROR: No push location known or specified. ' 'To push to the parent branch ' '(at %s), use \'bzr push :parent\'.\n' % urlutils.unescape_for_display(tree_b.branch.base, 'utf-8')))
def get_bookmarks(self): bookmarks = {} if self.branch is None: return bookmarks config = self.branch.get_config() for source_class in config.option_sources: source = source_class() try: filename = source._get_filename() except AttributeError: filename = source._config._transport.base + source._config._filename filename = unescape_for_display(filename, 'utf-8') for section_name, extra_path in source._get_matching_sections(): parser = source._get_parser() if section_name in parser: section = parser[section_name] elif section_name == "DEFAULT": section = parser else: section = {} for name, value in section.items(): if name.startswith('bookmark_'): bookmarks[name[9:]] = filename, value return bookmarks
def run(self, new_base=None, verbose=False, idmap_file=None, directory="."): from bzrlib import urlutils from bzrlib.branch import Branch from bzrlib.workingtree import WorkingTree from bzrlib.plugins.rewrite.pseudonyms import ( find_pseudonyms, generate_rebase_map_from_pseudonyms, pseudonyms_as_dict, ) from bzrlib.plugins.rewrite.upgrade import create_deterministic_revid, upgrade_branch from bzrlib.foreign import update_workingtree_fileids try: wt_to = WorkingTree.open(directory) branch_to = wt_to.branch except NoWorkingTree: wt_to = None branch_to = Branch.open(directory) stored_loc = branch_to.get_parent() if new_base is None: if stored_loc is None: raise BzrCommandError(gettext("No pull location known or" " specified.")) else: display_url = urlutils.unescape_for_display(stored_loc, self.outf.encoding) self.outf.write(gettext("Using saved location: %s\n") % display_url) new_base = Branch.open(stored_loc) else: new_base = Branch.open(new_base) branch_to.repository.fetch(new_base.repository, revision_id=branch_to.last_revision()) pseudonyms = pseudonyms_as_dict(find_pseudonyms(branch_to.repository, branch_to.repository.all_revision_ids())) def generate_rebase_map(revision_id): return generate_rebase_map_from_pseudonyms( pseudonyms, branch_to.repository.get_ancestry(revision_id), branch_to.repository.get_ancestry(new_base.last_revision()), ) def determine_new_revid(old_revid, new_parents): return create_deterministic_revid(old_revid, new_parents) branch_to.lock_write() try: graph = branch_to.repository.get_graph() renames = upgrade_branch( branch_to, generate_rebase_map, determine_new_revid, allow_changes=True, verbose=verbose ) if wt_to is not None: basis_tree = wt_to.basis_tree() basis_tree.lock_read() try: update_workingtree_fileids(wt_to, basis_tree) finally: basis_tree.unlock() finally: branch_to.unlock() if renames == {}: note(gettext("Nothing to do.")) if idmap_file is not None: f = open(idmap_file, "w") try: for oldid, newid in renames.iteritems(): f.write("%s\t%s\n" % (oldid, newid)) finally: f.close() if wt_to is not None: wt_to.set_last_revision(branch_to.last_revision())
def test_no_public_branch(self): b = self.make_branch('.') error = errors.NoPublicBranch(b) url = urlutils.unescape_for_display(b.base, 'ascii') self.assertEqualDiff('There is no public branch set for "%s".' % url, str(error))
def test(expected, url, encoding='utf-8'): disp_url = urlutils.unescape_for_display(url, encoding=encoding) self.assertIsInstance(disp_url, unicode) self.assertEqual(expected, disp_url)
def friendly_location(url): path = urlutils.unescape_for_display(url, 'ascii') try: return osutils.relpath(osutils.getcwd(), path) except errors.PathNotChild: return path
def show_missing_xml(self, other_branch=None, reverse=False, mine_only=False, theirs_only=False, log_format=None, long=False, short=False, line=False, show_ids=False, verbose=False, this=False, other=False): """output missing info as xml""" if this: mine_only = this if other: theirs_only = other local_branch = Branch.open_containing(u".")[0] parent = local_branch.get_parent() if other_branch is None: other_branch = parent if other_branch is None: raise errors.BzrCommandError("No peer location known" " or specified.") display_url = urlutils.unescape_for_display(other_branch, self.outf.encoding) remote_branch = Branch.open(other_branch) if remote_branch.base == local_branch.base: remote_branch = local_branch local_branch.lock_read() try: remote_branch.lock_read() self.outf.write('<?xml version="1.0" encoding="%s"?>' % \ osutils.get_user_encoding()) self.outf.write('<missing>') try: self.outf.write('<last_location>' + display_url + \ '</last_location>') local_extra, remote_extra = find_unmerged(local_branch, remote_branch) if log_format is None: registry = log_formatter_registry log_format = registry.get_default(local_branch) if reverse is False: local_extra.reverse() remote_extra.reverse() if local_extra and not theirs_only: self.outf.write('<extra_revisions size="%d">' % len(local_extra)) if local_extra > 0: lf = log_format(to_file=self.outf, show_ids=show_ids, show_timezone='original') showlogs(self, iter_log_revisions(local_extra, local_branch.repository, verbose), lf) self.outf.write('</extra_revisions>') if remote_extra and not mine_only: self.outf.write('<missing_revisions size="%d">' % len(remote_extra)) if remote_extra > 0: lf = log_format(to_file=self.outf, show_ids=show_ids, show_timezone='original') showlogs(self, iter_log_revisions(remote_extra, remote_branch.repository, verbose), lf) self.outf.write('</missing_revisions>') if not remote_extra and not local_extra: status_code = 0 # self.outf.write("Branches are up to date.\n") else: status_code = 1 finally: remote_branch.unlock() finally: self.outf.write('</missing>') local_branch.unlock() if not status_code and parent is None and other_branch is not None: local_branch.lock_write() try: # handle race conditions - a parent might be set while we run. if local_branch.get_parent() is None: local_branch.set_parent(remote_branch.base) finally: local_branch.unlock() return status_code
def show_missing_xml(self, other_branch=None, reverse=False, mine_only=False, theirs_only=False, log_format=None, long=False, short=False, line=False, show_ids=False, verbose=False, this=False, other=False): """output missing info as xml""" if this: mine_only = this if other: theirs_only = other local_branch = Branch.open_containing(u".")[0] parent = local_branch.get_parent() if other_branch is None: other_branch = parent if other_branch is None: raise errors.BzrCommandError("No peer location known" " or specified.") display_url = urlutils.unescape_for_display(other_branch, self.outf.encoding) remote_branch = Branch.open(other_branch) if remote_branch.base == local_branch.base: remote_branch = local_branch local_branch.lock_read() try: remote_branch.lock_read() self.outf.write('<?xml version="1.0" encoding="%s"?>' % \ osutils.get_user_encoding()) self.outf.write('<missing>') try: self.outf.write('<last_location>' + display_url + \ '</last_location>') local_extra, remote_extra = find_unmerged(local_branch, remote_branch) if log_format is None: registry = log_formatter_registry log_format = registry.get_default(local_branch) if reverse is False: local_extra.reverse() remote_extra.reverse() if local_extra and not theirs_only: self.outf.write('<extra_revisions size="%d">' % len(local_extra)) if local_extra > 0: lf = log_format(to_file=self.outf, show_ids=show_ids, show_timezone='original') showlogs( self, iter_log_revisions(local_extra, local_branch.repository, verbose), lf) self.outf.write('</extra_revisions>') if remote_extra and not mine_only: self.outf.write('<missing_revisions size="%d">' % len(remote_extra)) if remote_extra > 0: lf = log_format(to_file=self.outf, show_ids=show_ids, show_timezone='original') showlogs( self, iter_log_revisions(remote_extra, remote_branch.repository, verbose), lf) self.outf.write('</missing_revisions>') if not remote_extra and not local_extra: status_code = 0 # self.outf.write("Branches are up to date.\n") else: status_code = 1 finally: remote_branch.unlock() finally: self.outf.write('</missing>') local_branch.unlock() if not status_code and parent is None and other_branch is not None: local_branch.lock_write() try: # handle race conditions - a parent might be set while we run. if local_branch.get_parent() is None: local_branch.set_parent(remote_branch.base) finally: local_branch.unlock() return status_code
def run(self, location=None, remember=False, directory=None, no_rebase=False, strict=None): from bzrlib import urlutils from bzrlib.controldir import ControlDir from bzrlib.errors import BzrCommandError, NoWorkingTree from bzrlib.workingtree import WorkingTree if directory is None: directory = "." try: source_wt = WorkingTree.open_containing(directory)[0] source_branch = source_wt.branch except NoWorkingTree: source_branch = Branch.open(directory) source_wt = None if source_wt is not None: source_wt.check_changed_or_out_of_date( strict, 'dpush_strict', more_error='Use --no-strict to force the push.', more_warning='Uncommitted changes will not be pushed.') stored_loc = source_branch.get_push_location() if location is None: if stored_loc is None: raise BzrCommandError( gettext("No push location known or specified.")) else: display_url = urlutils.unescape_for_display( stored_loc, self.outf.encoding) self.outf.write( gettext("Using saved location: %s\n") % display_url) location = stored_loc controldir = ControlDir.open(location) target_branch = controldir.open_branch() target_branch.lock_write() try: try: push_result = source_branch.push(target_branch, lossy=True) except errors.LossyPushToSameVCS: raise BzrCommandError( gettext( "{0!r} and {1!r} are in the same VCS, lossy " "push not necessary. Please use regular push.").format( source_branch, target_branch)) # We successfully created the target, remember it if source_branch.get_push_location() is None or remember: # FIXME: Should be done only if we succeed ? -- vila 2012-01-18 source_branch.set_push_location(target_branch.base) if not no_rebase: old_last_revid = source_branch.last_revision() source_branch.pull(target_branch, overwrite=True) new_last_revid = source_branch.last_revision() if source_wt is not None and old_last_revid != new_last_revid: source_wt.lock_write() try: target = source_wt.branch.repository.revision_tree( new_last_revid) update_workingtree_fileids(source_wt, target) finally: source_wt.unlock() push_result.report(self.outf) finally: target_branch.unlock()
def test_no_public_branch(self): b = self.make_branch('.') error = errors.NoPublicBranch(b) url = urlutils.unescape_for_display(b.base, 'ascii') self.assertEqualDiff( 'There is no public branch set for "%s".' % url, str(error))
def subjects_for_branch(branch, new_revno, old_revno, is_push, message): """ Generates the event's subject for commit, push and pull events branch - the bzrlib.branch.Branch instance new_revno - the new revision number generated after a commit, after a push or a pull old_revno - the old revno which is was present before the commit, push or pull event is_push - True then push, is False then pull, is None then commit message - the Commit message """ from zeitgeist.datamodel import Subject, Interpretation, Manifestation location = urlutils.unescape_for_display(branch.base, "utf-8").decode("utf-8") if is_push is None: # Commit text = u"Revision: %s, Message: %s" %(new_revno, message) elif is_push is True: # Push text = "Pushed branch from %s to %s" %(old_revno, new_revno) elif is_push is False: # Pull text = "Updated branch from %s to %s" %(old_revno, new_revno) folder_subj = Subject.new_for_values( uri=unicode(branch.base), interpretation=unicode(Interpretation.FOLDER), manifestation=unicode(Manifestation.FILE_DATA_OBJECT), text=text, origin=unicode(branch.base), mimetype="application/x-bzr", ) all_subjs = [folder_subj, ] # List down the files only in case of Commit as in case of # push or pull, remote URI is used using which constructing # file URI is not possible if is_push is None: prev_tree = branch.repository.revision_tree(branch.get_rev_id(old_revno)) next_tree = branch.repository.revision_tree(branch.get_rev_id(new_revno)) for (file_id, path, content_change, versioned, parent_id, name, kind, executable) in next_tree.iter_changes(prev_tree): name = path[0] if path[0] is not None else path[1] uri = os.path.join(unicode(branch.base), name) # fi.get_content_type returns the file mimetype f=Gio.File.new_for_uri(uri) fi=f.query_info("*", Gio.FileQueryInfoFlags.NONE, None) subj = Subject.new_for_values( uri=uri, manifestation=unicode(Manifestation.FILE_DATA_OBJECT), text=text, origin=unicode(branch.base), mimetype=fi.get_content_type(), ) all_subjs.append(subj) return all_subjs
def run(self, location=None, remember=False, directory=None, no_rebase=False): from bzrlib import urlutils from bzrlib.bzrdir import BzrDir from bzrlib.errors import BzrCommandError, NoWorkingTree from bzrlib.trace import info from bzrlib.workingtree import WorkingTree from upgrade import update_workingtree_fileids if directory is None: directory = "." try: source_wt = WorkingTree.open_containing(directory)[0] source_branch = source_wt.branch except NoWorkingTree: source_branch = Branch.open_containing(directory)[0] source_wt = None stored_loc = source_branch.get_push_location() if location is None: if stored_loc is None: raise BzrCommandError("No push location known or specified.") else: display_url = urlutils.unescape_for_display( stored_loc, self.outf.encoding) self.outf.write("Using saved location: %s\n" % display_url) location = stored_loc bzrdir = BzrDir.open(location) target_branch = bzrdir.open_branch() target_branch.lock_write() try: if not isinstance(target_branch, ForeignBranch): info( "target branch is not a foreign branch, using regular push." ) target_branch.pull(source_branch) no_rebase = True else: revid_map = target_branch.dpull(source_branch) # We successfully created the target, remember it if source_branch.get_push_location() is None or remember: source_branch.set_push_location(target_branch.base) if not no_rebase: _, old_last_revid = source_branch.last_revision_info() new_last_revid = revid_map[old_last_revid] if source_wt is not None: source_wt.pull(target_branch, overwrite=True, stop_revision=new_last_revid) source_wt.lock_write() try: update_workingtree_fileids( source_wt, source_wt.branch.repository.revision_tree( old_last_revid), source_wt.branch.repository.revision_tree( new_last_revid)) finally: source_wt.unlock() else: source_branch.pull(target_branch, overwrite=True, stop_revision=new_last_revid) finally: target_branch.unlock()