Skip to content

Commit 552deef

Browse files
committed
Auto merge of #4366 - Turbo87:git-anyhow, r=Turbo87
Repository: Use `anyhow` errors as return types This makes it easier to provide additional context on some of these errors and ensure that the source error is correctly propagated.
2 parents 516f473 + 9d1bc87 commit 552deef

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/git.rs

Lines changed: 30 additions & 19 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,8 +196,11 @@ 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> {
201-
let checkout_path = tempfile::Builder::new().prefix("git").tempdir()?;
199+
pub fn open(repository_config: &RepositoryConfig) -> anyhow::Result<Self> {
200+
let checkout_path = tempfile::Builder::new()
201+
.prefix("git")
202+
.tempdir()
203+
.context("Failed to create temporary directory")?;
202204

203205
let repository = git2::build::RepoBuilder::new()
204206
.fetch_options(Self::fetch_options(&repository_config.credentials))
@@ -213,13 +215,21 @@ impl Repository {
213215
.clone(
214216
repository_config.index_location.as_str(),
215217
checkout_path.path(),
216-
)?;
218+
)
219+
.context("Failed to clone index repository")?;
217220

218221
// All commits to the index registry made through crates.io will be made by bors, the Rust
219222
// community's friendly GitHub bot.
220-
let mut cfg = repository.config()?;
221-
cfg.set_str("user.name", "bors")?;
222-
cfg.set_str("user.email", "[email protected]")?;
223+
224+
let mut cfg = repository
225+
.config()
226+
.context("Failed to read git configuration")?;
227+
228+
cfg.set_str("user.name", "bors")
229+
.context("Failed to set user name")?;
230+
231+
cfg.set_str("user.email", "[email protected]")
232+
.context("Failed to set user email address")?;
223233

224234
Ok(Self {
225235
checkout_path,
@@ -260,16 +270,18 @@ impl Repository {
260270
///
261271
/// - If the `HEAD` pointer can't be retrieved.
262272
///
263-
pub fn head_oid(&self) -> Result<git2::Oid, PerformError> {
264-
Ok(self.repository.head()?.target().unwrap())
273+
pub fn head_oid(&self) -> anyhow::Result<git2::Oid> {
274+
let repo = &self.repository;
275+
let head = repo.head().context("Failed to read HEAD reference")?;
276+
Ok(head.target().unwrap())
265277
}
266278

267279
/// Commits the specified file with the specified commit message and pushes
268280
/// the commit to the `master` branch on the `origin` remote.
269281
///
270282
/// Note that `modified_file` expects a file path **relative** to the
271283
/// repository working folder!
272-
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> Result<(), PerformError> {
284+
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> anyhow::Result<()> {
273285
// git add $file
274286
let mut index = self.repository.index()?;
275287
index.add_path(modified_file)?;
@@ -288,7 +300,7 @@ impl Repository {
288300
}
289301

290302
/// Push the current branch to the provided refname
291-
fn push(&self, refspec: &str) -> Result<(), PerformError> {
303+
fn push(&self, refspec: &str) -> anyhow::Result<()> {
292304
let mut ref_status = Ok(());
293305
let mut callback_called = false;
294306
{
@@ -299,7 +311,7 @@ impl Repository {
299311
});
300312
callbacks.push_update_reference(|_, status| {
301313
if let Some(s) = status {
302-
ref_status = Err(format!("failed to push a ref: {}", s).into())
314+
ref_status = Err(anyhow!("failed to push a ref: {}", s))
303315
}
304316
callback_called = true;
305317
Ok(())
@@ -310,7 +322,7 @@ impl Repository {
310322
}
311323

312324
if !callback_called {
313-
ref_status = Err("update_reference callback was not called".into());
325+
ref_status = Err(anyhow!("update_reference callback was not called"));
314326
}
315327

316328
ref_status
@@ -323,7 +335,7 @@ impl Repository {
323335
///
324336
/// This function also prints the commit message and a success or failure
325337
/// message to the console.
326-
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> Result<(), PerformError> {
338+
pub fn commit_and_push(&self, message: &str, modified_file: &Path) -> anyhow::Result<()> {
327339
println!("Committing and pushing \"{}\"", message);
328340

329341
let relative_path = modified_file.strip_prefix(self.checkout_path.path())?;
@@ -337,7 +349,7 @@ impl Repository {
337349

338350
/// Fetches any changes from the `origin` remote and performs a hard reset
339351
/// to the tip of the `origin/master` branch.
340-
pub fn reset_head(&self) -> Result<(), PerformError> {
352+
pub fn reset_head(&self) -> anyhow::Result<()> {
341353
let mut origin = self.repository.find_remote("origin")?;
342354
let original_head = self.head_oid()?;
343355
origin.fetch(
@@ -371,7 +383,7 @@ impl Repository {
371383
}
372384

373385
/// 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> {
386+
pub fn squash_to_single_commit(&self, msg: &str) -> anyhow::Result<()> {
375387
let tree = self.repository.find_commit(self.head_oid()?)?.tree()?;
376388
let sig = self.repository.signature()?;
377389

@@ -393,7 +405,7 @@ impl Repository {
393405
///
394406
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
395407
/// variable to ensure that `git push` commands are able to succeed.
396-
pub fn run_command(&self, command: &mut Command) -> Result<(), PerformError> {
408+
pub fn run_command(&self, command: &mut Command) -> anyhow::Result<()> {
397409
let checkout_path = self.checkout_path.path();
398410
command.current_dir(checkout_path);
399411

@@ -409,8 +421,7 @@ impl Repository {
409421
let output = command.output()?;
410422
if !output.status.success() {
411423
let stderr = String::from_utf8_lossy(&output.stderr);
412-
let message = format!("Running git command failed with: {}", stderr);
413-
return Err(message.into());
424+
return Err(anyhow!("Running git command failed with: {}", stderr));
414425
}
415426

416427
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)