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>
diff --git a/gclient.py b/gclient.py
index 1637190..21157d3 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,12 @@
             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.
+    self.GetCipdRoot().run(command)
+
     return 0
 
   def PrintRevInfo(self):