Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

git_utils.py 1.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  1. from typing import List
  2. try:
  3. import git
  4. except ImportError:
  5. raise ImportError("The 'git' library is required but not found. Please install the `gitpython` with version as specified in `requirements.dev.txt`.")
  6. class GitHelper:
  7. """A helper class to interact with a (local) Git repository."""
  8. def __init__(self, git_path: str):
  9. """
  10. :param git_path: Path to the Git repository.
  11. """
  12. self.repo = git.Repo(git_path)
  13. def diff_files(self, source_branch: str, current_branch: str) -> List[str]:
  14. """Get the differences in files between the source branch and the current branch, only considering '.py' files.
  15. :param source_branch: The source branch for comparison.
  16. :param current_branch: The current branch for comparison.
  17. :return: List of file paths that have changed between the two branches, considering only '.py' files.
  18. """
  19. source_commit = self.repo.commit(source_branch)
  20. current_commit = self.repo.commit(current_branch)
  21. return [diff.a_path for diff in source_commit.diff(current_commit) if ".py" in diff.a_path]
  22. def load_branch_file(self, branch: str, file_path: str) -> str:
  23. """Load the contents of a file from a specific branch. Return an empty string if the file does not exist.
  24. :param branch: The branch from which to load the file.
  25. :param file_path: The path of the file within the repository.
  26. :return: The content of the file as a string or an empty string if the file does not exist.
  27. """
  28. tree = self.repo.commit(branch).tree
  29. try: # It looks like there is no simple way to check if a file exists in the tree... So we directly check with try/except
  30. return tree[file_path].data_stream.read()
  31. except KeyError:
  32. return ""
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...