File size: 1,032 Bytes
462062c
 
 
 
 
 
44f31ea
 
 
 
 
 
 
 
 
462062c
 
44f31ea
 
 
 
462062c
 
44f31ea
 
 
 
462062c
44f31ea
 
 
 
 
462062c
44f31ea
 
 
462062c
44f31ea
 
 
 
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
import os
import shutil
import subprocess
import streamlit as st


SESSION_DEFAULTS = {
    "messages":   [],
    "chroma_db":  None,
    "db_loaded":  False,
    "repo_path":  "./cloned_repo",
    "git_form":   False,
    "qa":         None,
    "db_name":    None,
}


def init_session_state():
    for key, default in SESSION_DEFAULTS.items():
        if key not in st.session_state:
            st.session_state[key] = default


def clone_repo(git_url: str, repo_path: str):
    """Clone a public GitHub repo, stripping the .git metadata afterwards."""
    if os.path.exists(repo_path):
        shutil.rmtree(repo_path)

    result = subprocess.run(
        ["git", "clone", "--depth", "1", git_url, repo_path],
        capture_output=True,
        text=True,
    )

    if result.returncode != 0:
        st.error(f"Git clone failed:\n```\n{result.stderr}\n```")
        st.stop()

    # Remove .git to save disk space
    git_dir = os.path.join(repo_path, ".git")
    if os.path.exists(git_dir):
        shutil.rmtree(git_dir)