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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::ffi::OsStr;
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()
.map(str::to_owned)
.context("Couldn't detect git repository")
})
.and_then(|repo| {
repo.split_whitespace()
.nth(1)
.map(str::to_owned)
.context("Couldn't detect git repository")
})
.map(PathBuf::from)
.and_then(|mut git_path| {
let repo = git_path
.file_name()
.map(OsStr::to_owned)
.context("Couldn't find repo name in git path")?;
git_path.pop();
let owner = git_path
.file_name()
.map(OsStr::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
.chars()
.take_while(alphanumeric_or_dash)
.collect::<String>()
})
.context("Couldn't convert repo name into string")?;
let owner = owner
.to_str()
.map(|owner_name| {
owner_name
.chars()
.rev()
.take_while(alphanumeric_or_dash)
.collect::<String>()
.chars()
.rev()
.collect::<String>()
})
.context("Couldn't convert repo name into string")?;
Ok(RepoAndOwner { repo, owner })
})
}
fn alphanumeric_or_dash(char: &char) -> bool {
char.is_alphanumeric() || '-'.eq(char)
}