File tree 5 files changed +28
-13
lines changed
5 files changed +28
-13
lines changed Original file line number Diff line number Diff line change @@ -19,18 +19,21 @@ fn run() -> Result<()> {
19
19
return Err ( "Git failed to execute!" . into ( ) ) ;
20
20
}
21
21
22
- // Load command line options.
23
- let options = Cli :: new ( ) ?;
22
+ let config = Cli :: new ( ) ?;
24
23
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 {
26
29
return cli_utils:: print_supported_languages ( ) ;
27
30
}
28
31
29
- if options . print_package_managers {
32
+ if config . print_package_managers {
30
33
return cli_utils:: print_supported_package_managers ( ) ;
31
34
}
32
35
33
- let info = info:: Info :: new ( options ) ?;
36
+ let info = info:: Info :: new ( config ) ?;
34
37
35
38
let mut printer = cli_utils:: Printer :: new ( io:: BufWriter :: new ( io:: stdout ( ) ) , info) ;
36
39
Original file line number Diff line number Diff line change 13
13
} ;
14
14
15
15
pub struct Cli {
16
- pub path : String ,
16
+ pub repo_path : String ,
17
17
pub ascii_input : Option < String > ,
18
18
pub ascii_language : Option < Language > ,
19
19
pub ascii_colors : Vec < String > ,
@@ -49,8 +49,11 @@ impl Cli {
49
49
. setting ( AppSettings :: DeriveDisplayOrder )
50
50
. setting ( AppSettings :: UnifiedHelpMessage )
51
51
. 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." ,
54
57
) )
55
58
. arg (
56
59
Arg :: with_name ( "ascii-language" )
@@ -263,7 +266,7 @@ impl Cli {
263
266
16
264
267
} ;
265
268
266
- let path = String :: from ( matches. value_of ( "input" ) . unwrap ( ) ) ;
269
+ let repo_path = String :: from ( matches. value_of ( "input" ) . unwrap ( ) ) ;
267
270
268
271
let ascii_input = matches. value_of ( "ascii-input" ) . map ( String :: from) ;
269
272
@@ -296,7 +299,7 @@ impl Cli {
296
299
} ;
297
300
298
301
Ok ( Cli {
299
- path ,
302
+ repo_path ,
300
303
ascii_input,
301
304
ascii_language,
302
305
ascii_colors,
Original file line number Diff line number Diff line change @@ -3,8 +3,10 @@ use crate::onefetch::{
3
3
language:: Language ,
4
4
} ;
5
5
use colored:: Color ;
6
+ use git2:: { Repository , RepositoryOpenFlags } ;
6
7
use std:: env;
7
8
use std:: io:: Write ;
9
+ use std:: path:: Path ;
8
10
use strum:: IntoEnumIterator ;
9
11
10
12
pub struct Printer < W > {
@@ -85,6 +87,13 @@ impl<W: Write> Printer<W> {
85
87
}
86
88
}
87
89
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
+
88
97
pub fn print_supported_languages ( ) -> Result < ( ) > {
89
98
for l in Language :: iter ( ) {
90
99
println ! ( "{}" , l) ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ error_chain! {
12
12
Json ( json:: Error ) ;
13
13
Regex ( regex:: Error ) ;
14
14
Toml ( toml:: de:: Error ) ;
15
+ Git2 ( git2:: Error ) ;
15
16
}
16
17
}
17
18
Original file line number Diff line number Diff line change @@ -212,9 +212,8 @@ impl std::fmt::Display for Info {
212
212
213
213
impl Info {
214
214
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 ( ) ;
218
217
let workdir_str = workdir. to_str ( ) . unwrap ( ) ;
219
218
let ( languages_stats, lines_of_code) =
220
219
Language :: get_language_statistics ( workdir_str, & config. excluded ) ?;
You can’t perform that action at this time.
0 commit comments