Skip to content

Commit e0dd9ef

Browse files
committedNov 11, 2020
migrate get_number_of_tags_branches
1 parent c28404d commit e0dd9ef

File tree

2 files changed

+24
-51
lines changed

2 files changed

+24
-51
lines changed
 

‎src/onefetch/info.rs

+9-49
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ impl Info {
216216
let head_refs = repo.get_head_refs()?;
217217
let pending = repo.get_pending_changes();
218218
let version = repo.get_version()?;
219+
let git_username = repo.get_git_username()?;
220+
let number_of_tags = repo.get_number_of_tags()?;
221+
let number_of_branches = repo.get_number_of_branches()?;
219222
let git_history = Info::get_git_history(&workdir, config.no_merges);
220223
let creation_date = Info::get_creation_date(&git_history);
221224
let number_of_commits = Info::get_number_of_commits(&git_history);
222225
let authors = Info::get_authors(&git_history, config.number_of_authors);
223226
let last_change = Info::get_date_of_last_commit(&git_history);
224-
let (number_of_tags, number_of_branches) = Info::get_number_of_tags_branches(&workdir);
225-
let (git_v, git_user) = Info::get_git_version_and_username(&workdir);
227+
let git_version = Info::get_git_version()?;
226228
let repo_size = Info::get_packed_size(&workdir);
227229
let project_license = Detector::new()?.get_project_license(&workdir);
228230
let dependencies = deps::DependencyDetector::new().get_dependencies(&workdir)?;
@@ -238,8 +240,8 @@ impl Info {
238240
let text_colors = TextColor::get_text_colors(&config.text_colors, &ascii_colors);
239241

240242
Ok(Info {
241-
git_version: git_v,
242-
git_username: git_user,
243+
git_version,
244+
git_username,
243245
project_name: repository_name,
244246
head_refs,
245247
version,
@@ -277,36 +279,6 @@ impl Info {
277279
output.lines().map(|x| x.to_string()).collect::<Vec<_>>()
278280
}
279281

280-
fn get_number_of_tags_branches(dir: &str) -> (usize, usize) {
281-
let tags = {
282-
let output = Command::new("git")
283-
.args(vec!["-C", dir, "tag"])
284-
.output()
285-
.expect("Failed to execute git.");
286-
287-
let tags = String::from_utf8_lossy(&output.stdout);
288-
289-
tags.lines().count()
290-
};
291-
292-
let branches = {
293-
let output = Command::new("git")
294-
.args(vec!["-C", dir, "branch", "-r"])
295-
.output()
296-
.expect("Failed to execute git.");
297-
298-
let branches = String::from_utf8_lossy(&output.stdout);
299-
300-
if branches.lines().count() > 0 {
301-
branches.lines().count() - 1 //Exclude origin/HEAD -> origin/master
302-
} else {
303-
0
304-
}
305-
};
306-
307-
(tags, branches)
308-
}
309-
310282
fn get_authors(git_history: &[String], n: usize) -> Vec<(String, usize, usize)> {
311283
let mut authors = std::collections::HashMap::new();
312284
let mut author_name_by_email = std::collections::HashMap::new();
@@ -339,21 +311,9 @@ impl Info {
339311
authors
340312
}
341313

342-
fn get_git_version_and_username(dir: &str) -> (String, String) {
343-
let version =
344-
Command::new("git").arg("--version").output().expect("Failed to execute git.");
345-
let version = String::from_utf8_lossy(&version.stdout).replace('\n', "");
346-
347-
let username = Command::new("git")
348-
.arg("-C")
349-
.arg(dir)
350-
.arg("config")
351-
.arg("--get")
352-
.arg("user.name")
353-
.output()
354-
.expect("Failed to execute git.");
355-
let username = String::from_utf8_lossy(&username.stdout).replace('\n', "");
356-
(version, username)
314+
fn get_git_version() -> Result<String> {
315+
let version = Command::new("git").arg("--version").output()?;
316+
Ok(String::from_utf8_lossy(&version.stdout).replace('\n', ""))
357317
}
358318

359319
fn get_number_of_commits(git_history: &[String]) -> String {

‎src/onefetch/repo.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::onefetch::{commit_info::CommitInfo, error::*};
2-
use git2::{Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow};
2+
use git2::{BranchType, Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow};
33
use regex::Regex;
44
use std::path::Path;
55

@@ -20,6 +20,20 @@ impl Repo {
2020
}
2121
}
2222

23+
pub fn get_number_of_tags(&self) -> Result<usize> {
24+
Ok(self.repo.tag_names(None)?.len())
25+
}
26+
27+
pub fn get_number_of_branches(&self) -> Result<usize> {
28+
Ok(self.repo.branches(Some(BranchType::Remote))?.count() - 1)
29+
}
30+
31+
pub fn get_git_username(&self) -> Result<String> {
32+
let config = self.repo.config()?;
33+
let username = config.get_entry("user.name")?;
34+
Ok(username.value().unwrap_or("unknown").to_string())
35+
}
36+
2337
pub fn get_version(&self) -> Result<String> {
2438
let mut version_name = String::new();
2539
let mut most_recent: i64 = 0;
@@ -150,6 +164,5 @@ impl Repo {
150164

151165
pub fn is_valid(repo_path: &str) -> Result<bool> {
152166
let repo = Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
153-
154167
Ok(repo.is_ok() && !repo?.is_bare())
155168
}

0 commit comments

Comments
 (0)