Always update bazel repo: try pull first, and recreate if failed.

--
PiperOrigin-RevId: 148751928
MOS_MIGRATED_REVID=148751928
diff --git a/src/tools/benchmark/java/com/google/devtools/build/benchmark/BazelBuilder.java b/src/tools/benchmark/java/com/google/devtools/build/benchmark/BazelBuilder.java
index 6d49b7b..974ae17 100644
--- a/src/tools/benchmark/java/com/google/devtools/build/benchmark/BazelBuilder.java
+++ b/src/tools/benchmark/java/com/google/devtools/build/benchmark/BazelBuilder.java
@@ -20,6 +20,9 @@
 import com.google.devtools.build.lib.shell.Command;
 import com.google.devtools.build.lib.shell.CommandException;
 import com.google.devtools.build.lib.shell.CommandResult;
+import com.google.devtools.build.lib.vfs.FileSystem;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -34,6 +37,7 @@
 class BazelBuilder implements Builder {
 
   private static final Logger logger = Logger.getLogger(BazelBuilder.class.getName());
+  private static final FileSystem fileSystem = new JavaIoFileSystem();
 
   private static final String BAZEL_BINARY_PATH = "bazel-bin/src/bazel";
   private static final Pattern ELAPSED_TIME_PATTERN = Pattern.compile("(?<=Elapsed time: )[0-9.]+");
@@ -151,13 +155,15 @@
   }
 
   void prepareFromGitRepo(String gitRepo) throws IOException, CommandException {
-    if (builderDir.toFile().exists() && !builderDir.toFile().isDirectory()) {
+    // Try to pull git repo first, delete directory if failed.
+    if (builderDir.toFile().isDirectory()) {
       try {
-        Files.delete(builderDir);
-      } catch (IOException e) {
-        throw new IOException(builderDir + " is a file and cannot be deleted", e);
+        pullGitRepo();
+      } catch (CommandException e) {
+        FileSystemUtils.deleteTree(fileSystem.getPath(builderDir.toString()));
       }
     }
+
     if (Files.notExists(builderDir)) {
       try {
         Files.createDirectories(builderDir);
@@ -172,6 +178,12 @@
     // Assume the directory is what we need if not empty
   }
 
+  private void pullGitRepo() throws CommandException {
+    String[] gitCloneCommand = {"git", "pull"};
+    Command cmd = new Command(gitCloneCommand, null, builderDir.toFile());
+    cmd.execute();
+  }
+
   private ImmutableList<String> getListOfOutputFromCommand(String... command)
       throws CommandException{
     Command cmd = new Command(command, null, builderDir.toFile());
diff --git a/src/tools/benchmark/java/com/google/devtools/build/benchmark/Main.java b/src/tools/benchmark/java/com/google/devtools/build/benchmark/Main.java
index c0b9bea..463bb5f 100644
--- a/src/tools/benchmark/java/com/google/devtools/build/benchmark/Main.java
+++ b/src/tools/benchmark/java/com/google/devtools/build/benchmark/Main.java
@@ -46,6 +46,10 @@
 
     // Prepare paths
     File workspace = new File(opt.workspace);
+    if (workspace.isFile()) {
+      logger.log(Level.SEVERE, "Workspace directory is an existing file: " + opt.workspace);
+      System.exit(1);
+    }
     if (!workspace.exists() && !workspace.mkdirs()) {
       logger.log(Level.SEVERE, "Failed to create workspace directory: " + opt.workspace);
       System.exit(1);