unify stash logic

master
Ales Katona 2 years ago
parent b94276f559
commit d248ed7877
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -193,55 +193,40 @@ impl Vcs {
return Ok(()); return Ok(());
} }
let current_profile = self.current_profile()?; self.set_projects_using(profile_name, |project| {
project.switch_to_profile(profile_name)
for project in &mut self.projects { })
let mut stashed_changes = false;
if let Some(current_profile_name) = &current_profile {
stashed_changes = project.repo.stash_changes(current_profile_name)?;
}
let ref_name = project.switch_to_profile(profile_name)?;
let unstashed_changes = project.repo.unstash_changes(profile_name)?;
println!(
"{} set to {} {}{}",
project.name().bold().with(PROJECT_COLOR),
ref_name.with(REFS_COLOR),
if stashed_changes {
"«".cyan()
} else {
"".bold()
},
if unstashed_changes {
"»".dark_blue()
} else {
"".bold()
},
);
}
Ok(())
} }
pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> { pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> {
self.set_projects_using(branch_name, |project| project.repo.checkout(branch_name))
}
fn set_projects_using<F>(&mut self, dest_name: &str, setter: F) -> Result<(), Error>
where
F: Fn(&mut Project) -> Result<String, Error>,
{
for project in &mut self.projects { for project in &mut self.projects {
let current_ref = project.repo.current_ref()?; let current_ref = project.repo.current_ref()?;
// stash changes before moving project off to a new ref
let stashed_changes = project.repo.stash_changes(&current_ref)?; let stashed_changes = project.repo.stash_changes(&current_ref)?;
let new_ref = match project.repo.checkout(branch_name) { // try to switch to new ref using provided setter
let new_ref = match setter(project) {
Ok(val) => val, Ok(val) => val,
Err(err) => { Err(err) => {
eprintln!( eprintln!(
"{} unable to set {} ({})", "{} unable to set {} ({})",
project.name().bold().with(PROJECT_COLOR), project.name().bold().with(PROJECT_COLOR),
branch_name.with(REFS_COLOR), dest_name.with(REFS_COLOR),
err.to_string().with(ERROR_COLOR), err.to_string().with(ERROR_COLOR),
); );
continue; continue;
} }
}; };
let unstashed_changes = project.repo.unstash_changes(branch_name)?;
// unstash changes if we had previously stashed any on this ref
let unstashed_changes = project.repo.unstash_changes(&new_ref)?;
println!( println!(
"{} set to {} {}{}", "{} set to {} {}{}",
@ -259,6 +244,7 @@ impl Vcs {
}, },
); );
} }
Ok(()) Ok(())
} }

@ -39,8 +39,22 @@ impl Repository for GitRepository {
return Ok(false); return Ok(false);
} }
let repo_signature = self.repo.signature()?; let mut err = None;
let stash_name = self.stash_name(name); let stash_name = self.stash_name(name);
self.repo.stash_foreach(|_, name, _| {
if name.contains(&stash_name) {
err = Some(Error::project_error("Existing stashed changes found"));
false
} else {
true
}
})?;
if let Some(err) = err {
return Err(err);
}
let repo_signature = self.repo.signature()?;
self.repo.stash_save(&repo_signature, &stash_name, None)?; self.repo.stash_save(&repo_signature, &stash_name, None)?;
Ok(true) Ok(true)

Loading…
Cancel
Save