Script to clone the rust-lang/rust repo
diff --git a/.gitignore b/.gitignore
index 07d9fa6..aba0fb5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 target
 Cargo.lock
-tests/rust
+tests/rust/*
+!tests/rust/clone.sh
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 91aaa61..451d2de 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -4,7 +4,6 @@
 extern crate syntax;
 
 use std::env;
-use std::path::Path;
 use std::process::Command;
 use std::u32;
 
@@ -109,29 +108,7 @@
 }
 
 pub fn clone_rust() {
-    if Path::new("tests/rust").is_dir() {
-        println!("found rust repo in tests/rust");
-        return;
-    }
-
-    println!("cloning rust-lang/rust");
-    let result = Command::new("git")
-                    .arg("clone")
-                    .arg("https://github.com/rust-lang/rust")
-                    .arg("tests/rust")
-                    .status()
-                    .unwrap();
-    println!("result: {}", result);
-    assert!(result.success());
-
-    println!("reset to known-good rev");
-    let result = Command::new("git")
-                    .arg("reset")
-                    .arg("--hard")
-                    .arg("eb8f2586ebd842dec49d3d7f50e49a985ab31493")
-                    .current_dir("tests/rust")
-                    .status()
-                    .unwrap();
+    let result = Command::new("tests/rust/clone.sh").status().unwrap();
     println!("result: {}", result);
     assert!(result.success());
 }
diff --git a/tests/rust/clone.sh b/tests/rust/clone.sh
new file mode 100755
index 0000000..1ba2599
--- /dev/null
+++ b/tests/rust/clone.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+REMOTE=rust
+REPO=https://github.com/rust-lang/rust
+REV=eb8f2586ebd842dec49d3d7f50e49a985ab31493
+
+set -euo pipefail
+cd "$(dirname "${BASH_SOURCE[0]}")"
+
+git init
+
+if git remote | grep --fixed-strings --line-regexp --quiet "$REMOTE"; then
+    git remote set-url "$REMOTE" "$REPO"
+else
+    git remote add "$REMOTE" "$REPO"
+fi
+
+if ! git cat-file -t "$REV" >/dev/null 2>&1; then
+    git fetch "$REMOTE" master
+fi
+
+git checkout "$REV"