From a774182c05731797cfe3353ff9618c0cd80b6f35 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Tue, 19 Sep 2023 09:02:52 -0400 Subject: [PATCH 1/4] Fix black exclusions to omit .gitignore dirs This replaces "exclude" with "extend-exclude" in the black configuration, so that it keeps its default exclusions, of which all directories listed in .gitignore are automatically a part. That makes it possible to run "black ." to format just the files that should be formatted (git/ files, test/ files, and setup.py), while automatically omitting .venv/, .tox/, build/, and so on. This commit does not change how black is run yet, it just fixes the way its exclusions are configured. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 42bb31eda..fa06458eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,4 +45,4 @@ omit = ["*/git/ext/*"] [tool.black] line-length = 120 target-version = ['py37'] -exclude = "git/ext/gitdb" +extend-exclude = "git/ext/gitdb" From e39ecb7269fe266311a4bf766c626de5b95a9f9f Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Tue, 19 Sep 2023 09:06:58 -0400 Subject: [PATCH 2/4] Small manual formatting improvements This adds a trailing "," in a few multi-line function calls in test/, where putting one argument per line was intended and is clearer. This is so that when black is run over test/, it recognizes the form and avoids collapsing it. --- test/test_commit.py | 4 ++-- test/test_docs.py | 2 +- test/test_repo.py | 2 +- test/test_submodule.py | 2 +- test/test_util.py | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_commit.py b/test/test_commit.py index d13db1410..560497547 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -173,12 +173,12 @@ def check_entries(path, changes): ".github/workflows/Future.yml" : { 'insertions': 57, 'deletions': 0, - 'lines': 57 + 'lines': 57, }, ".github/workflows/test_pytest.yml" : { 'insertions': 0, 'deletions': 55, - 'lines': 55 + 'lines': 55, }, } assert path in expected diff --git a/test/test_docs.py b/test/test_docs.py index 505b50f77..4c23e9f81 100644 --- a/test/test_docs.py +++ b/test/test_docs.py @@ -481,7 +481,7 @@ def test_references_and_objects(self, rw_dir): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find SHA for submodule", - raises=ValueError + raises=ValueError, ) def test_submodules(self): # [1-test_submodules] diff --git a/test/test_repo.py b/test/test_repo.py index abae5ad78..1b46fba7c 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -1115,7 +1115,7 @@ def test_repo_odbtype(self): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find submodule SHA", - raises=ValueError + raises=ValueError, ) def test_submodules(self): self.assertEqual(len(self.rorepo.submodules), 1) # non-recursive diff --git a/test/test_submodule.py b/test/test_submodule.py index 5a7f26207..f7195626f 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -469,7 +469,7 @@ def test_base_bare(self, rwrepo): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin GitPython can't find submodule SHA", - raises=ValueError + raises=ValueError, ) @skipIf( HIDE_WINDOWS_KNOWN_ERRORS, diff --git a/test/test_util.py b/test/test_util.py index 517edd65c..42edc57cf 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -159,7 +159,7 @@ def test_lock_file(self): @pytest.mark.xfail( sys.platform == "cygwin", reason="Cygwin fails here for some reason, always", - raises=AssertionError + raises=AssertionError, ) def test_blocking_lock_file(self): my_file = tempfile.mktemp() From 288cf03e120ed6f7e62d6b0e5c974649e50e69de Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Tue, 19 Sep 2023 09:09:08 -0400 Subject: [PATCH 3/4] Don't limit black to git/ This changes the documentation in README.md to recommend running "black ." and changes the command to that in tox.ini, so that more paths are covered (in practice, test/ and setup.py). --- README.md | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69d69c56f..dbec36024 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ mypy -p git For automatic code formatting, run: ```bash -black git +black . ``` Configuration for flake8 is in the `./.flake8` file. diff --git a/tox.ini b/tox.ini index 8d64b929b..82a41e22c 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ ignore_outcome = true [testenv:black] description = Check style with black base_python = py39 -commands = black --check --diff git +commands = black --check --diff . # Run "tox -e html" for this. It is deliberately excluded from env_list, as # unlike the other environments, this one writes outside the .tox/ directory. From 15c736dc79922a1cead221f6fbda5378564e0b6d Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Tue, 19 Sep 2023 10:20:28 -0400 Subject: [PATCH 4/4] Reformat tests with black This actually runs black on the whole project, but the only changes are in test/ (as expected). --- test/performance/test_streams.py | 1 - test/test_commit.py | 17 ++++++++-------- test/test_diff.py | 28 +++++++++++++-------------- test/test_index.py | 2 +- test/test_quick_doc.py | 27 ++++++++++++-------------- test/test_repo.py | 28 ++++++++++++++++----------- test/test_submodule.py | 33 ++++++++++++++++++++++---------- 7 files changed, 75 insertions(+), 61 deletions(-) diff --git a/test/performance/test_streams.py b/test/performance/test_streams.py index 5588212e0..25e081578 100644 --- a/test/performance/test_streams.py +++ b/test/performance/test_streams.py @@ -15,7 +15,6 @@ class TestObjDBPerformance(TestBigRepoR): - large_data_size_bytes = 1000 * 1000 * 10 # some MiB should do it moderate_data_size_bytes = 1000 * 1000 * 1 # just 1 MiB diff --git a/test/test_commit.py b/test/test_commit.py index 560497547..f6fb49d50 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -93,7 +93,6 @@ def assert_commit_serialization(self, rwrepo, commit_id, print_performance_info= class TestCommit(TestCommitSerialization): def test_bake(self): - commit = self.rorepo.commit("2454ae89983a4496a445ce347d7a41c0bb0ea7ae") # commits have no dict self.assertRaises(AttributeError, setattr, commit, "someattr", 1) @@ -170,15 +169,15 @@ def test_renames(self): def check_entries(path, changes): expected = { - ".github/workflows/Future.yml" : { - 'insertions': 57, - 'deletions': 0, - 'lines': 57, + ".github/workflows/Future.yml": { + "insertions": 57, + "deletions": 0, + "lines": 57, }, - ".github/workflows/test_pytest.yml" : { - 'insertions': 0, - 'deletions': 55, - 'lines': 55, + ".github/workflows/test_pytest.yml": { + "insertions": 0, + "deletions": 55, + "lines": 55, }, } assert path in expected diff --git a/test/test_diff.py b/test/test_diff.py index dacbdc3bc..9c3888f03 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -419,7 +419,7 @@ def test_rename_override(self, rw_dir): # create and commit file_a.txt repo = Repo.init(rw_dir) file_a = osp.join(rw_dir, "file_a.txt") - with open(file_a, "w", encoding='utf-8') as outfile: + with open(file_a, "w", encoding="utf-8") as outfile: outfile.write("hello world\n") repo.git.add(Git.polish_url(file_a)) repo.git.commit(message="Added file_a.txt") @@ -429,21 +429,21 @@ def test_rename_override(self, rw_dir): # create and commit file_b.txt with similarity index of 52 file_b = osp.join(rw_dir, "file_b.txt") - with open(file_b, "w", encoding='utf-8') as outfile: + with open(file_b, "w", encoding="utf-8") as outfile: outfile.write("hello world\nhello world") repo.git.add(Git.polish_url(file_b)) repo.git.commit(message="Removed file_a.txt. Added file_b.txt") - commit_a = repo.commit('HEAD') - commit_b = repo.commit('HEAD~1') + commit_a = repo.commit("HEAD") + commit_b = repo.commit("HEAD~1") # check default diff command with renamed files enabled diffs = commit_b.diff(commit_a) self.assertEqual(1, len(diffs)) diff = diffs[0] self.assertEqual(True, diff.renamed_file) - self.assertEqual('file_a.txt', diff.rename_from) - self.assertEqual('file_b.txt', diff.rename_to) + self.assertEqual("file_a.txt", diff.rename_from) + self.assertEqual("file_b.txt", diff.rename_to) # check diff with rename files disabled diffs = commit_b.diff(commit_a, no_renames=True) @@ -452,31 +452,31 @@ def test_rename_override(self, rw_dir): # check fileA.txt deleted diff = diffs[0] self.assertEqual(True, diff.deleted_file) - self.assertEqual('file_a.txt', diff.a_path) + self.assertEqual("file_a.txt", diff.a_path) # check fileB.txt added diff = diffs[1] self.assertEqual(True, diff.new_file) - self.assertEqual('file_b.txt', diff.a_path) + self.assertEqual("file_b.txt", diff.a_path) # check diff with high similarity index - diffs = commit_b.diff(commit_a, split_single_char_options=False, M='75%') + diffs = commit_b.diff(commit_a, split_single_char_options=False, M="75%") self.assertEqual(2, len(diffs)) # check fileA.txt deleted diff = diffs[0] self.assertEqual(True, diff.deleted_file) - self.assertEqual('file_a.txt', diff.a_path) + self.assertEqual("file_a.txt", diff.a_path) # check fileB.txt added diff = diffs[1] self.assertEqual(True, diff.new_file) - self.assertEqual('file_b.txt', diff.a_path) + self.assertEqual("file_b.txt", diff.a_path) # check diff with low similarity index - diffs = commit_b.diff(commit_a, split_single_char_options=False, M='40%') + diffs = commit_b.diff(commit_a, split_single_char_options=False, M="40%") self.assertEqual(1, len(diffs)) diff = diffs[0] self.assertEqual(True, diff.renamed_file) - self.assertEqual('file_a.txt', diff.rename_from) - self.assertEqual('file_b.txt', diff.rename_to) + self.assertEqual("file_a.txt", diff.rename_from) + self.assertEqual("file_b.txt", diff.rename_to) diff --git a/test/test_index.py b/test/test_index.py index 9b7ba52a6..fba9c78ec 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -946,7 +946,7 @@ def test_commit_msg_hook_fail(self, rw_repo): else: raise AssertionError("Should have caught a HookExecutionError") - @with_rw_repo('HEAD') + @with_rw_repo("HEAD") def test_index_add_pathlike(self, rw_repo): git_dir = Path(rw_repo.git_dir) diff --git a/test/test_quick_doc.py b/test/test_quick_doc.py index eaee4e581..9dc7b8d2e 100644 --- a/test/test_quick_doc.py +++ b/test/test_quick_doc.py @@ -13,14 +13,13 @@ def tearDown(self): @with_rw_directory def test_init_repo_object(self, path_to_dir): - # [1-test_init_repo_object] # $ git init from git import Repo repo = Repo.init(path_to_dir) - # ![1-test_init_repo_object] + # ![1-test_init_repo_object] # [2-test_init_repo_object] repo = Repo(path_to_dir) @@ -28,9 +27,9 @@ def test_init_repo_object(self, path_to_dir): @with_rw_directory def test_cloned_repo_object(self, local_dir): - from git import Repo import git + # code to clone from url # [1-test_cloned_repo_object] # $ git clone @@ -44,9 +43,9 @@ def test_cloned_repo_object(self, local_dir): # [2-test_cloned_repo_object] # We must make a change to a file so that we can add the update to git - update_file = 'dir1/file2.txt' # we'll use local_dir/dir1/file2.txt - with open(f"{local_dir}/{update_file}", 'a') as f: - f.write('\nUpdate version 2') + update_file = "dir1/file2.txt" # we'll use local_dir/dir1/file2.txt + with open(f"{local_dir}/{update_file}", "a") as f: + f.write("\nUpdate version 2") # ![2-test_cloned_repo_object] # [3-test_cloned_repo_object] @@ -82,7 +81,7 @@ def test_cloned_repo_object(self, local_dir): # Untracked files - create new file # [7-test_cloned_repo_object] - f = open(f'{local_dir}/untracked.txt', 'w') # creates an empty file + f = open(f"{local_dir}/untracked.txt", "w") # creates an empty file f.close() # ![7-test_cloned_repo_object] @@ -95,8 +94,8 @@ def test_cloned_repo_object(self, local_dir): # [9-test_cloned_repo_object] # Let's modify one of our tracked files - with open(f'{local_dir}/Downloads/file3.txt', 'w') as f: - f.write('file3 version 2') # overwrite file 3 + with open(f"{local_dir}/Downloads/file3.txt", "w") as f: + f.write("file3 version 2") # overwrite file 3 # ![9-test_cloned_repo_object] # [10-test_cloned_repo_object] @@ -126,7 +125,7 @@ def test_cloned_repo_object(self, local_dir): # ![11.1-test_cloned_repo_object] # [11.2-test_cloned_repo_object] # lets add untracked.txt - repo.index.add(['untracked.txt']) + repo.index.add(["untracked.txt"]) diffs = repo.index.diff(repo.head.commit) for d in diffs: print(d.a_path) @@ -146,9 +145,7 @@ def test_cloned_repo_object(self, local_dir): # dir1/file2.txt # ![11.3-test_cloned_repo_object] - - - '''Trees and Blobs''' + """Trees and Blobs""" # Latest commit tree # [12-test_cloned_repo_object] @@ -195,7 +192,7 @@ def print_files_from_git(root, level=0): # Printing text files # [17-test_cloned_repo_object] - print_file = 'dir1/file2.txt' + print_file = "dir1/file2.txt" tree[print_file] # the head commit tree # Output @@ -221,4 +218,4 @@ def print_files_from_git(root, level=0): # Output # file 2 version 1 - # ![18.1-test_cloned_repo_object] \ No newline at end of file + # ![18.1-test_cloned_repo_object] diff --git a/test/test_repo.py b/test/test_repo.py index 1b46fba7c..6432b8c6f 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -251,7 +251,9 @@ def test_clone_from_with_path_contains_unicode(self): self.fail("Raised UnicodeEncodeError") @with_rw_directory - @skip("the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control") + @skip( + "the referenced repository was removed, and one needs to setup a new password controlled repo under the orgs control" + ) def test_leaking_password_in_clone_logs(self, rw_dir): password = "fakepassword1234" try: @@ -391,7 +393,9 @@ def test_clone_from_unsafe_options_allowed(self, rw_repo): for i, unsafe_option in enumerate(unsafe_options): destination = tmp_dir / str(i) assert not destination.exists() - Repo.clone_from(rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True) + Repo.clone_from( + rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True + ) assert destination.exists() @with_rw_repo("HEAD") @@ -755,8 +759,8 @@ def test_blame_complex_revision(self, git): @mock.patch.object(Git, "_call_process") def test_blame_accepts_rev_opts(self, git): res = self.rorepo.blame("HEAD", "README.md", rev_opts=["-M", "-C", "-C"]) - expected_args = ['blame', 'HEAD', '-M', '-C', '-C', '--', 'README.md'] - boilerplate_kwargs = {"p" : True, "stdout_as_string": False} + expected_args = ["blame", "HEAD", "-M", "-C", "-C", "--", "README.md"] + boilerplate_kwargs = {"p": True, "stdout_as_string": False} git.assert_called_once_with(*expected_args, **boilerplate_kwargs) @skipIf( @@ -1415,14 +1419,16 @@ def test_ignored_items_reported(self): gi = tmp_dir / "repo" / ".gitignore" - with open(gi, 'w') as file: - file.write('ignored_file.txt\n') - file.write('ignored_dir/\n') + with open(gi, "w") as file: + file.write("ignored_file.txt\n") + file.write("ignored_dir/\n") - assert temp_repo.ignored(['included_file.txt', 'included_dir/file.txt']) == [] - assert temp_repo.ignored(['ignored_file.txt']) == ['ignored_file.txt'] - assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt']) == ['ignored_file.txt'] - assert temp_repo.ignored(['included_file.txt', 'ignored_file.txt', 'included_dir/file.txt', 'ignored_dir/file.txt']) == ['ignored_file.txt', 'ignored_dir/file.txt'] + assert temp_repo.ignored(["included_file.txt", "included_dir/file.txt"]) == [] + assert temp_repo.ignored(["ignored_file.txt"]) == ["ignored_file.txt"] + assert temp_repo.ignored(["included_file.txt", "ignored_file.txt"]) == ["ignored_file.txt"] + assert temp_repo.ignored( + ["included_file.txt", "ignored_file.txt", "included_dir/file.txt", "ignored_dir/file.txt"] + ) == ["ignored_file.txt", "ignored_dir/file.txt"] def test_ignored_raises_error_w_symlink(self): with tempfile.TemporaryDirectory() as tdir: diff --git a/test/test_submodule.py b/test/test_submodule.py index f7195626f..88717e220 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -39,11 +39,14 @@ def _patch_git_config(name, value): # This is recomputed each time the context is entered, for compatibility with # existing GIT_CONFIG_* environment variables, even if changed in this process. - patcher = mock.patch.dict(os.environ, { - "GIT_CONFIG_COUNT": str(pair_index + 1), - f"GIT_CONFIG_KEY_{pair_index}": name, - f"GIT_CONFIG_VALUE_{pair_index}": value, - }) + patcher = mock.patch.dict( + os.environ, + { + "GIT_CONFIG_COUNT": str(pair_index + 1), + f"GIT_CONFIG_KEY_{pair_index}": name, + f"GIT_CONFIG_VALUE_{pair_index}": value, + }, + ) with patcher: yield @@ -914,17 +917,17 @@ def test_ignore_non_submodule_file(self, rwdir): os.mkdir(smp) with open(osp.join(smp, "a"), "w", encoding="utf-8") as f: - f.write('test\n') + f.write("test\n") with open(osp.join(rwdir, ".gitmodules"), "w", encoding="utf-8") as f: - f.write("[submodule \"a\"]\n") + f.write('[submodule "a"]\n') f.write(" path = module\n") f.write(" url = https://github.com/chaconinc/DbConnector\n") parent.git.add(Git.polish_url(osp.join(smp, "a"))) parent.git.add(Git.polish_url(osp.join(rwdir, ".gitmodules"))) - parent.git.commit(message='test') + parent.git.commit(message="test") assert len(parent.submodules) == 0 @@ -1200,7 +1203,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo): # The options will be allowed, but the command will fail. with self.assertRaises(GitCommandError): Submodule.add( - rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True + rw_repo, + "new", + "new", + str(tmp_dir), + clone_multi_options=[unsafe_option], + allow_unsafe_options=True, ) assert not tmp_file.exists() @@ -1211,7 +1219,12 @@ def test_submodule_add_unsafe_options_allowed(self, rw_repo): for unsafe_option in unsafe_options: with self.assertRaises(GitCommandError): Submodule.add( - rw_repo, "new", "new", str(tmp_dir), clone_multi_options=[unsafe_option], allow_unsafe_options=True + rw_repo, + "new", + "new", + str(tmp_dir), + clone_multi_options=[unsafe_option], + allow_unsafe_options=True, ) @with_rw_repo("HEAD")