bisect-kit: helper script to prepare source trees for ChromeOS bisection

Typical usage:

  Initial setup:
    $ setup_cros_bisect.py init --chromeos
    $ setup_cros_bisect.py init --chrome
    $ setup_cros_bisect.py init --android=pi-arc-dev

  Sync code if necessary:
    $ setup_cros_bisect.py sync

  Create source trees for bisection
    $ setup_cros_bisect.py new --session=12345

  After bisection finished, delete trees
    $ setup_cros_bisect.py delete --session=12345

BUG=None
TEST=run above commands

Change-Id: I6f94b57b9a2967addf6d654dd571a68b2d06e0d7
Reviewed-on: https://chromium-review.googlesource.com/1242666
Commit-Ready: Kuang-che Wu <kcwu@chromium.org>
Tested-by: Kuang-che Wu <kcwu@chromium.org>
Reviewed-by: Chung-yih Wang <cywang@chromium.org>
diff --git a/bisect_kit/repo_util.py b/bisect_kit/repo_util.py
index 0d29bc0..ca18ee1 100644
--- a/bisect_kit/repo_util.py
+++ b/bisect_kit/repo_util.py
@@ -37,12 +37,30 @@
   return url
 
 
+def find_repo_root(path):
+  """Find the root path of a repo project
+
+  Args:
+    path: path
+
+  Returns:
+    project root if path is inside a repo project; otherwise None
+  """
+  path = os.path.abspath(path)
+  while not os.path.exists(os.path.join(path, '.repo')):
+    if path == '/':
+      return None
+    path = os.path.dirname(path)
+  return path
+
+
 def init(repo_dir,
          manifest_url,
          manifest_branch=None,
          manifest_name=None,
          repo_url=None,
-         reference=None):
+         reference=None,
+         mirror=False):
   """Repo init.
 
   Args:
@@ -52,7 +70,13 @@
     manifest_name: initial manifest file name
     repo_url: repo repository location
     reference: location of mirror directory
+    mirror: indicates repo mirror
   """
+  root = find_repo_root(repo_dir)
+  if root and root != repo_dir:
+    raise Exception(
+        '%s should not be inside another repo project at %s' % (repo_dir, root))
+
   cmd = ['repo', 'init', '--manifest-url', manifest_url]
   if manifest_name:
     cmd += ['--manifest-name', manifest_name]
@@ -62,6 +86,8 @@
     cmd += ['--repo-url', repo_url]
   if reference:
     cmd += ['--reference', reference]
+  if mirror:
+    cmd.append('--mirror')
   util.check_call(*cmd, cwd=repo_dir)