Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
imp: Ignore files in accordance with .gitignore
Browse files Browse the repository at this point in the history
The -a flag is provided for the old behavior.

The only .gitignore file used is the one in the directory in which cargo
count is called.
  • Loading branch information
m-n committed Dec 23, 2015
1 parent ae21af6 commit a0c3070
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
49 changes: 48 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = "~1.5"
glob = "~0.2"
tabwriter = "~0.1"
regex = "~0.1"
gitignore = "~1"
ansi_term = {version = "~0.7", optional = true}
clippy = {version = "~0.0", optional = true}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ USAGE:
FLAGS:
-S, --follow-symlinks Follows symlinks and counts source files it finds
-a, --all Do not ignore .gitignored paths
(Defaults to false when omitted)
-h, --help Prints help information
--unsafe-statistics Displays lines and percentages of "unsafe" code
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ arg_enum! {
#[derive(Debug)]
pub struct Config<'a> {
pub verbose: bool,
pub all: bool,
pub thousands: Option<char>,
pub utf8_rule: Utf8Rule,
pub usafe: bool,
Expand All @@ -40,6 +41,7 @@ impl<'a> Config<'a> {
}
Ok(Config {
verbose: m.is_present("verbose"),
all: m.is_present("all"),
thousands: m.value_of("sep").map(|s| s.chars().nth(0).unwrap()),
usafe: m.is_present("unsafe-statistics"),
utf8_rule: value_t!(m.value_of("rule"), Utf8Rule).unwrap_or(Utf8Rule::Strict),
Expand Down
15 changes: 14 additions & 1 deletion src/count/counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::{self, Read, Write};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::f64;
use std::env;

use tabwriter::TabWriter;

Expand All @@ -12,6 +13,7 @@ use error::{CliError, CliResult};
use fsutil;
use fmt::{self, Format};
use language::Language;
use gitignore;

pub struct Counts<'c> {
cfg: &'c Config<'c>,
Expand Down Expand Up @@ -40,10 +42,21 @@ impl<'c> Counts<'c> {

pub fn fill_from(&mut self) {
debugln!("executing; fill_from; cfg={:?}", self.cfg);
let cd;
let gitignore = if self.cfg.all {
None
} else {
cd = env::current_dir().unwrap().join(".gitignore");
gitignore::File::new(&cd).ok()
};
for path in &self.cfg.to_count {
debugln!("iter; path={:?};", path);
let mut files = vec![];
fsutil::get_all_files(&mut files, &path, &self.cfg.exclude, self.cfg.follow_links);
fsutil::get_all_files(&mut files,
&path,
&self.cfg.exclude,
self.cfg.follow_links,
&gitignore);

for file in files {
debugln!("iter; file={:?};", file);
Expand Down
27 changes: 22 additions & 5 deletions src/fsutil.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use std::fs;
use std::io::Result;
use std::path::PathBuf;
use gitignore::File;

use glob;

pub fn get_all_files<'a>(v: &mut Vec<PathBuf>,
path: &PathBuf,
exclude: &[PathBuf],
follow_links: bool) {
debugln!("executing; get_all_files; path={:?}; exclude={:?};",
follow_links: bool,
gitignore: &Option<File>) {
debugln!("executing; get_all_files; path={:?}; exclude={:?}; all={:?}",
path,
exclude);
exclude,
all);
if exclude.contains(path) {
return;
}

if let Some(ref f) = *gitignore {
if f.is_excluded(path).unwrap() {
return;
}
}

debugln!("Getting metadata");
if let Ok(result) = get_metadata(&path, follow_links) {
debugln!("Found");
Expand All @@ -24,7 +33,11 @@ pub fn get_all_files<'a>(v: &mut Vec<PathBuf>,
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(v, &file_path.to_path_buf(), &exclude, follow_links);
get_all_files(v,
&file_path.to_path_buf(),
&exclude,
follow_links,
gitignore);
}
} else {
debugln!("It's a file");
Expand All @@ -42,7 +55,11 @@ pub fn get_all_files<'a>(v: &mut Vec<PathBuf>,
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(v, &file_path.to_path_buf(), &exclude, follow_links);
get_all_files(v,
&file_path.to_path_buf(),
&exclude,
follow_links,
gitignore);
}
} else {
debugln!("It's a file");
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
//! FLAGS:
//! -S, --follow-symlinks Follows symlinks and counts source files it
//! finds
//! -a, --all Do not ignore .gitignored paths
//! (Defaults to false when omitted)
//! -h, --help Prints help information
//! --unsafe-statistics Displays lines and percentages of "unsafe"
Expand Down Expand Up @@ -184,6 +185,7 @@ extern crate ansi_term;
extern crate tabwriter;
extern crate glob;
extern crate regex;
extern crate gitignore;

use std::io::Write;
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -227,6 +229,7 @@ fn main() {
.author("Kevin K. <kbknapp@gmail.com>")
.about("Displays line counts of code for cargo projects")
.args_from_usage("-e, --exclude [paths]... 'Files or directories to exclude (automatically includes \'.git\')'
-a, --all 'Do not ignore .gitignore'd paths'
--unsafe-statistics 'Displays lines and percentages of \"unsafe\" code'
-l, --language [exts]... 'Only count these languges (by source code extension){n}\
(i.e. \'-l js py cpp\')'
Expand Down

0 comments on commit a0c3070

Please sign in to comment.