Remove more unversioned directories.

Currently, if an unversioned directory is present where we would expect
a versioned repository to be, the following error is printed:

  Can't update/checkout %s if an unversioned directory is present.
  Delete the directory and try again.

If --reset and --delete_unversioned_trees are used, gclient should delete
the unversioned directory in this case.

This problem can be reproduced using the following recipe:
  $ rm -rf src/third_party/webrtc/.svn
  $ gclient sync -nRftD

BUG=none
TEST=Verify that above error is fixed. Run all smoke tests.


Review URL: https://chromiumcodereview.appspot.com/11366239

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@167942 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gclient_scm.py b/gclient_scm.py
index ef93e56..eb880c8 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -929,7 +929,23 @@
       forced_revision = False
       rev_str = ''
 
-    if not os.path.exists(self.checkout_path):
+    # Get the existing scm url and the revision number of the current checkout.
+    exists = os.path.exists(self.checkout_path)
+    if exists and managed:
+      try:
+        from_info = scm.SVN.CaptureLocalInfo(
+            [], os.path.join(self.checkout_path, '.'))
+      except (gclient_utils.Error, subprocess2.CalledProcessError):
+        if options.reset and options.delete_unversioned_trees:
+          print 'Removing troublesome path %s' % self.checkout_path
+          gclient_utils.rmtree(self.checkout_path)
+          exists = False
+        else:
+          msg = ('Can\'t update/checkout %s if an unversioned directory is '
+                 'present. Delete the directory and try again.')
+          raise gclient_utils.Error(msg % self.checkout_path)
+
+    if not exists:
       gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
       # We need to checkout.
       command = ['checkout', url, self.checkout_path]
@@ -941,15 +957,6 @@
       print ('________ unmanaged solution; skipping %s' % self.relpath)
       return
 
-    # Get the existing scm url and the revision number of the current checkout.
-    try:
-      from_info = scm.SVN.CaptureLocalInfo(
-          [], os.path.join(self.checkout_path, '.'))
-    except (gclient_utils.Error, subprocess2.CalledProcessError):
-      raise gclient_utils.Error(
-          ('Can\'t update/checkout %s if an unversioned directory is present. '
-           'Delete the directory and try again.') % self.checkout_path)
-
     if 'URL' not in from_info:
       raise gclient_utils.Error(
           ('gclient is confused. Couldn\'t get the url for %s.\n'