Codebot / repochat /utils.py
rcs3's picture
update utlis.py
44f31ea verified
Raw History Blame Contribute Delete
1.03 kB
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)