ucalyptus commited on
Commit
7762d1f
Β·
1 Parent(s): 463e7fa

first232hjhkhk

Browse files
Files changed (1) hide show
  1. app.py +80 -86
app.py CHANGED
@@ -4,96 +4,53 @@ import tempfile
4
  import os
5
  import shutil
6
  from urllib.parse import urlparse
 
7
 
8
- # [Previous helper functions remain the same]
9
- def validate_repo_url(url):
10
- """Validate if the input is a valid repository URL or GitHub shorthand."""
11
- if not url:
12
- return False
 
13
 
14
- # Check if it's a GitHub shorthand (user/repo)
15
- if '/' in url and len(url.split('/')) == 2:
16
- return True
17
-
18
  try:
19
- result = urlparse(url)
20
- return all([result.scheme, result.netloc])
 
 
 
 
 
 
 
21
  except:
22
- return False
 
 
23
 
24
- def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
25
- """Pack a repository using Repomix and return the output."""
26
- if not repo_url:
27
- return "Error: Please provide a repository URL"
28
-
29
- if not validate_repo_url(repo_url):
30
- return "Error: Invalid repository URL format"
31
-
32
- temp_dir = None
33
- try:
34
- # Create temporary directory that persists until we're done
35
- temp_dir = tempfile.mkdtemp()
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="Repo to TXT", theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
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 # Enable built-in copy button
 
 
 
 
 
 
 
 
 
138
  )
139
 
140
  # Handle the pack button click
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  pack_button.click(
142
- fn=pack_repository,
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()