From df7a5fa2819787aab84e2ae7e8472e5b27d38b0e Mon Sep 17 00:00:00 2001 From: epaez Date: Thu, 13 Nov 2025 12:03:40 -0500 Subject: [PATCH] impl restore file from files_tab --- src/commander/files.rs | 25 +++++++++++++++++++++++++ src/ui/files_tab.rs | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/commander/files.rs b/src/commander/files.rs index d9929b84..ff66080a 100644 --- a/src/commander/files.rs +++ b/src/commander/files.rs @@ -183,6 +183,31 @@ impl Commander { true, )?)) } + + #[instrument(level = "trace", skip(self))] + pub fn restore_file(&self, current_file: &File) -> Result, 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('"', "\\\"")); + Ok(Some(self.execute_jj_command( + vec!["restore", &fileset], + false, + true, + )?)) + } } #[cfg(test)] diff --git a/src/ui/files_tab.rs b/src/ui/files_tab.rs index 3d758b4f..bd64cb8e 100644 --- a/src/ui/files_tab.rs +++ b/src/ui/files_tab.rs @@ -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(); @@ -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)?; @@ -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![