Skip to content

Commit aebf743

Browse files
committed
feat: show number of tags and branches
This simply counts and displays the number of branches and tags that the repository has locally. Closes #202
1 parent 020409b commit aebf743

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

Diff for: src/info.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub struct Info {
3232
pending: String,
3333
repo_size: String,
3434
number_of_lines: usize,
35+
number_of_tags: usize,
36+
number_of_branches: usize,
3537
license: String,
3638
custom_logo: Language,
3739
custom_colors: Vec<String>,
@@ -74,10 +76,38 @@ impl std::fmt::Display for Info {
7476
)?;
7577
}
7678
if !self.disable_fields.project {
77-
write_buf(
78-
&mut buf,
79-
&self.get_formatted_info_label("Project: ", color),
80-
&self.project_name,
79+
let branches_str = if self.number_of_branches == 1 {
80+
String::from("1 branch")
81+
} else if self.number_of_branches > 1 {
82+
format!("{} branches", self.number_of_branches)
83+
} else {
84+
String::new()
85+
};
86+
87+
let tags_str = if self.number_of_tags == 1 {
88+
String::from("1 tag")
89+
} else if self.number_of_tags > 1 {
90+
format!("{} tags", self.number_of_tags)
91+
} else {
92+
String::new()
93+
};
94+
95+
let branches_tags_str = if tags_str.is_empty() && !branches_str.is_empty() {
96+
format!("( {} )", branches_str)
97+
} else if branches_str.is_empty() && !tags_str.is_empty() {
98+
format!("( {} )", tags_str)
99+
} else if !branches_str.is_empty() {
100+
format!("( {}, {} )", tags_str, branches_str)
101+
} else {
102+
String::new()
103+
};
104+
105+
let project_str = &self.get_formatted_info_label("Project: ", color);
106+
107+
writeln!(
108+
buf,
109+
"{}{} {}",
110+
project_str, self.project_name, branches_tags_str
81111
)?;
82112
}
83113

@@ -309,6 +339,7 @@ impl Info {
309339
let (
310340
(repository_name, repository_url),
311341
git_history,
342+
(number_of_tags, number_of_branches),
312343
current_commit_info,
313344
(git_v, git_user),
314345
version,
@@ -319,6 +350,7 @@ impl Info {
319350
) = futures::join!(
320351
Info::get_repo_name_and_url(&repo),
321352
Info::get_git_history(workdir_str, no_merges),
353+
Info::get_number_of_tags_branches(workdir_str),
322354
Info::get_current_commit_info(&repo),
323355
Info::get_git_version_and_username(workdir_str),
324356
Info::get_version(workdir_str),
@@ -349,6 +381,8 @@ impl Info {
349381
pending: pending?,
350382
repo_size: repo_size?,
351383
number_of_lines,
384+
number_of_tags,
385+
number_of_branches,
352386
license: project_license?,
353387
custom_logo: logo,
354388
custom_colors: colors,
@@ -378,6 +412,34 @@ impl Info {
378412
output.lines().map(|x| x.to_string()).collect::<Vec<_>>()
379413
}
380414

415+
async fn get_number_of_tags_branches(dir: &str) -> (usize, usize) {
416+
let tags = async {
417+
let output = Command::new("git")
418+
.args(vec!["-C", dir, "tag"])
419+
.output()
420+
.await
421+
.expect("Failed to execute git.");
422+
423+
let tags = String::from_utf8_lossy(&output.stdout);
424+
425+
tags.lines().count()
426+
};
427+
428+
let branches = async {
429+
let output = Command::new("git")
430+
.args(vec!["-C", dir, "branch", "-r"])
431+
.output()
432+
.await
433+
.expect("Failed to execute git.");
434+
435+
let branches = String::from_utf8_lossy(&output.stdout);
436+
437+
branches.lines().count()
438+
};
439+
440+
futures::join!(tags, branches)
441+
}
442+
381443
async fn get_repo_name_and_url(repo: &Repository) -> (String, String) {
382444
let config = repo.config().map_err(|_| Error::NoGitData);
383445
let mut remote_url = String::new();

0 commit comments

Comments
 (0)