summaryrefslogtreecommitdiffstats
path: root/subcmds/checkout.py
diff options
context:
space:
mode:
authorJason Chang <jasonnc@google.com>2023-08-08 14:12:53 -0700
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-08-10 23:46:31 +0000
commit1a3612fe6d347e458a53d7a9e920a91ea502e6ba (patch)
tree02b1a61f1d97e32201ea5fa309bf1f1b6050e929 /subcmds/checkout.py
parentf0aeb220def22edfac9838288ad251f86da782c1 (diff)
downloadgit-repo-1a3612fe6d347e458a53d7a9e920a91ea502e6ba.tar.gz
Raise RepoExitError in place of sys.exit
Bug: b/293344017 Change-Id: Icae4932b00e4068cba502a5ab4a0274fd7854d9d Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/382214 Reviewed-by: Gavin Mak <gavinmak@google.com> Tested-by: Jason Chang <jasonnc@google.com> Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com> Commit-Queue: Jason Chang <jasonnc@google.com>
Diffstat (limited to 'subcmds/checkout.py')
-rw-r--r--subcmds/checkout.py50
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 @@
15import functools 15import functools
16import sys 16import sys
17 17
18from typing import NamedTuple
18from command import Command, DEFAULT_LOCAL_JOBS 19from command import Command, DEFAULT_LOCAL_JOBS
19from progress import Progress 20from progress import Progress
21from project import Project
22from error import GitError, RepoExitError
23
24
25class 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
32class CheckoutCommandError(RepoExitError):
33 """Exception thrown when checkout command fails."""
34
35
36class MissingBranchError(RepoExitError):
37 """Exception thrown when no project has specified branch."""
20 38
21 39
22class Checkout(Command): 40class 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)