Improve git cl split

This CL changes the behavior of `git cl split` to split the change
by the size of the resulting CLs. For now, this is based on the number
of bytes changed, and not by the number of changed lines. Depending
on the shape of change, this may still produce more CLs than expected
(and possibly more than before).

A future change will switch the split to be based on the number
of affected lines, and also introduce a mode to base the split
on the number of affected files.

Bug: 998922
Change-Id: I49f868972a61b89b426ef9e2ceedc733eacb4350
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1778744
Commit-Queue: Yannic Bonenberger <yannic.bonenberger@gmail.com>
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
changes/44/1778744/9
Yannic Bonenberger 5 years ago committed by LUCI CQ
parent bdd89366d3
commit 684096347b

@ -4530,8 +4530,10 @@ def CMDsplit(parser, args):
Creates a branch and uploads a CL for each group of files modified in the
current branch that share a common OWNERS file. In the CL description and
comment, the string '$directory', is replaced with the directory containing
the shared OWNERS file.
comment, '$directory' is replaced with the directory containing the changes
in this CL, '$cl_index' is replaced with the index of the CL we're currently
sending out, and '$num_cls' is replaced with the total number of CLs that
we're sending out in this split.
"""
parser.add_option('-d', '--description', dest='description_file',
help='A text file containing a CL description in which '

@ -880,6 +880,22 @@ class _GitDiffCache(_DiffCache):
return scm.GIT.GetOldContents(local_root, path, branch=self._upstream)
def _ParseDiffHeader(line):
"""Searches |line| for diff headers and returns a tuple
(header, old_line, old_size, new_line, new_size), or None if line doesn't
contain a diff header.
This relies on the scm diff output describing each changed code section
with a line of the form
^@@ <old line num>,<old size> <new line num>,<new size> @@$
"""
m = re.match(r'^@@ \-([0-9]+)\,([0-9]+) \+([0-9]+)\,([0-9]+) @@', line)
if m:
return (m.group(0), int(m.group(1)), int(m.group(2)), int(m.group(3)),
int(m.group(4)))
class AffectedFile(object):
"""Representation of a file in a change."""
@ -893,6 +909,7 @@ class AffectedFile(object):
self._local_root = repository_root
self._is_directory = None
self._cached_changed_contents = None
self._cached_change_size_in_bytes = None
self._cached_new_contents = None
self._diff_cache = diff_cache
logging.debug('%s(%s)', self.__class__.__name__, self._path)
@ -969,9 +986,9 @@ class AffectedFile(object):
line_num = 0
for line in self.GenerateScmDiff().splitlines():
m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line)
if m:
line_num = int(m.groups(1)[0])
h = _ParseDiffHeader(line)
if h:
line_num = h[3]
continue
if line.startswith('+') and not line.startswith('++'):
self._cached_changed_contents.append((line_num, line[1:]))
@ -979,6 +996,25 @@ class AffectedFile(object):
line_num += 1
return self._cached_changed_contents[:]
def ChangeSizeInBytes(self):
"""Returns a list of tuples (deleted bytes, added bytes) of all changes
in this file.
This relies on the scm diff output describing each changed code section
with a line of the form
^@@ <old line num>,<old size> <new line num>,<new size> @@$
"""
if self._cached_change_size_in_bytes is not None:
return self._cached_change_size_in_bytes[:]
self._cached_change_size_in_bytes = []
for line in self.GenerateScmDiff().splitlines():
h = _ParseDiffHeader(line)
if h:
self._cached_change_size_in_bytes.append((h[2], h[4]))
return self._cached_change_size_in_bytes[:]
def __str__(self):
return self.LocalPath()

@ -9,6 +9,7 @@ from __future__ import print_function
import collections
import os
import random
import re
import subprocess2
import sys
@ -20,6 +21,8 @@ import owners_finder
import git_common as git
import third_party.pygtrie as trie
# If a call to `git cl split` will generate more than this number of CLs, the
# command will prompt the user to make sure they know what they're doing. Large
@ -40,23 +43,25 @@ def EnsureInGitRepository():
git.run('rev-parse')
def CreateBranchForDirectory(prefix, directory, upstream):
"""Creates a branch named |prefix| + "_" + |directory| + "_split".
def CreateBranchForDirectory(prefix, cl_index, directory, upstream):
"""Creates a branch named |prefix| + "_" + |cl_index| + "_" + |directory|.
Return false if the branch already exists. |upstream| is used as upstream for
the created branch.
"""
existing_branches = set(git.branches(use_limit = False))
branch_name = prefix + '_' + directory + '_split'
branch_name = '_'.join([prefix, cl_index, directory])
if branch_name in existing_branches:
return False
git.run('checkout', '-t', upstream, '-b', branch_name)
return True
def FormatDescriptionOrComment(txt, directory):
"""Replaces $directory with |directory| in |txt|."""
return txt.replace('$directory', '/' + directory)
def FormatDescriptionOrComment(txt, directory, cl_index, num_cls):
"""Replaces $directory with |directory|, $cl_index with |cl_index|, and
$num_cls with |num_cls| in |txt|."""
return txt.replace('$directory', '/' + directory).replace(
'$cl_index', str(cl_index)).replace('$num_cls', str(num_cls))
def AddUploadedByGitClSplitToDescription(description):
@ -75,12 +80,14 @@ def AddUploadedByGitClSplitToDescription(description):
return '\n'.join(lines)
def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
description, comment, reviewers, changelist, cmd_upload,
cq_dry_run, enable_auto_submit):
def UploadCl(cl_index, num_cls, refactor_branch, refactor_branch_upstream,
directory, files, description, comment, reviewer, changelist,
cmd_upload, cq_dry_run, enable_auto_submit):
"""Uploads a CL with all changes to |files| in |refactor_branch|.
Args:
cl_index: The index of this CL in the list of CLs to upload.
num_cls: The total number of CLs that will be uploaded.
refactor_branch: Name of the branch that contains the changes to upload.
refactor_branch_upstream: Name of the upstream of |refactor_branch|.
directory: Path to the directory that contains the OWNERS file for which
@ -88,16 +95,17 @@ def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
files: List of AffectedFile instances to include in the uploaded CL.
description: Description of the uploaded CL.
comment: Comment to post on the uploaded CL.
reviewers: A set of reviewers for the CL.
reviewer: The reviewer for the CL.
changelist: The Changelist class.
cmd_upload: The function associated with the git cl upload command.
cq_dry_run: If CL uploads should also do a cq dry run.
enable_auto_submit: If CL uploads should also enable auto submit.
"""
# Create a branch.
if not CreateBranchForDirectory(
refactor_branch, directory, refactor_branch_upstream):
print('Skipping ' + directory + ' for which a branch already exists.')
if not CreateBranchForDirectory(refactor_branch, cl_index, directory,
refactor_branch_upstream):
print('Skipping CL ' + cl_index + ' for directory "' + directory +
'" for which a branch already exists.')
return
# Checkout all changes to files in |files|.
@ -112,14 +120,15 @@ def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
# can be deleted manually after git has read it rather than automatically
# when it is closed.
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(FormatDescriptionOrComment(description, directory))
tmp_file.write(
FormatDescriptionOrComment(description, directory, cl_index, num_cls))
# Close the file to let git open it at the next line.
tmp_file.close()
git.run('commit', '-F', tmp_file.name)
os.remove(tmp_file.name)
# Upload a CL.
upload_args = ['-f', '-r', ','.join(reviewers)]
upload_args = ['-f', '-r', reviewer]
if cq_dry_run:
upload_args.append('--cq-dry-run')
if not comment:
@ -129,26 +138,140 @@ def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
print('Uploading CL for ' + directory + '.')
cmd_upload(upload_args)
if comment:
changelist().AddComment(FormatDescriptionOrComment(comment, directory),
publish=True)
changelist().AddComment(
FormatDescriptionOrComment(comment, directory, cl_index, num_cls),
publish=True)
class ChangeList(object):
"""Representation of a CL and the files affected by it."""
def __init__(self, path, owners_db, author, files):
self._path = path
self._files = files
self._owners_db = owners_db
self._author = author
self._owners = None
def _EnsureOwners(self):
if not self._owners:
self._owners = set()
files = [f.LocalPath() for f in self.GetFiles()]
if not files:
files = [self.GetPath()]
possible_owners = self._owners_db.all_possible_owners(
files, self._author).keys()
for owner in possible_owners:
if 0 == len(self._owners_db.files_not_covered_by(files, [owner])):
self._owners |= set([owner])
assert len(self._owners)
def Merge(self, other):
self._owners = self.GetCommonOwners(other)
self._files |= other.GetFiles()
def GetPath(self):
return self._path
def GetFiles(self):
return self._files
def GetOwners(self):
self._EnsureOwners()
return self._owners
def GetCommonOwners(self, other):
return self.GetOwners() & other.GetOwners()
def HaveCommonOwners(self, other):
return len(self.GetCommonOwners(other)) > 0
def GetFilesSplitByOwners(owners_database, files):
def GetChangeSizeInBytes(self):
return sum(
[c[0] + c[1] for f in self._files for c in f.ChangeSizeInBytes()])
def SplitCLs(owners_database, author, 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)
# The target CL size in # of changed bytes.
# TODO(yannic): Use # of changed lines instead and make this configurable.
max_cl_size = 1000
candidates = trie.Trie()
# Enable sorting so dry-run will split the CL the same way the CL is uploaded.
candidates.enable_sorting()
# 1. Create one CL candidate for every affected file.
for f in files:
files_split_by_owners[owners_database.enclosing_dir_with_owners(
f.LocalPath())].append(f)
return files_split_by_owners
path = f.LocalPath()
candidates[path] = ChangeList(path, owners_database, author, set([f]))
change_lists = []
# 2. Try to merge CL in common directories up to a maximum size of
# |max_cl_size|.
# This is O( len(files) * max([len(f.path) for f in files]) ).
edited = True
while edited:
edited = False
# 2.1. Iterate over all candidates and merge candidates into the candidate
# for their parent directory if the resulting CL doesn't exceed
# |max_cl_size|.
for item in candidates.items():
path = ''.join(item[0])
candidate = item[1]
# The number of CL candidates in subdirectories is equivalent to the
# number of nodes with prefix |path| in the Trie.
# Only try to merge |candidate| with the candidate for the parent
# directory if there are no more CLs for subdirectories.
sub_cls = len([''.join(k) for k in candidates.keys(path)]) - 1
if not sub_cls:
parent_path = os.path.dirname(path)
if len(parent_path) < 1:
# Don't create CLs for more than one top-level directory.
continue
if parent_path not in candidates:
candidates[parent_path] = ChangeList(parent_path, owners_database,
author, set())
parent_cl = candidates[parent_path]
if not parent_cl.HaveCommonOwners(candidate):
# Don't merge if the resulting CL would need more than one reviewer.
continue
# Merge |candidate| into the CL for it's parent directory and remove
# candidate.
edited = True
del candidates[path]
parent_cl.Merge(candidate)
# Add |parent_cl| to list of CLs to submit if the CL is larger than
# |max_cl_size|.
# TODO(yannic): Doing it this way, we might end up with CLs of size
# 2 * max_cl_size if we merged two candidates that just don't exceed
# the maximal size.
if parent_cl.GetChangeSizeInBytes() > max_cl_size:
change_lists.append(parent_cl)
del candidates[parent_path]
# 3. Add all remaining candidates to the list of CLs.
for item in candidates.items():
change_lists.append(item[1])
return change_lists
def PrintClInfo(cl_index, num_cls, directory, file_paths, description,
reviewers):
reviewer):
"""Prints info about a CL.
Args:
@ -158,20 +281,42 @@ def PrintClInfo(cl_index, num_cls, directory, file_paths, description,
to upload a CL.
file_paths: A list of files in this CL.
description: The CL description.
reviewers: A set of reviewers for this CL.
reviewer: The reviewer for this CL.
"""
description_lines = FormatDescriptionOrComment(description,
directory).splitlines()
description_lines = FormatDescriptionOrComment(
description, directory, cl_index, num_cls).splitlines()
indented_description = '\n'.join([' ' + l for l in description_lines])
print('CL {}/{}'.format(cl_index, num_cls))
print('Path: {}'.format(directory))
print('Reviewers: {}'.format(', '.join(reviewers)))
print('Reviewers: {}'.format(reviewer))
print('\n' + indented_description + '\n')
print('\n'.join(file_paths))
print()
def _SelectReviewer(possible_owners, used_reviewers):
"""Select a reviewer from |owners| and adds them to the set of used reviewers.
Returns:
The reviewer.
"""
# It's debatable whether we want to avoid reusing reviewers. It could be
# easier to ask the smallest possible amount of reviewers to become familiar
# with the change being split. However, doing so would mean we send all CLs to
# top-level owners, which might be too much to ask from them.
# We may revisit this decicion later.
unused_reviewers = possible_owners.difference(used_reviewers)
if len(unused_reviewers) < 1:
unused_reviewers = possible_owners
# Pick a random reviwer from the set of owners so we don't prefer owners
# with emails of low lexical order.
reviewer = random.choice(tuple(unused_reviewers))
used_reviewers.add(reviewer)
return reviewer
def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
cq_dry_run, enable_auto_submit):
""""Splits a branch into smaller branches and uploads CLs.
@ -212,11 +357,9 @@ def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
owners_database = owners.Database(change.RepositoryRoot(), file, os.path)
owners_database.load_data_needed_for([f.LocalPath() for f in files])
files_split_by_owners = GetFilesSplitByOwners(owners_database, files)
change_lists = SplitCLs(owners_database, author, set(files))
num_cls = len(files_split_by_owners)
print('Will split current branch (' + refactor_branch + ') into ' +
str(num_cls) + ' CLs.\n')
num_cls = len(change_lists)
if cq_dry_run and num_cls > CL_SPLIT_FORCE_LIMIT:
print(
'This will generate "%r" CLs. This many CLs can potentially generate'
@ -228,21 +371,21 @@ def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
if answer.lower() != 'y':
return 0
for cl_index, (directory, files) in \
enumerate(files_split_by_owners.items(), 1):
reviewers = set()
for cl_index, cl in enumerate(change_lists, 1):
# Use '/' as a path separator in the branch name and the CL description
# and comment.
directory = directory.replace(os.path.sep, '/')
file_paths = [f.LocalPath() for f in files]
reviewers = owners_database.reviewers_for(file_paths, author)
directory = cl.GetPath().replace(os.path.sep, '/')
file_paths = [f.LocalPath() for f in cl.GetFiles()]
reviewer = _SelectReviewer(cl.GetOwners(), reviewers)
if dry_run:
PrintClInfo(cl_index, num_cls, directory, file_paths, description,
reviewers)
reviewer)
else:
UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
description, comment, reviewers, changelist, cmd_upload,
cq_dry_run, enable_auto_submit)
UploadCl(cl_index, num_cls, refactor_branch, refactor_branch_upstream,
directory, files, description, comment, reviewer, changelist,
cmd_upload, cq_dry_run, enable_auto_submit)
# Go back to the original branch.
git.run('checkout', refactor_branch)

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ -0,0 +1,10 @@
URL: https://github.com/google/pygtrie
Version: 64ee0836f41a59919ecf8a59b0c7e2f7f1b8c5ba
License: Apache 2.0
License File: LICENSE
Description:
This directory contains the Python pygtrie module.
Local Modifications:
None

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save