@@ -81,7 +81,7 @@ fn run(squash: bool, max_commits: usize) -> Result<(), Box<dyn Error>> {
81
81
let diff = repo. diff_tree_to_index ( Some ( & head_tree) , None , None ) ?;
82
82
let upstream = get_upstream ( & repo, & head_branch) ?;
83
83
let commit_to_amend =
84
- create_fixup_commit ( & repo, & head_branch, & upstream, & diff, squash, max_commits) ?;
84
+ create_fixup_commit ( & repo, & head_branch, upstream, & diff, squash, max_commits) ?;
85
85
println ! (
86
86
"selected: {} {}" ,
87
87
& commit_to_amend. id( ) . to_string( ) [ 0 ..10 ] ,
@@ -100,7 +100,7 @@ fn run(squash: bool, max_commits: usize) -> Result<(), Box<dyn Error>> {
100
100
fn get_upstream < ' a > (
101
101
repo : & ' a Repository ,
102
102
head_branch : & ' a Branch ,
103
- ) -> Result < Object < ' a > , Box < dyn Error > > {
103
+ ) -> Result < Option < Object < ' a > > , Box < dyn Error > > {
104
104
let upstream = if let Ok ( upstream_name) = env:: var ( UPSTREAM_VAR ) {
105
105
let branch = repo
106
106
. branches ( None ) ?
@@ -124,20 +124,20 @@ fn get_upstream<'a>(
124
124
125
125
commit
126
126
} else {
127
- head_branch
128
- . upstream ( )
129
- . unwrap ( )
130
- . into_reference ( )
131
- . peel ( ObjectType :: Commit ) ?
127
+ if let Ok ( upstream ) = head_branch. upstream ( ) {
128
+ upstream. into_reference ( ) . peel ( ObjectType :: Commit ) ?
129
+ } else {
130
+ return Ok ( None ) ;
131
+ }
132
132
} ;
133
133
134
- Ok ( upstream)
134
+ Ok ( Some ( upstream) )
135
135
}
136
136
137
137
fn create_fixup_commit < ' a > (
138
138
repo : & ' a Repository ,
139
139
head_branch : & ' a Branch ,
140
- upstream : & ' a Object ,
140
+ upstream : Option < Object < ' a > > ,
141
141
diff : & ' a Diff ,
142
142
squash : bool ,
143
143
max_commits : usize ,
@@ -164,7 +164,7 @@ fn create_fixup_commit<'a>(
164
164
println ! ( "Staged changes:" ) ;
165
165
print_diff ( Changes :: Staged ) ?;
166
166
}
167
- let commit_to_amend = select_commit_to_amend ( & repo, Some ( upstream) , max_commits) ?;
167
+ let commit_to_amend = select_commit_to_amend ( & repo, upstream, max_commits) ?;
168
168
do_fixup_commit ( & repo, & head_branch, & commit_to_amend, squash) ?;
169
169
Ok ( commit_to_amend)
170
170
}
@@ -191,12 +191,12 @@ fn do_fixup_commit<'a>(
191
191
192
192
fn select_commit_to_amend < ' a > (
193
193
repo : & ' a Repository ,
194
- upstream : Option < & Object < ' a > > ,
194
+ upstream : Option < Object < ' a > > ,
195
195
max_commits : usize ,
196
196
) -> Result < Commit < ' a > , Box < dyn Error > > {
197
197
let mut walker = repo. revwalk ( ) ?;
198
198
walker. push_head ( ) ?;
199
- let commits = if let Some ( upstream) = upstream {
199
+ let commits = if let Some ( upstream) = upstream. as_ref ( ) {
200
200
let upstream_oid = upstream. id ( ) ;
201
201
walker
202
202
. flat_map ( |r| r)
@@ -221,7 +221,11 @@ fn select_commit_to_amend<'a>(
221
221
)
222
222
} )
223
223
. collect :: < Vec < _ > > ( ) ;
224
- eprintln ! ( "Select a commit to amend:" ) ;
224
+ if upstream. is_none ( ) {
225
+ eprintln ! ( "Select a commit to amend (no upstream for HEAD):" ) ;
226
+ } else {
227
+ eprintln ! ( "Select a commit to amend:" ) ;
228
+ }
225
229
let selected = Select :: new ( ) . items ( & rev_aliases) . default ( 0 ) . interact ( ) ;
226
230
Ok ( repo. find_commit ( commits[ selected?] . id ( ) ) ?)
227
231
}
0 commit comments