Skip to content

Commit a3749be

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 6a37786 commit a3749be

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/git.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::io::Write;
44
use std::path::{Path, PathBuf};
55
use std::process::Command;
66

7-
use swirl::PerformError;
87
use tempfile::TempDir;
98
use url::Url;
109

@@ -197,7 +196,7 @@ impl Repository {
197196
/// - If cloning the crate index fails.
198197
/// - If reading the global git config fails.
199198
///
200-
pub fn open(repository_config: &RepositoryConfig) -> Result<Self, PerformError> {
199+
pub fn open(repository_config: &RepositoryConfig) -> anyhow::Result<Self> {
201200
let checkout_path = tempfile::Builder::new().prefix("git").tempdir()?;
202201

203202
let repository = git2::build::RepoBuilder::new()
@@ -260,7 +259,7 @@ impl Repository {
260259
///
261260
/// - If the `HEAD` pointer can't be retrieved.
262261
///
263-
pub fn head_oid(&self) -> Result<git2::Oid, PerformError> {
262+
pub fn head_oid(&self) -> anyhow::Result<git2::Oid> {
264263
Ok(self.repository.head()?.target().unwrap())
265264
}
266265

@@ -269,7 +268,7 @@ impl Repository {
269268
///
270269
/// Note that `modified_file` expects a file path **relative** to the
271270
/// repository working folder!
272-
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> Result<(), PerformError> {
271+
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> anyhow::Result<()> {
273272
// git add $file
274273
let mut index = self.repository.index()?;
275274
index.add_path(modified_file)?;
@@ -288,7 +287,7 @@ impl Repository {
288287
}
289288

290289
/// Push the current branch to the provided refname
291-
fn push(&self, refspec: &str) -> Result<(), PerformError> {
290+
fn push(&self, refspec: &str) -> anyhow::Result<()> {
292291
let mut ref_status = Ok(());
293292
let mut callback_called = false;
294293
{
@@ -299,7 +298,7 @@ impl Repository {
299298
});
300299
callbacks.push_update_reference(|_, status| {
301300
if let Some(s) = status {
302-
ref_status = Err(format!("failed to push a ref: {}", s).into())
301+
ref_status = Err(anyhow!("failed to push a ref: {}", s))
303302
}
304303
callback_called = true;
305304
Ok(())
@@ -310,7 +309,7 @@ impl Repository {
310309
}
311310

312311
if !callback_called {
313-
ref_status = Err("update_reference callback was not called".into());
312+
ref_status = Err(anyhow!("update_reference callback was not called"));
314313
}
315314

316315
ref_status
@@ -323,7 +322,7 @@ impl Repository {
323322
///
324323
/// This function also prints the commit message and a success or failure
325324
/// message to the console.
326-
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> Result<(), PerformError> {
325+
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> anyhow::Result<()> {
327326
println!("Committing and pushing \"{}\"", message);
328327

329328
let relative_path = modified_file.strip_prefix(self.checkout_path.path())?;
@@ -337,7 +336,7 @@ impl Repository {
337336

338337
/// Fetches any changes from the `origin` remote and performs a hard reset
339338
/// to the tip of the `origin/master` branch.
340-
pub fn reset_head(&self) -> Result<(), PerformError> {
339+
pub fn reset_head(&self) -> anyhow::Result<()> {
341340
let mut origin = self.repository.find_remote("origin")?;
342341
let original_head = self.head_oid()?;
343342
origin.fetch(
@@ -371,7 +370,7 @@ impl Repository {
371370
}
372371

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

@@ -393,7 +392,7 @@ impl Repository {
393392
///
394393
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
395394
/// variable to ensure that `git push` commands are able to succeed.
396-
pub fn run_command(&self, command: &mut Command) -> Result<(), PerformError> {
395+
pub fn run_command(&self, command: &mut Command) -> anyhow::Result<()> {
397396
let checkout_path = self.checkout_path.path();
398397
command.current_dir(checkout_path);
399398

@@ -409,8 +408,7 @@ impl Repository {
409408
let output = command.output()?;
410409
if !output.status.success() {
411410
let stderr = String::from_utf8_lossy(&output.stderr);
412-
let message = format!("Running git command failed with: {}", stderr);
413-
return Err(message.into());
411+
return Err(anyhow!("Running git command failed with: {}", stderr));
414412
}
415413

416414
Ok(())

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)