diff --git a/src/vcs.rs b/src/vcs.rs index 31a0f6d..76415e5 100644 --- a/src/vcs.rs +++ b/src/vcs.rs @@ -224,13 +224,39 @@ impl Vcs { Ok(()) } - pub fn set_branch(&self, branch_name: &str) -> Result<(), Error> { - for project in &self.projects { - project.repo.checkout(branch_name)?; + pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> { + for project in &mut self.projects { + let current_ref = project.repo.current_ref()?; + let stashed_changes = project.repo.stash_changes(¤t_ref)?; + + let new_ref = match project.repo.checkout(branch_name) { + Ok(val) => val, + Err(err) => { + eprintln!( + "{} unable to set {} ({})", + project.name().bold().with(PROJECT_COLOR), + branch_name.with(REFS_COLOR), + err.to_string().with(ERROR_COLOR), + ); + continue; + } + }; + let unstashed_changes = project.repo.unstash_changes(branch_name)?; + println!( - "{} is set to branch: {}", - project.name().with(PROJECT_COLOR), - branch_name.bold().with(REFS_COLOR), + "{} set to {} {}{}", + project.name().bold().with(PROJECT_COLOR), + new_ref.with(REFS_COLOR), + if stashed_changes { + "«".cyan() + } else { + "".bold() + }, + if unstashed_changes { + "»".dark_blue() + } else { + "".bold() + }, ); } Ok(()) diff --git a/src/vcs/systems.rs b/src/vcs/systems.rs index 5437936..9914657 100644 --- a/src/vcs/systems.rs +++ b/src/vcs/systems.rs @@ -5,11 +5,11 @@ use super::Error; pub trait Repository { fn current_ref(&self) -> Result; - fn checkout(&self, ref_name: &str) -> Result<(), Error>; + fn checkout(&self, ref_name: &str) -> Result; - fn stash_changes(&mut self, profile_name: &str) -> Result; + fn stash_changes(&mut self, name: &str) -> Result; - fn unstash_changes(&mut self, profile_name: &str) -> Result; + fn unstash_changes(&mut self, name: &str) -> Result; fn uncommitted_changes(&self) -> Result, Error>; } diff --git a/src/vcs/systems/git.rs b/src/vcs/systems/git.rs index 196959f..760aa7c 100644 --- a/src/vcs/systems/git.rs +++ b/src/vcs/systems/git.rs @@ -22,7 +22,7 @@ impl Repository for GitRepository { } } - fn checkout(&self, name: &str) -> Result<(), Error> { + fn checkout(&self, name: &str) -> Result { let target_ref = self.repo.resolve_reference_from_short_name(name)?; let ref_name = target_ref .name() @@ -31,24 +31,24 @@ impl Repository for GitRepository { self.checkout_ref(ref_name)?; self.repo.set_head(ref_name)?; - Ok(()) + Ok(ref_name.into()) } - fn stash_changes(&mut self, profile_name: &str) -> Result { + fn stash_changes(&mut self, name: &str) -> Result { if self.uncommitted_changes()?.is_empty() { return Ok(false); } let repo_signature = self.repo.signature()?; - let stash_name = self.stash_name(profile_name); + let stash_name = self.stash_name(name); self.repo.stash_save(&repo_signature, &stash_name, None)?; Ok(true) } - fn unstash_changes(&mut self, profile_name: &str) -> Result { + fn unstash_changes(&mut self, name: &str) -> Result { let mut found_stash = None; - let stash_name = self.stash_name(profile_name); + let stash_name = self.stash_name(name); self.repo.stash_foreach(|idx, name, _| { if name.contains(&stash_name) { @@ -116,8 +116,8 @@ impl GitRepository { Ok(()) } - fn stash_name(&mut self, profile_name: &str) -> String { - format!("cargo-vcs_{}_{}", self.machine_id, profile_name) + fn stash_name(&mut self, name: &str) -> String { + format!("cargo-vcs_{}_{}", self.machine_id, name) } fn match_tag(&self) -> Result {