Skip to content

Commit ef9b750

Browse files
committed
Auto merge of #3591 - jtgeibel:git-tweaks, r=Turbo87
Log a message if the background worker resets HEAD The first commit refactors some background job logic for the index repository. The second commit logs a message if the background worker sees a different upstream `HEAD` than the currently expected commit. Background workers obtain a lock on the registry index so a reset should only occur if the last job modified the index but failed to push its commit, or the index was mutated externally (ex: to delete crates). r? `@Turbo87`
2 parents 5e2ca64 + d012525 commit ef9b750

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/git.rs

Lines changed: 23 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)
@@ -243,12 +252,18 @@ impl Repository {
243252

244253
pub fn reset_head(&self) -> Result<(), PerformError> {
245254
let mut origin = self.repository.find_remote("origin")?;
255+
let original_head = self.head_oid()?;
246256
origin.fetch(
247257
&["refs/heads/*:refs/heads/*"],
248258
Some(&mut Self::fetch_options(&self.credentials)),
249259
None,
250260
)?;
251-
let head = self.repository.head()?.target().unwrap();
261+
let head = self.head_oid()?;
262+
263+
if head != original_head {
264+
println!("Resetting index from {} to {}", original_head, head);
265+
}
266+
252267
let obj = self.repository.find_object(head, None)?;
253268
self.repository.reset(&obj, git2::ResetType::Hard, None)?;
254269
Ok(())

0 commit comments

Comments
 (0)