File size: 5,015 Bytes
c4a5b3f
 
 
 
 
 
 
2730a68
 
 
 
 
 
 
 
c4a5b3f
2730a68
7762d1f
a0d7ecf
 
 
 
 
 
 
 
 
 
 
 
52e0e86
2730a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52e0e86
b63da50
52e0e86
2730a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0d7ecf
 
 
 
2730a68
 
 
 
 
 
 
 
 
 
 
 
c4a5b3f
2730a68
c4a5b3f
 
 
 
 
 
 
 
 
 
52e0e86
 
 
 
 
c4a5b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463e7fa
 
 
 
 
7762d1f
 
2730a68
7762d1f
 
 
 
a0d7ecf
7762d1f
463e7fa
c4a5b3f
 
2730a68
52e0e86
7762d1f
c4a5b3f
 
 
 
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
import gradio as gr
import subprocess
import tempfile
import os
import shutil
from urllib.parse import urlparse

def validate_repo_url(url):
    if not url:
        return False
    if '/' in url and len(url.split('/')) == 2:
        return True
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc])
    except:
        return False

def create_download_file(content):
    if not content:
        return None
    try:
        temp_file = tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt')
        temp_file.write(content)
        temp_file.close()
        return temp_file.name
    except Exception as e:
        print(f"Error creating download file: {str(e)}")
        return None

def pack_repository(repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check):
    if not repo_url:
        return "Error: Please provide a repository URL", None
        
    if not validate_repo_url(repo_url):
        return "Error: Invalid repository URL format", None
        
    temp_dir = None
    try:
        temp_dir = tempfile.mkdtemp()
        cmd = ["npx", "repomix"]
        cmd.extend(["--remote", repo_url])
        
        if branch:
            cmd.extend(["--remote-branch", branch])
            
        if gh_token:
            cmd.extend(["--token", gh_token])
            
        cmd.extend(["--style", output_style])
        
        if remove_comments:
            cmd.append("--remove-comments")
        if remove_empty_lines:
            cmd.append("--remove-empty-lines")
        if not security_check:
            cmd.append("--no-security-check")
            
        output_file = os.path.join(temp_dir, "repomix-output.txt")
        cmd.extend(["-o", output_file])
        
        result = subprocess.run(
            cmd, 
            capture_output=True, 
            text=True,
            cwd=temp_dir
        )
        
        if result.returncode != 0:
            return f"Error running Repomix: {result.stderr}", None
            
        if not os.path.exists(output_file):
            return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}", None
            
        try:
            with open(output_file, 'r', encoding='utf-8') as f:
                content = f.read()
            
            download_path = create_download_file(content)
            return content, download_path
            
        except Exception as e:
            return f"Error reading output file: {str(e)}", None
            
    except Exception as e:
        return f"Error: {str(e)}", None
        
    finally:
        if temp_dir and os.path.exists(temp_dir):
            try:
                shutil.rmtree(temp_dir)
            except Exception as e:
                print(f"Warning: Could not remove temporary directory: {str(e)}")

with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
    with gr.Row():
        with gr.Column():
            repo_url = gr.Textbox(
                label="Repository URL or GitHub Shorthand",
                placeholder="e.g., https://github.com/user/repo or user/repo"
            )
            branch = gr.Textbox(
                label="Branch/Tag/Commit (optional)",
                placeholder="e.g., main, master, v1.0.0"
            )
            gh_token = gr.Textbox(
                label="GitHub Token (for private repos)",
                placeholder="ghp_xxxxxxxxxxxxxx",
                type="password"
            )
            
            with gr.Row():
                output_style = gr.Radio(
                    choices=["plain", "xml", "markdown"],
                    value="plain",
                    label="Output Style"
                )
            
            with gr.Row():
                remove_comments = gr.Checkbox(
                    label="Remove Comments",
                    value=False
                )
                remove_empty_lines = gr.Checkbox(
                    label="Remove Empty Lines",
                    value=False
                )
                security_check = gr.Checkbox(
                    label="Enable Security Check",
                    value=True
                )
            
            pack_button = gr.Button("Pack Repository", variant="primary")
    
    with gr.Row():
        output_text = gr.TextArea(
            label="Output",
            placeholder="Packed repository content will appear here...",
            lines=20,
            show_copy_button=True
        )
        
    with gr.Row():
        download_button = gr.File(
            label="Download Output",
            file_count="single",
            type="filepath",
            interactive=False
        )
    
    pack_button.click(
        fn=pack_repository,
        inputs=[repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check],
        outputs=[output_text, download_button]
    )

if __name__ == "__main__":
    demo.launch()