Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/commander/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ impl Commander {
true,
)?))
}

#[instrument(level = "trace", skip(self))]
pub fn restore_file(&self, current_file: &File) -> Result<Option<String>, CommandError> {
let Some(path) = current_file.path.as_ref() else {
return Ok(None);
};

let path = if let Some(DiffType::Renamed) = current_file.diff_type
&& let Some(captures) = RENAME_REGEX.captures(path)
{
match captures.get(2) {
Some(path) => path.as_str(),
None => return Ok(None),
}
} else {
path
};

let fileset = format!("file:\"{}\"", path.replace('"', "\\\""));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also changed this so files with double quotes can be restored.

Ok(Some(self.execute_jj_command(
vec!["restore", &fileset],
false,
true,
)?))
}
}

#[cfg(test)]
Expand Down
21 changes: 21 additions & 0 deletions src/ui/files_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ impl FilesTab {
Ok(())
}

pub fn restore_file(&mut self, commander: &mut Commander) -> Result<()> {
self.file
.as_ref()
.map(|current_file| commander.restore_file(current_file))
.transpose()?;
Ok(())
}

fn scroll_files(&mut self, commander: &mut Commander, scroll: isize) -> Result<()> {
if let Ok(files) = self.files_output.as_ref() {
let current_file_index = self.get_current_file_index();
Expand Down Expand Up @@ -355,6 +363,18 @@ impl Component for FilesTab {
}
self.set_head(commander, &commander.get_current_head()?)?;
}
KeyCode::Char('r') => {
if let Err(err) = self.restore_file(commander) {
return Ok(ComponentInputResult::HandledAction(
ComponentAction::SetPopup(Some(Box::new(MessagePopup {
title: "Can't restore file".into(),
messages: err.to_string().into(),
text_align: None,
}))),
));
}
self.set_head(commander, &commander.get_current_head()?)?;
}
KeyCode::Char('R') | KeyCode::F(5) => {
self.head = commander.get_head_latest(&self.head)?;
self.refresh_files(commander)?;
Expand All @@ -371,6 +391,7 @@ impl Component for FilesTab {
("j/k".to_owned(), "scroll down/up".to_owned()),
("J/K".to_owned(), "scroll down by ½ page".to_owned()),
("x".to_owned(), "untrack file".to_owned()),
("r".to_owned(), "restore file".to_owned()),
("@".to_owned(), "view current change files".to_owned()),
],
vec![
Expand Down
Loading