[autorollers] Delete .repo dir before resyncing after a sync failure

No idea why this happens but deleting .repo seems to fix it.

Bug: skia:12146
Change-Id: I6c12fc7c6aa6b64e4b892410451e0899dbe2e295
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/430421
Reviewed-by: Eric Boren <borenet@google.com>
Commit-Queue: Ravi Mistry <rmistry@google.com>
diff --git a/autoroll/go/repo_manager/android_repo_manager.go b/autoroll/go/repo_manager/android_repo_manager.go
index 6a0b2db..c67bfed 100644
--- a/autoroll/go/repo_manager/android_repo_manager.go
+++ b/autoroll/go/repo_manager/android_repo_manager.go
@@ -178,12 +178,26 @@
 	}
 
 	// Run repo init and sync commands.
-	if _, err := exec.RunCwd(ctx, r.workdir, r.repoToolPath, "init", "-u", fmt.Sprintf("%s/a/platform/manifest", r.parentRepoURL), "-g", "all,-notdefault,-darwin", "-b", r.parentBranch.String()); err != nil {
+	initCmd := []string{r.repoToolPath, "init", "-u", fmt.Sprintf("%s/a/platform/manifest", r.parentRepoURL), "-g", "all,-notdefault,-darwin", "-b", r.parentBranch.String()}
+	if _, err := exec.RunCwd(ctx, r.workdir, initCmd...); err != nil {
 		return err
 	}
 	// Sync only the child path and the repohooks directory (needed to upload changes).
-	if _, err := exec.RunCwd(ctx, r.workdir, r.repoToolPath, "sync", "--force-sync", r.childPath, "tools/repohooks", "-j32"); err != nil {
-		return err
+	syncCmd := []string{r.repoToolPath, "sync", "--force-sync", r.childPath, "tools/repohooks", "-j32"}
+	if _, err := exec.RunCwd(ctx, r.workdir, syncCmd...); err != nil {
+		sklog.Warningf("repo sync error: %s", err)
+		// Try deleting .repo in the workdir and re-initing and re-syncing (skbug.com/12146).
+		repoDirPath := path.Join(r.workdir, ".repo")
+		if err := os.RemoveAll(repoDirPath); err != nil {
+			return skerr.Wrapf(err, "Could not delete %s before attempting a resync", repoDirPath)
+		}
+		if _, err := exec.RunCwd(ctx, r.workdir, initCmd...); err != nil {
+			return err
+		}
+		sklog.Info("Retrying sync after deleting %s", repoDirPath)
+		if _, err := exec.RunCwd(ctx, r.workdir, syncCmd...); err != nil {
+			return err
+		}
 	}
 
 	// Set color.ui=true so that the repo tool does not prompt during upload.