From 3232adc6a7abe5ced1624b24f722b8bc2b770fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Jul 2023 16:02:23 +0200 Subject: [PATCH] Add: Extend git.log to allow setting a pretty format Allow formatting git log output. --- pontos/git/git.py | 12 +++++++++++- tests/git/test_git.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pontos/git/git.py b/pontos/git/git.py index a5d90b1c8..b6dee7c9e 100644 --- a/pontos/git/git.py +++ b/pontos/git/git.py @@ -524,16 +524,26 @@ def checkout( self.exec(*args) - def log(self, *log_args: str, oneline: Optional[bool] = None) -> List[str]: + def log( + self, + *log_args: str, + oneline: Optional[bool] = None, + format: Optional[str] = None, + ) -> List[str]: """ Get log of a git repository Args: + format: Pretty format the output. log_args: Additional arguments for git log oneline: Print the abbreviated commit id and commit message in one line per commit """ args = ["log"] + + if format: + args.append(f"--format={format}") + if oneline: args.append("--oneline") diff --git a/tests/git/test_git.py b/tests/git/test_git.py index d9598918d..15498c910 100644 --- a/tests/git/test_git.py +++ b/tests/git/test_git.py @@ -556,6 +556,25 @@ def test_log_with_oneline(self, exec_git_mock): self.assertEqual(logs[0], "50f9963 Add CircleCI config for pontos") self.assertEqual(logs[5], "464f24d Initial commit") + @patch("pontos.git.git.exec_git") + def test_log_with_format(self, exec_git_mock): + exec_git_mock.return_value = """Add CircleCI config for pontos +Rename to pontos only +Update README for installation and development +Update README +Add a draft for a README.md document +Initial commit""" + + git = Git() + logs = git.log(format="format:%s") + + exec_git_mock.assert_called_once_with( + "log", "--format=format:%s", cwd=None + ) + + self.assertEqual(logs[0], "Add CircleCI config for pontos") + self.assertEqual(logs[5], "Initial commit") + @patch("pontos.git.git.exec_git") def test_rev_list(self, exec_git_mock): git = Git()