core_lib/daemon/
utiles.rs1#![allow(dead_code)]
2
3use anyhow::Result;
4pub fn extract_repo_path(remote: &str) -> Result<String> {
5 let s = remote.trim();
6 if s.is_empty() {
7 return Err(anyhow::anyhow!("empty remote"));
8 }
9
10 if let Some(scheme_pos) = s.find("://") {
11 let after_scheme = &s[scheme_pos + 3..];
12
13 let slash_idx = after_scheme
14 .find('/')
15 .ok_or_else(|| anyhow::anyhow!("No '/' found after scheme in remote URL"))?;
16
17 let mut path = &after_scheme[slash_idx..];
18
19 if let Some(cut) = path.find(['?', '#']) {
20 path = &path[..cut];
21 }
22
23 return normalize_git_path(path);
24 }
25 if let Some(colon_idx) = s.rfind(':') {
26 let path = &s[colon_idx + 1..];
27 return normalize_git_path(path);
28 }
29 if s.contains('/')
30 && !s.contains(' ')
31 && let Some(slash_idx) = s.find('/')
32 {
33 let path = &s[slash_idx..];
34 return normalize_git_path(path);
35 }
36
37 Err(anyhow::anyhow!("Failed to extract repo remote path"))
38}
39
40fn normalize_git_path(p: &str) -> Result<String> {
41 let mut path = p.trim_matches('/').to_string();
42
43 if let Some(cut) = path.find(['?', '#']) {
44 path.truncate(cut);
45 }
46 while path.ends_with('/') {
47 path.pop();
48 }
49 if let Some(stripped) = path.strip_suffix(".git") {
50 path = stripped.to_string();
51 }
52 while path.ends_with('/') {
53 path.pop();
54 }
55 let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
56 if segments.len() < 2 {
57 return Err(anyhow::anyhow!("Incorrect remote path: {}", path));
58 }
59
60 Ok(segments.join("/"))
61}