Skip to content

Commit f23a5f2

Browse files
committed
Merge pull request #115 from vrinek/init-with-gitignore-take-2
Move `.gitignore` directly under the root folder
2 parents 5316089 + bc41efe commit f23a5f2

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/bin/mdbook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
115115

116116
}
117117

118-
// Because of `src/book/mdbook.rs#L37-L39`, the following will always evaluate to `true`
118+
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`
119119
let is_dest_inside_root = book.get_dest().starts_with(book.get_root());
120120

121121
if !args.is_present("force") && is_dest_inside_root {

src/book/mdbook.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)