Skip to content

Commit f58f9de

Browse files
committed
Repository: Use anyhow errors as return types
This makes it easier to provide additional context on some of these errors
1 parent 910ac64 commit f58f9de

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/git.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use anyhow::anyhow;
12
use std::collections::HashMap;
23
use std::path::{Path, PathBuf};
34

4-
use swirl::PerformError;
55
use tempfile::TempDir;
66
use url::Url;
77

@@ -143,7 +143,7 @@ pub struct Repository {
143143
}
144144

145145
impl Repository {
146-
pub fn open(repository_config: &RepositoryConfig) -> Result<Self, PerformError> {
146+
pub fn open(repository_config: &RepositoryConfig) -> anyhow::Result<Self> {
147147
let checkout_path = tempfile::Builder::new().prefix("git").tempdir()?;
148148

149149
let repository = git2::build::RepoBuilder::new()
@@ -190,11 +190,11 @@ impl Repository {
190190
}
191191
}
192192

193-
pub fn head_oid(&self) -> Result<git2::Oid, PerformError> {
193+
pub fn head_oid(&self) -> anyhow::Result<git2::Oid> {
194194
Ok(self.repository.head()?.target().unwrap())
195195
}
196196

197-
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> Result<(), PerformError> {
197+
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> anyhow::Result<()> {
198198
// git add $file
199199
let mut index = self.repository.index()?;
200200
index.add_path(modified_file)?;
@@ -213,7 +213,7 @@ impl Repository {
213213
}
214214

215215
/// Push the current branch to the provided refname
216-
fn push(&self, refspec: &str) -> Result<(), PerformError> {
216+
fn push(&self, refspec: &str) -> anyhow::Result<()> {
217217
let mut ref_status = Ok(());
218218
let mut callback_called = false;
219219
{
@@ -224,7 +224,7 @@ impl Repository {
224224
});
225225
callbacks.push_update_reference(|_, status| {
226226
if let Some(s) = status {
227-
ref_status = Err(format!("failed to push a ref: {}", s).into())
227+
ref_status = Err(anyhow!("failed to push a ref: {}", s))
228228
}
229229
callback_called = true;
230230
Ok(())
@@ -235,13 +235,13 @@ impl Repository {
235235
}
236236

237237
if !callback_called {
238-
ref_status = Err("update_reference callback was not called".into());
238+
ref_status = Err(anyhow!("update_reference callback was not called"));
239239
}
240240

241241
ref_status
242242
}
243243

244-
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> Result<(), PerformError> {
244+
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> anyhow::Result<()> {
245245
println!("Committing and pushing \"{}\"", message);
246246

247247
let relative_path = modified_file.strip_prefix(self.checkout_path.path())?;
@@ -253,7 +253,7 @@ impl Repository {
253253
})
254254
}
255255

256-
pub fn reset_head(&self) -> Result<(), PerformError> {
256+
pub fn reset_head(&self) -> anyhow::Result<()> {
257257
let mut origin = self.repository.find_remote("origin")?;
258258
let original_head = self.head_oid()?;
259259
origin.fetch(
@@ -287,7 +287,7 @@ impl Repository {
287287
}
288288

289289
/// Reset `HEAD` to a single commit with all the index contents, but no parent
290-
pub fn squash_to_single_commit(&self, msg: &str) -> Result<(), PerformError> {
290+
pub fn squash_to_single_commit(&self, msg: &str) -> anyhow::Result<()> {
291291
let tree = self.repository.find_commit(self.head_oid()?)?.tree()?;
292292
let sig = self.repository.signature()?;
293293

src/worker/git.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub fn add_crate(env: &Environment, krate: Crate) -> Result<(), PerformError> {
2323

2424
let message: String = format!("Updating crate `{}#{}`", krate.name, krate.vers);
2525

26-
repo.commit_and_push(&message, &dst)
26+
repo.commit_and_push(&message, &dst)?;
27+
28+
Ok(())
2729
}
2830

2931
/// Yanks or unyanks a crate version. This requires finding the index

0 commit comments

Comments
 (0)