diff options
Diffstat (limited to 'src/journal.rs')
-rw-r--r-- | src/journal.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/journal.rs b/src/journal.rs index 41e6d3d..2610a44 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -1,4 +1,5 @@ use crate::error::JournalError; +use crate::git; use crate::template::Templates; use chrono::{DateTime, Local}; use std::path::{Path, PathBuf}; @@ -21,6 +22,7 @@ impl Journal { pub fn init(path: &Path, entries: &Path) -> Result<Self, JournalError> { std::fs::create_dir(path) .map_err(|err| JournalError::CreateDirectory(path.to_path_buf(), err))?; + git::init(path)?; std::fs::create_dir(entries) .map_err(|err| JournalError::CreateDirectory(entries.to_path_buf(), err))?; Ok(Self { @@ -49,6 +51,13 @@ impl Journal { &self.dirname } + fn relative(&self, path: &Path) -> Result<PathBuf, JournalError> { + let path = path.strip_prefix(self.dirname()).map_err(|err| { + JournalError::RelativePath(path.to_path_buf(), self.dirname().to_path_buf(), err) + })?; + Ok(path.to_path_buf()) + } + fn drafts(&self) -> PathBuf { self.dirname().join("drafts") } @@ -153,6 +162,12 @@ impl Journal { std::fs::rename(filename, &entry).map_err(|err| { JournalError::RenameEntry(filename.to_path_buf(), entry.to_path_buf(), err) })?; + + let entry = self.relative(&entry)?; + git::add(self.dirname(), &[&entry])?; + + let msg = format!("journal entry {}", entry.display()); + git::commit(self.dirname(), &msg)?; Ok(()) } @@ -165,6 +180,13 @@ impl Journal { std::fs::write(&pathname, text) .map_err(|err| JournalError::WriteTopic(pathname.to_path_buf(), err))?; self.edit(editor, &pathname)?; + + let topic = self.relative(&pathname)?; + git::add(self.dirname(), &[&topic])?; + + let msg = format!("new topic {}", topic.display()); + git::commit(self.dirname(), &msg)?; + Ok(()) } } |