Skip to content

Commit f303b2a

Browse files
committed
create Repo struct and migrate get_version
1 parent c995f3e commit f303b2a

File tree

5 files changed

+185
-154
lines changed

5 files changed

+185
-154
lines changed

Diff for: src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![recursion_limit = "1024"]
33
#![cfg_attr(feature = "fail-on-deprecated", deny(deprecated))]
44

5-
use onefetch::{cli::Cli, cli_utils, error::*, git_utils, info};
5+
use onefetch::{cli::Cli, cli_utils, error::*, info, repo};
66

77
use {
88
process::{Command, Stdio},
@@ -21,7 +21,7 @@ fn run() -> Result<()> {
2121

2222
let config = Cli::new()?;
2323

24-
if !git_utils::is_valid_repo(&config.repo_path)? {
24+
if !repo::is_valid(&config.repo_path)? {
2525
return Err("please run onefetch inside of a non-bare git repository".into());
2626
}
2727

Diff for: src/onefetch/git_utils.rs

-121
This file was deleted.

Diff for: src/onefetch/info.rs

+10-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use {
22
crate::onefetch::{
3-
cli::Cli, commit_info::CommitInfo, deps, error::*, git_utils, language::Language,
4-
license::Detector, text_color::TextColor,
3+
cli::Cli, commit_info::CommitInfo, deps, error::*, language::Language, license::Detector,
4+
repo::Repo, text_color::TextColor,
55
},
66
colored::{Color, ColoredString, Colorize},
7-
git2::Repository,
87
std::process::Command,
98
};
109

@@ -83,7 +82,7 @@ impl std::fmt::Display for Info {
8382
)?;
8483
}
8584

86-
if !self.config.disabled_fields.version {
85+
if !self.config.disabled_fields.version && !self.version.is_empty(){
8786
writeln!(
8887
f,
8988
"{}{}",
@@ -211,19 +210,19 @@ impl std::fmt::Display for Info {
211210

212211
impl Info {
213212
pub fn new(config: Cli) -> Result<Info> {
214-
let repo = Repository::discover(&config.repo_path)?;
215-
let workdir = git_utils::get_repo_work_dir(&repo)?;
216-
let (repository_name, repository_url) = git_utils::get_repo_name_and_url(&repo)?;
217-
let current_commit_info = git_utils::get_current_commit_info(&repo);
218-
let pending = git_utils::get_pending_changes(&repo);
213+
let repo = Repo::new(&config.repo_path)?;
214+
let workdir = repo.get_work_dir()?;
215+
let (repository_name, repository_url) = repo.get_name_and_url()?;
216+
let current_commit_info = repo.get_current_commit_info();
217+
let pending = repo.get_pending_changes();
218+
let version = repo.get_version()?;
219219
let git_history = Info::get_git_history(&workdir, config.no_merges);
220220
let creation_date = Info::get_creation_date(&git_history);
221221
let number_of_commits = Info::get_number_of_commits(&git_history);
222222
let authors = Info::get_authors(&git_history, config.number_of_authors);
223223
let last_change = Info::get_date_of_last_commit(&git_history);
224224
let (number_of_tags, number_of_branches) = Info::get_number_of_tags_branches(&workdir);
225225
let (git_v, git_user) = Info::get_git_version_and_username(&workdir);
226-
let version = Info::get_version(&workdir);
227226
let repo_size = Info::get_packed_size(&workdir);
228227
let project_license = Detector::new()?.get_project_license(&workdir);
229228
let dependencies = deps::DependencyDetector::new().get_dependencies(&workdir)?;
@@ -243,7 +242,7 @@ impl Info {
243242
git_username: git_user,
244243
project_name: repository_name,
245244
current_commit: current_commit_info?,
246-
version: version?,
245+
version,
247246
creation_date: creation_date?,
248247
dominant_language,
249248
languages: languages_stats,
@@ -357,25 +356,6 @@ impl Info {
357356
(version, username)
358357
}
359358

360-
fn get_version(dir: &str) -> Result<String> {
361-
let output = Command::new("git")
362-
.arg("-C")
363-
.arg(dir)
364-
.arg("describe")
365-
.arg("--abbrev=0")
366-
.arg("--tags")
367-
.output()
368-
.expect("Failed to execute git.");
369-
370-
let output = String::from_utf8_lossy(&output.stdout);
371-
372-
if output == "" {
373-
Ok("??".into())
374-
} else {
375-
Ok(output.to_string().replace('\n', ""))
376-
}
377-
}
378-
379359
fn get_number_of_commits(git_history: &[String]) -> String {
380360
let number_of_commits = git_history.len();
381361
number_of_commits.to_string()

Diff for: src/onefetch/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ pub mod cli_utils;
44
pub mod commit_info;
55
pub mod deps;
66
pub mod error;
7-
pub mod git_utils;
87
pub mod image_backends;
98
pub mod info;
109
pub mod info_fields;
1110
pub mod language;
1211
pub mod license;
12+
pub mod repo;
1313
pub mod text_color;

0 commit comments

Comments
 (0)