atomcode_core/
telemetry_bootstrap.rs1use atomcode_telemetry::{RepoHost, RepoOrigin};
4use std::path::Path;
5use std::process::Command;
6
7pub fn detect_repo_origin(cwd: &Path) -> RepoOrigin {
8 let output = Command::new("git")
9 .args(["-C"])
10 .arg(cwd)
11 .args(["remote", "get-url", "origin"])
12 .output();
13 match output {
14 Ok(o) if o.status.success() => {
15 let url = String::from_utf8_lossy(&o.stdout).trim().to_string();
16 RepoOrigin {
17 host: classify_host(&url),
18 has_git: true,
19 }
20 }
21 Ok(_) => RepoOrigin {
22 host: RepoHost::None,
23 has_git: has_git_dir(cwd),
24 },
25 Err(_) => RepoOrigin {
26 host: RepoHost::None,
27 has_git: has_git_dir(cwd),
28 },
29 }
30}
31
32fn classify_host(url: &str) -> RepoHost {
33 let u = url.to_ascii_lowercase();
34 if u.contains("gitcode.com") {
35 RepoHost::Gitcode
36 } else if u.contains("atomgit.com") {
37 RepoHost::Atomgit
38 } else if u.contains("github.com") {
39 RepoHost::Github
40 } else if u.contains("gitlab.") {
41 RepoHost::Gitlab
42 } else if u.is_empty() {
43 RepoHost::None
44 } else {
45 RepoHost::Other
46 }
47}
48
49fn has_git_dir(cwd: &Path) -> bool {
50 cwd.join(".git").exists()
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn classify_hosts() {
59 assert!(matches!(
60 classify_host("git@gitcode.com:foo/bar.git"),
61 RepoHost::Gitcode
62 ));
63 assert!(matches!(
64 classify_host("https://atomgit.com/x/y"),
65 RepoHost::Atomgit
66 ));
67 assert!(matches!(
68 classify_host("https://github.com/x/y"),
69 RepoHost::Github
70 ));
71 assert!(matches!(
72 classify_host("ssh://git@gitlab.foo/x"),
73 RepoHost::Gitlab
74 ));
75 assert!(matches!(
76 classify_host("https://other.net/x"),
77 RepoHost::Other
78 ));
79 assert!(matches!(classify_host(""), RepoHost::None));
80 }
81}