@@ -186,6 +186,10 @@ impl Repository {
186
186
}
187
187
}
188
188
189
+ fn head_oid ( & self ) -> Result < git2:: Oid , PerformError > {
190
+ Ok ( self . repository . head ( ) ?. target ( ) . unwrap ( ) )
191
+ }
192
+
189
193
fn perform_commit_and_push ( & self , msg : & str , modified_file : & Path ) -> Result < ( ) , PerformError > {
190
194
// git add $file
191
195
let mut index = self . repository . index ( ) ?;
@@ -195,13 +199,18 @@ impl Repository {
195
199
let tree = self . repository . find_tree ( tree_id) ?;
196
200
197
201
// git commit -m "..."
198
- let head = self . repository . head ( ) ?;
199
- let parent = self . repository . find_commit ( head. target ( ) . unwrap ( ) ) ?;
202
+ let head = self . head_oid ( ) ?;
203
+ let parent = self . repository . find_commit ( head) ?;
200
204
let sig = self . repository . signature ( ) ?;
201
205
self . repository
202
206
. commit ( Some ( "HEAD" ) , & sig, & sig, & msg, & tree, & [ & parent] ) ?;
203
207
204
- // git push
208
+ self . push ( )
209
+ }
210
+
211
+ /// Push the current branch to "refs/heads/master"
212
+ fn push ( & self ) -> Result < ( ) , PerformError > {
213
+ let refname = "refs/heads/master" ;
205
214
let mut ref_status = Ok ( ( ) ) ;
206
215
let mut callback_called = false ;
207
216
{
@@ -210,8 +219,8 @@ impl Repository {
210
219
callbacks. credentials ( |_, user_from_url, cred_type| {
211
220
self . credentials . git2_callback ( user_from_url, cred_type)
212
221
} ) ;
213
- callbacks. push_update_reference ( |refname , status| {
214
- assert_eq ! ( refname, "refs/heads/master" ) ;
222
+ callbacks. push_update_reference ( |cb_refname , status| {
223
+ assert_eq ! ( refname, cb_refname ) ;
215
224
if let Some ( s) = status {
216
225
ref_status = Err ( format ! ( "failed to push a ref: {}" , s) . into ( ) )
217
226
}
@@ -220,7 +229,7 @@ impl Repository {
220
229
} ) ;
221
230
let mut opts = git2:: PushOptions :: new ( ) ;
222
231
opts. remote_callbacks ( callbacks) ;
223
- origin. push ( & [ "refs/heads/master" ] , Some ( & mut opts) ) ?;
232
+ origin. push ( & [ refname ] , Some ( & mut opts) ) ?;
224
233
}
225
234
226
235
if !callback_called {
@@ -230,7 +239,7 @@ impl Repository {
230
239
ref_status
231
240
}
232
241
233
- pub fn commit_and_push ( & self , message : & str , modified_file : & Path ) -> Result < ( ) , PerformError > {
242
+ fn commit_and_push ( & self , message : & str , modified_file : & Path ) -> Result < ( ) , PerformError > {
234
243
println ! ( "Committing and pushing \" {}\" " , message) ;
235
244
236
245
self . perform_commit_and_push ( message, modified_file)
@@ -248,7 +257,7 @@ impl Repository {
248
257
Some ( & mut Self :: fetch_options ( & self . credentials ) ) ,
249
258
None ,
250
259
) ?;
251
- let head = self . repository . head ( ) ?. target ( ) . unwrap ( ) ;
260
+ let head = self . head_oid ( ) ?;
252
261
let obj = self . repository . find_object ( head, None ) ?;
253
262
self . repository . reset ( & obj, git2:: ResetType :: Hard , None ) ?;
254
263
Ok ( ( ) )
0 commit comments