From 1a3612fe6d347e458a53d7a9e920a91ea502e6ba Mon Sep 17 00:00:00 2001 From: Jason Chang Date: Tue, 8 Aug 2023 14:12:53 -0700 Subject: 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 Tested-by: Jason Chang Reviewed-by: Aravind Vasudevan Commit-Queue: Jason Chang --- project.py | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'project.py') diff --git a/project.py b/project.py index c3eb09c9..6e6a605e 100644 --- a/project.py +++ b/project.py @@ -1733,8 +1733,7 @@ class Project(object): cmd.append( "refs/changes/%2.2d/%d/%d" % (change_id % 100, change_id, patch_id) ) - if GitCommand(self, cmd, bare=True).Wait() != 0: - return None + GitCommand(self, cmd, bare=True, verify_command=True).Wait() return DownloadedChange( self, self.GetRevisionId(), @@ -1911,7 +1910,10 @@ class Project(object): all_refs = self.bare_ref.all if R_HEADS + name in all_refs: - return GitCommand(self, ["checkout", "-q", name, "--"]).Wait() == 0 + GitCommand( + self, ["checkout", "-q", name, "--"], verify_command=True + ).Wait() + return True branch = self.GetBranch(name) branch.remote = self.GetRemote() @@ -1938,15 +1940,13 @@ class Project(object): branch.Save() return True - if ( - GitCommand( - self, ["checkout", "-q", "-b", branch.name, revid] - ).Wait() - == 0 - ): - branch.Save() - return True - return False + GitCommand( + self, + ["checkout", "-q", "-b", branch.name, revid], + verify_command=True, + ).Wait() + branch.Save() + return True def CheckoutBranch(self, name): """Checkout a local topic branch. @@ -1955,8 +1955,8 @@ class Project(object): name: The name of the branch to checkout. Returns: - True if the checkout succeeded; False if it didn't; None if the - branch didn't exist. + True if the checkout succeeded; False if the + branch doesn't exist. """ rev = R_HEADS + name head = self.work_git.GetHead() @@ -1969,7 +1969,7 @@ class Project(object): revid = all_refs[rev] except KeyError: # Branch does not exist in this project. - return None + return False if head.startswith(R_HEADS): try: @@ -1986,15 +1986,14 @@ class Project(object): ) return True - return ( - GitCommand( - self, - ["checkout", name, "--"], - capture_stdout=True, - capture_stderr=True, - ).Wait() - == 0 - ) + GitCommand( + self, + ["checkout", name, "--"], + capture_stdout=True, + capture_stderr=True, + verify_command=True, + ).Wait() + return True def AbandonBranch(self, name): """Destroy a local topic branch. @@ -4458,9 +4457,12 @@ class ManifestProject(MetaProject): syncbuf.Finish() if is_new or self.CurrentBranch is None: - if not self.StartBranch("default"): + try: + self.StartBranch("default") + except GitError as e: + msg = str(e) print( - "fatal: cannot create default in manifest", + f"fatal: cannot create default in manifest {msg}", file=sys.stderr, ) return False -- cgit v1.2.3-54-g00ecf