Skip to content

Commit d3f8859

Browse files
committed
utils.git: Add single_use param to .are_valid_objects() and .filter_valid_commits()
This is meant to help the problem of OSError: [Errno 24] Too many open files in the common case when e.g. .filter_valid_commits() is used once per GitRepo.
1 parent b857131 commit d3f8859

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/diffannotator/utils/git.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,8 @@ def is_valid_commit(self, commit: str) -> bool:
16501650
return self.to_oid(str(commit) + '^{commit}') is not None
16511651

16521652
def are_valid_objects(self, objects: Iterable[str],
1653-
object_type: Optional[str] = "commit") -> list[None|bool]:
1653+
object_type: Optional[str] = "commit",
1654+
single_use: bool = False) -> list[None|bool]:
16541655
"""Check which of given `objects` are present in the repository
16551656
16561657
You can ensure that `objects` not only exist but are of a specific
@@ -1667,7 +1668,11 @@ def are_valid_objects(self, objects: Iterable[str],
16671668
One of "commit", "tree", "blob", "tag", or None.
16681669
If not None, the type is used to restrict the type of object:
16691670
only objects of given `object_type` are considered valid,
1670-
which means that the object must exist and be of specified type.
1671+
which means that the object must exist and be of a specified type.
1672+
1673+
single_use
1674+
If True, do not keep the connection to `git cat-file --batch-command`
1675+
open, but close it automatically.
16711676
16721677
Returns
16731678
-------
@@ -1702,10 +1707,15 @@ def are_valid_objects(self, objects: Iterable[str],
17021707
else:
17031708
results.append(True)
17041709

1710+
if single_use:
1711+
self.close_batch_command()
1712+
17051713
return results
17061714

1707-
def filter_valid_commits(self, commits: Iterable[str], to_oid: bool = False) -> Iterable[str]:
1708-
"""Filter out invalid commits from given list of commits
1715+
def filter_valid_commits(self, commits: Iterable[str],
1716+
to_oid: bool = False,
1717+
single_use: bool = False) -> Iterable[str]:
1718+
"""Filter out invalid commits from the given list of commits
17091719
17101720
Commit is considered invalid if it does not exist in the repository,
17111721
or is not a commit.
@@ -1717,6 +1727,9 @@ def filter_valid_commits(self, commits: Iterable[str], to_oid: bool = False) ->
17171727
to_oid
17181728
Whether to convert elements in `commits` to SHA-1 object identifiers,
17191729
for example, "HEAD" to "3a27ee24b37a3e9572a0acc0aaecd22cc9c10bc7"
1730+
single_use
1731+
If True, do not keep the connection to `git cat-file --batch-command`
1732+
open, but close it automatically.
17201733
17211734
Yields
17221735
------
@@ -1745,6 +1758,9 @@ def filter_valid_commits(self, commits: Iterable[str], to_oid: bool = False) ->
17451758
if len(info) == 3 and info[1] == 'commit':
17461759
yield info[0] if to_oid else commit_id
17471760

1761+
if single_use:
1762+
self.close_batch_command()
1763+
17481764
def get_current_branch(self) -> Union[str, None]:
17491765
"""Return short name of the current branch
17501766

0 commit comments

Comments
 (0)