Spaces:
Sleeping
Sleeping
first232hjhkhk
Browse files
app.py
CHANGED
@@ -4,96 +4,53 @@ import tempfile
|
|
4 |
import os
|
5 |
import shutil
|
6 |
from urllib.parse import urlparse
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
if
|
12 |
-
|
|
|
13 |
|
14 |
-
#
|
15 |
-
if '/' in url and len(url.split('/')) == 2:
|
16 |
-
return True
|
17 |
-
|
18 |
try:
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
except:
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
def
|
25 |
-
"""
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# Prepare command
|
38 |
-
cmd = ["npx", "repomix"]
|
39 |
-
|
40 |
-
# Add remote repository options
|
41 |
-
cmd.extend(["--remote", repo_url])
|
42 |
-
|
43 |
-
if branch:
|
44 |
-
cmd.extend(["--remote-branch", branch])
|
45 |
-
|
46 |
-
# Add style option
|
47 |
-
cmd.extend(["--style", output_style])
|
48 |
-
|
49 |
-
# Add other options
|
50 |
-
if remove_comments:
|
51 |
-
cmd.append("--remove-comments")
|
52 |
-
if remove_empty_lines:
|
53 |
-
cmd.append("--remove-empty-lines")
|
54 |
-
if not security_check:
|
55 |
-
cmd.append("--no-security-check")
|
56 |
-
|
57 |
-
# Set output path
|
58 |
-
output_file = os.path.join(temp_dir, "repomix-output.txt")
|
59 |
-
cmd.extend(["-o", output_file])
|
60 |
-
|
61 |
-
# Execute Repomix
|
62 |
-
result = subprocess.run(
|
63 |
-
cmd,
|
64 |
-
capture_output=True,
|
65 |
-
text=True,
|
66 |
-
cwd=temp_dir
|
67 |
-
)
|
68 |
-
|
69 |
-
if result.returncode != 0:
|
70 |
-
return f"Error running Repomix: {result.stderr}"
|
71 |
-
|
72 |
-
# Check if the file exists
|
73 |
-
if not os.path.exists(output_file):
|
74 |
-
return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}"
|
75 |
-
|
76 |
-
# Read the output file
|
77 |
-
try:
|
78 |
-
with open(output_file, 'r', encoding='utf-8') as f:
|
79 |
-
content = f.read()
|
80 |
-
return content
|
81 |
-
except Exception as e:
|
82 |
-
return f"Error reading output file: {str(e)}"
|
83 |
-
|
84 |
-
except Exception as e:
|
85 |
-
return f"Error: {str(e)}"
|
86 |
-
|
87 |
-
finally:
|
88 |
-
# Clean up temporary directory
|
89 |
-
if temp_dir and os.path.exists(temp_dir):
|
90 |
-
try:
|
91 |
-
shutil.rmtree(temp_dir)
|
92 |
-
except Exception as e:
|
93 |
-
print(f"Warning: Could not remove temporary directory: {str(e)}")
|
94 |
|
95 |
# Create the Gradio interface
|
96 |
-
with gr.Blocks(title="
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
with gr.Row():
|
99 |
with gr.Column():
|
@@ -134,15 +91,52 @@ with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
|
134 |
label="Output",
|
135 |
placeholder="Packed repository content will appear here...",
|
136 |
lines=20,
|
137 |
-
show_copy_button=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
)
|
139 |
|
140 |
# Handle the pack button click
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
pack_button.click(
|
142 |
-
fn=
|
143 |
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check],
|
144 |
-
outputs=output_text
|
145 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
demo.launch()
|
|
|
4 |
import os
|
5 |
import shutil
|
6 |
from urllib.parse import urlparse
|
7 |
+
import re
|
8 |
|
9 |
+
def get_repo_details(repo_url):
|
10 |
+
"""Extract username and repo name from GitHub URL or shorthand."""
|
11 |
+
# Handle GitHub shorthand (user/repo)
|
12 |
+
if '/' in repo_url and len(repo_url.split('/')) == 2:
|
13 |
+
username, repo = repo_url.split('/')
|
14 |
+
return username, repo.replace('.git', '')
|
15 |
|
16 |
+
# Handle full GitHub URL
|
|
|
|
|
|
|
17 |
try:
|
18 |
+
# Parse URL and path
|
19 |
+
parsed = urlparse(repo_url)
|
20 |
+
path_parts = parsed.path.strip('/').split('/')
|
21 |
+
|
22 |
+
# Extract username and repo
|
23 |
+
if len(path_parts) >= 2:
|
24 |
+
username = path_parts[0]
|
25 |
+
repo = path_parts[1].replace('.git', '')
|
26 |
+
return username, repo
|
27 |
except:
|
28 |
+
pass
|
29 |
+
|
30 |
+
return None, None
|
31 |
|
32 |
+
def format_filename(repo_url, branch):
|
33 |
+
"""Generate filename based on repository details."""
|
34 |
+
username, repo_name = get_repo_details(repo_url)
|
35 |
+
if not username or not repo_name:
|
36 |
+
return "repository-output.txt"
|
37 |
+
|
38 |
+
# Clean and format branch name
|
39 |
+
branch = branch if branch else "main"
|
40 |
+
branch = re.sub(r'[^\w\-\.]', '-', branch)
|
41 |
+
|
42 |
+
return f"{username}-{repo_name}-{branch}.txt"
|
43 |
+
|
44 |
+
[Previous helper functions remain the same]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Create the Gradio interface
|
47 |
+
with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo:
|
48 |
+
gr.Markdown("""
|
49 |
+
# π¦ Repomix Web Interface
|
50 |
+
Pack your GitHub repository into a single, AI-friendly file. Perfect for use with LLMs like Claude, ChatGPT, and Gemini.
|
51 |
+
|
52 |
+
Enter a GitHub repository URL (e.g., `https://github.com/user/repo`) or shorthand format (e.g., `user/repo`).
|
53 |
+
""")
|
54 |
|
55 |
with gr.Row():
|
56 |
with gr.Column():
|
|
|
91 |
label="Output",
|
92 |
placeholder="Packed repository content will appear here...",
|
93 |
lines=20,
|
94 |
+
show_copy_button=True
|
95 |
+
)
|
96 |
+
|
97 |
+
# Add download button
|
98 |
+
with gr.Row():
|
99 |
+
download_button = gr.File(
|
100 |
+
label="Download Output",
|
101 |
+
file_count="single",
|
102 |
+
type="file",
|
103 |
+
interactive=False
|
104 |
)
|
105 |
|
106 |
# Handle the pack button click
|
107 |
+
def on_pack(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
|
108 |
+
content = pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check)
|
109 |
+
|
110 |
+
# Generate file for download if content is not an error message
|
111 |
+
if not content.startswith("Error"):
|
112 |
+
filename = format_filename(repo_url, branch)
|
113 |
+
temp_path = tempfile.mktemp(suffix='.txt')
|
114 |
+
|
115 |
+
with open(temp_path, 'w', encoding='utf-8') as f:
|
116 |
+
f.write(content)
|
117 |
+
|
118 |
+
return content, temp_path
|
119 |
+
|
120 |
+
return content, None
|
121 |
+
|
122 |
pack_button.click(
|
123 |
+
fn=on_pack,
|
124 |
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check],
|
125 |
+
outputs=[output_text, download_button]
|
126 |
)
|
127 |
+
|
128 |
+
gr.Markdown("""
|
129 |
+
### π Notes
|
130 |
+
- The packed output is optimized for use with AI models
|
131 |
+
- Security check helps identify potentially sensitive information
|
132 |
+
- Different output styles (plain, XML, markdown) are available for different use cases
|
133 |
+
- Click the copy icon in the output box to copy the content to clipboard
|
134 |
+
- Use the download button to save the output as a text file
|
135 |
+
|
136 |
+
### π Links
|
137 |
+
- [Repomix GitHub Repository](https://github.com/yamadashy/repomix)
|
138 |
+
- [Documentation](https://github.com/yamadashy/repomix#-quick-start)
|
139 |
+
""")
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch()
|