-
Notifications
You must be signed in to change notification settings - Fork 2
/
repo_to_md.py
302 lines (248 loc) · 9.83 KB
/
repo_to_md.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Stuffs all the human-readable files in a single markdown file. Maybe use it with an LLM 🤭.
Example:
poetry run python repo_to_md.py --tree -v --include .ipynb .py
Will create a markdown file named mastodon_sim.md with all the .ipynb and .py files
in the repo, and print the tree of processed files.
"""
import argparse
import mimetypes
import os
import subprocess
import sys
from typing import TextIO
import tiktoken
from pathspec import PathSpec
from pathspec.patterns import GitWildMatchPattern
# Ensure .md files are recognized as text/markdown
mimetypes.add_type("text/markdown", ".md")
def is_human_readable(file_path: str) -> bool:
"""
Check if a file is human-readable based on its MIME type or extension.
Args:
file_path (str): The path to the file to check.
Returns
-------
bool: True if the file is human-readable, False otherwise.
"""
_, ext = os.path.splitext(file_path)
if ext.lower() in {".txt", ".py", ".ipynb", ".md"}:
return True
mime_type, _ = mimetypes.guess_type(file_path)
return mime_type is not None and mime_type.startswith("text")
def get_git_root() -> str:
"""
Get the root directory of the git repository.
Returns
-------
str: The path to the root of the git repository.
"""
try:
return subprocess.check_output(
["git", "rev-parse", "--show-toplevel"], universal_newlines=True
).strip()
except subprocess.CalledProcessError:
print("Error: Not a git repository. Please run this script from within a git repository.")
sys.exit(1)
def get_gitignore_spec(repo_root: str) -> PathSpec:
"""
Create a PathSpec object from the .gitignore file.
Args:
repo_root (str): The root directory of the git repository.
Returns
-------
PathSpec: A PathSpec object representing the .gitignore rules.
"""
gitignore_path = os.path.join(repo_root, ".gitignore")
if os.path.exists(gitignore_path):
with open(gitignore_path) as gitignore_file:
gitignore_content = gitignore_file.read()
return PathSpec.from_lines(GitWildMatchPattern, gitignore_content.splitlines())
return PathSpec([])
def get_human_readable_extensions(directory: str, gitignore_spec: PathSpec) -> set[str]:
"""
Get all human-readable file extensions in the repository, respecting .gitignore.
Args:
directory (str): The directory to process.
gitignore_spec (PathSpec): A PathSpec object representing the .gitignore rules.
Returns
-------
Set[str]: Set of human-readable file extensions.
"""
extensions = set()
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory)
if not gitignore_spec.match_file(relative_path) and is_human_readable(file_path):
_, ext = os.path.splitext(file)
if ext:
extensions.add(ext.lower())
return extensions
def build_file_tree(processed_files: list[str]) -> dict:
"""
Build a tree structure of the processed files.
Args:
processed_files (List[str]): List of processed file paths.
Returns
-------
Dict: A tree structure representing the processed files.
"""
tree: dict = {}
for file_path in processed_files:
parts = file_path.split(os.sep)
current = tree
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = None
return tree
def print_file_tree(tree: dict, prefix: str = "") -> None:
"""
Print the file tree structure.
Args:
tree (Dict): The tree structure to print.
prefix (str): The prefix to use for indentation.
"""
items = list(tree.items())
for i, (name, subtree) in enumerate(items):
if i == len(items) - 1:
print(f"{prefix}└── {name}")
if subtree is not None:
print_file_tree(subtree, prefix + " ")
else:
print(f"{prefix}├── {name}")
if subtree is not None:
print_file_tree(subtree, prefix + "│ ")
def process_directory(
directory: str,
output_file: TextIO,
tokenizer: tiktoken.Encoding,
allowed_extensions: set[str],
gitignore_spec: PathSpec,
) -> tuple[int, set[str], list[str]]:
"""
Recursively process a directory and write the content of human-readable files to the output file.
Args:
directory (str): The directory to process.
output_file (TextIO): The file object to write the output to.
tokenizer (tiktoken.Encoding): The tokenizer to use for counting tokens.
allowed_extensions (Set[str]): Set of allowed file extensions.
gitignore_spec (PathSpec): A PathSpec object representing the .gitignore rules.
Returns
-------
Tuple[int, Set[str], List[str]]: Total number of tokens written, set of processed extensions, and list of processed files.
"""
total_tokens = 0
processed_extensions = set()
processed_files = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory)
_, ext = os.path.splitext(file)
ext = ext.lower()
# Skip files that are not in allowed_extensions or are ignored by .gitignore
if ext not in allowed_extensions or gitignore_spec.match_file(relative_path):
continue
# Skip the output file itself and hidden files/directories
if (
is_human_readable(file_path)
and os.path.basename(output_file.name) != file
and not file.startswith(".")
and not any(part.startswith(".") for part in relative_path.split(os.sep))
):
with open(file_path, encoding="utf-8") as f:
content = f.read()
# Write the file name and content to the output file
header = f"## {relative_path}\n\n"
footer = "\n```\n\n"
full_content = header + "```\n" + content + footer
output_file.write(full_content)
# Count tokens
tokens = tokenizer.encode(full_content)
total_tokens += len(tokens)
processed_extensions.add(ext)
processed_files.append(relative_path)
return total_tokens, processed_extensions, processed_files
def parse_arguments() -> argparse.Namespace:
"""
Parse command-line arguments.
Returns
-------
argparse.Namespace: Parsed arguments.
"""
parser = argparse.ArgumentParser(
description="Aggregate human-readable files from a git repository into a single markdown file."
)
parser.add_argument(
"-o",
"--output",
default="mastodon_sim.md",
help="Output file name (default: mastodon_sim.md)",
)
parser.add_argument(
"--include",
nargs="+",
default=[
".py",
".ipynb",
],
help="File extensions to include (default: .py)",
)
parser.add_argument(
"--list-extensions",
action="store_true",
help="List all human-readable file extensions in the repository and exit",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output")
parser.add_argument("--tree", action="store_true", help="Print the tree of processed files")
return parser.parse_args()
def repo_to_md() -> None:
"""Aggregate all human-readable files in the repo into a single markdown file."""
args = parse_arguments()
# Get the root directory of the git repository
repo_root = get_git_root()
# Get the .gitignore rules
gitignore_spec = get_gitignore_spec(repo_root)
if args.list_extensions:
extensions = get_human_readable_extensions(repo_root, gitignore_spec)
print("Human-readable file extensions in the repository (respecting .gitignore):")
for ext in sorted(extensions):
print(ext)
return
output_path = os.path.join(repo_root, args.output)
# Load the tokenizer
tokenizer = tiktoken.get_encoding("cl100k_base")
# Determine allowed extensions
all_extensions = get_human_readable_extensions(repo_root, gitignore_spec)
print(f"args.include: {args.include}")
allowed_extensions = {
ext.lower() if ext.startswith(".") else f".{ext.lower()}" for ext in args.include
}
allowed_extensions &= all_extensions # Only include extensions that actually exist in the repo
if args.verbose:
print(
f"All detected extensions (respecting .gitignore): {', '.join(sorted(all_extensions))}"
)
print(f"Allowed extensions: {', '.join(sorted(allowed_extensions))}")
# Open the output file and process the directory
with open(output_path, "w", encoding="utf-8") as output_file:
total_tokens, processed_extensions, processed_files = process_directory(
repo_root, output_file, tokenizer, allowed_extensions, gitignore_spec
)
print(
f"All human-readable files from the git repository have been aggregated into '{output_path}'."
)
print(f"Total tokens (tiktoken cl100k_base): {total_tokens}")
print(f"Processed file types: {', '.join(sorted(processed_extensions))}")
if args.verbose:
print(
f"Allowed but not processed: {', '.join(sorted(allowed_extensions - processed_extensions))}"
)
if args.tree:
print("\nProcessed files tree:")
file_tree = build_file_tree(processed_files)
print_file_tree(file_tree)
if __name__ == "__main__":
repo_to_md()