bisect-kit: remove empty parents when remove git repos

When add repos, parent directories were created automatically. When
remove repos, we should remove them.

BUG=b:176320467
TEST=./switch_cros_cr_localbuild_internal.py '89.0.4357.0~89.0.4358.0/437'

Change-Id: I4fea53c6c20e02028f351af1a6c197e8eb417518
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/bisect-kit/+/2606415
Commit-Queue: Kuang-che Wu <kcwu@chromium.org>
Tested-by: Kuang-che Wu <kcwu@chromium.org>
Auto-Submit: Kuang-che Wu <kcwu@chromium.org>
Reviewed-by: Zheng-Jie Chang <zjchang@chromium.org>
diff --git a/bisect_kit/codechange.py b/bisect_kit/codechange.py
index 0ec6248..b8a05d6 100644
--- a/bisect_kit/codechange.py
+++ b/bisect_kit/codechange.py
@@ -417,12 +417,22 @@
 
   def apply(self, code_storage, root_dir):
     assert self.path
+    root_dir = os.path.normpath(root_dir)
     git_repo = os.path.join(root_dir, self.path)
     assert git_util.is_git_root(git_repo), '%r should be a git repo' % git_repo
     # TODO(kcwu): other projects may be sub-tree of `git_repo`.
     # They should not be deleted. (crbug/930047)
     shutil.rmtree(git_repo)
 
+    # Remove empty parents. (But don't delete `root_dir` and its upper parents.)
+    parent = os.path.dirname(git_repo)
+    while (parent != root_dir and
+           os.path.commonpath([parent, root_dir]) == root_dir):
+      if os.listdir(parent):
+        break
+      os.rmdir(parent)
+      parent = os.path.dirname(parent)
+
     code_storage.remove_from_project_list(root_dir, self.path)
 
   def summary(self):