rcs3 commited on
Commit
da33af7
·
verified ·
1 Parent(s): 88cd04d

update git.py

Browse files
Files changed (1) hide show
  1. 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.title("GitHub Link")
9
- with st.form("git"):
10
- git_url = st.text_input("Enter GitHub Repository Link")
11
- submit_git = st.form_submit_button("Submit")
12
- if submit_git:
13
- with st.spinner("Checking GitHub URL"):
14
- if not(git_url):
15
- st.warning("Enter GitHub URL")
16
- st.stop()
17
- try:
18
- response = requests.get(git_url)
19
- if response.status_code == 200 and url_name(git_url):
20
- st.success("GitHub Link loaded successfully!")
21
- db_name = url_name(git_url)
22
- else:
23
- st.error("Enter Valid GitHub Repo")
24
- st.stop()
25
- except requests.exceptions.MissingSchema:
26
- st.error("Invalid URL. Please include the scheme (e.g., https://)")
27
- st.stop()
28
-
29
- with st.spinner(f"Cloning {db_name} Repository"):
30
- clone_repo(git_url, repo_path)
31
- st.success("Cloned successfully!")
32
- return db_name, 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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