From ea2e330e43c182dc16b0111ebc69ee5a71ee4ce1 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Sat, 11 Mar 2023 06:46:20 +0000 Subject: Format codebase with black and check formatting in CQ Apply rules set by https://gerrit-review.googlesource.com/c/git-repo/+/362954/ across the codebase and fix any lingering errors caught by flake8. Also check black formatting in run_tests (and CQ). Bug: b/267675342 Change-Id: I972d77649dac351150dcfeb1cd1ad0ea2efc1956 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/363474 Reviewed-by: Mike Frysinger Tested-by: Gavin Mak Commit-Queue: Gavin Mak --- subcmds/branches.py | 287 ++++++++++++++++++++++++++-------------------------- 1 file changed, 145 insertions(+), 142 deletions(-) (limited to 'subcmds/branches.py') diff --git a/subcmds/branches.py b/subcmds/branches.py index fcf67ef5..4d5bb196 100644 --- a/subcmds/branches.py +++ b/subcmds/branches.py @@ -20,51 +20,51 @@ from command import Command, DEFAULT_LOCAL_JOBS class BranchColoring(Coloring): - def __init__(self, config): - Coloring.__init__(self, config, 'branch') - self.current = self.printer('current', fg='green') - self.local = self.printer('local') - self.notinproject = self.printer('notinproject', fg='red') + def __init__(self, config): + Coloring.__init__(self, config, "branch") + self.current = self.printer("current", fg="green") + self.local = self.printer("local") + self.notinproject = self.printer("notinproject", fg="red") class BranchInfo(object): - def __init__(self, name): - self.name = name - self.current = 0 - self.published = 0 - self.published_equal = 0 - self.projects = [] - - def add(self, b): - if b.current: - self.current += 1 - if b.published: - self.published += 1 - if b.revision == b.published: - self.published_equal += 1 - self.projects.append(b) - - @property - def IsCurrent(self): - return self.current > 0 - - @property - def IsSplitCurrent(self): - return self.current != 0 and self.current != len(self.projects) - - @property - def IsPublished(self): - return self.published > 0 - - @property - def IsPublishedEqual(self): - return self.published_equal == len(self.projects) + def __init__(self, name): + self.name = name + self.current = 0 + self.published = 0 + self.published_equal = 0 + self.projects = [] + + def add(self, b): + if b.current: + self.current += 1 + if b.published: + self.published += 1 + if b.revision == b.published: + self.published_equal += 1 + self.projects.append(b) + + @property + def IsCurrent(self): + return self.current > 0 + + @property + def IsSplitCurrent(self): + return self.current != 0 and self.current != len(self.projects) + + @property + def IsPublished(self): + return self.published > 0 + + @property + def IsPublishedEqual(self): + return self.published_equal == len(self.projects) class Branches(Command): - COMMON = True - helpSummary = "View current topic branches" - helpUsage = """ + COMMON = True + helpSummary = "View current topic branches" + helpUsage = """ %prog [...] Summarizes the currently available topic branches. @@ -95,111 +95,114 @@ the branch appears in, or does not appear in. If no project list is shown, then the branch appears in all projects. """ - PARALLEL_JOBS = DEFAULT_LOCAL_JOBS - - def Execute(self, opt, args): - projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only) - out = BranchColoring(self.manifest.manifestProject.config) - all_branches = {} - project_cnt = len(projects) - - def _ProcessResults(_pool, _output, results): - for name, b in itertools.chain.from_iterable(results): - if name not in all_branches: - all_branches[name] = BranchInfo(name) - all_branches[name].add(b) - - self.ExecuteInParallel( - opt.jobs, - expand_project_to_branches, - projects, - callback=_ProcessResults) - - names = sorted(all_branches) - - if not names: - print(' (no branches)', file=sys.stderr) - return - - width = 25 - for name in names: - if width < len(name): - width = len(name) - - for name in names: - i = all_branches[name] - in_cnt = len(i.projects) - - if i.IsCurrent: - current = '*' - hdr = out.current - else: - current = ' ' - hdr = out.local - - if i.IsPublishedEqual: - published = 'P' - elif i.IsPublished: - published = 'p' - else: - published = ' ' - - hdr('%c%c %-*s' % (current, published, width, name)) - out.write(' |') - - _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) - if in_cnt < project_cnt: - fmt = out.write - paths = [] - non_cur_paths = [] - if i.IsSplitCurrent or (in_cnt <= project_cnt - in_cnt): - in_type = 'in' - for b in i.projects: - relpath = _RelPath(b.project) - if not i.IsSplitCurrent or b.current: - paths.append(relpath) + PARALLEL_JOBS = DEFAULT_LOCAL_JOBS + + def Execute(self, opt, args): + projects = self.GetProjects( + args, all_manifests=not opt.this_manifest_only + ) + out = BranchColoring(self.manifest.manifestProject.config) + all_branches = {} + project_cnt = len(projects) + + def _ProcessResults(_pool, _output, results): + for name, b in itertools.chain.from_iterable(results): + if name not in all_branches: + all_branches[name] = BranchInfo(name) + all_branches[name].add(b) + + self.ExecuteInParallel( + opt.jobs, + expand_project_to_branches, + projects, + callback=_ProcessResults, + ) + + names = sorted(all_branches) + + if not names: + print(" (no branches)", file=sys.stderr) + return + + width = 25 + for name in names: + if width < len(name): + width = len(name) + + for name in names: + i = all_branches[name] + in_cnt = len(i.projects) + + if i.IsCurrent: + current = "*" + hdr = out.current else: - non_cur_paths.append(relpath) - else: - fmt = out.notinproject - in_type = 'not in' - have = set() - for b in i.projects: - have.add(_RelPath(b.project)) - for p in projects: - if _RelPath(p) not in have: - paths.append(_RelPath(p)) - - s = ' %s %s' % (in_type, ', '.join(paths)) - if not i.IsSplitCurrent and (width + 7 + len(s) < 80): - fmt = out.current if i.IsCurrent else fmt - fmt(s) - else: - fmt(' %s:' % in_type) - fmt = out.current if i.IsCurrent else out.write - for p in paths: - out.nl() - fmt(width * ' ' + ' %s' % p) - fmt = out.write - for p in non_cur_paths: + current = " " + hdr = out.local + + if i.IsPublishedEqual: + published = "P" + elif i.IsPublished: + published = "p" + else: + published = " " + + hdr("%c%c %-*s" % (current, published, width, name)) + out.write(" |") + + _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) + if in_cnt < project_cnt: + fmt = out.write + paths = [] + non_cur_paths = [] + if i.IsSplitCurrent or (in_cnt <= project_cnt - in_cnt): + in_type = "in" + for b in i.projects: + relpath = _RelPath(b.project) + if not i.IsSplitCurrent or b.current: + paths.append(relpath) + else: + non_cur_paths.append(relpath) + else: + fmt = out.notinproject + in_type = "not in" + have = set() + for b in i.projects: + have.add(_RelPath(b.project)) + for p in projects: + if _RelPath(p) not in have: + paths.append(_RelPath(p)) + + s = " %s %s" % (in_type, ", ".join(paths)) + if not i.IsSplitCurrent and (width + 7 + len(s) < 80): + fmt = out.current if i.IsCurrent else fmt + fmt(s) + else: + fmt(" %s:" % in_type) + fmt = out.current if i.IsCurrent else out.write + for p in paths: + out.nl() + fmt(width * " " + " %s" % p) + fmt = out.write + for p in non_cur_paths: + out.nl() + fmt(width * " " + " %s" % p) + else: + out.write(" in all projects") out.nl() - fmt(width * ' ' + ' %s' % p) - else: - out.write(' in all projects') - out.nl() def expand_project_to_branches(project): - """Expands a project into a list of branch names & associated information. - - Args: - project: project.Project - - Returns: - List[Tuple[str, git_config.Branch]] - """ - branches = [] - for name, b in project.GetBranches().items(): - b.project = project - branches.append((name, b)) - return branches + """Expands a project into a list of branch names & associated information. + + Args: + project: project.Project + + Returns: + List[Tuple[str, git_config.Branch]] + """ + branches = [] + for name, b in project.GetBranches().items(): + b.project = project + branches.append((name, b)) + return branches -- cgit v1.2.3-54-g00ecf