diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 8aafc0f6..4032bfb4 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -115,7 +115,7 @@ fn init(args: &ArgMatches) -> Result<(), Box> { } - // Because of `src/book/mdbook.rs#L37-L39`, the following will always evaluate to `true` + // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root` let is_dest_inside_root = book.get_dest().starts_with(book.get_root()); if !args.is_present("force") && is_dest_inside_root { diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 41038d70..0b0cb20f 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -166,6 +166,17 @@ impl MDBook { if !gitignore.exists() { // Gitignore does not exist, create it + // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`. If it + // is not, `strip_prefix` will return an Error. + if !self.get_dest().starts_with(self.get_root()) { + return; + } + + let relative = self.get_dest().strip_prefix(self.get_root()) + .expect("Destination is not relative to root."); + let relative = relative.to_str() + .expect("Path could not be yielded into a string slice."); + debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore); let mut f = File::create(&gitignore) @@ -173,15 +184,7 @@ impl MDBook { debug!("[*]: Writing to .gitignore"); - writeln!(f, "# Ignore everything within this folder") - .expect("Could not write to file."); - writeln!(f, "*") - .expect("Could not write to file."); - writeln!(f, "") - .expect("Could not write to file."); - writeln!(f, "# Except this file") - .expect("Could not write to file."); - writeln!(f, "!.gitignore") + writeln!(f, "{}", relative) .expect("Could not write to file."); } } @@ -206,7 +209,7 @@ impl MDBook { pub fn get_gitignore(&self) -> PathBuf { - self.config.get_dest().join(".gitignore") + self.config.get_root().join(".gitignore") } pub fn copy_theme(&self) -> Result<(), Box> {