From a943fa39bd2363f1d5e3b5442858935bc3ac99f2 Mon Sep 17 00:00:00 2001 From: Aur Saraf Date: Tue, 30 Jul 2024 04:08:49 +0300 Subject: [PATCH 1/3] deprecation warning for Git.show() w/o string_newline=False --- git/cmd.py | 7 +++++++ test/test_repo.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/git/cmd.py b/git/cmd.py index 90fc39cd6..7bdd646ef 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -1210,6 +1210,13 @@ def execute( if self.GIT_PYTHON_TRACE and (self.GIT_PYTHON_TRACE != "full" or as_process): _logger.info(" ".join(redacted_command)) + if strip_newline_in_stdout and command[:2] == ["git", "show"]: + warnings.warn( + "Git.show() has strip_newline_in_stdout=True by default, which probably isn't what you want and will " + "change in a future version. It is recommended to use Git.show(..., strip_newline_in_stdout=False)", + DeprecationWarning + ) + # Allow the user to have the command executed in their working dir. try: cwd = self._working_dir or os.getcwd() # type: Union[None, str] diff --git a/test/test_repo.py b/test/test_repo.py index e38da5bb6..ee1dfe0c7 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1377,13 +1377,23 @@ def test_rebasing(self, rw_dir): @with_rw_directory def test_do_not_strip_newline_in_stdout(self, rw_dir): + r = self.create_repo_commit_hello_newline(rw_dir) + self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline_in_stdout=False), "hello\n") + + def create_repo_commit_hello_newline(self, rw_dir): r = Repo.init(rw_dir) fp = osp.join(rw_dir, "hello.txt") with open(fp, "w") as fs: fs.write("hello\n") r.git.add(Git.polish_url(fp)) r.git.commit(message="init") - self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline_in_stdout=False), "hello\n") + return r + + @with_rw_directory + def test_warn_when_strip_newline_in_stdout(self, rw_dir): + r = self.create_repo_commit_hello_newline(rw_dir) + with pytest.warns(DeprecationWarning): + self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline_in_stdout=True), "hello") @pytest.mark.xfail( sys.platform == "win32", From 10fb8613b47ed766b1f0ce2048cfebc0e4808aca Mon Sep 17 00:00:00 2001 From: Aur Saraf Date: Tue, 30 Jul 2024 04:25:35 +0300 Subject: [PATCH 2/3] fix tests that assumed default initial branch setting --- test/test_diff.py | 2 +- test/test_repo.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_diff.py b/test/test_diff.py index 612fbd9e0..695025396 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -51,7 +51,7 @@ def _assert_diff_format(self, diffs): @with_rw_directory def test_diff_with_staged_file(self, rw_dir): # SET UP INDEX WITH MULTIPLE STAGES - r = Repo.init(rw_dir) + r = Repo.init(rw_dir, initial_branch="master") fp = osp.join(rw_dir, "hello.txt") with open(fp, "w") as fs: fs.write("hello world") diff --git a/test/test_repo.py b/test/test_repo.py index ee1dfe0c7..7a9b2a110 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1193,7 +1193,7 @@ def test_remote_method(self): @with_rw_directory def test_empty_repo(self, rw_dir): """Assure we can handle empty repositories""" - r = Repo.init(rw_dir, mkdir=False) + r = Repo.init(rw_dir, mkdir=False, initial_branch="master") # It's ok not to be able to iterate a commit, as there is none. self.assertRaises(ValueError, r.iter_commits) self.assertEqual(r.active_branch.name, "master") @@ -1352,7 +1352,7 @@ def test_git_work_tree_env(self, rw_dir): @with_rw_directory def test_rebasing(self, rw_dir): - r = Repo.init(rw_dir) + r = Repo.init(rw_dir, initial_branch="master") fp = osp.join(rw_dir, "hello.txt") r.git.commit( "--allow-empty", From 52541595ed0cc0d8676ab4875b2cb1f448de1247 Mon Sep 17 00:00:00 2001 From: Aur Saraf Date: Tue, 30 Jul 2024 04:57:38 +0300 Subject: [PATCH 3/3] run pre-commit --- git/cmd.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/git/cmd.py b/git/cmd.py index 7bdd646ef..a4cedc134 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -1214,7 +1214,8 @@ def execute( warnings.warn( "Git.show() has strip_newline_in_stdout=True by default, which probably isn't what you want and will " "change in a future version. It is recommended to use Git.show(..., strip_newline_in_stdout=False)", - DeprecationWarning + DeprecationWarning, + stacklevel=1, ) # Allow the user to have the command executed in their working dir.