-
Notifications
You must be signed in to change notification settings - Fork 484
/
git.rs
57 lines (49 loc) · 1.79 KB
/
git.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::common::shell::ShellSpawnError;
use crate::prelude::*;
use std::process::Command;
pub fn shallow_clone(uri: &str, target: &str) -> Result<()> {
Command::new("git")
.args(["clone", uri, target, "--depth", "1"])
.spawn()
.map_err(|e| ShellSpawnError::new("git clone", e))?
.wait()
.context("Unable to git clone")?;
Ok(())
}
pub fn meta(uri: &str) -> (String, String, String) {
let actual_uri = if uri.contains("://") || uri.contains('@') {
uri.to_string()
} else {
format!("https://github.com/{}", uri)
};
let uri_to_split = actual_uri.replace(':', "/");
let parts: Vec<&str> = uri_to_split.split('/').collect();
let user = parts[parts.len() - 2];
let repo = parts[parts.len() - 1].replace(".git", "");
(actual_uri, user.to_string(), repo)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_meta_github_https() {
let (actual_uri, user, repo) = meta("https://github.com/denisidoro/navi");
assert_eq!(actual_uri, "https://github.com/denisidoro/navi".to_string());
assert_eq!(user, "denisidoro".to_string());
assert_eq!(repo, "navi".to_string());
}
#[test]
fn test_meta_github_ssh() {
let (actual_uri, user, repo) = meta("git@github.com:denisidoro/navi.git");
assert_eq!(actual_uri, "git@github.com:denisidoro/navi.git".to_string());
assert_eq!(user, "denisidoro".to_string());
assert_eq!(repo, "navi".to_string());
}
#[test]
fn test_meta_gitlab_https() {
let (actual_uri, user, repo) = meta("https://gitlab.com/user/repo.git");
assert_eq!(actual_uri, "https://gitlab.com/user/repo.git".to_string());
assert_eq!(user, "user".to_string());
assert_eq!(repo, "repo".to_string());
}
}