Exemplo n.º 1
0
 def git_checkout_block(self, ref: str = None):
     """Allows temporarily checkout another git-ref."""
     current_head = self._get_ref_from_git_repo()
     if ref:
         logger.debug(
             f"Leaving old ref {current_head!r} and checkout new ref {ref!r}"
         )
         if ref not in self.git_repo.refs:
             if not is_a_git_ref(self.git_repo, ref):
                 raise PackitException(
                     f"Git ref {ref!r} not found, cannot checkout.")
             ref = self.git_repo.commit(ref).hexsha
         self.git_repo.git.checkout(ref)
     yield
     if ref:
         logger.debug(
             f"Leaving new ref {ref!r} and checkout old ref {current_head!r}"
         )
         self.git_repo.git.checkout(current_head)
Exemplo n.º 2
0
    def get_commits_to_upstream(
        self, upstream: str, add_upstream_head_commit=False
    ) -> List[git.Commit]:
        """
        Return the list of different commits between current branch and upstream rev/tag.

        Always choosing the first-parent, so we have a line/path of the commits.
        It contains merge-commits from the master and commits on top of the master.
        (e.g. commits from PR)

        :param add_upstream_head_commit: bool
        :param upstream: str -- git branch or tag
        :return: list of commits (last commit on the current branch.).
        """

        if is_a_git_ref(repo=self.local_project.git_repo, ref=upstream):
            upstream_ref = upstream
        else:
            upstream_ref = f"origin/{upstream}"
            if upstream_ref not in self.local_project.git_repo.refs:
                raise Exception(
                    f"Upstream {upstream_ref} branch nor {upstream} tag not found."
                )

        commits = list(
            self.local_project.git_repo.iter_commits(
                rev=f"{upstream_ref}..{self.local_project.ref}",
                reverse=True,
                first_parent=True,
            )
        )
        if add_upstream_head_commit:
            commits.insert(0, self.local_project.git_repo.commit(upstream_ref))

        logger.debug(
            f"Delta ({upstream_ref}..{self.local_project.ref}): {len(commits)}"
        )
        return commits
Exemplo n.º 3
0
    def get_commits_since_ref(
        self,
        git_ref: str,
        add_upstream_head_commit: bool = True,
        no_merge_commits: bool = True,
    ) -> List[git.Commit]:
        """
        Return a list of different commits between HEAD and selected git_ref

        :param git_ref: get commits since this git ref
        :param add_upstream_head_commit: add also upstream rev/tag commit as a first value
        :param no_merge_commits: do not include merge commits in the list if True
        :return: list of commits (last commit on the current branch.).
        """

        if is_a_git_ref(repo=self.lp.git_repo, ref=git_ref):
            upstream_ref = git_ref
        else:
            upstream_ref = f"origin/{git_ref}"
            if upstream_ref not in self.lp.git_repo.refs:
                raise Exception(
                    f"Upstream {upstream_ref!r} branch nor {git_ref!r} tag not found."
                )

        commits = list(
            self.lp.git_repo.iter_commits(
                rev=f"{git_ref}..{self.lp.ref}",
                reverse=True,
                no_merges=
                no_merge_commits,  # do not include merge commits in the list
            ))
        if add_upstream_head_commit:
            commits.insert(0, self.lp.git_repo.commit(upstream_ref))

        logger.debug(f"Delta ({upstream_ref}..{self.lp.ref}): {len(commits)}")
        return commits