Пример #1
0
    def run(self, merge_type=None, directory="."):
        from bzrlib.plugins.rewrite.rebase import RebaseState1, WorkingTreeRevisionRewriter
        from bzrlib.workingtree import WorkingTree

        wt = WorkingTree.open_containing(directory)[0]
        wt.lock_write()
        try:
            state = RebaseState1(wt)
            replayer = WorkingTreeRevisionRewriter(wt, state, merge_type=merge_type)
            # Abort if there are any conflicts
            if len(wt.conflicts()) != 0:
                raise BzrCommandError(
                    gettext(
                        "There are still conflicts present. "
                        "Resolve the conflicts and then run "
                        "'bzr resolve' and try again."
                    )
                )
            # Read plan file
            try:
                replace_map = state.read_plan()[1]
            except NoSuchFile:
                raise BzrCommandError(gettext("No rebase to continue"))
            oldrevid = state.read_active_revid()
            if oldrevid is not None:
                oldrev = wt.branch.repository.get_revision(oldrevid)
                replayer.commit_rebase(oldrev, replace_map[oldrevid][0])
            finish_rebase(state, wt, replace_map, replayer)
        finally:
            wt.unlock()
Пример #2
0
    def run(self, directory="."):
        from bzrlib.plugins.rewrite.rebase import RebaseState1, rebase_todo
        from bzrlib.workingtree import WorkingTree

        wt = WorkingTree.open_containing(directory)[0]
        wt.lock_read()
        try:
            state = RebaseState1(wt)
            try:
                replace_map = state.read_plan()[1]
            except NoSuchFile:
                raise BzrCommandError(gettext("No rebase in progress"))
            currentrevid = state.read_active_revid()
            if currentrevid is not None:
                note(gettext("Currently replaying: %s") % currentrevid)
            for revid in rebase_todo(wt.branch.repository, replace_map):
                note(gettext("{0} -> {1}").format(revid, replace_map[revid][0]))
        finally:
            wt.unlock()
Пример #3
0
    def run(self, location, revision=None, merge_type=None, directory="."):
        from bzrlib.branch import Branch
        from bzrlib.workingtree import WorkingTree
        from bzrlib import ui
        from bzrlib.plugins.rewrite.rebase import RebaseState1, regenerate_default_revid, WorkingTreeRevisionRewriter

        from_branch = Branch.open_containing(location)[0]

        if revision is not None:
            if len(revision) == 1:
                if revision[0] is not None:
                    todo = [revision[0].as_revision_id(from_branch)]
            elif len(revision) == 2:
                from_revno, from_revid = revision[0].in_history(from_branch)
                to_revno, to_revid = revision[1].in_history(from_branch)
                if to_revid is None:
                    to_revno = from_branch.revno()
                todo = []
                for revno in range(from_revno, to_revno + 1):
                    todo.append(from_branch.get_rev_id(revno))
            else:
                raise BzrCommandError(gettext("--revision takes only one or two arguments"))
        else:
            raise BzrCommandError(gettext("--revision is mandatory"))

        wt = WorkingTree.open(directory)
        wt.lock_write()
        try:
            state = RebaseState1(wt)
            replayer = WorkingTreeRevisionRewriter(wt, state, merge_type=merge_type)
            pb = ui.ui_factory.nested_progress_bar()
            try:
                for revid in todo:
                    pb.update(gettext("replaying commits"), todo.index(revid), len(todo))
                    wt.branch.repository.fetch(from_branch.repository, revid)
                    newrevid = regenerate_default_revid(wt.branch.repository, revid)
                    replayer(revid, newrevid, [wt.last_revision()])
            finally:
                pb.finished()
        finally:
            wt.unlock()
Пример #4
0
def finish_rebase(state, wt, replace_map, replayer):
    from bzrlib.plugins.rewrite.rebase import rebase

    try:
        # Start executing plan from current Branch.last_revision()
        rebase(wt.branch.repository, replace_map, replayer)
    except ConflictsInTree:
        raise BzrCommandError(
            gettext(
                "A conflict occurred replaying a commit."
                " Resolve the conflict and run 'bzr rebase-continue' or "
                "run 'bzr rebase-abort'."
            )
        )
    # Remove plan file
    state.remove_plan()
