-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathproject.rs
151 lines (130 loc) · 4.13 KB
/
project.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::info::info_field::{InfoField, InfoType};
use anyhow::Result;
use git_repository::{bstr::ByteSlice, Repository};
use serde::Serialize;
#[derive(Serialize)]
pub struct ProjectInfo {
pub repo_name: String,
pub number_of_branches: usize,
pub number_of_tags: usize,
}
impl ProjectInfo {
pub fn new(repo: &Repository, repo_url: &str) -> Result<Self> {
let repo_name = get_name(repo_url)?;
let number_of_branches = get_number_of_branches(repo)?;
let number_of_tags = get_number_of_tags(repo)?;
Ok(Self {
repo_name,
number_of_branches,
number_of_tags,
})
}
}
pub fn get_name(repo_url: &str) -> Result<String> {
let url = git_repository::url::parse(repo_url.into())?;
let path = git_repository::path::from_bstr(url.path.as_bstr());
let repo_name = path
.with_extension("")
.file_name()
.expect("non-empty path")
.to_string_lossy()
.into_owned();
Ok(repo_name)
}
// This collects the repo size excluding .git
pub fn get_number_of_tags(repo: &Repository) -> Result<usize> {
Ok(repo.references()?.tags()?.count())
}
pub fn get_number_of_branches(repo: &Repository) -> Result<usize> {
let mut number_of_branches = repo.references()?.remote_branches()?.count();
if number_of_branches > 0 {
//Exclude origin/HEAD -> origin/main
number_of_branches -= 1;
}
Ok(number_of_branches)
}
impl std::fmt::Display for ProjectInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let branches_str = match self.number_of_branches {
0 => String::new(),
1 => String::from("1 branch"),
_ => format!("{} branches", self.number_of_branches),
};
let tags_str = match self.number_of_tags {
0 => String::new(),
1 => String::from("1 tag"),
_ => format!("{} tags", self.number_of_tags),
};
if tags_str.is_empty() && branches_str.is_empty() {
write!(f, "{}", self.repo_name)
} else if branches_str.is_empty() || tags_str.is_empty() {
write!(f, "{} ({}{})", self.repo_name, tags_str, branches_str)
} else {
write!(f, "{} ({}, {})", self.repo_name, branches_str, tags_str)
}
}
}
impl InfoField for ProjectInfo {
const TYPE: InfoType = InfoType::Project;
fn value(&self) -> String {
self.to_string()
}
fn title(&self) -> String {
String::from("Project")
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_display_project_info() {
let project_info = ProjectInfo {
repo_name: "onefetch".to_string(),
number_of_branches: 3,
number_of_tags: 2,
};
assert_eq!(
project_info.value(),
"onefetch (3 branches, 2 tags)".to_string()
);
}
#[test]
fn test_display_project_info_no_branches_no_tags() {
let project_info = ProjectInfo {
repo_name: "onefetch".to_string(),
number_of_branches: 0,
number_of_tags: 0,
};
assert_eq!(project_info.value(), "onefetch".to_string());
}
#[test]
fn test_display_project_info_no_tags() {
let project_info = ProjectInfo {
repo_name: "onefetch".to_string(),
number_of_branches: 3,
number_of_tags: 0,
};
assert_eq!(project_info.value(), "onefetch (3 branches)".to_string());
}
#[test]
fn test_display_project_info_no_branches() {
let project_info = ProjectInfo {
repo_name: "onefetch".to_string(),
number_of_branches: 0,
number_of_tags: 2,
};
assert_eq!(project_info.value(), "onefetch (2 tags)".to_string());
}
#[test]
fn test_display_project_info_one_branche_one_tag() {
let project_info = ProjectInfo {
repo_name: "onefetch".to_string(),
number_of_branches: 1,
number_of_tags: 1,
};
assert_eq!(
project_info.value(),
"onefetch (1 branch, 1 tag)".to_string()
);
}
}