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
58
59
60
61
62
63
64
65
66
67
68
use std::path::PathBuf;
use anyhow::Context;
#[derive(Debug, Clone)]
pub struct RepoAndOwner {
pub repo: String,
pub owner: String,
}
pub fn get_repo_owner() -> anyhow::Result<RepoAndOwner> {
std::process::Command::new("git")
.arg("remote")
.arg("-v")
.output()
.context("Couldn't detect git repository")
.map(|output| String::from_utf8_lossy(output.stdout.as_slice()).to_string())
.and_then(|output| {
output
.lines()
.next()
.and_then(|repo| repo.split_whitespace().nth(1).map(|repo| repo.to_owned()))
.context("Couldn't detect git repository")
})
.map(PathBuf::from)
.and_then(|mut git_path| {
let repo = git_path
.file_name()
.map(|name| name.to_owned())
.context("Couldn't find repo name in git path")?;
git_path.pop();
let owner = git_path
.file_name()
.map(|name| name.to_owned())
.context("Couldn't find repo owner in git path")?;
Ok((repo, owner))
})
.and_then(|(repo, owner)| {
let repo = repo
.to_str()
.map(|repo_name| repo_name.trim().to_owned())
.context("Couldn't convert repo name into string")?;
let owner = owner
.to_str()
.map(|repo_name| {
repo_name
.chars()
.rev()
.take_while(char::is_ascii_alphanumeric)
.collect::<String>()
.chars()
.rev()
.collect::<String>()
})
.context("Couldn't convert repo name into string")?;
Ok(RepoAndOwner { repo, owner })
})
}
#[test]
fn cod_repo_name() -> anyhow::Result<()> {
let repo_and_owner = get_repo_owner()?;
assert_eq!(repo_and_owner.repo.as_str(), "codeberg-cli");
assert_eq!(repo_and_owner.owner.as_str(), "RobWalt");
Ok(())
}