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 PackitException( f"Couldn't not find upstream branch {upstream_ref!r} and tag {git_ref!r}." ) commits = self.get_commits_in_range( revision_range=f"{git_ref}..{self.lp.ref}", no_merge_commits=no_merge_commits, ) 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
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
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)