Reland "gclient: delete unversioned directory before adding cipd dep for the same path"

This is a reland of 67ef3f67e816839ce8b3984ecba9406961583eff

We no longer call cipd unconditionally.

Original change's description:
> gclient: delete unversioned directory before adding cipd dep for the same path
>
> Bug: 882611
> Change-Id: I46e41cc9693b90874b5d6569a12ec638eaac1050
> Reviewed-on: https://chromium-review.googlesource.com/1228655
> Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
> Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>

Bug: 882611
Change-Id: I683bfc62bd1eebfec0853583f96f3981c2c6bdf2
Reviewed-on: https://chromium-review.googlesource.com/1232891
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/gclient.py b/gclient.py
index 1637190..2b8ae35 100755
--- a/gclient.py
+++ b/gclient.py
@@ -1614,9 +1614,6 @@
               patch_repo + '@' + patch_ref
               for patch_repo, patch_ref in patch_refs.iteritems())))
 
-    if self._cipd_root:
-      self._cipd_root.run(command)
-
     # Once all the dependencies have been processed, it's now safe to write
     # out the gn_args_file and run the hooks.
     if command == 'update':
@@ -1724,6 +1721,13 @@
             gclient_utils.rmtree(e_dir)
       # record the current list of entries for next time
       self._SaveEntries()
+
+    # Sync CIPD dependencies once removed deps are deleted. In case a git
+    # dependency was moved to CIPD, we want to remove the old git directory
+    # first and then sync the CIPD dep.
+    if self._cipd_root:
+      self._cipd_root.run(command)
+
     return 0
 
   def PrintRevInfo(self):