1
+ extern crate bytecount;
1
2
extern crate colored;
2
3
extern crate git2;
3
4
extern crate license;
4
5
extern crate tokei;
5
- extern crate bytecount;
6
6
7
7
use colored:: Color ;
8
8
use colored:: * ;
@@ -12,6 +12,7 @@ use std::{
12
12
cmp,
13
13
collections:: HashMap ,
14
14
convert:: From ,
15
+ env,
15
16
ffi:: OsStr ,
16
17
fmt,
17
18
fmt:: Write ,
@@ -261,16 +262,32 @@ fn main() -> Result<()> {
261
262
return Err ( Error :: GitNotInstalled ) ;
262
263
}
263
264
264
- let tokei_langs = project_languages ( ) ;
265
+ let mut args = env:: args ( ) ;
266
+
267
+ if args. next ( ) . is_none ( ) {
268
+ return Err ( Error :: TooFewArgs ) ;
269
+ } ;
270
+
271
+ let dir = if let Some ( arg) = args. next ( ) {
272
+ arg
273
+ } else {
274
+ String :: from ( "." )
275
+ } ;
276
+
277
+ if args. next ( ) . is_some ( ) {
278
+ return Err ( Error :: TooManyArgs ) ;
279
+ } ;
280
+
281
+ let tokei_langs = project_languages ( & dir) ;
265
282
let languages_stat = get_languages_stat ( & tokei_langs) . ok_or ( Error :: SourceCodeNotFound ) ?;
266
283
let mut languages_stat_vec: Vec < ( _ , _ ) > = languages_stat. into_iter ( ) . collect ( ) ;
267
284
languages_stat_vec. sort_by ( |a, b| a. 1 . partial_cmp ( & b. 1 ) . unwrap ( ) . reverse ( ) ) ;
268
285
let dominant_language = languages_stat_vec[ 0 ] . 0 . clone ( ) ;
269
286
270
- let authors = get_authors ( 3 ) ;
271
- let config = get_configuration ( ) ?;
272
- let version = get_version ( ) ?;
273
- let commits = get_commits ( ) ?;
287
+ let authors = get_authors ( & dir , 3 ) ;
288
+ let config = get_configuration ( & dir ) ?;
289
+ let version = get_version ( & dir ) ?;
290
+ let commits = get_commits ( & dir ) ?;
274
291
275
292
let info = Info {
276
293
project_name : config. repository_name ,
@@ -281,17 +298,17 @@ fn main() -> Result<()> {
281
298
repo : config. repository_url ,
282
299
commits,
283
300
number_of_lines : get_total_loc ( & tokei_langs) ,
284
- license : project_license ( ) ?,
301
+ license : project_license ( & dir ) ?,
285
302
} ;
286
303
287
304
println ! ( "{}" , info) ;
288
305
Ok ( ( ) )
289
306
}
290
307
291
- fn project_languages ( ) -> tokei:: Languages {
308
+ fn project_languages ( dir : & str ) -> tokei:: Languages {
292
309
let mut languages = tokei:: Languages :: new ( ) ;
293
310
let required_languages = get_all_language_types ( ) ;
294
- languages. get_statistics ( & [ "." ] , vec ! [ ".git" , "target" ] , Some ( required_languages) ) ;
311
+ languages. get_statistics ( & [ & dir ] , vec ! [ ".git" , "target" ] , Some ( required_languages) ) ;
295
312
languages
296
313
}
297
314
@@ -314,8 +331,8 @@ fn get_languages_stat(languages: &tokei::Languages) -> Option<HashMap<Language,
314
331
}
315
332
}
316
333
317
- fn project_license ( ) -> Result < String > {
318
- let output = fs:: read_dir ( "." )
334
+ fn project_license ( dir : & str ) -> Result < String > {
335
+ let output = fs:: read_dir ( dir )
319
336
. map_err ( |_| Error :: ReadDirectory ) ?
320
337
. filter_map ( result:: Result :: ok)
321
338
. map ( |entry| entry. path ( ) )
@@ -344,8 +361,10 @@ fn project_license() -> Result<String> {
344
361
}
345
362
}
346
363
347
- fn get_version ( ) -> Result < String > {
364
+ fn get_version ( dir : & str ) -> Result < String > {
348
365
let output = Command :: new ( "git" )
366
+ . arg ( "-C" )
367
+ . arg ( dir)
349
368
. arg ( "describe" )
350
369
. arg ( "--abbrev=0" )
351
370
. arg ( "--tags" )
@@ -361,8 +380,10 @@ fn get_version() -> Result<String> {
361
380
}
362
381
}
363
382
364
- fn get_commits ( ) -> Result < String > {
383
+ fn get_commits ( dir : & str ) -> Result < String > {
365
384
let output = Command :: new ( "git" )
385
+ . arg ( "-C" )
386
+ . arg ( dir)
366
387
. arg ( "rev-list" )
367
388
. arg ( "--count" )
368
389
. arg ( "HEAD" )
@@ -392,8 +413,8 @@ struct Configuration {
392
413
pub repository_url : String ,
393
414
}
394
415
395
- fn get_configuration ( ) -> Result < Configuration > {
396
- let repo = Repository :: open ( "./" ) . map_err ( |_| Error :: NotGitRepo ) ?;
416
+ fn get_configuration ( dir : & str ) -> Result < Configuration > {
417
+ let repo = Repository :: open ( dir ) . map_err ( |_| Error :: NotGitRepo ) ?;
397
418
let config = repo. config ( ) . map_err ( |_| Error :: NoGitData ) ?;
398
419
let mut remote_url = String :: new ( ) ;
399
420
let mut repository_name = String :: new ( ) ;
@@ -432,9 +453,11 @@ fn get_configuration() -> Result<Configuration> {
432
453
}
433
454
434
455
// Return first n most active commiters as authors within this project.
435
- fn get_authors ( n : usize ) -> Vec < String > {
456
+ fn get_authors ( dir : & str , n : usize ) -> Vec < String > {
436
457
use std:: collections:: HashMap ;
437
458
let output = Command :: new ( "git" )
459
+ . arg ( "-C" )
460
+ . arg ( dir)
438
461
. arg ( "log" )
439
462
. arg ( "--format='%aN'" )
440
463
. output ( )
@@ -585,6 +608,10 @@ enum Error {
585
608
ReadDirectory ,
586
609
/// Not in a Git Repo
587
610
NotGitRepo ,
611
+ /// Too few arguments
612
+ TooFewArgs ,
613
+ /// Too many arguments
614
+ TooManyArgs ,
588
615
}
589
616
590
617
impl fmt:: Debug for Error {
@@ -595,6 +622,8 @@ impl fmt::Debug for Error {
595
622
Error :: NoGitData => "Could not retrieve git configuration data" ,
596
623
Error :: ReadDirectory => "Could not read directory ./" ,
597
624
Error :: NotGitRepo => "You are not at the root of a Git Repo" ,
625
+ Error :: TooFewArgs => "Too few arguments. Expected program name and a single argument." ,
626
+ Error :: TooManyArgs => "Too many arguments. Expected a single argument." ,
598
627
} ;
599
628
write ! ( f, "{}" , content)
600
629
}
0 commit comments