normalize path in watch cmd

This commit is contained in:
leonzchang 2023-10-31 12:13:25 +08:00
parent 94e0a44e15
commit fbb629c02e
No known key found for this signature in database
GPG Key ID: DBC5671A30EB9CA3
1 changed files with 7 additions and 1 deletions

View File

@ -4,6 +4,7 @@ use ignore::gitignore::Gitignore;
use mdbook::errors::Result; use mdbook::errors::Result;
use mdbook::utils; use mdbook::utils;
use mdbook::MDBook; use mdbook::MDBook;
use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread::sleep; use std::thread::sleep;
@ -87,11 +88,16 @@ fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
} }
fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> { fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
let current_dir = env::current_dir().expect("Unable to determine the current directory");
paths paths
.iter() .iter()
.filter(|path| { .filter(|path| {
let normalized_path = path
.strip_prefix(&current_dir)
.expect("Could not normalize path");
!ignore !ignore
.matched_path_or_any_parents(path, path.is_dir()) .matched_path_or_any_parents(normalized_path, normalized_path.is_dir())
.is_ignore() .is_ignore()
}) })
.map(|path| path.to_path_buf()) .map(|path| path.to_path_buf())