Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Gracefully fails whenunable to parse old api
Browse files Browse the repository at this point in the history
Previous system stopped the whole program at the first error, which
sometimes stopped the program while HEAD was pointing to an old commit.
This commit ensures that we always (try to) checkout back to the initial
git reference before stopping the program execution.
  • Loading branch information
Sasha Pourcelot committed Jun 9, 2021
1 parent 1547dc0 commit 56dafaf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ use anyhow::{Context, Result as AnyResult};
use git2::{Repository, StashFlags, StatusOptions};

pub(crate) trait GitBackend: Sized {
fn run_in<F, O>(&mut self, id: &str, f: F) -> AnyResult<O>
where
F: FnOnce() -> O,
{
self.switch_to(id)
.with_context(|| format!("Failed to checkout to {}", id))?;

let rslt = f();

self.switch_back().with_context(|| {
format!(
"Failed to switch back to {}",
self.previous_branch().unwrap()
)
})?;

Ok(rslt)
}

fn switch_to(&mut self, id: &str) -> AnyResult<()> {
if self.needs_stash() {
self.stash_push().context("Failed to stash changes")?;
Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ pub fn run() -> AnyResult<()> {

let mut repo = CrateRepo::current().context("Failed to fetch repository data")?;

let current_api = glue::extract_api().context("Failed to get crate API")?;
let version = manifest::get_crate_version().context("Failed to get crate version")?;

repo.switch_to(config.comparaison_ref.as_str())
.with_context(|| format!("Failed to checkout to `{}`", config.comparaison_ref))?;

let previous_api = glue::extract_api().context("Failed to get crate API")?;
let current_api = glue::extract_api().context("Failed to get crate API")?;

repo.switch_back()
.context("Failed to go back to initial branch")?;
let previous_api = repo.run_in(config.comparaison_ref.as_str(), || {
glue::extract_api().context("Failed to get crate API")
})??;

let api_comparator = ApiComparator::new(previous_api, current_api);

Expand Down

0 comments on commit 56dafaf

Please sign in to comment.