@@ -166,22 +166,25 @@ impl MDBook {
166166 if !gitignore. exists ( ) {
167167 // Gitignore does not exist, create it
168168
169+ // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`. If it
170+ // is not, `strip_prefix` will return an Error.
171+ if !self . get_dest ( ) . starts_with ( self . get_root ( ) ) {
172+ return ;
173+ }
174+
175+ let relative = self . get_dest ( ) . strip_prefix ( self . get_root ( ) )
176+ . expect ( "Destination is not relative to root." ) ;
177+ let relative = relative. to_str ( )
178+ . expect ( "Path could not be yielded into a string slice." ) ;
179+
169180 debug ! ( "[*]: {:?} does not exist, trying to create .gitignore" , gitignore) ;
170181
171182 let mut f = File :: create ( & gitignore)
172183 . expect ( "Could not create file." ) ;
173184
174185 debug ! ( "[*]: Writing to .gitignore" ) ;
175186
176- writeln ! ( f, "# Ignore everything within this folder" )
177- . expect ( "Could not write to file." ) ;
178- writeln ! ( f, "*" )
179- . expect ( "Could not write to file." ) ;
180- writeln ! ( f, "" )
181- . expect ( "Could not write to file." ) ;
182- writeln ! ( f, "# Except this file" )
183- . expect ( "Could not write to file." ) ;
184- writeln ! ( f, "!.gitignore" )
187+ writeln ! ( f, "{}" , relative)
185188 . expect ( "Could not write to file." ) ;
186189 }
187190 }
@@ -206,7 +209,7 @@ impl MDBook {
206209
207210
208211 pub fn get_gitignore ( & self ) -> PathBuf {
209- self . config . get_dest ( ) . join ( ".gitignore" )
212+ self . config . get_root ( ) . join ( ".gitignore" )
210213 }
211214
212215 pub fn copy_theme ( & self ) -> Result < ( ) , Box < Error > > {
0 commit comments