Skip to content

Commit 64e4604

Browse files
committed
Reset to original HEAD in GitResetOnDrop
1 parent 2950c89 commit 64e4604

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/sync.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ To prepare for merging from {UPSTREAM_REPO}."#,
118118
.context("cannot create preparation commit")?;
119119

120120
// Make sure that we reset the above commit if something fails
121-
let mut git_reset = GitResetOnDrop::default();
121+
let mut git_reset = GitResetOnDrop::new(orig_head);
122122

123123
// Fetch given rustc commit.
124124
check_output(&["git", "fetch", &josh_url])
@@ -311,13 +311,20 @@ fn prepare_rustc_checkout() -> anyhow::Result<PathBuf> {
311311
Ok(PathBuf::from(path))
312312
}
313313

314-
/// Removes the last commit on drop, unless `disarm` is called first.
315-
#[derive(Default)]
314+
/// Restores HEAD to `reset_to` on drop, unless `disarm` is called first.
316315
struct GitResetOnDrop {
317316
disarmed: bool,
317+
reset_to: String,
318318
}
319319

320320
impl GitResetOnDrop {
321+
fn new(current_sha: String) -> Self {
322+
Self {
323+
disarmed: false,
324+
reset_to: current_sha,
325+
}
326+
}
327+
321328
fn disarm(&mut self) {
322329
self.disarmed = true;
323330
}
@@ -326,9 +333,9 @@ impl GitResetOnDrop {
326333
impl Drop for GitResetOnDrop {
327334
fn drop(&mut self) {
328335
if !self.disarmed {
329-
eprintln!("Reverting last commit");
330-
check_output(&["git", "reset", "--hard", "HEAD^"])
331-
.expect("cannot reset last git commit");
336+
eprintln!("Reverting HEAD to {}", self.reset_to);
337+
check_output(&["git", "reset", "--hard", &self.reset_to])
338+
.expect(&format!("cannot reset current branch to {}", self.reset_to));
332339
}
333340
}
334341
}

0 commit comments

Comments
 (0)