Skip to content

Commit 09c3bbb

Browse files
jtgeibelTurbo87
authored andcommitted
Refactor some of the index repository logic
1 parent 5e2ca64 commit 09c3bbb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/git.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl Repository {
186186
}
187187
}
188188

189+
fn head_oid(&self) -> Result<git2::Oid, PerformError> {
190+
Ok(self.repository.head()?.target().unwrap())
191+
}
192+
189193
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> Result<(), PerformError> {
190194
// git add $file
191195
let mut index = self.repository.index()?;
@@ -195,13 +199,18 @@ impl Repository {
195199
let tree = self.repository.find_tree(tree_id)?;
196200

197201
// 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)?;
200204
let sig = self.repository.signature()?;
201205
self.repository
202206
.commit(Some("HEAD"), &sig, &sig, &msg, &tree, &[&parent])?;
203207

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";
205214
let mut ref_status = Ok(());
206215
let mut callback_called = false;
207216
{
@@ -210,8 +219,8 @@ impl Repository {
210219
callbacks.credentials(|_, user_from_url, cred_type| {
211220
self.credentials.git2_callback(user_from_url, cred_type)
212221
});
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);
215224
if let Some(s) = status {
216225
ref_status = Err(format!("failed to push a ref: {}", s).into())
217226
}
@@ -220,7 +229,7 @@ impl Repository {
220229
});
221230
let mut opts = git2::PushOptions::new();
222231
opts.remote_callbacks(callbacks);
223-
origin.push(&["refs/heads/master"], Some(&mut opts))?;
232+
origin.push(&[refname], Some(&mut opts))?;
224233
}
225234

226235
if !callback_called {
@@ -230,7 +239,7 @@ impl Repository {
230239
ref_status
231240
}
232241

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> {
234243
println!("Committing and pushing \"{}\"", message);
235244

236245
self.perform_commit_and_push(message, modified_file)
@@ -248,7 +257,7 @@ impl Repository {
248257
Some(&mut Self::fetch_options(&self.credentials)),
249258
None,
250259
)?;
251-
let head = self.repository.head()?.target().unwrap();
260+
let head = self.head_oid()?;
252261
let obj = self.repository.find_object(head, None)?;
253262
self.repository.reset(&obj, git2::ResetType::Hard, None)?;
254263
Ok(())

0 commit comments

Comments
 (0)