about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <git@emile.space>2022-02-03 21:53:51 +0100
committerEmile <git@emile.space>2022-02-03 21:53:51 +0100
commitef7c7c7f6eaa0fb7b14dbe344ab749a1c86896cd (patch)
tree5e41c9e74e8eca2582ee876dee71e52697e71ec6
parent2be33f020de35372ec219dbe8024cce0e02fcfbe (diff)
don't display files part of the .gitignore
-rw-r--r--src/main.rs39
1 files changed, 36 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 9385e01..56a3f7f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -501,13 +501,29 @@ fn recursive_read_dir(dir: &PathBuf, dir_only: bool) -> io::Result<Vec<PathBuf>>
         return Ok(vec![]);
     }
 
+    // get all entries in the gitignore file, if it exists
+    let gitignore_entries: Vec<PathBuf> = gitignore_entries(&dir)?;
+
     // store the child pathes
     let mut entries: Vec<PathBuf> = Vec::new();
     
     // iterate over all items in the dir, pushing the dirs pathes to the dirs
-    // vector for returnin it
-    for entry in fs::read_dir(dir)? {
-        let path = &entry?.path();
+    // vector for returning it
+    'outer: for entry in fs::read_dir(dir)? {
+        let dir_entry = &entry?;
+        let path = dir_entry.path();
+
+        // check if the current entry is part of the gitignore, if so, skip it
+        for gitignore_entry in &gitignore_entries {
+            if gitignore_entry.to_str() == Some("") {
+                continue;
+            }
+            if path.ends_with(gitignore_entry) {
+                println!("gitignore: gitignore_entry: {:?}", gitignore_entry);
+                println!("gitignore: path: {:?}", path);
+                continue 'outer;
+            }
+        }
 
         if dir_only == true {
             if path.is_dir() {
@@ -527,4 +543,21 @@ fn recursive_read_dir(dir: &PathBuf, dir_only: bool) -> io::Result<Vec<PathBuf>>
 
     // return the dirs, the ones from this folder and the ones from all child folders
     Ok(entries)
+}
+
+// try to open the gitignore file and read all entries from there.
+fn gitignore_entries(dir: &PathBuf) -> io::Result<Vec<PathBuf>> {
+    let gitignore_path = Path::new(&dir)
+        .join(Path::new(".gitignore"));
+
+    let mut entries: Vec<PathBuf> = Vec::new();
+    if let Ok(gitignore) = File::open(&gitignore_path) {
+        let reader = BufReader::new(gitignore);
+
+        for line in reader.lines() {
+            entries.push(PathBuf::from(line?));
+        }
+    }
+
+    Ok(entries)
 }
\ No newline at end of file