Spaces:
Sleeping
Sleeping
first232hjhkhkdsafdsf
Browse files
app.py
CHANGED
|
@@ -4,53 +4,95 @@ import tempfile
|
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
from urllib.parse import urlparse
|
| 7 |
-
import re
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
username, repo = repo_url.split('/')
|
| 14 |
-
return username, repo.replace('.git', '')
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
parsed = urlparse(repo_url)
|
| 20 |
-
path_parts = parsed.path.strip('/').split('/')
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
repo = path_parts[1].replace('.git', '')
|
| 26 |
-
return username, repo
|
| 27 |
except:
|
| 28 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Create the Gradio interface
|
| 47 |
-
with gr.Blocks(title="
|
| 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():
|
|
@@ -93,8 +135,7 @@ with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo:
|
|
| 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",
|
|
@@ -104,39 +145,11 @@ with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo:
|
|
| 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=
|
| 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()
|
|
|
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
from urllib.parse import urlparse
|
|
|
|
| 7 |
|
| 8 |
+
def validate_repo_url(url):
|
| 9 |
+
"""Validate if the input is a valid repository URL or GitHub shorthand."""
|
| 10 |
+
if not url:
|
| 11 |
+
return False
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Check if it's a GitHub shorthand (user/repo)
|
| 14 |
+
if '/' in url and len(url.split('/')) == 2:
|
| 15 |
+
return True
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
try:
|
| 18 |
+
result = urlparse(url)
|
| 19 |
+
return all([result.scheme, result.netloc])
|
|
|
|
|
|
|
| 20 |
except:
|
| 21 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
|
| 24 |
+
"""Pack a repository using Repomix and return the output."""
|
| 25 |
+
if not repo_url:
|
| 26 |
+
return "Error: Please provide a repository URL", None
|
| 27 |
+
|
| 28 |
+
if not validate_repo_url(repo_url):
|
| 29 |
+
return "Error: Invalid repository URL format", None
|
| 30 |
+
|
| 31 |
+
temp_dir = None
|
| 32 |
+
try:
|
| 33 |
+
# Create temporary directory that persists until we're done
|
| 34 |
+
temp_dir = tempfile.mkdtemp()
|
| 35 |
+
|
| 36 |
+
# Prepare command
|
| 37 |
+
cmd = ["npx", "repomix"]
|
| 38 |
+
|
| 39 |
+
# Add remote repository options
|
| 40 |
+
cmd.extend(["--remote", repo_url])
|
| 41 |
+
|
| 42 |
+
if branch:
|
| 43 |
+
cmd.extend(["--remote-branch", branch])
|
| 44 |
+
|
| 45 |
+
# Add style option
|
| 46 |
+
cmd.extend(["--style", output_style])
|
| 47 |
+
|
| 48 |
+
# Add other options
|
| 49 |
+
if remove_comments:
|
| 50 |
+
cmd.append("--remove-comments")
|
| 51 |
+
if remove_empty_lines:
|
| 52 |
+
cmd.append("--remove-empty-lines")
|
| 53 |
+
if not security_check:
|
| 54 |
+
cmd.append("--no-security-check")
|
| 55 |
+
|
| 56 |
+
# Set output path
|
| 57 |
+
output_file = os.path.join(temp_dir, "repomix-output.txt")
|
| 58 |
+
cmd.extend(["-o", output_file])
|
| 59 |
+
|
| 60 |
+
# Execute Repomix
|
| 61 |
+
result = subprocess.run(
|
| 62 |
+
cmd,
|
| 63 |
+
capture_output=True,
|
| 64 |
+
text=True,
|
| 65 |
+
cwd=temp_dir
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if result.returncode != 0:
|
| 69 |
+
return f"Error running Repomix: {result.stderr}", None
|
| 70 |
+
|
| 71 |
+
# Check if the file exists
|
| 72 |
+
if not os.path.exists(output_file):
|
| 73 |
+
return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}", None
|
| 74 |
+
|
| 75 |
+
# Read the output file
|
| 76 |
+
try:
|
| 77 |
+
with open(output_file, 'r', encoding='utf-8') as f:
|
| 78 |
+
content = f.read()
|
| 79 |
+
return content, content
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error reading output file: {str(e)}", None
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"Error: {str(e)}", None
|
| 85 |
+
|
| 86 |
+
finally:
|
| 87 |
+
# Clean up temporary directory
|
| 88 |
+
if temp_dir and os.path.exists(temp_dir):
|
| 89 |
+
try:
|
| 90 |
+
shutil.rmtree(temp_dir)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Warning: Could not remove temporary directory: {str(e)}")
|
| 93 |
|
| 94 |
# Create the Gradio interface
|
| 95 |
+
with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
|
|
|
| 135 |
lines=20,
|
| 136 |
show_copy_button=True
|
| 137 |
)
|
| 138 |
+
|
|
|
|
| 139 |
with gr.Row():
|
| 140 |
download_button = gr.File(
|
| 141 |
label="Download Output",
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
# Handle the pack button click
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
pack_button.click(
|
| 149 |
+
fn=pack_repository,
|
| 150 |
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check],
|
| 151 |
outputs=[output_text, download_button]
|
| 152 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
demo.launch()
|