diff options
Diffstat (limited to 'subcmds/checkout.py')
-rw-r--r-- | subcmds/checkout.py | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/subcmds/checkout.py b/subcmds/checkout.py index 6448518f..033fd349 100644 --- a/subcmds/checkout.py +++ b/subcmds/checkout.py | |||
@@ -15,8 +15,26 @@ | |||
15 | import functools | 15 | import functools |
16 | import sys | 16 | import sys |
17 | 17 | ||
18 | from typing import NamedTuple | ||
18 | from command import Command, DEFAULT_LOCAL_JOBS | 19 | from command import Command, DEFAULT_LOCAL_JOBS |
19 | from progress import Progress | 20 | from progress import Progress |
21 | from project import Project | ||
22 | from error import GitError, RepoExitError | ||
23 | |||
24 | |||
25 | class CheckoutBranchResult(NamedTuple): | ||
26 | # Whether the Project is on the branch (i.e. branch exists and no errors) | ||
27 | result: bool | ||
28 | project: Project | ||
29 | error: Exception | ||
30 | |||
31 | |||
32 | class CheckoutCommandError(RepoExitError): | ||
33 | """Exception thrown when checkout command fails.""" | ||
34 | |||
35 | |||
36 | class MissingBranchError(RepoExitError): | ||
37 | """Exception thrown when no project has specified branch.""" | ||
20 | 38 | ||
21 | 39 | ||
22 | class Checkout(Command): | 40 | class Checkout(Command): |
@@ -41,23 +59,30 @@ The command is equivalent to: | |||
41 | 59 | ||
42 | def _ExecuteOne(self, nb, project): | 60 | def _ExecuteOne(self, nb, project): |
43 | """Checkout one project.""" | 61 | """Checkout one project.""" |
44 | return (project.CheckoutBranch(nb), project) | 62 | error = None |
63 | result = None | ||
64 | try: | ||
65 | result = project.CheckoutBranch(nb) | ||
66 | except GitError as e: | ||
67 | error = e | ||
68 | return CheckoutBranchResult(result, project, error) | ||
45 | 69 | ||
46 | def Execute(self, opt, args): | 70 | def Execute(self, opt, args): |
47 | nb = args[0] | 71 | nb = args[0] |
48 | err = [] | 72 | err = [] |
73 | err_projects = [] | ||
49 | success = [] | 74 | success = [] |
50 | all_projects = self.GetProjects( | 75 | all_projects = self.GetProjects( |
51 | args[1:], all_manifests=not opt.this_manifest_only | 76 | args[1:], all_manifests=not opt.this_manifest_only |
52 | ) | 77 | ) |
53 | 78 | ||
54 | def _ProcessResults(_pool, pm, results): | 79 | def _ProcessResults(_pool, pm, results): |
55 | for status, project in results: | 80 | for result in results: |
56 | if status is not None: | 81 | if result.error is not None: |
57 | if status: | 82 | err.append(result.error) |
58 | success.append(project) | 83 | err_projects.append(result.project) |
59 | else: | 84 | elif result.result: |
60 | err.append(project) | 85 | success.append(result.project) |
61 | pm.update(msg="") | 86 | pm.update(msg="") |
62 | 87 | ||
63 | self.ExecuteInParallel( | 88 | self.ExecuteInParallel( |
@@ -70,13 +95,14 @@ The command is equivalent to: | |||
70 | ), | 95 | ), |
71 | ) | 96 | ) |
72 | 97 | ||
73 | if err: | 98 | if err_projects: |
74 | for p in err: | 99 | for p in err_projects: |
75 | print( | 100 | print( |
76 | "error: %s/: cannot checkout %s" % (p.relpath, nb), | 101 | "error: %s/: cannot checkout %s" % (p.relpath, nb), |
77 | file=sys.stderr, | 102 | file=sys.stderr, |
78 | ) | 103 | ) |
79 | sys.exit(1) | 104 | raise CheckoutCommandError(aggregate_errors=err) |
80 | elif not success: | 105 | elif not success: |
81 | print("error: no project has branch %s" % nb, file=sys.stderr) | 106 | msg = f"error: no project has branch {nb}" |
82 | sys.exit(1) | 107 | print(msg, file=sys.stderr) |
108 | raise MissingBranchError(msg) | ||