Skip to content

Commit

Permalink
Added 'undo' command.
Browse files Browse the repository at this point in the history
  • Loading branch information
svenslaggare committed Aug 19, 2023
1 parent 0c9309d commit fcca7b6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ impl App {
return Err(err);
}
}
InputCommand::Undo { commit } => {
self.create_and_execute_commands(vec![
Command::UndoCommit { commit }
])?;
}
InputCommand::RunSnippet { path, save_output } => {
let path = self.get_path(path)?;

Expand Down Expand Up @@ -535,6 +540,11 @@ pub enum InputCommand {
#[structopt(long, short)]
recursive: bool
},
/// Undo the given commit
Undo {
/// The git commit to undo
commit: String
},
/// Runs the code snippet contained in a note.
#[structopt(name="run")]
RunSnippet {
Expand Down
64 changes: 53 additions & 11 deletions src/app_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ print([x * x for x in xs])
}

#[test]
fn test_add_and_run_snippet() {
fn test_run_snippet() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -159,7 +159,7 @@ print([x * x for x in xs])
}

#[test]
fn test_add_and_move() {
fn test_move() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -195,7 +195,7 @@ print(np.square(np.arange(0, 10)))
}

#[test]
fn test_add_and_move_to_existing1() {
fn test_move_to_existing1() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -238,7 +238,7 @@ fn test_add_and_move_to_existing1() {
}

#[test]
fn test_add_and_move_to_existing2() {
fn test_move_to_existing2() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -277,7 +277,7 @@ fn test_add_and_move_to_existing2() {
}

#[test]
fn test_add_and_move_dir1() {
fn test_move_dir1() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -331,7 +331,7 @@ print(np.square(np.arange(0, 15)))
}

#[test]
fn test_add_and_move_dir2() {
fn test_move_dir2() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -385,7 +385,7 @@ print(np.square(np.arange(0, 15)))
}

#[test]
fn test_add_and_move_dir_to_existing1() {
fn test_move_dir_to_existing1() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -448,7 +448,7 @@ fn test_add_and_move_dir_to_existing1() {
}

#[test]
fn test_add_and_move_file_to_dir() {
fn test_move_file_to_dir() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -489,7 +489,7 @@ print(np.square(np.arange(0, 10)))
}

#[test]
fn test_add_and_remove() {
fn test_remove() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -524,7 +524,7 @@ print(np.square(np.arange(0, 10)))
}

#[test]
fn test_add_and_remove_recursive() {
fn test_remove_recursive() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -564,7 +564,7 @@ fn test_add_and_remove_recursive() {
}

#[test]
fn test_add_and_change_tags() {
fn test_change_tags() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -776,4 +776,46 @@ print([x * x for x in xs])
}).unwrap();
assert_eq!(note_content, app.note_metadata_storage().unwrap().get_content(note_path).unwrap());
assert_eq!(3, repository.reflog("HEAD").unwrap().len());
}

#[test]
fn test_undo() {
use tempfile::TempDir;

let temp_repository_dir = TempDir::new().unwrap();
let config = Config::from_env(FileConfig::new(&temp_repository_dir.path().to_path_buf()));
let repository = git2::Repository::init(&config.repository).unwrap();

let note_path = Path::new("2023/07/sample");
let note_content1 = "Test1".to_owned();
let note_content2 = "Test2".to_owned();

let mut app = App::new(config).unwrap();

app.create_and_execute_commands(vec![
Command::AddNoteWithContent {
path: note_path.to_path_buf(),
tags: vec![],
content: note_content1.clone()
},
]).unwrap();
assert_eq!(note_content1, app.note_metadata_storage().unwrap().get_content(note_path).unwrap());
assert_eq!(1, repository.reflog("HEAD").unwrap().len());

app.create_and_execute_commands(vec![
Command::EditNoteSetContent {
path: note_path.to_path_buf(),
clear_tags: false,
add_tags: vec![],
content: note_content2.clone()
},
]).unwrap();
assert_eq!(note_content2, app.note_metadata_storage().unwrap().get_content(note_path).unwrap());
assert_eq!(2, repository.reflog("HEAD").unwrap().len());
let commit_id = repository.reflog("HEAD").unwrap().get(0).unwrap().id_new();

app.run(InputCommand::Undo { commit: commit_id.to_string() }).unwrap();
assert_eq!(note_content1, app.note_metadata_storage().unwrap().get_content(note_path).unwrap());
assert_eq!(3, repository.reflog("HEAD").unwrap().len());

}
33 changes: 31 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum Command {
RemoveNote {
path: PathBuf
},
UndoCommit {
commit: String
},
RunSnippet {
path: PathBuf,
save_output: bool
Expand Down Expand Up @@ -195,6 +198,21 @@ impl CommandInterpreter {
Command::RemoveNote { path } => {
self.remove_note(&path)?;
}
Command::UndoCommit { commit } => {
let git_commit_id = {
let repository = self.repository.borrow_mut();
let git_commit = repository.revparse_single(&commit)?;
let git_commit = git_commit.as_commit().ok_or_else(|| CommitNotFound(commit.clone()))?;
let git_commit_id = git_commit.as_object().short_id().unwrap().as_str().unwrap().to_owned();

repository.revert(&git_commit, None).map_err(|err| FailedToUndo(err.to_string()))?;
repository.cleanup_state()?;

git_commit_id
};

self.commit_message_lines.insert(format!("Undo commit '{}'.", git_commit_id));
},
Command::RunSnippet { path, save_output } => {
let id = self.get_note_id(&path)?;
let (relative_note_path, abs_note_path) = self.get_note_storage_path(&id);
Expand Down Expand Up @@ -357,11 +375,17 @@ impl CommandInterpreter {
index.add_path(&relative_metadata_path)?;
index.write()?;

let tags_str = if !metadata.tags.is_empty() {
format!(" using tags: {}", metadata.tags.join(", "))
} else {
String::new()
};

self.commit_message_lines.insert(format!(
"Added note '{}' (id: {}) using tags: {}.",
"Added note '{}' (id: {}) {}.",
path.to_str().unwrap(),
id,
metadata.tags.join(", ")
tags_str
));

Ok(())
Expand Down Expand Up @@ -555,6 +579,8 @@ pub enum CommandError {
FailedToRemoveNote(String),
#[error("Failed to commit: {0}")]
FailedToCommit(String),
#[error("Failed to undo commit: {0}")]
FailedToUndo(String),

#[error("Failed to update metadata: {0}")]
FailedToUpdateMetadata(String),
Expand All @@ -565,6 +591,9 @@ pub enum CommandError {
#[error("Existing note at destination '{0}', use -f to delete that note before moving")]
NoteExistsAtDestination(PathBuf),

#[error("Commit {0} not found")]
CommitNotFound(String),

#[error("Failed to run snippet: {0}")]
Snippet(SnippetError),

Expand Down

0 comments on commit fcca7b6

Please sign in to comment.