summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Chang <jasonnc@google.com>2023-06-22 15:04:06 -0700
committerJason Chang <jasonnc@google.com>2023-06-26 15:38:30 +0000
commitc657844efe40b97700c3654989bdbe3a33e409d7 (patch)
treeceb3c380cd3b0c5faf8616396dee3af1fe9b7387
parent1d3b4fbeec576db819b78227befc943fa6f2c6f1 (diff)
downloadgit-repo-c657844efe40b97700c3654989bdbe3a33e409d7.tar.gz
main: Fix exitcode loggingv2.35
Fixed a couple of bugs in ExitEvent logging: - log exitcode 130 on KeyboardInterrupt - log exitcode 1 on unhandled Exception - log errorevent with specific reason for exit Before this CL an exitcode of 0 would be logged, and it would be difficult to determine the cause of non-zero exit codes Bug: b/287105597 Change-Id: I2d34f180581f9fbd77a1c78c966ebed065223af6 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/377834 Tested-by: Jason Chang <jasonnc@google.com> Reviewed-by: Josip Sokcevic <sokcevic@google.com>
-rwxr-xr-xmain.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/main.py b/main.py
index 6dcb66f6..90aba144 100755
--- a/main.py
+++ b/main.py
@@ -25,6 +25,7 @@ import netrc
25import optparse 25import optparse
26import os 26import os
27import shlex 27import shlex
28import signal
28import sys 29import sys
29import textwrap 30import textwrap
30import time 31import time
@@ -95,6 +96,7 @@ else:
95 file=sys.stderr, 96 file=sys.stderr,
96 ) 97 )
97 98
99KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT
98 100
99global_options = optparse.OptionParser( 101global_options = optparse.OptionParser(
100 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]", 102 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]",
@@ -374,7 +376,11 @@ class _Repo(object):
374 git_trace2_event_log.StartEvent() 376 git_trace2_event_log.StartEvent()
375 git_trace2_event_log.CommandEvent(name="repo", subcommands=[name]) 377 git_trace2_event_log.CommandEvent(name="repo", subcommands=[name])
376 378
377 try: 379 def execute_command_helper():
380 """
381 Execute the subcommand.
382 """
383 nonlocal result
378 cmd.CommonValidateOptions(copts, cargs) 384 cmd.CommonValidateOptions(copts, cargs)
379 cmd.ValidateOptions(copts, cargs) 385 cmd.ValidateOptions(copts, cargs)
380 386
@@ -409,6 +415,23 @@ class _Repo(object):
409 if hasattr(copts, "manifest_branch"): 415 if hasattr(copts, "manifest_branch"):
410 child_argv.extend(["--manifest-branch", spec.revision]) 416 child_argv.extend(["--manifest-branch", spec.revision])
411 result = self._Run(name, gopts, child_argv) or result 417 result = self._Run(name, gopts, child_argv) or result
418
419 def execute_command():
420 """
421 Execute the command and log uncaught exceptions.
422 """
423 try:
424 execute_command_helper()
425 except (KeyboardInterrupt, SystemExit, Exception) as e:
426 ok = isinstance(e, SystemExit) and not e.code
427 if not ok:
428 exception_name = type(e).__name__
429 git_trace2_event_log.ErrorEvent(
430 f"RepoExitError:{exception_name}")
431 raise
432
433 try:
434 execute_command()
412 except ( 435 except (
413 DownloadError, 436 DownloadError,
414 ManifestInvalidRevisionError, 437 ManifestInvalidRevisionError,
@@ -448,6 +471,12 @@ class _Repo(object):
448 if e.code: 471 if e.code:
449 result = e.code 472 result = e.code
450 raise 473 raise
474 except KeyboardInterrupt:
475 result = KEYBOARD_INTERRUPT_EXIT
476 raise
477 except Exception:
478 result = 1
479 raise
451 finally: 480 finally:
452 finish = time.time() 481 finish = time.time()
453 elapsed = finish - start 482 elapsed = finish - start
@@ -813,7 +842,7 @@ def _Main(argv):
813 result = repo._Run(name, gopts, argv) or 0 842 result = repo._Run(name, gopts, argv) or 0
814 except KeyboardInterrupt: 843 except KeyboardInterrupt:
815 print("aborted by user", file=sys.stderr) 844 print("aborted by user", file=sys.stderr)
816 result = 1 845 result = KEYBOARD_INTERRUPT_EXIT
817 except ManifestParseError as mpe: 846 except ManifestParseError as mpe:
818 print("fatal: %s" % mpe, file=sys.stderr) 847 print("fatal: %s" % mpe, file=sys.stderr)
819 result = 1 848 result = 1