From d35998f5f420db780a30d73b703296498e3aa531 Mon Sep 17 00:00:00 2001 From: Guillaume Cardoen Date: Mon, 17 Jun 2024 09:28:32 +0200 Subject: [PATCH 1/3] fix: fix beginning whitespace error --- git/diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/diff.py b/git/diff.py index 8d2646f99..9c6ae59e0 100644 --- a/git/diff.py +++ b/git/diff.py @@ -695,7 +695,7 @@ def _handle_diff_line(lines_bytes: bytes, repo: "Repo", index: DiffIndex["Diff"] change_type: Lit_change_type = cast(Lit_change_type, _change_type[0]) score_str = "".join(_change_type[1:]) score = int(score_str) if score_str.isdigit() else None - path = path.strip() + path = path.strip("\n") a_path = path.encode(defenc) b_path = path.encode(defenc) deleted_file = False From 9910a886ddeb05a39b774e9f3520837fd9a76dca Mon Sep 17 00:00:00 2001 From: Guillaume Cardoen Date: Mon, 17 Jun 2024 09:31:51 +0200 Subject: [PATCH 2/3] test: add test for diff with beginning whitespace --- test/test_diff.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/test_diff.py b/test/test_diff.py index 928a9f428..6cae3fbf2 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -529,3 +529,23 @@ def test_diff_patch_with_external_engine(self, rw_dir): self.assertEqual(len(index_against_head), 1) index_against_working_tree = repo.index.diff(None, create_patch=True) self.assertEqual(len(index_against_working_tree), 1) + + @with_rw_directory + def test_beginning_space(self, rw_dir): + # Create a file beginning by a whitespace + repo = Repo.init(rw_dir) + file = osp.join(rw_dir, " file.txt") + with open(file, "w") as f: + f.write("hello world") + repo.git.add(Git.polish_url(file)) + repo.index.commit("first commit") + + # Diff the commit with an empty tree + # and check the paths + diff_index = repo.head.commit.diff(NULL_TREE) + d = diff_index[0] + a_path = d.a_path + b_path = d.b_path + self.assertEqual(a_path, " file.txt") + self.assertEqual(b_path, " file.txt") + \ No newline at end of file From 97fad9cb8322e510647cf58cc023702a7b7e077f Mon Sep 17 00:00:00 2001 From: Guillaume Cardoen Date: Tue, 18 Jun 2024 08:51:00 +0200 Subject: [PATCH 3/3] style: ruff --- test/test_diff.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_diff.py b/test/test_diff.py index 6cae3fbf2..612fbd9e0 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -539,7 +539,7 @@ def test_beginning_space(self, rw_dir): f.write("hello world") repo.git.add(Git.polish_url(file)) repo.index.commit("first commit") - + # Diff the commit with an empty tree # and check the paths diff_index = repo.head.commit.diff(NULL_TREE) @@ -548,4 +548,3 @@ def test_beginning_space(self, rw_dir): b_path = d.b_path self.assertEqual(a_path, " file.txt") self.assertEqual(b_path, " file.txt") - \ No newline at end of file