fix branch setting to ignore errors per project

master
Ales Katona 2 years ago
parent 57f3271155
commit bae3180a15
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -224,13 +224,39 @@ impl Vcs {
Ok(()) Ok(())
} }
pub fn set_branch(&self, branch_name: &str) -> Result<(), Error> { pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> {
for project in &self.projects { for project in &mut self.projects {
project.repo.checkout(branch_name)?; let current_ref = project.repo.current_ref()?;
let stashed_changes = project.repo.stash_changes(&current_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!( println!(
"{} is set to branch: {}", "{} set to {} {}{}",
project.name().with(PROJECT_COLOR), project.name().bold().with(PROJECT_COLOR),
branch_name.bold().with(REFS_COLOR), new_ref.with(REFS_COLOR),
if stashed_changes {
"«".cyan()
} else {
"".bold()
},
if unstashed_changes {
"»".dark_blue()
} else {
"".bold()
},
); );
} }
Ok(()) Ok(())

@ -5,11 +5,11 @@ use super::Error;
pub trait Repository { pub trait Repository {
fn current_ref(&self) -> Result<String, Error>; fn current_ref(&self) -> Result<String, Error>;
fn checkout(&self, ref_name: &str) -> Result<(), Error>; fn checkout(&self, ref_name: &str) -> Result<String, Error>;
fn stash_changes(&mut self, profile_name: &str) -> Result<bool, Error>; fn stash_changes(&mut self, name: &str) -> Result<bool, Error>;
fn unstash_changes(&mut self, profile_name: &str) -> Result<bool, Error>; fn unstash_changes(&mut self, name: &str) -> Result<bool, Error>;
fn uncommitted_changes(&self) -> Result<Vec<String>, Error>; fn uncommitted_changes(&self) -> Result<Vec<String>, Error>;
} }

@ -22,7 +22,7 @@ impl Repository for GitRepository {
} }
} }
fn checkout(&self, name: &str) -> Result<(), Error> { fn checkout(&self, name: &str) -> Result<String, Error> {
let target_ref = self.repo.resolve_reference_from_short_name(name)?; let target_ref = self.repo.resolve_reference_from_short_name(name)?;
let ref_name = target_ref let ref_name = target_ref
.name() .name()
@ -31,24 +31,24 @@ impl Repository for GitRepository {
self.checkout_ref(ref_name)?; self.checkout_ref(ref_name)?;
self.repo.set_head(ref_name)?; self.repo.set_head(ref_name)?;
Ok(()) Ok(ref_name.into())
} }
fn stash_changes(&mut self, profile_name: &str) -> Result<bool, Error> { fn stash_changes(&mut self, name: &str) -> Result<bool, Error> {
if self.uncommitted_changes()?.is_empty() { if self.uncommitted_changes()?.is_empty() {
return Ok(false); return Ok(false);
} }
let repo_signature = self.repo.signature()?; 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)?; self.repo.stash_save(&repo_signature, &stash_name, None)?;
Ok(true) Ok(true)
} }
fn unstash_changes(&mut self, profile_name: &str) -> Result<bool, Error> { fn unstash_changes(&mut self, name: &str) -> Result<bool, Error> {
let mut found_stash = None; 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, _| { self.repo.stash_foreach(|idx, name, _| {
if name.contains(&stash_name) { if name.contains(&stash_name) {
@ -116,8 +116,8 @@ impl GitRepository {
Ok(()) Ok(())
} }
fn stash_name(&mut self, profile_name: &str) -> String { fn stash_name(&mut self, name: &str) -> String {
format!("cargo-vcs_{}_{}", self.machine_id, profile_name) format!("cargo-vcs_{}_{}", self.machine_id, name)
} }
fn match_tag(&self) -> Result<String, Error> { fn match_tag(&self) -> Result<String, Error> {

Loading…
Cancel
Save