diff mbox

git: force reset + clean the repository before applying patches

Message ID 20170203052125.22790-1-andrew.donnellan@au1.ibm.com
State Accepted
Headers show

Commit Message

Andrew Donnellan Feb. 3, 2017, 5:21 a.m. UTC
From: Russell Currey <ruscur@russell.cc>

We've been seeing some strange heisenbugs where patches refuse to apply
due to a dirty index. Do a hard reset and git clean when changing branches
which may help with this.

Signed-off-by: Russell Currey <ruscur@russell.cc>
Signed-off-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>

---

already merged, for information only

---
 src/git.rs | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
diff mbox

Patch

diff --git a/src/git.rs b/src/git.rs
index e67caac..8443616 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -58,10 +58,45 @@  pub fn pull(repo: &Repository) -> Result<Output, &'static str> {
 }
 
 pub fn checkout_branch(repo: &Repository, branch: &str) -> () {
+    let workdir = repo.workdir().unwrap(); // TODO: support bare repositories
+
+    // Make sure there's no junk lying around before we switch
+    Command::new("git")
+        .arg("reset")
+        .arg("--hard")
+        .current_dir(&workdir)
+        .output()
+        .unwrap();
+
+    Command::new("git")
+        .arg("clean")
+        .arg("-f") // force remove files we don't need
+        .arg("-d") // ...including directories
+        .current_dir(&workdir)
+        .output()
+        .unwrap();
+
     repo.set_head(&format!("{}/{}", GIT_REF_BASE, &branch))
         .unwrap_or_else(|err| panic!("Couldn't set HEAD: {}", err));
     repo.checkout_head(Some(&mut CheckoutBuilder::new().force()))
         .unwrap_or_else(|err| panic!("Couldn't checkout HEAD: {}", err));
+
+    // Clean up again, just to be super sure
+    Command::new("git")
+        .arg("reset")
+        .arg("--hard")
+        .current_dir(&workdir)
+        .output()
+        .unwrap();
+
+    Command::new("git")
+        .arg("clean")
+        .arg("-f") // force remove files we don't need
+        .arg("-d") // ...including directories
+        .current_dir(&workdir)
+        .output()
+        .unwrap();
+    ()
 }
 
 pub fn apply_patch(repo: &Repository, path: &Path)