From 396e5570bb81f5df982a77a2bd4d6d060d2f3b6c Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Sun, 15 Jun 2025 15:45:55 -0400 Subject: [PATCH 1/2] Update the code --- diff_cover/diff_reporter.py | 30 ++++++++++++++---------------- diff_cover/git_diff.py | 6 ++---- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/diff_cover/diff_reporter.py b/diff_cover/diff_reporter.py index 411586e3..3ecf2cc9 100644 --- a/diff_cover/diff_reporter.py +++ b/diff_cover/diff_reporter.py @@ -349,22 +349,20 @@ def _parse_source_sections(self, diff_str): # Every other line is stored in the dictionary for this source file # once we find a hunk section - else: - # Only add lines if we're in a hunk section - # (ignore index and files changed lines) - if found_hunk or line.startswith("@@"): - # Remember that we found a hunk - found_hunk = True - - if src_path is not None: - source_dict[src_path].append(line) - - else: - # We tolerate other information before we have - # a source file defined, unless it's a hunk line - if line.startswith("@@"): - msg = f"Hunk has no source file: '{line}'" - raise GitDiffError(msg) + # Only add lines if we're in a hunk section + # (ignore index and files changed lines) + elif found_hunk or line.startswith("@@"): + # Remember that we found a hunk + found_hunk = True + + if src_path is not None: + source_dict[src_path].append(line) + + # We tolerate other information before we have + # a source file defined, unless it's a hunk line + elif line.startswith("@@"): + msg = f"Hunk has no source file: '{line}'" + raise GitDiffError(msg) return source_dict diff --git a/diff_cover/git_diff.py b/diff_cover/git_diff.py index 86ced3fe..61033e08 100644 --- a/diff_cover/git_diff.py +++ b/diff_cover/git_diff.py @@ -65,9 +65,7 @@ def diff_committed(self, compare_branch="origin/main"): Raises a `GitDiffError` if `git diff` outputs anything to stderr. """ - diff_range = "{branch}{notation}HEAD".format( - branch=compare_branch, notation=self.range_notation - ) + diff_range = f"{compare_branch}{self.range_notation}HEAD" try: return execute( self._default_git_args + self._default_diff_args + [diff_range] @@ -134,7 +132,7 @@ def diff_committed(self, compare_branch="origin/main"): Raises a `GitDiffError` if the file cannot be read. """ try: - with open(self.diff_file_path, "r") as file: + with open(self.diff_file_path, encoding="utf-8") as file: return file.read() except OSError as e: error_message = ( From e070959be0f39ad61106f5bd2d57d4df64fbfcf5 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Sun, 15 Jun 2025 15:49:18 -0400 Subject: [PATCH 2/2] Update git_diff.py --- diff_cover/git_diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff_cover/git_diff.py b/diff_cover/git_diff.py index 61033e08..cf285519 100644 --- a/diff_cover/git_diff.py +++ b/diff_cover/git_diff.py @@ -132,7 +132,7 @@ def diff_committed(self, compare_branch="origin/main"): Raises a `GitDiffError` if the file cannot be read. """ try: - with open(self.diff_file_path, encoding="utf-8") as file: + with open(self.diff_file_path, "r") as file: return file.read() except OSError as e: error_message = (