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(())
}
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(&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!(
"{} 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(())

@ -5,11 +5,11 @@ use super::Error;
pub trait Repository {
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>;
}

@ -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 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<bool, Error> {
fn stash_changes(&mut self, name: &str) -> Result<bool, Error> {
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<bool, Error> {
fn unstash_changes(&mut self, name: &str) -> Result<bool, Error> {
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<String, Error> {

Loading…
Cancel
Save