diff options
Diffstat (limited to 'subcmds/info.py')
| -rw-r--r-- | subcmds/info.py | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/subcmds/info.py b/subcmds/info.py index 594e633a3..e120cae33 100644 --- a/subcmds/info.py +++ b/subcmds/info.py | |||
| @@ -18,7 +18,7 @@ import io | |||
| 18 | import json | 18 | import json |
| 19 | import optparse | 19 | import optparse |
| 20 | import sys | 20 | import sys |
| 21 | from typing import Any, Dict, List, NamedTuple | 21 | from typing import Any, Dict, List, NamedTuple, Optional, Tuple |
| 22 | 22 | ||
| 23 | from color import Coloring | 23 | from color import Coloring |
| 24 | from command import DEFAULT_LOCAL_JOBS | 24 | from command import DEFAULT_LOCAL_JOBS |
| @@ -189,18 +189,26 @@ class Info(PagedCommand): | |||
| 189 | "superproject_revision": srev, | 189 | "superproject_revision": srev, |
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | @staticmethod | ||
| 193 | def _GetCurrentBranch(branches: Dict[str, Any]) -> Optional[str]: | ||
| 194 | """Return the name of the current branch from a GetBranches mapping.""" | ||
| 195 | return next( | ||
| 196 | (name for name, branch in branches.items() if branch.current), None | ||
| 197 | ) | ||
| 198 | |||
| 192 | @classmethod | 199 | @classmethod |
| 193 | def _getProjectData(cls, project) -> Dict[str, Any]: | 200 | def _getProjectData(cls, project) -> Dict[str, Any]: |
| 194 | """Gather project data as a dict.""" | 201 | """Gather project data as a dict.""" |
| 202 | branches = project.GetBranches() | ||
| 203 | currentBranch = cls._GetCurrentBranch(branches) | ||
| 195 | data = { | 204 | data = { |
| 196 | "name": project.name, | 205 | "name": project.name, |
| 197 | "mount_path": project.worktree, | 206 | "mount_path": project.worktree, |
| 198 | "current_revision": project.GetHeadRevisionId() | 207 | "current_revision": project.GetHeadRevisionId() |
| 199 | or project.GetRevisionId(), | 208 | or project.GetRevisionId(), |
| 200 | "manifest_revision": project.revisionExpr, | 209 | "manifest_revision": project.revisionExpr, |
| 201 | "local_branches": list(project.GetBranches()), | 210 | "local_branches": list(branches), |
| 202 | } | 211 | } |
| 203 | currentBranch = project.CurrentBranch | ||
| 204 | if currentBranch: | 212 | if currentBranch: |
| 205 | data["current_branch"] = currentBranch | 213 | data["current_branch"] = currentBranch |
| 206 | return data | 214 | return data |
| @@ -285,6 +293,9 @@ class Info(PagedCommand): | |||
| 285 | text = out.nofmt_printer("text") | 293 | text = out.nofmt_printer("text") |
| 286 | dimtext = out.printer("dimtext", attr="dim") | 294 | dimtext = out.printer("dimtext", attr="dim") |
| 287 | 295 | ||
| 296 | branches = project.GetBranches() | ||
| 297 | currentBranch = cls._GetCurrentBranch(branches) | ||
| 298 | |||
| 288 | heading("Project: ") | 299 | heading("Project: ") |
| 289 | headtext(project.name) | 300 | headtext(project.name) |
| 290 | out.nl() | 301 | out.nl() |
| @@ -297,7 +308,6 @@ class Info(PagedCommand): | |||
| 297 | headtext(project.GetHeadRevisionId() or project.GetRevisionId()) | 308 | headtext(project.GetHeadRevisionId() or project.GetRevisionId()) |
| 298 | out.nl() | 309 | out.nl() |
| 299 | 310 | ||
| 300 | currentBranch = project.CurrentBranch | ||
| 301 | if currentBranch: | 311 | if currentBranch: |
| 302 | heading("Current branch: ") | 312 | heading("Current branch: ") |
| 303 | headtext(currentBranch) | 313 | headtext(currentBranch) |
| @@ -307,7 +317,7 @@ class Info(PagedCommand): | |||
| 307 | headtext(project.revisionExpr) | 317 | headtext(project.revisionExpr) |
| 308 | out.nl() | 318 | out.nl() |
| 309 | 319 | ||
| 310 | localBranches = list(project.GetBranches().keys()) | 320 | localBranches = list(branches) |
| 311 | heading("Local Branches: ") | 321 | heading("Local Branches: ") |
| 312 | redtext(str(len(localBranches))) | 322 | redtext(str(len(localBranches))) |
| 313 | if localBranches: | 323 | if localBranches: |
| @@ -327,25 +337,10 @@ class Info(PagedCommand): | |||
| 327 | branch = branch[len(R_HEADS) :] | 337 | branch = branch[len(R_HEADS) :] |
| 328 | logTarget = R_M + branch | 338 | logTarget = R_M + branch |
| 329 | 339 | ||
| 330 | bareTmp = project.bare_git._bare | 340 | localCommits, originCommits = cls._GetDiffCommits( |
| 331 | project.bare_git._bare = False | 341 | project, logTarget |
| 332 | localCommits = project.bare_git.rev_list( | ||
| 333 | "--abbrev=8", | ||
| 334 | "--abbrev-commit", | ||
| 335 | "--pretty=oneline", | ||
| 336 | logTarget + "..", | ||
| 337 | "--", | ||
| 338 | ) | 342 | ) |
| 339 | 343 | ||
| 340 | originCommits = project.bare_git.rev_list( | ||
| 341 | "--abbrev=8", | ||
| 342 | "--abbrev-commit", | ||
| 343 | "--pretty=oneline", | ||
| 344 | ".." + logTarget, | ||
| 345 | "--", | ||
| 346 | ) | ||
| 347 | project.bare_git._bare = bareTmp | ||
| 348 | |||
| 349 | heading("Local Commits: ") | 344 | heading("Local Commits: ") |
| 350 | redtext(str(len(localCommits))) | 345 | redtext(str(len(localCommits))) |
| 351 | dimtext(" (on current branch)") | 346 | dimtext(" (on current branch)") |
| @@ -375,6 +370,35 @@ class Info(PagedCommand): | |||
| 375 | 370 | ||
| 376 | return buf.getvalue() | 371 | return buf.getvalue() |
| 377 | 372 | ||
| 373 | @classmethod | ||
| 374 | def _GetDiffCommits( | ||
| 375 | cls, project: Any, log_target: str | ||
| 376 | ) -> Tuple[List[str], List[str]]: | ||
| 377 | """Return local-only and remote-only commits from one history walk.""" | ||
| 378 | git = getattr(project, "work_git", None) or getattr( | ||
| 379 | project, "bare_git", None | ||
| 380 | ) | ||
| 381 | if git is None: | ||
| 382 | return [], [] | ||
| 383 | commits = git.rev_list( | ||
| 384 | "--left-right", | ||
| 385 | "--abbrev=8", | ||
| 386 | "--abbrev-commit", | ||
| 387 | "--pretty=oneline", | ||
| 388 | f"HEAD...{log_target}", | ||
| 389 | "--", | ||
| 390 | ) | ||
| 391 | if isinstance(commits, str): | ||
| 392 | commits = commits.splitlines() | ||
| 393 | local = [] | ||
| 394 | remote = [] | ||
| 395 | for commit in commits: | ||
| 396 | if commit.startswith("<"): | ||
| 397 | local.append(commit[1:]) | ||
| 398 | elif commit.startswith(">"): | ||
| 399 | remote.append(commit[1:]) | ||
| 400 | return local, remote | ||
| 401 | |||
| 378 | def _printDiffInfo(self, opt, args): | 402 | def _printDiffInfo(self, opt, args): |
| 379 | projs = self.GetProjects(args, all_manifests=not opt.this_manifest_only) | 403 | projs = self.GetProjects(args, all_manifests=not opt.this_manifest_only) |
| 380 | 404 | ||
| @@ -404,10 +428,18 @@ class Info(PagedCommand): | |||
| 404 | project = cls.get_parallel_context()["projects"][project_idx] | 428 | project = cls.get_parallel_context()["projects"][project_idx] |
| 405 | 429 | ||
| 406 | branches = [] | 430 | branches = [] |
| 407 | br = [project.GetUploadableBranch(x) for x in project.GetBranches()] | 431 | local_branches = project.GetBranches() |
| 408 | br = [x for x in br if x] | 432 | current_branch = cls._GetCurrentBranch(local_branches) |
| 409 | if opt.current_branch: | 433 | if opt.current_branch: |
| 410 | br = [x for x in br if x.name == project.CurrentBranch] | 434 | candidate_branches = ( |
| 435 | [current_branch] | ||
| 436 | if current_branch and current_branch in local_branches | ||
| 437 | else [] | ||
| 438 | ) | ||
| 439 | else: | ||
| 440 | candidate_branches = local_branches | ||
| 441 | br = [project.GetUploadableBranch(x) for x in candidate_branches] | ||
| 442 | br = [x for x in br if x] | ||
| 411 | 443 | ||
| 412 | for b in br: | 444 | for b in br: |
| 413 | branches.append( | 445 | branches.append( |
| @@ -416,7 +448,7 @@ class Info(PagedCommand): | |||
| 416 | name=b.name, | 448 | name=b.name, |
| 417 | commits=b.commits, | 449 | commits=b.commits, |
| 418 | date=b.date, | 450 | date=b.date, |
| 419 | is_current=b.name == project.CurrentBranch, | 451 | is_current=b.name == current_branch, |
| 420 | ) | 452 | ) |
| 421 | ) | 453 | ) |
| 422 | return branches | 454 | return branches |
