def _linearise(self, branch, sequence, previous_import):

        counter = len(sequence) - 1
        ancestors = set()

        self._set_branch(branch, previous_import, checkout=True, force=True)
        root = previous_import.hexsha
        while counter > 0:
            # add commit to list of ancestors to check
            ancestors.add(root)

            # look for merge commits that are not part of ancestry path
            for idx in xrange(counter - 1, -1, -1):
                commit = sequence[idx]
                # if there is only one parent, no need to check the others
                if len(commit.parents) < 2:
                    ancestors.add(commit.hexsha)
                elif any(p.hexsha not in ancestors for p in commit.parents):
                    self.log.debug("Rebase upto commit SHA1: %s",
                                   commit.hexsha)
                    idx = idx + 1
                    break
                else:
                    ancestors.add(commit.hexsha)
            tip = sequence[idx].hexsha

            self.log.info("Rebasing from %s to %s", root, tip)
            previous = self.git.rev_parse(branch)
            self.log.info("Rebasing onto '%s'", previous)
            if root == previous and idx == 0:
                # special case, we are already linear
                self.log.info("Already in a linear layout")
                return
            self._set_branch(branch, tip, force=True)
            try:
                self.log.debug(
                    """
                        git rebase -p --onto=%s \\
                            %s %s
                    """, previous, root, branch)
                self.git.rebase(root, branch, onto=previous, p=True)
            except Exception:
                self.git.rebase(abort=True, with_exceptions=False)
                raise
            counter = idx - 1
            # set root commit for next loop
            root = sequence[counter].hexsha
dataset/ETHPy150Open openstack/git-upstream/git_upstream/lib/importupstream.py/ImportUpstream._linearise/original