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(());
}
let current_profile = self.current_profile()?;
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()
},
);
self.set_projects_using(profile_name, |project| {
project.switch_to_profile(profile_name)
})
}
Ok(())
pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> {
self.set_projects_using(branch_name, |project| project.repo.checkout(branch_name))
}
pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> {
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 {
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 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,
Err(err) => {
eprintln!(
"{} unable to set {} ({})",
project.name().bold().with(PROJECT_COLOR),
branch_name.with(REFS_COLOR),
dest_name.with(REFS_COLOR),
err.to_string().with(ERROR_COLOR),
);
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!(
"{} set to {} {}{}",
@ -259,6 +244,7 @@ impl Vcs {
},
);
}
Ok(())
}

@ -39,8 +39,22 @@ impl Repository for GitRepository {
return Ok(false);
}
let repo_signature = self.repo.signature()?;
let mut err = None;
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)?;
Ok(true)

Loading…
Cancel
Save