-
Notifications
You must be signed in to change notification settings - Fork 229
/
gpt_repository_loader.py
executable file
·74 lines (61 loc) · 3.01 KB
/
gpt_repository_loader.py
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
69
70
71
72
73
74
#!/usr/bin/env python3
import os
import sys
import fnmatch
def get_ignore_list(ignore_file_path):
ignore_list = []
with open(ignore_file_path, 'r') as ignore_file:
for line in ignore_file:
if sys.platform == "win32":
line = line.replace("/", "\\")
ignore_list.append(line.strip())
return ignore_list
def should_ignore(file_path, ignore_list):
for pattern in ignore_list:
if fnmatch.fnmatch(file_path, pattern):
return True
return False
def process_repository(repo_path, ignore_list, output_file):
for root, _, files in os.walk(repo_path):
for file in files:
file_path = os.path.join(root, file)
relative_file_path = os.path.relpath(file_path, repo_path)
if not should_ignore(relative_file_path, ignore_list):
with open(file_path, 'r', errors='ignore') as file:
contents = file.read()
output_file.write("-" * 4 + "\n")
output_file.write(f"{relative_file_path}\n")
output_file.write(f"{contents}\n")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python git_to_text.py /path/to/git/repository [-p /path/to/preamble.txt] [-o /path/to/output_file.txt]")
sys.exit(1)
repo_path = sys.argv[1]
ignore_file_path = os.path.join(repo_path, ".gptignore")
if sys.platform == "win32":
ignore_file_path = ignore_file_path.replace("/", "\\")
if not os.path.exists(ignore_file_path):
# try and use the .gptignore file in the current directory as a fallback.
HERE = os.path.dirname(os.path.abspath(__file__))
ignore_file_path = os.path.join(HERE, ".gptignore")
preamble_file = None
if "-p" in sys.argv:
preamble_file = sys.argv[sys.argv.index("-p") + 1]
output_file_path = 'output.txt'
if "-o" in sys.argv:
output_file_path = sys.argv[sys.argv.index("-o") + 1]
if os.path.exists(ignore_file_path):
ignore_list = get_ignore_list(ignore_file_path)
else:
ignore_list = []
with open(output_file_path, 'w') as output_file:
if preamble_file:
with open(preamble_file, 'r') as pf:
preamble_text = pf.read()
output_file.write(f"{preamble_text}\n")
else:
output_file.write("The following text is a Git repository with code. The structure of the text are sections that begin with ----, followed by a single line containing the file path and file name, followed by a variable amount of lines containing the file contents. The text representing the Git repository ends when the symbols --END-- are encounted. Any further text beyond --END-- are meant to be interpreted as instructions using the aforementioned Git repository as context.\n")
process_repository(repo_path, ignore_list, output_file)
with open(output_file_path, 'a') as output_file:
output_file.write("--END--")
print(f"Repository contents written to {output_file_path}.")