@@ -4,7 +4,6 @@ use std::io::Write;
4
4
use std:: path:: { Path , PathBuf } ;
5
5
use std:: process:: Command ;
6
6
7
- use swirl:: PerformError ;
8
7
use tempfile:: TempDir ;
9
8
use url:: Url ;
10
9
@@ -197,8 +196,11 @@ impl Repository {
197
196
/// - If cloning the crate index fails.
198
197
/// - If reading the global git config fails.
199
198
///
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" ) ?;
202
204
203
205
let repository = git2:: build:: RepoBuilder :: new ( )
204
206
. fetch_options ( Self :: fetch_options ( & repository_config. credentials ) )
@@ -213,13 +215,21 @@ impl Repository {
213
215
. clone (
214
216
repository_config. index_location . as_str ( ) ,
215
217
checkout_path. path ( ) ,
216
- ) ?;
218
+ )
219
+ . context ( "Failed to clone index repository" ) ?;
217
220
218
221
// All commits to the index registry made through crates.io will be made by bors, the Rust
219
222
// 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" ) ?;
223
233
224
234
Ok ( Self {
225
235
checkout_path,
@@ -260,16 +270,18 @@ impl Repository {
260
270
///
261
271
/// - If the `HEAD` pointer can't be retrieved.
262
272
///
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 ( ) )
265
277
}
266
278
267
279
/// Commits the specified file with the specified commit message and pushes
268
280
/// the commit to the `master` branch on the `origin` remote.
269
281
///
270
282
/// Note that `modified_file` expects a file path **relative** to the
271
283
/// 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 < ( ) > {
273
285
// git add $file
274
286
let mut index = self . repository . index ( ) ?;
275
287
index. add_path ( modified_file) ?;
@@ -288,7 +300,7 @@ impl Repository {
288
300
}
289
301
290
302
/// Push the current branch to the provided refname
291
- fn push ( & self , refspec : & str ) -> Result < ( ) , PerformError > {
303
+ fn push ( & self , refspec : & str ) -> anyhow :: Result < ( ) > {
292
304
let mut ref_status = Ok ( ( ) ) ;
293
305
let mut callback_called = false ;
294
306
{
@@ -299,7 +311,7 @@ impl Repository {
299
311
} ) ;
300
312
callbacks. push_update_reference ( |_, status| {
301
313
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) )
303
315
}
304
316
callback_called = true ;
305
317
Ok ( ( ) )
@@ -310,7 +322,7 @@ impl Repository {
310
322
}
311
323
312
324
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" ) ) ;
314
326
}
315
327
316
328
ref_status
@@ -323,7 +335,7 @@ impl Repository {
323
335
///
324
336
/// This function also prints the commit message and a success or failure
325
337
/// 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 < ( ) > {
327
339
println ! ( "Committing and pushing \" {}\" " , message) ;
328
340
329
341
let relative_path = modified_file. strip_prefix ( self . checkout_path . path ( ) ) ?;
@@ -337,7 +349,7 @@ impl Repository {
337
349
338
350
/// Fetches any changes from the `origin` remote and performs a hard reset
339
351
/// to the tip of the `origin/master` branch.
340
- pub fn reset_head ( & self ) -> Result < ( ) , PerformError > {
352
+ pub fn reset_head ( & self ) -> anyhow :: Result < ( ) > {
341
353
let mut origin = self . repository . find_remote ( "origin" ) ?;
342
354
let original_head = self . head_oid ( ) ?;
343
355
origin. fetch (
@@ -371,7 +383,7 @@ impl Repository {
371
383
}
372
384
373
385
/// 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 < ( ) > {
375
387
let tree = self . repository . find_commit ( self . head_oid ( ) ?) ?. tree ( ) ?;
376
388
let sig = self . repository . signature ( ) ?;
377
389
@@ -393,7 +405,7 @@ impl Repository {
393
405
///
394
406
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
395
407
/// 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 < ( ) > {
397
409
let checkout_path = self . checkout_path . path ( ) ;
398
410
command. current_dir ( checkout_path) ;
399
411
@@ -409,8 +421,7 @@ impl Repository {
409
421
let output = command. output ( ) ?;
410
422
if !output. status . success ( ) {
411
423
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) ) ;
414
425
}
415
426
416
427
Ok ( ( ) )
0 commit comments