Ejemplo n.º 1
0
    def commit(self) -> None:
        """
        Determine whether there are uncommitted changes, and ask the user what to do about them
        """

        index: IndexFile = self._local_repo.index
        if len(index.diff("HEAD")) > 0:
            Utils.log(LogType.INFO, "You have staged but uncommited changes.")
            create_commit: bool = Utils.ask_bool(
                "do you want to create a new commit?")

            if create_commit:
                # We can't use self.local_repo().git.commit() here, as it would
                # start the editor in the background
                try:
                    subprocess.check_call(["git", "commit"])
                except subprocess.CalledProcessError:
                    Utils.log(LogType.ERROR, "git exited with an error code")
                    sys.exit(1)
Ejemplo n.º 2
0
    def checkout(self, merge_request_id: int) -> None:
        """
        Checks out the merge request with the specified id in the local worktree
        """
        self.__mr = self._remote_project.mergerequests.get(merge_request_id, lazy=False)
        print('Checking out merge request "{}"...'.format(self.__mr.title))
        print("  branch:", self.__mr.source_branch)

        fetch_info = self._local_repo.remotes.origin.fetch(
            "merge-requests/{}/head".format(merge_request_id)
        )[0]
        if self.__mr.source_branch in self._local_repo.refs:
            # Make sure not to overwrite local changes
            overwrite = Utils.ask_bool(
                'Branch "{}" already exists locally, do you want to overwrite it?'.format(
                    self.__mr.source_branch
                )
            )

            if not overwrite:
                print("Aborting")
                sys.exit(1)

            # If the branch that we want to overwrite is currently checked out,
            # that will of course not work, so try to switch to another branch in the meantime.
            if self.__mr.source_branch == self._local_repo.head.reference.name:
                if "main" in self._local_repo.refs:
                    self._local_repo.refs.main.checkout()
                elif "master" in self._local_repo.refs:
                    self._local_repo.refs.master.checkout()
                else:
                    Utils.log(
                        LogType.ERROR,
                        "The branch that you want to overwrite is currently checked out \
                        and no other branch to temporarily switch to could be found. Please check out \
                        a different branch and try again.",
                    )
                    sys.exit(1)

            self._local_repo.delete_head(self.__mr.source_branch, "-f")

        head = self._local_repo.create_head(self.__mr.source_branch, fetch_info.ref)
        head.checkout()