summaryrefslogtreecommitdiff
path: root/src/bin/git-restore-autosave.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/git-restore-autosave.rs')
-rw-r--r--src/bin/git-restore-autosave.rs85
1 files changed, 17 insertions, 68 deletions
diff --git a/src/bin/git-restore-autosave.rs b/src/bin/git-restore-autosave.rs
index a8fdb9a..482e2b5 100644
--- a/src/bin/git-restore-autosave.rs
+++ b/src/bin/git-restore-autosave.rs
@@ -10,24 +10,10 @@
// - if there is more than one option, enter pick mode
// - if there are no options, tell the user to use --all-users or --all-branches or --anytime
-use std::fmt::Display;
-
use auth_git2::GitAuthenticator;
-use chrono::Local;
-use git_autosave::{Autosave, Config, authenticate::Inquirer};
+use git_autosave::{Config, authenticate::Inquirer, inquire::AutosaveFilters};
use git2::{RemoteCallbacks, Repository, build::CheckoutBuilder};
-struct AutosaveOption {
- text: String,
- value: Autosave,
-}
-
-impl Display for AutosaveOption {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- self.text.fmt(f)
- }
-}
-
fn main() -> Result<(), anyhow::Error> {
let all_users = std::env::args().any(|arg| arg == "--all-users");
let all_branches = std::env::args().any(|arg| arg == "--all-branches");
@@ -48,23 +34,21 @@ fn main() -> Result<(), anyhow::Error> {
callbacks.credentials(auth.credentials(&gitconfig));
git_autosave::fetch_autosaves(&repository, callbacks)?;
- let mut autosaves = git_autosave::autosaves(&repository)?
- .into_iter()
- .filter(|autosave| {
- all_users
- || signature
- .name()
- .zip(autosave.author.clone())
- .is_some_and(|(a, b)| a == b)
- || signature
- .email()
- .zip(autosave.email.clone())
- .is_some_and(|(a, b)| a == b)
- })
- .filter(|autosave| all_branches || autosave.branch_name == branch)
- .filter(|autosave| anytime || autosave.time > earliest_time)
- .filter(|autosave| all_devices || autosave.repo_id.as_bytes() != repo_id.as_bytes())
- .collect::<Vec<_>>();
+ let autosaves = git_autosave::autosaves(&repository)?;
+ let autosaves = git_autosave::inquire::filter_autosaves(
+ autosaves,
+ AutosaveFilters {
+ signature: &signature,
+ branch: &branch,
+ earliest_time,
+ repo_id: &repo_id,
+ all_users,
+ all_branches,
+ anytime,
+ all_devices,
+ },
+ )
+ .collect::<Vec<_>>();
if autosaves.is_empty() {
eprintln!("ERROR: There are no available autosaves for the given filters.");
@@ -76,42 +60,7 @@ fn main() -> Result<(), anyhow::Error> {
std::process::exit(1);
}
- let autosave = if autosaves.len() > 1 {
- let autosaves = autosaves
- .into_iter()
- .map(|autosave| {
- let device = autosave.host_name.as_ref().unwrap_or(&autosave.repo_id);
- let time = chrono::DateTime::from_timestamp(autosave.time.seconds(), 0)
- .map(|time| time.with_timezone(&Local))
- .map(|time| time.to_rfc2822())
- .unwrap_or(autosave.time.seconds().to_string());
- let branch = if all_branches {
- format!(" on {}", &autosave.branch_name)
- } else {
- String::new()
- };
- let author = if let Some(author) =
- autosave.author.as_ref().or(autosave.email.as_ref())
- && all_users
- {
- format!(" by {author}")
- } else {
- String::new()
- };
- AutosaveOption {
- text: format!("{device} ({time}{branch}{author})"),
- value: autosave,
- }
- })
- .collect();
- inquire::Select::new("Select an autosave:", autosaves)
- .prompt()?
- .value
- } else {
- autosaves
- .pop()
- .expect("There should be an autosave to select but there isn't. This is a bug!")
- };
+ let autosave = git_autosave::inquire::select_autosave(autosaves, all_branches, all_users)?;
let autosaved_commit = repository.find_commit(autosave.commit_id)?;
let workdir = repository.find_tree(git_autosave::utils::workdir_to_tree(&repository)?)?;
let new_tree =