Skip to content

Commit b6fa83e

Browse files
committed
perf: small optimizations to get_sha()
In porting #180 to rust (cpp-linter/cpp-linter-rs#260), I found a few spots for improvement. These changes should be more performant when there are staged changes.
1 parent 9a5897a commit b6fa83e

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.py text eol=lf
77
*.rst text eol=lf
88
*.sh text eol=lf
9+
*.json text eol=lf
910
*.cpp text eol=lf
1011
*.hpp text eol=lf
1112
*.yml text eol=lf

cpp_linter/git/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def get_sha(repo: Repository, parent: None | int | str = None) -> GitObject:
4848

4949

5050
STAGED_STATUS = (
51-
GIT_STATUS_INDEX_NEW,
52-
GIT_STATUS_INDEX_MODIFIED,
53-
GIT_STATUS_INDEX_RENAMED,
51+
GIT_STATUS_INDEX_NEW | GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_INDEX_RENAMED
5452
)
5553

5654

@@ -66,15 +64,12 @@ def get_diff(parents: None | int | str = None, ignore_index: bool = False) -> Di
6664
:returns: A `pygit2.Diff` object representing the fetched diff.
6765
"""
6866
repo = Repository(".")
69-
head = get_sha(repo).peel(Commit)
70-
71-
has_staged_files = False
72-
for _, status in repo.status().items():
73-
if status in STAGED_STATUS:
74-
has_staged_files = True
75-
break
7667

77-
use_index = not ignore_index and has_staged_files
68+
use_index = (
69+
False
70+
if ignore_index
71+
else any(status & STAGED_STATUS for status in repo.status().values())
72+
)
7873

7974
if not use_index and parents is None:
8075
parents = 1
@@ -86,6 +81,7 @@ def get_diff(parents: None | int | str = None, ignore_index: bool = False) -> Di
8681
diff_obj = index.diff_to_tree(base.tree)
8782
diff_name = f"HEAD...{base.short_id}"
8883
else:
84+
head = get_sha(repo).peel(Commit)
8985
diff_obj = repo.diff(base, head)
9086
diff_name = f"{head.short_id}...{base.short_id}"
9187

@@ -112,7 +108,7 @@ def parse_diff(
112108
:param lines_changed_only: A value that dictates what file changes to focus on.
113109
:returns: A `list` of `FileObj` describing information about the files changed.
114110
115-
.. note:: Deleted files are omitted because we only want to analyze updates.
111+
.. note:: Deleted files are omitted because we only want to analyze additions.
116112
"""
117113
file_objects: list[FileObj] = []
118114
if isinstance(diff_obj, str):

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import time
1010
from typing import cast
1111
from importlib.metadata import version as get_version
12-
import docutils
12+
import docutils # type: ignore[import-untyped]
1313
from sphinx.application import Sphinx
1414
from sphinx.util.docutils import SphinxRole
1515
from sphinx_immaterial.inline_icons import load_svg_into_builder_env

tests/capture_tools_output/test_tools_output.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def prep_tmp_dir(
188188
],
189189
ids=["none", "HEAD", 2],
190190
)
191+
@pytest.mark.no_clang
191192
def test_get_sha(
192193
tmp_path: Path,
193194
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)