split-cl: Walk the tree instead of using owners db.
Change-Id: I9dc9cda5a30833938fe13476deb76f4ed5770eab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2666668
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
diff --git a/split_cl.py b/split_cl.py
index 5ed8a57..59f80b9 100644
--- a/split_cl.py
+++ b/split_cl.py
@@ -30,13 +30,6 @@
CL_SPLIT_FORCE_LIMIT = 10
-def ReadFile(file_path):
- """Returns the content of |file_path|."""
- with open(file_path) as f:
- content = f.read()
- return content
-
-
def EnsureInGitRepository():
"""Throws an exception if the current directory is not a git repository."""
git.run('rev-parse')
@@ -142,21 +135,21 @@
publish=True)
-def GetFilesSplitByOwners(owners_database, files):
+def GetFilesSplitByOwners(files):
"""Returns a map of files split by OWNERS file.
Returns:
A map where keys are paths to directories containing an OWNERS file and
values are lists of files sharing an OWNERS file.
"""
- files_split_by_owners = collections.defaultdict(list)
+ files_split_by_owners = {}
for action, path in files:
- enclosing_dir = owners_database.enclosing_dir_with_owners(path)
- # Anything matching a per-file rule will return its own path.
- # Aggregate up to the parent directory so as not to over-split.
- if enclosing_dir == path:
- enclosing_dir = os.path.dirname(path)
- files_split_by_owners[enclosing_dir].append((action, path))
+ dir_with_owners = os.path.dirname(path)
+ # Find the closest parent directory with an OWNERS file.
+ while (dir_with_owners not in files_split_by_owners
+ and not os.path.isfile(os.path.join(dir_with_owners, 'OWNERS'))):
+ dir_with_owners = os.path.dirname(dir_with_owners)
+ files_split_by_owners.setdefault(dir_with_owners, []).append((action, path))
return files_split_by_owners
@@ -201,8 +194,9 @@
Returns:
0 in case of success. 1 in case of error.
"""
- description = AddUploadedByGitClSplitToDescription(ReadFile(description_file))
- comment = ReadFile(comment_file) if comment_file else None
+ description = AddUploadedByGitClSplitToDescription(
+ gclient_utils.FileRead(description_file))
+ comment = gclient_utils.FileRead(comment_file) if comment_file else None
try:
EnsureInGitRepository()
@@ -228,7 +222,7 @@
owners_database = owners.Database(repository_root, open, os.path)
owners_database.load_data_needed_for([f for _, f in files])
- files_split_by_owners = GetFilesSplitByOwners(owners_database, files)
+ files_split_by_owners = GetFilesSplitByOwners(files)
num_cls = len(files_split_by_owners)
print('Will split current branch (' + refactor_branch + ') into ' +