def grep_for_commits(self, branch, grep_for, reverse=False, path=None, log_format='format:%H'): """Returns a list of matching commits shas. :param str branch: which branch to grep on :param str grep_for: what to grep for :param bool reverse: whether to return in reversed order :param str path: path to limit the search to, optionally :param str log_format: log format output. Defaults to format:%H. Please refer to git-log documentation about PRETTY FORMATS :return: A list of resulting commit matching the pattern. """ commits = [] params = [branch, f"--format={log_format}", f"--grep={grep_for}"] if reverse: params += ['--reverse'] if path: if path not in self.git_repo.repo.tree(): msg = f"Path {path} doesn't exist in repo." raise exceptions.FileDoesntExistException(msg) params += [path] results = self.git_repo.repo.git.log(*params) # git.log returns an empty string if there are no matching commits, don't split in that case if len(results) > 0: commits = results.split("\n") return commits
def _expand_file_path(self, path): """Expand a given path into an absolute path and check for presence. :param str path: Path """ full_path = os.path.realpath(os.path.expanduser(path)) if not os.path.isfile(full_path): raise exceptions.FileDoesntExistException('{path} is not a file.'.format(path=full_path)) return full_path
def reverse_diff(self, diff_path): """Reverse a diff that was applied to the workspace. :param str diff_path: Path to the diff file """ full_path = os.path.expanduser(diff_path) if not os.path.isfile(full_path): raise exceptions.FileDoesntExistException('{path} is not a file.'.format(path=full_path)) try: self.git_repo.git.apply(full_path, reverse=True) except git.GitCommandError as ex: msg = "Reversing diff failed. Error: {0}".format(ex) raise_from(exceptions.RevertException(msg), ex)