aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
blob: d62fe8e1e86d2068afee7e90881e15edda6c3dcc (plain)
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
use std::{
    fs::File,
    io::{Result, Write},
    path::Path
};

pub fn get_repo_name(url: &str) -> &str {
    let start = url.rfind('/').unwrap_or_default() + 1;
    if url.ends_with(".git")
    {
        return &url[start..url.len() - 4];
    }

    &url[start..]
}

pub fn set_description(path: &Path, description: &str) -> Result<()> {
    let mut path_buf = path.to_path_buf();
    path_buf.push(".git/description");
    let mut file = File::create(&path_buf)?;
    file.write_all(description.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_repo_name_test() {
        assert_eq!(get_repo_name("https://github.com/Bond-009/git-mirror.git"), "git-mirror");
        assert_eq!(get_repo_name("https://git.zx2c4.com/cgit"), "cgit");
    }
}