1use std::cell::RefCell;
11use std::env;
12use std::fs::File;
13use std::io::{self, BufRead, Read};
14use std::path::{Path, PathBuf};
15use std::str;
16use std::sync::Arc;
17
18use globset::{Candidate, GlobBuilder, GlobSet, GlobSetBuilder};
19use regex::bytes::Regex;
20use thread_local::ThreadLocal;
21
22use crate::pathutil::{is_file_name, strip_prefix};
23use crate::{Error, Match, PartialErrorBuilder};
24
25#[derive(Clone, Debug)]
30pub struct Glob {
31 from: Option<PathBuf>,
33 original: String,
35 actual: String,
37 is_whitelist: bool,
39 is_only_dir: bool,
41}
42
43impl Glob {
44 pub fn from(&self) -> Option<&Path> {
46 self.from.as_ref().map(|p| &**p)
47 }
48
49 pub fn original(&self) -> &str {
51 &self.original
52 }
53
54 pub fn actual(&self) -> &str {
57 &self.actual
58 }
59
60 pub fn is_whitelist(&self) -> bool {
62 self.is_whitelist
63 }
64
65 pub fn is_only_dir(&self) -> bool {
67 self.is_only_dir
68 }
69
70 fn has_doublestar_prefix(&self) -> bool {
72 self.actual.starts_with("**/") || self.actual == "**"
73 }
74}
75
76#[derive(Clone, Debug)]
79pub struct Gitignore {
80 set: GlobSet,
81 root: PathBuf,
82 globs: Vec<Glob>,
83 num_ignores: u64,
84 num_whitelists: u64,
85 matches: Option<Arc<ThreadLocal<RefCell<Vec<usize>>>>>,
86}
87
88impl Gitignore {
89 pub fn new<P: AsRef<Path>>(
102 gitignore_path: P,
103 ) -> (Gitignore, Option<Error>) {
104 let path = gitignore_path.as_ref();
105 let parent = path.parent().unwrap_or(Path::new("/"));
106 let mut builder = GitignoreBuilder::new(parent);
107 let mut errs = PartialErrorBuilder::default();
108 errs.maybe_push_ignore_io(builder.add(path));
109 match builder.build() {
110 Ok(gi) => (gi, errs.into_error_option()),
111 Err(err) => {
112 errs.push(err);
113 (Gitignore::empty(), errs.into_error_option())
114 }
115 }
116 }
117
118 pub fn global() -> (Gitignore, Option<Error>) {
129 GitignoreBuilder::new("").build_global()
130 }
131
132 pub fn empty() -> Gitignore {
136 Gitignore {
137 set: GlobSet::empty(),
138 root: PathBuf::from(""),
139 globs: vec![],
140 num_ignores: 0,
141 num_whitelists: 0,
142 matches: None,
143 }
144 }
145
146 pub fn path(&self) -> &Path {
150 &*self.root
151 }
152
153 pub fn is_empty(&self) -> bool {
156 self.set.is_empty()
157 }
158
159 pub fn len(&self) -> usize {
162 self.set.len()
163 }
164
165 pub fn num_ignores(&self) -> u64 {
167 self.num_ignores
168 }
169
170 pub fn num_whitelists(&self) -> u64 {
172 self.num_whitelists
173 }
174
175 pub fn matched<P: AsRef<Path>>(
187 &self,
188 path: P,
189 is_dir: bool,
190 ) -> Match<&Glob> {
191 if self.is_empty() {
192 return Match::None;
193 }
194 self.matched_stripped(self.strip(path.as_ref()), is_dir)
195 }
196
197 pub fn matched_path_or_any_parents<P: AsRef<Path>>(
219 &self,
220 path: P,
221 is_dir: bool,
222 ) -> Match<&Glob> {
223 if self.is_empty() {
224 return Match::None;
225 }
226 let mut path = self.strip(path.as_ref());
227 assert!(!path.has_root(), "path is expected to be under the root");
228
229 match self.matched_stripped(path, is_dir) {
230 Match::None => (), a_match => return a_match,
232 }
233 while let Some(parent) = path.parent() {
234 match self.matched_stripped(parent, true) {
235 Match::None => path = parent, a_match => return a_match,
237 }
238 }
239 Match::None
240 }
241
242 fn matched_stripped<P: AsRef<Path>>(
244 &self,
245 path: P,
246 is_dir: bool,
247 ) -> Match<&Glob> {
248 if self.is_empty() {
249 return Match::None;
250 }
251 let path = path.as_ref();
252 let _matches = self.matches.as_ref().unwrap().get_or_default();
253 let mut matches = _matches.borrow_mut();
254 let candidate = Candidate::new(path);
255 self.set.matches_candidate_into(&candidate, &mut *matches);
256 for &i in matches.iter().rev() {
257 let glob = &self.globs[i];
258 if !glob.is_only_dir() || is_dir {
259 return if glob.is_whitelist() {
260 Match::Whitelist(glob)
261 } else {
262 Match::Ignore(glob)
263 };
264 }
265 }
266 Match::None
267 }
268
269 fn strip<'a, P: 'a + AsRef<Path> + ?Sized>(
272 &'a self,
273 path: &'a P,
274 ) -> &'a Path {
275 let mut path = path.as_ref();
276 if let Some(p) = strip_prefix("./", path) {
280 path = p;
281 }
282 if self.root != Path::new(".") && !is_file_name(path) {
291 if let Some(p) = strip_prefix(&self.root, path) {
292 path = p;
293 if let Some(p) = strip_prefix("/", path) {
295 path = p;
296 }
297 }
298 }
299 path
300 }
301}
302
303#[derive(Clone, Debug)]
305pub struct GitignoreBuilder {
306 builder: GlobSetBuilder,
307 root: PathBuf,
308 globs: Vec<Glob>,
309 case_insensitive: bool,
310}
311
312impl GitignoreBuilder {
313 pub fn new<P: AsRef<Path>>(root: P) -> GitignoreBuilder {
320 let root = root.as_ref();
321 GitignoreBuilder {
322 builder: GlobSetBuilder::new(),
323 root: strip_prefix("./", root).unwrap_or(root).to_path_buf(),
324 globs: vec![],
325 case_insensitive: false,
326 }
327 }
328
329 pub fn build(&self) -> Result<Gitignore, Error> {
333 let nignore = self.globs.iter().filter(|g| !g.is_whitelist()).count();
334 let nwhite = self.globs.iter().filter(|g| g.is_whitelist()).count();
335 let set = self
336 .builder
337 .build()
338 .map_err(|err| Error::Glob { glob: None, err: err.to_string() })?;
339 Ok(Gitignore {
340 set: set,
341 root: self.root.clone(),
342 globs: self.globs.clone(),
343 num_ignores: nignore as u64,
344 num_whitelists: nwhite as u64,
345 matches: Some(Arc::new(ThreadLocal::default())),
346 })
347 }
348
349 pub fn build_global(mut self) -> (Gitignore, Option<Error>) {
359 match gitconfig_excludes_path() {
360 None => (Gitignore::empty(), None),
361 Some(path) => {
362 if !path.is_file() {
363 (Gitignore::empty(), None)
364 } else {
365 let mut errs = PartialErrorBuilder::default();
366 errs.maybe_push_ignore_io(self.add(path));
367 match self.build() {
368 Ok(gi) => (gi, errs.into_error_option()),
369 Err(err) => {
370 errs.push(err);
371 (Gitignore::empty(), errs.into_error_option())
372 }
373 }
374 }
375 }
376 }
377 }
378
379 pub fn add<P: AsRef<Path>>(&mut self, path: P) -> Option<Error> {
387 let path = path.as_ref();
388 let file = match File::open(path) {
389 Err(err) => return Some(Error::Io(err).with_path(path)),
390 Ok(file) => file,
391 };
392 let rdr = io::BufReader::new(file);
393 let mut errs = PartialErrorBuilder::default();
394 for (i, line) in rdr.lines().enumerate() {
395 let lineno = (i + 1) as u64;
396 let line = match line {
397 Ok(line) => line,
398 Err(err) => {
399 errs.push(Error::Io(err).tagged(path, lineno));
400 break;
401 }
402 };
403 if let Err(err) = self.add_line(Some(path.to_path_buf()), &line) {
404 errs.push(err.tagged(path, lineno));
405 }
406 }
407 errs.into_error_option()
408 }
409
410 #[cfg(test)]
417 fn add_str(
418 &mut self,
419 from: Option<PathBuf>,
420 gitignore: &str,
421 ) -> Result<&mut GitignoreBuilder, Error> {
422 for line in gitignore.lines() {
423 self.add_line(from.clone(), line)?;
424 }
425 Ok(self)
426 }
427
428 pub fn add_line(
435 &mut self,
436 from: Option<PathBuf>,
437 mut line: &str,
438 ) -> Result<&mut GitignoreBuilder, Error> {
439 #![allow(deprecated)]
440
441 if line.starts_with("#") {
442 return Ok(self);
443 }
444 if !line.ends_with("\\ ") {
445 line = line.trim_right();
446 }
447 if line.is_empty() {
448 return Ok(self);
449 }
450 let mut glob = Glob {
451 from: from,
452 original: line.to_string(),
453 actual: String::new(),
454 is_whitelist: false,
455 is_only_dir: false,
456 };
457 let mut is_absolute = false;
458 if line.starts_with("\\!") || line.starts_with("\\#") {
459 line = &line[1..];
460 is_absolute = line.chars().nth(0) == Some('/');
461 } else {
462 if line.starts_with("!") {
463 glob.is_whitelist = true;
464 line = &line[1..];
465 }
466 if line.starts_with("/") {
467 line = &line[1..];
472 is_absolute = true;
473 }
474 }
475 if line.as_bytes().last() == Some(&b'/') {
478 glob.is_only_dir = true;
479 line = &line[..line.len() - 1];
480 if line.as_bytes().last() == Some(&b'\\') {
483 line = &line[..line.len() - 1];
484 }
485 }
486 glob.actual = line.to_string();
487 if !is_absolute && !line.chars().any(|c| c == '/') {
491 if !glob.has_doublestar_prefix() {
493 glob.actual = format!("**/{}", glob.actual);
494 }
495 }
496 if glob.actual.ends_with("/**") {
500 glob.actual = format!("{}/*", glob.actual);
501 }
502 let parsed = GlobBuilder::new(&glob.actual)
503 .literal_separator(true)
504 .case_insensitive(self.case_insensitive)
505 .backslash_escape(true)
506 .build()
507 .map_err(|err| Error::Glob {
508 glob: Some(glob.original.clone()),
509 err: err.kind().to_string(),
510 })?;
511 self.builder.add(parsed);
512 self.globs.push(glob);
513 Ok(self)
514 }
515
516 pub fn case_insensitive(
523 &mut self,
524 yes: bool,
525 ) -> Result<&mut GitignoreBuilder, Error> {
526 self.case_insensitive = yes;
529 Ok(self)
530 }
531}
532
533fn gitconfig_excludes_path() -> Option<PathBuf> {
537 match gitconfig_home_contents().and_then(|x| parse_excludes_file(&x)) {
542 Some(path) => return Some(path),
543 None => {}
544 }
545 match gitconfig_xdg_contents().and_then(|x| parse_excludes_file(&x)) {
546 Some(path) => return Some(path),
547 None => {}
548 }
549 excludes_file_default()
550}
551
552fn gitconfig_home_contents() -> Option<Vec<u8>> {
555 let home = match home_dir() {
556 None => return None,
557 Some(home) => home,
558 };
559 let mut file = match File::open(home.join(".gitconfig")) {
560 Err(_) => return None,
561 Ok(file) => io::BufReader::new(file),
562 };
563 let mut contents = vec![];
564 file.read_to_end(&mut contents).ok().map(|_| contents)
565}
566
567fn gitconfig_xdg_contents() -> Option<Vec<u8>> {
570 let path = env::var_os("XDG_CONFIG_HOME")
571 .and_then(|x| if x.is_empty() { None } else { Some(PathBuf::from(x)) })
572 .or_else(|| home_dir().map(|p| p.join(".config")))
573 .map(|x| x.join("git/config"));
574 let mut file = match path.and_then(|p| File::open(p).ok()) {
575 None => return None,
576 Some(file) => io::BufReader::new(file),
577 };
578 let mut contents = vec![];
579 file.read_to_end(&mut contents).ok().map(|_| contents)
580}
581
582fn excludes_file_default() -> Option<PathBuf> {
586 env::var_os("XDG_CONFIG_HOME")
587 .and_then(|x| if x.is_empty() { None } else { Some(PathBuf::from(x)) })
588 .or_else(|| home_dir().map(|p| p.join(".config")))
589 .map(|x| x.join("git/ignore"))
590}
591
592fn parse_excludes_file(data: &[u8]) -> Option<PathBuf> {
595 lazy_static::lazy_static! {
599 static ref RE: Regex =
600 Regex::new(r"(?im)^\s*excludesfile\s*=\s*(.+)\s*$").unwrap();
601 };
602 let caps = match RE.captures(data) {
603 None => return None,
604 Some(caps) => caps,
605 };
606 str::from_utf8(&caps[1]).ok().map(|s| PathBuf::from(expand_tilde(s)))
607}
608
609fn expand_tilde(path: &str) -> String {
611 let home = match home_dir() {
612 None => return path.to_string(),
613 Some(home) => home.to_string_lossy().into_owned(),
614 };
615 path.replace("~", &home)
616}
617
618fn home_dir() -> Option<PathBuf> {
620 #![allow(deprecated)]
624 env::home_dir()
625}
626
627#[cfg(test)]
628mod tests {
629 use super::{Gitignore, GitignoreBuilder};
630 use std::path::Path;
631
632 fn gi_from_str<P: AsRef<Path>>(root: P, s: &str) -> Gitignore {
633 let mut builder = GitignoreBuilder::new(root);
634 builder.add_str(None, s).unwrap();
635 builder.build().unwrap()
636 }
637
638 macro_rules! ignored {
639 ($name:ident, $root:expr, $gi:expr, $path:expr) => {
640 ignored!($name, $root, $gi, $path, false);
641 };
642 ($name:ident, $root:expr, $gi:expr, $path:expr, $is_dir:expr) => {
643 #[test]
644 fn $name() {
645 let gi = gi_from_str($root, $gi);
646 assert!(gi.matched($path, $is_dir).is_ignore());
647 }
648 };
649 }
650
651 macro_rules! not_ignored {
652 ($name:ident, $root:expr, $gi:expr, $path:expr) => {
653 not_ignored!($name, $root, $gi, $path, false);
654 };
655 ($name:ident, $root:expr, $gi:expr, $path:expr, $is_dir:expr) => {
656 #[test]
657 fn $name() {
658 let gi = gi_from_str($root, $gi);
659 assert!(!gi.matched($path, $is_dir).is_ignore());
660 }
661 };
662 }
663
664 const ROOT: &'static str = "/home/foobar/rust/rg";
665
666 ignored!(ig1, ROOT, "months", "months");
667 ignored!(ig2, ROOT, "*.lock", "Cargo.lock");
668 ignored!(ig3, ROOT, "*.rs", "src/main.rs");
669 ignored!(ig4, ROOT, "src/*.rs", "src/main.rs");
670 ignored!(ig5, ROOT, "/*.c", "cat-file.c");
671 ignored!(ig6, ROOT, "/src/*.rs", "src/main.rs");
672 ignored!(ig7, ROOT, "!src/main.rs\n*.rs", "src/main.rs");
673 ignored!(ig8, ROOT, "foo/", "foo", true);
674 ignored!(ig9, ROOT, "**/foo", "foo");
675 ignored!(ig10, ROOT, "**/foo", "src/foo");
676 ignored!(ig11, ROOT, "**/foo/**", "src/foo/bar");
677 ignored!(ig12, ROOT, "**/foo/**", "wat/src/foo/bar/baz");
678 ignored!(ig13, ROOT, "**/foo/bar", "foo/bar");
679 ignored!(ig14, ROOT, "**/foo/bar", "src/foo/bar");
680 ignored!(ig15, ROOT, "abc/**", "abc/x");
681 ignored!(ig16, ROOT, "abc/**", "abc/x/y");
682 ignored!(ig17, ROOT, "abc/**", "abc/x/y/z");
683 ignored!(ig18, ROOT, "a/**/b", "a/b");
684 ignored!(ig19, ROOT, "a/**/b", "a/x/b");
685 ignored!(ig20, ROOT, "a/**/b", "a/x/y/b");
686 ignored!(ig21, ROOT, r"\!xy", "!xy");
687 ignored!(ig22, ROOT, r"\#foo", "#foo");
688 ignored!(ig23, ROOT, "foo", "./foo");
689 ignored!(ig24, ROOT, "target", "grep/target");
690 ignored!(ig25, ROOT, "Cargo.lock", "./tabwriter-bin/Cargo.lock");
691 ignored!(ig26, ROOT, "/foo/bar/baz", "./foo/bar/baz");
692 ignored!(ig27, ROOT, "foo/", "xyz/foo", true);
693 ignored!(ig28, "./src", "/llvm/", "./src/llvm", true);
694 ignored!(ig29, ROOT, "node_modules/ ", "node_modules", true);
695 ignored!(ig30, ROOT, "**/", "foo/bar", true);
696 ignored!(ig31, ROOT, "path1/*", "path1/foo");
697 ignored!(ig32, ROOT, ".a/b", ".a/b");
698 ignored!(ig33, "./", ".a/b", ".a/b");
699 ignored!(ig34, ".", ".a/b", ".a/b");
700 ignored!(ig35, "./.", ".a/b", ".a/b");
701 ignored!(ig36, "././", ".a/b", ".a/b");
702 ignored!(ig37, "././.", ".a/b", ".a/b");
703 ignored!(ig38, ROOT, "\\[", "[");
704 ignored!(ig39, ROOT, "\\?", "?");
705 ignored!(ig40, ROOT, "\\*", "*");
706 ignored!(ig41, ROOT, "\\a", "a");
707 ignored!(ig42, ROOT, "s*.rs", "sfoo.rs");
708 ignored!(ig43, ROOT, "**", "foo.rs");
709 ignored!(ig44, ROOT, "**/**/*", "a/foo.rs");
710
711 not_ignored!(ignot1, ROOT, "amonths", "months");
712 not_ignored!(ignot2, ROOT, "monthsa", "months");
713 not_ignored!(ignot3, ROOT, "/src/*.rs", "src/grep/src/main.rs");
714 not_ignored!(ignot4, ROOT, "/*.c", "mozilla-sha1/sha1.c");
715 not_ignored!(ignot5, ROOT, "/src/*.rs", "src/grep/src/main.rs");
716 not_ignored!(ignot6, ROOT, "*.rs\n!src/main.rs", "src/main.rs");
717 not_ignored!(ignot7, ROOT, "foo/", "foo", false);
718 not_ignored!(ignot8, ROOT, "**/foo/**", "wat/src/afoo/bar/baz");
719 not_ignored!(ignot9, ROOT, "**/foo/**", "wat/src/fooa/bar/baz");
720 not_ignored!(ignot10, ROOT, "**/foo/bar", "foo/src/bar");
721 not_ignored!(ignot11, ROOT, "#foo", "#foo");
722 not_ignored!(ignot12, ROOT, "\n\n\n", "foo");
723 not_ignored!(ignot13, ROOT, "foo/**", "foo", true);
724 not_ignored!(
725 ignot14,
726 "./third_party/protobuf",
727 "m4/ltoptions.m4",
728 "./third_party/protobuf/csharp/src/packages/repositories.config"
729 );
730 not_ignored!(ignot15, ROOT, "!/bar", "foo/bar");
731 not_ignored!(ignot16, ROOT, "*\n!**/", "foo", true);
732 not_ignored!(ignot17, ROOT, "src/*.rs", "src/grep/src/main.rs");
733 not_ignored!(ignot18, ROOT, "path1/*", "path2/path1/foo");
734 not_ignored!(ignot19, ROOT, "s*.rs", "src/foo.rs");
735
736 fn bytes(s: &str) -> Vec<u8> {
737 s.to_string().into_bytes()
738 }
739
740 fn path_string<P: AsRef<Path>>(path: P) -> String {
741 path.as_ref().to_str().unwrap().to_string()
742 }
743
744 #[test]
745 fn parse_excludes_file1() {
746 let data = bytes("[core]\nexcludesFile = /foo/bar");
747 let got = super::parse_excludes_file(&data).unwrap();
748 assert_eq!(path_string(got), "/foo/bar");
749 }
750
751 #[test]
752 fn parse_excludes_file2() {
753 let data = bytes("[core]\nexcludesFile = ~/foo/bar");
754 let got = super::parse_excludes_file(&data).unwrap();
755 assert_eq!(path_string(got), super::expand_tilde("~/foo/bar"));
756 }
757
758 #[test]
759 fn parse_excludes_file3() {
760 let data = bytes("[core]\nexcludeFile = /foo/bar");
761 assert!(super::parse_excludes_file(&data).is_none());
762 }
763
764 #[test]
766 fn regression_106() {
767 gi_from_str("/", " ");
768 }
769
770 #[test]
771 fn case_insensitive() {
772 let gi = GitignoreBuilder::new(ROOT)
773 .case_insensitive(true)
774 .unwrap()
775 .add_str(None, "*.html")
776 .unwrap()
777 .build()
778 .unwrap();
779 assert!(gi.matched("foo.html", false).is_ignore());
780 assert!(gi.matched("foo.HTML", false).is_ignore());
781 assert!(!gi.matched("foo.htm", false).is_ignore());
782 assert!(!gi.matched("foo.HTM", false).is_ignore());
783 }
784
785 ignored!(cs1, ROOT, "*.html", "foo.html");
786 not_ignored!(cs2, ROOT, "*.html", "foo.HTML");
787 not_ignored!(cs3, ROOT, "*.html", "foo.htm");
788 not_ignored!(cs4, ROOT, "*.html", "foo.HTM");
789}