Пример #5
0
    def run(
        self,
        upstream_location=None,
        onto=None,
        revision=None,
        merge_type=None,
        verbose=False,
        dry_run=False,
        always_rebase_merges=False,
        pending_merges=False,
        directory=".",
    ):
        from bzrlib.branch import Branch
        from bzrlib.revisionspec import RevisionSpec
        from bzrlib.workingtree import WorkingTree
        from bzrlib.plugins.rewrite.rebase import (
            generate_simple_plan,
            rebase,
            RebaseState1,
            WorkingTreeRevisionRewriter,
            regenerate_default_revid,
            rebase_todo,
        )

        if revision is not None and pending_merges:
            raise BzrCommandError(gettext("--revision and --pending-merges are mutually exclusive"))

        wt = WorkingTree.open_containing(directory)[0]
        wt.lock_write()
        try:
            state = RebaseState1(wt)
            if upstream_location is None:
                if pending_merges:
                    upstream_location = directory
                else:
                    upstream_location = wt.branch.get_parent()
                    if upstream_location is None:
                        raise BzrCommandError(gettext("No upstream branch specified."))
                    note(gettext("Rebasing on %s"), upstream_location)
            upstream = Branch.open_containing(upstream_location)[0]
            upstream_repository = upstream.repository
            upstream_revision = upstream.last_revision()
            # Abort if there already is a plan file
            if state.has_plan():
                raise BzrCommandError(
                    gettext(
                        "A rebase operation was interrupted. "
                        "Continue using 'bzr rebase-continue' or abort using 'bzr "
                        "rebase-abort'"
                    )
                )

            start_revid = None
            stop_revid = None
            if revision is not None:
                if len(revision) == 1:
                    if revision[0] is not None:
                        stop_revid = revision[0].as_revision_id(wt.branch)
                elif len(revision) == 2:
                    if revision[0] is not None:
                        start_revid = revision[0].as_revision_id(wt.branch)
                    if revision[1] is not None:
                        stop_revid = revision[1].as_revision_id(wt.branch)
                else:
                    raise BzrCommandError(gettext("--revision takes only one or two arguments"))

            if pending_merges:
                wt_parents = wt.get_parent_ids()
                if len(wt_parents) in (0, 1):
                    raise BzrCommandError(gettext("No pending merges present."))
                elif len(wt_parents) > 2:
                    raise BzrCommandError(gettext("Rebasing more than one pending merge not supported"))
                stop_revid = wt_parents[1]
                assert stop_revid is not None, "stop revid invalid"

            # Check for changes in the working tree.
            if not pending_merges and wt.basis_tree().changes_from(wt).has_changed():
                raise UncommittedChanges(wt)

            # Pull required revisions
            wt.branch.repository.fetch(upstream_repository, upstream_revision)
            if onto is None:
                onto = upstream.last_revision()
            else:
                rev_spec = RevisionSpec.from_string(onto)
                onto = rev_spec.as_revision_id(upstream)

            wt.branch.repository.fetch(upstream_repository, onto)

            if stop_revid is None:
                stop_revid = wt.branch.last_revision()
            repo_graph = wt.branch.repository.get_graph()
            our_new, onto_unique = repo_graph.find_difference(stop_revid, onto)

            if start_revid is None:
                if not onto_unique:
                    self.outf.write(gettext("No revisions to rebase.\n"))
                    return
                if not our_new:
                    self.outf.write(gettext("Base branch is descendant of current " "branch. Pulling instead.\n"))
                    if not dry_run:
                        wt.pull(upstream, onto)
                    return
            # else: include extra revisions needed to make start_revid mean
            # something.

            # Create plan
            replace_map = generate_simple_plan(
                our_new,
                start_revid,
                stop_revid,
                onto,
                repo_graph,
                lambda revid, ps: regenerate_default_revid(wt.branch.repository, revid),
                not always_rebase_merges,
            )

            if verbose or dry_run:
                todo = list(rebase_todo(wt.branch.repository, replace_map))
                note(gettext("%d revisions will be rebased:") % len(todo))
                for revid in todo:
                    note("%s" % revid)

            if not dry_run:
                # Write plan file
                state.write_plan(replace_map)

                replayer = WorkingTreeRevisionRewriter(wt, state, merge_type=merge_type)

                finish_rebase(state, wt, replace_map, replayer)
        finally:
            wt.unlock()
Пример #6
0
    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())