Skip to content

Commit a3e0a5d

Browse files
committed
add is_valid_repo check in main
1 parent 71649ab commit a3e0a5d

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

Diff for: src/main.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ fn run() -> Result<()> {
1919
return Err("Git failed to execute!".into());
2020
}
2121

22-
// Load command line options.
23-
let options = Cli::new()?;
22+
let config = Cli::new()?;
2423

25-
if options.print_languages {
24+
if !cli_utils::is_valid_repo(&config.repo_path)? {
25+
return Err("please run onefetch inside of a non-bare git repository".into());
26+
}
27+
28+
if config.print_languages {
2629
return cli_utils::print_supported_languages();
2730
}
2831

29-
if options.print_package_managers {
32+
if config.print_package_managers {
3033
return cli_utils::print_supported_package_managers();
3134
}
3235

33-
let info = info::Info::new(options)?;
36+
let info = info::Info::new(config)?;
3437

3538
let mut printer = cli_utils::Printer::new(io::BufWriter::new(io::stdout()), info);
3639

Diff for: src/onefetch/cli.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use {
1313
};
1414

1515
pub struct Cli {
16-
pub path: String,
16+
pub repo_path: String,
1717
pub ascii_input: Option<String>,
1818
pub ascii_language: Option<Language>,
1919
pub ascii_colors: Vec<String>,
@@ -49,8 +49,11 @@ impl Cli {
4949
.setting(AppSettings::DeriveDisplayOrder)
5050
.setting(AppSettings::UnifiedHelpMessage)
5151
.setting(AppSettings::HidePossibleValuesInHelp)
52-
.arg(Arg::with_name("input").default_value(".").hide_default_value(true).help(
53-
"Run as if onefetch was started in <input> instead of the current working directory.",
52+
.arg(
53+
Arg::with_name("input")
54+
.default_value(".")
55+
.hide_default_value(true)
56+
.help("Run as if onefetch was started in <input> instead of the current working directory.",
5457
))
5558
.arg(
5659
Arg::with_name("ascii-language")
@@ -263,7 +266,7 @@ impl Cli {
263266
16
264267
};
265268

266-
let path = String::from(matches.value_of("input").unwrap());
269+
let repo_path = String::from(matches.value_of("input").unwrap());
267270

268271
let ascii_input = matches.value_of("ascii-input").map(String::from);
269272

@@ -296,7 +299,7 @@ impl Cli {
296299
};
297300

298301
Ok(Cli {
299-
path,
302+
repo_path,
300303
ascii_input,
301304
ascii_language,
302305
ascii_colors,

Diff for: src/onefetch/cli_utils.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use crate::onefetch::{
33
language::Language,
44
};
55
use colored::Color;
6+
use git2::{Repository, RepositoryOpenFlags};
67
use std::env;
78
use std::io::Write;
9+
use std::path::Path;
810
use strum::IntoEnumIterator;
911

1012
pub struct Printer<W> {
@@ -85,6 +87,13 @@ impl<W: Write> Printer<W> {
8587
}
8688
}
8789

90+
pub fn is_valid_repo(repo_path: &str) -> Result<bool> {
91+
let repo =
92+
Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
93+
94+
Ok(repo.is_ok() && !repo?.is_bare())
95+
}
96+
8897
pub fn print_supported_languages() -> Result<()> {
8998
for l in Language::iter() {
9099
println!("{}", l);

Diff for: src/onefetch/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ error_chain! {
1212
Json(json::Error);
1313
Regex(regex::Error);
1414
Toml(toml::de::Error);
15+
Git2(git2::Error);
1516
}
1617
}
1718

Diff for: src/onefetch/info.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ impl std::fmt::Display for Info {
212212

213213
impl Info {
214214
pub fn new(config: Cli) -> Result<Info> {
215-
let repo = Repository::discover(&config.path)
216-
.chain_err(|| "Could not find a valid git repo on the current path")?;
217-
let workdir = repo.workdir().chain_err(|| "Unable to run onefetch on bare git repo")?;
215+
let repo = Repository::discover(&config.repo_path)?;
216+
let workdir = repo.workdir().unwrap();
218217
let workdir_str = workdir.to_str().unwrap();
219218
let (languages_stats, lines_of_code) =
220219
Language::get_language_statistics(workdir_str, &config.excluded)?;

0 commit comments

Comments
 (0)