diff options
Diffstat (limited to 'subcmds/branches.py')
| -rw-r--r-- | subcmds/branches.py | 287 |
1 files changed, 145 insertions, 142 deletions
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 | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | class BranchColoring(Coloring): | 22 | class BranchColoring(Coloring): |
| 23 | def __init__(self, config): | 23 | def __init__(self, config): |
| 24 | Coloring.__init__(self, config, 'branch') | 24 | Coloring.__init__(self, config, "branch") |
| 25 | self.current = self.printer('current', fg='green') | 25 | self.current = self.printer("current", fg="green") |
| 26 | self.local = self.printer('local') | 26 | self.local = self.printer("local") |
| 27 | self.notinproject = self.printer('notinproject', fg='red') | 27 | self.notinproject = self.printer("notinproject", fg="red") |
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | class BranchInfo(object): | 30 | class BranchInfo(object): |
| 31 | def __init__(self, name): | 31 | def __init__(self, name): |
| 32 | self.name = name | 32 | self.name = name |
| 33 | self.current = 0 | 33 | self.current = 0 |
| 34 | self.published = 0 | 34 | self.published = 0 |
| 35 | self.published_equal = 0 | 35 | self.published_equal = 0 |
| 36 | self.projects = [] | 36 | self.projects = [] |
| 37 | 37 | ||
| 38 | def add(self, b): | 38 | def add(self, b): |
| 39 | if b.current: | 39 | if b.current: |
| 40 | self.current += 1 | 40 | self.current += 1 |
| 41 | if b.published: | 41 | if b.published: |
| 42 | self.published += 1 | 42 | self.published += 1 |
| 43 | if b.revision == b.published: | 43 | if b.revision == b.published: |
| 44 | self.published_equal += 1 | 44 | self.published_equal += 1 |
| 45 | self.projects.append(b) | 45 | self.projects.append(b) |
| 46 | 46 | ||
| 47 | @property | 47 | @property |
| 48 | def IsCurrent(self): | 48 | def IsCurrent(self): |
| 49 | return self.current > 0 | 49 | return self.current > 0 |
| 50 | 50 | ||
| 51 | @property | 51 | @property |
| 52 | def IsSplitCurrent(self): | 52 | def IsSplitCurrent(self): |
| 53 | return self.current != 0 and self.current != len(self.projects) | 53 | return self.current != 0 and self.current != len(self.projects) |
| 54 | 54 | ||
| 55 | @property | 55 | @property |
| 56 | def IsPublished(self): | 56 | def IsPublished(self): |
| 57 | return self.published > 0 | 57 | return self.published > 0 |
| 58 | 58 | ||
| 59 | @property | 59 | @property |
| 60 | def IsPublishedEqual(self): | 60 | def IsPublishedEqual(self): |
| 61 | return self.published_equal == len(self.projects) | 61 | return self.published_equal == len(self.projects) |
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | class Branches(Command): | 64 | class Branches(Command): |
| 65 | COMMON = True | 65 | COMMON = True |
| 66 | helpSummary = "View current topic branches" | 66 | helpSummary = "View current topic branches" |
| 67 | helpUsage = """ | 67 | helpUsage = """ |
| 68 | %prog [<project>...] | 68 | %prog [<project>...] |
| 69 | 69 | ||
| 70 | Summarizes the currently available topic branches. | 70 | Summarizes the currently available topic branches. |
| @@ -95,111 +95,114 @@ the branch appears in, or does not appear in. If no project list | |||
| 95 | is shown, then the branch appears in all projects. | 95 | is shown, then the branch appears in all projects. |
| 96 | 96 | ||
| 97 | """ | 97 | """ |
| 98 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS | 98 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
| 99 | 99 | ||
| 100 | def Execute(self, opt, args): | 100 | def Execute(self, opt, args): |
| 101 | projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only) | 101 | projects = self.GetProjects( |
| 102 | out = BranchColoring(self.manifest.manifestProject.config) | 102 | args, all_manifests=not opt.this_manifest_only |
| 103 | all_branches = {} | 103 | ) |
| 104 | project_cnt = len(projects) | 104 | out = BranchColoring(self.manifest.manifestProject.config) |
| 105 | 105 | all_branches = {} | |
| 106 | def _ProcessResults(_pool, _output, results): | 106 | project_cnt = len(projects) |
| 107 | for name, b in itertools.chain.from_iterable(results): | 107 | |
| 108 | if name not in all_branches: | 108 | def _ProcessResults(_pool, _output, results): |
| 109 | all_branches[name] = BranchInfo(name) | 109 | for name, b in itertools.chain.from_iterable(results): |
| 110 | all_branches[name].add(b) | 110 | if name not in all_branches: |
| 111 | 111 | all_branches[name] = BranchInfo(name) | |
| 112 | self.ExecuteInParallel( | 112 | all_branches[name].add(b) |
| 113 | opt.jobs, | 113 | |
| 114 | expand_project_to_branches, | 114 | self.ExecuteInParallel( |
| 115 | projects, | 115 | opt.jobs, |
| 116 | callback=_ProcessResults) | 116 | expand_project_to_branches, |
| 117 | 117 | projects, | |
| 118 | names = sorted(all_branches) | 118 | callback=_ProcessResults, |
| 119 | 119 | ) | |
| 120 | if not names: | 120 | |
| 121 | print(' (no branches)', file=sys.stderr) | 121 | names = sorted(all_branches) |
| 122 | return | 122 | |
| 123 | 123 | if not names: | |
| 124 | width = 25 | 124 | print(" (no branches)", file=sys.stderr) |
| 125 | for name in names: | 125 | return |
| 126 | if width < len(name): | 126 | |
| 127 | width = len(name) | 127 | width = 25 |
| 128 | 128 | for name in names: | |
| 129 | for name in names: | 129 | if width < len(name): |
| 130 | i = all_branches[name] | 130 | width = len(name) |
| 131 | in_cnt = len(i.projects) | 131 | |
| 132 | 132 | for name in names: | |
| 133 | if i.IsCurrent: | 133 | i = all_branches[name] |
| 134 | current = '*' | 134 | in_cnt = len(i.projects) |
| 135 | hdr = out.current | 135 | |
| 136 | else: | 136 | if i.IsCurrent: |
| 137 | current = ' ' | 137 | current = "*" |
| 138 | hdr = out.local | 138 | hdr = out.current |
| 139 | |||
| 140 | if i.IsPublishedEqual: | ||
| 141 | published = 'P' | ||
| 142 | elif i.IsPublished: | ||
| 143 | published = 'p' | ||
| 144 | else: | ||
| 145 | published = ' ' | ||
| 146 | |||
| 147 | hdr('%c%c %-*s' % (current, published, width, name)) | ||
| 148 | out.write(' |') | ||
| 149 | |||
| 150 | _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) | ||
| 151 | if in_cnt < project_cnt: | ||
| 152 | fmt = out.write | ||
| 153 | paths = [] | ||
| 154 | non_cur_paths = [] | ||
| 155 | if i.IsSplitCurrent or (in_cnt <= project_cnt - in_cnt): | ||
| 156 | in_type = 'in' | ||
| 157 | for b in i.projects: | ||
| 158 | relpath = _RelPath(b.project) | ||
| 159 | if not i.IsSplitCurrent or b.current: | ||
| 160 | paths.append(relpath) | ||
| 161 | else: | 139 | else: |
| 162 | non_cur_paths.append(relpath) | 140 | current = " " |
| 163 | else: | 141 | hdr = out.local |
| 164 | fmt = out.notinproject | 142 | |
| 165 | in_type = 'not in' | 143 | if i.IsPublishedEqual: |
| 166 | have = set() | 144 | published = "P" |
| 167 | for b in i.projects: | 145 | elif i.IsPublished: |
| 168 | have.add(_RelPath(b.project)) | 146 | published = "p" |
| 169 | for p in projects: | 147 | else: |
| 170 | if _RelPath(p) not in have: | 148 | published = " " |
| 171 | paths.append(_RelPath(p)) | 149 | |
| 172 | 150 | hdr("%c%c %-*s" % (current, published, width, name)) | |
| 173 | s = ' %s %s' % (in_type, ', '.join(paths)) | 151 | out.write(" |") |
| 174 | if not i.IsSplitCurrent and (width + 7 + len(s) < 80): | 152 | |
| 175 | fmt = out.current if i.IsCurrent else fmt | 153 | _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) |
| 176 | fmt(s) | 154 | if in_cnt < project_cnt: |
| 177 | else: | 155 | fmt = out.write |
| 178 | fmt(' %s:' % in_type) | 156 | paths = [] |
| 179 | fmt = out.current if i.IsCurrent else out.write | 157 | non_cur_paths = [] |
| 180 | for p in paths: | 158 | if i.IsSplitCurrent or (in_cnt <= project_cnt - in_cnt): |
| 181 | out.nl() | 159 | in_type = "in" |
| 182 | fmt(width * ' ' + ' %s' % p) | 160 | for b in i.projects: |
| 183 | fmt = out.write | 161 | relpath = _RelPath(b.project) |
| 184 | for p in non_cur_paths: | 162 | if not i.IsSplitCurrent or b.current: |
| 163 | paths.append(relpath) | ||
| 164 | else: | ||
| 165 | non_cur_paths.append(relpath) | ||
| 166 | else: | ||
| 167 | fmt = out.notinproject | ||
| 168 | in_type = "not in" | ||
| 169 | have = set() | ||
| 170 | for b in i.projects: | ||
| 171 | have.add(_RelPath(b.project)) | ||
| 172 | for p in projects: | ||
| 173 | if _RelPath(p) not in have: | ||
| 174 | paths.append(_RelPath(p)) | ||
| 175 | |||
| 176 | s = " %s %s" % (in_type, ", ".join(paths)) | ||
| 177 | if not i.IsSplitCurrent and (width + 7 + len(s) < 80): | ||
| 178 | fmt = out.current if i.IsCurrent else fmt | ||
| 179 | fmt(s) | ||
| 180 | else: | ||
| 181 | fmt(" %s:" % in_type) | ||
| 182 | fmt = out.current if i.IsCurrent else out.write | ||
| 183 | for p in paths: | ||
| 184 | out.nl() | ||
| 185 | fmt(width * " " + " %s" % p) | ||
| 186 | fmt = out.write | ||
| 187 | for p in non_cur_paths: | ||
| 188 | out.nl() | ||
| 189 | fmt(width * " " + " %s" % p) | ||
| 190 | else: | ||
| 191 | out.write(" in all projects") | ||
| 185 | out.nl() | 192 | out.nl() |
| 186 | fmt(width * ' ' + ' %s' % p) | ||
| 187 | else: | ||
| 188 | out.write(' in all projects') | ||
| 189 | out.nl() | ||
| 190 | 193 | ||
| 191 | 194 | ||
| 192 | def expand_project_to_branches(project): | 195 | def expand_project_to_branches(project): |
| 193 | """Expands a project into a list of branch names & associated information. | 196 | """Expands a project into a list of branch names & associated information. |
| 194 | 197 | ||
| 195 | Args: | 198 | Args: |
| 196 | project: project.Project | 199 | project: project.Project |
| 197 | 200 | ||
| 198 | Returns: | 201 | Returns: |
| 199 | List[Tuple[str, git_config.Branch]] | 202 | List[Tuple[str, git_config.Branch]] |
| 200 | """ | 203 | """ |
| 201 | branches = [] | 204 | branches = [] |
| 202 | for name, b in project.GetBranches().items(): | 205 | for name, b in project.GetBranches().items(): |
| 203 | b.project = project | 206 | b.project = project |
| 204 | branches.append((name, b)) | 207 | branches.append((name, b)) |
| 205 | return branches | 208 | return branches |
