Skip to content

Commit f22d5f2

Browse files
committed
Introduce commit file search
1 parent aaac9f5 commit f22d5f2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/git_autograder/commit.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import Any, List, Optional, Sequence, Union
1+
import os
2+
from contextlib import contextmanager
3+
from typing import Any, Iterator, List, Optional, Sequence, TextIO, Union
24

35
from git import Commit, Stats
46

@@ -54,3 +56,14 @@ def file_change_type(self, file_name: str) -> Optional[str]:
5456
if file_name not in self.stats.files:
5557
return None
5658
return self.stats.files[file_name]["change_type"]
59+
60+
@contextmanager
61+
def file(
62+
self, file_path: Union[str, os.PathLike[str]]
63+
) -> Iterator[Optional[TextIO]]:
64+
try:
65+
rel_path = os.fspath(file_path)
66+
file_blob = self.commit.tree / rel_path
67+
yield file_blob.data_stream.read().decode()
68+
except Exception:
69+
yield None

0 commit comments

Comments
 (0)