@@ -17,6 +17,7 @@ use std::env;
17
17
use std:: error:: Error ;
18
18
use std:: process:: Command ;
19
19
20
+ use anyhow:: bail;
20
21
use console:: style;
21
22
use dialoguer:: { Confirmation , Select } ;
22
23
use git2:: { Branch , Commit , Diff , Object , ObjectType , Oid , Repository } ;
@@ -194,7 +195,7 @@ fn select_commit_to_amend<'a>(
194
195
repo : & ' a Repository ,
195
196
upstream : Option < Object < ' a > > ,
196
197
max_commits : usize ,
197
- ) -> Result < Commit < ' a > , Box < dyn Error > > {
198
+ ) -> Result < Commit < ' a > , anyhow :: Error > {
198
199
let mut walker = repo. revwalk ( ) ?;
199
200
walker. push_head ( ) ?;
200
201
let commits = if let Some ( upstream) = upstream. as_ref ( ) {
@@ -212,6 +213,13 @@ fn select_commit_to_amend<'a>(
212
213
. map ( |rev| repo. find_commit ( rev) )
213
214
. collect :: < Result < Vec < _ > , _ > > ( ) ?
214
215
} ;
216
+ if commits. len ( ) == 0 {
217
+ bail ! (
218
+ "No commits between {} and {:?}" ,
219
+ format_ref( & repo. head( ) ?) ?,
220
+ upstream. map( |u| u. id( ) ) . unwrap( )
221
+ ) ;
222
+ }
215
223
let branches: HashMap < Oid , String > = repo
216
224
. branches ( None ) ?
217
225
. filter_map ( |b| {
@@ -251,6 +259,12 @@ fn select_commit_to_amend<'a>(
251
259
Ok ( repo. find_commit ( commits[ selected?] . id ( ) ) ?)
252
260
}
253
261
262
+ fn format_ref ( rf : & git2:: Reference < ' _ > ) -> Result < String , anyhow:: Error > {
263
+ let shorthand = rf. shorthand ( ) . unwrap_or ( "<unnamed>" ) ;
264
+ let sha = rf. peel_to_commit ( ) ?. id ( ) . to_string ( ) ;
265
+ Ok ( format ! ( "{} ({})" , shorthand, & sha[ ..10 ] ) )
266
+ }
267
+
254
268
fn print_diff ( kind : Changes ) -> Result < ( ) , Box < dyn Error > > {
255
269
let mut args = vec ! [ "diff" , "--stat" ] ;
256
270
if kind == Changes :: Staged {
0 commit comments