update git.py
Browse files- repochat/git.py +63 -26
repochat/git.py
CHANGED
|
@@ -1,32 +1,69 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
from .utils import url_name, clone_repo
|
| 5 |
|
| 6 |
def git_form(repo_path):
|
| 7 |
with st.sidebar:
|
| 8 |
-
st.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
st.
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
import requests
|
| 3 |
import streamlit as st
|
| 4 |
+
from .utils import clone_repo
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def parse_github_url(url: str):
|
| 8 |
+
"""
|
| 9 |
+
Accept URLs like:
|
| 10 |
+
https://github.com/owner/repo
|
| 11 |
+
https://github.com/owner/repo.git
|
| 12 |
+
https://github.com/owner/repo/tree/branch
|
| 13 |
+
Returns (owner, repo, clean_clone_url) or None.
|
| 14 |
+
"""
|
| 15 |
+
pattern = r"https?://github\.com/([^/]+)/([^/\s\.]+)"
|
| 16 |
+
match = re.match(pattern, url.strip())
|
| 17 |
+
if not match:
|
| 18 |
+
return None
|
| 19 |
+
owner, repo = match.group(1), match.group(2)
|
| 20 |
+
clean_url = f"https://github.com/{owner}/{repo}.git"
|
| 21 |
+
return owner, repo, clean_url
|
| 22 |
|
|
|
|
| 23 |
|
| 24 |
def git_form(repo_path):
|
| 25 |
with st.sidebar:
|
| 26 |
+
st.markdown("## 🔗 GitHub Repository")
|
| 27 |
+
st.caption("Paste any public GitHub repo URL below.")
|
| 28 |
+
|
| 29 |
+
with st.form("git_form"):
|
| 30 |
+
git_url = st.text_input(
|
| 31 |
+
"Repository URL",
|
| 32 |
+
placeholder="https://github.com/owner/repo",
|
| 33 |
+
)
|
| 34 |
+
submitted = st.form_submit_button("🚀 Load Repository", use_container_width=True)
|
| 35 |
+
|
| 36 |
+
if submitted:
|
| 37 |
+
if not git_url.strip():
|
| 38 |
+
st.warning("Please enter a GitHub URL.")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
parsed = parse_github_url(git_url)
|
| 42 |
+
if not parsed:
|
| 43 |
+
st.error("Invalid GitHub URL. Expected: https://github.com/owner/repo")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
owner, repo, clone_url = parsed
|
| 47 |
+
db_name = f"{owner}_{repo}"
|
| 48 |
+
|
| 49 |
+
with st.spinner("Verifying repository…"):
|
| 50 |
+
try:
|
| 51 |
+
api_url = f"https://api.github.com/repos/{owner}/{repo}"
|
| 52 |
+
resp = requests.get(api_url, timeout=10)
|
| 53 |
+
if resp.status_code == 404:
|
| 54 |
+
st.error("Repository not found. Make sure it's public.")
|
| 55 |
+
return None
|
| 56 |
+
if resp.status_code != 200:
|
| 57 |
+
st.error(f"GitHub API error: {resp.status_code}")
|
| 58 |
+
return None
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
st.error(f"Network error: {e}")
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
with st.spinner(f"Cloning `{owner}/{repo}`…"):
|
| 64 |
+
clone_repo(clone_url, repo_path)
|
| 65 |
+
|
| 66 |
+
st.success(f"✅ Cloned **{owner}/{repo}**")
|
| 67 |
+
return db_name, True
|
| 68 |
+
|
| 69 |
+
return None
|