-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_read.py
More file actions
69 lines (57 loc) · 1.86 KB
/
git_read.py
File metadata and controls
69 lines (57 loc) · 1.86 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from git import Repo
import os
import tempfile
# load/Clone repo from Github to a temp file
def load_repo(path_or_url):
"""
Load or clone a Git repository from a local path or a remote URL.
"""
try:
if path_or_url.startswith("http"):
temp_dir = tempfile.mkdtemp()
print(f"Cloning repo to temp dir: {temp_dir}")
repo = Repo.clone_from(path_or_url, temp_dir)
else:
repo = Repo(path_or_url)
return repo
except Exception as e:
print(f"Error loading repository: {e}")
return None
# List all file paths in the repository
def list_file_paths(repo):
"""
List all file paths in the Git repository.
Args:
repo (Repo): The GitPython Repo object.
Returns:
list: A list of file paths in the repository.
"""
try:
return [item.path for item in repo.tree().traverse()]
except Exception as e:
print(f"Error listing file paths: {e}")
return []
import os
# Read file content
def read_file(repo, file_path):
try:
full_path = os.path.join(repo.working_tree_dir, file_path)
# Skip folders and binary files
if os.path.isdir(full_path) or file_path.endswith('.pyc'):
return None
# Try reading with UTF-8 first, then fallback
with open(full_path, 'r', encoding='utf-8') as f:
return f.read()
except UnicodeDecodeError:
try:
with open(full_path, 'r', encoding='latin-1') as f:
return f.read()
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return None
except PermissionError:
print(f"Permission denied: {file_path}")
return None
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return None