From 343d585ff99c5748d33823e92507fa06a037c608 Mon Sep 17 00:00:00 2001 From: Ian Kasprzak Date: Wed, 6 Jan 2021 16:26:31 -0800 Subject: Fix bug in git trace2 event Write() function when no config present. See https://bugs.chromium.org/p/gerrit/issues/detail?id=13706#c9 Added additional unit tests for Write() for additional test coverage. Testing: - Unit tests - Verified repo works with: - Valid trace2.eventtarget - Invalid trace2.eventtarget Bug: https://crbug.com/gerrit/13706 Tested-by: Ian Kasprzak Change-Id: I6b027cb2399bd03e453a132ad82e022a1f48476e Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/292762 Reviewed-by: Mike Frysinger --- git_trace2_event_log.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'git_trace2_event_log.py') diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index 4a8e0347..dfbded15 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py @@ -132,6 +132,30 @@ class EventLog(object): exit_event['code'] = result self._log.append(exit_event) + def _GetEventTargetPath(self): + """Get the 'trace2.eventtarget' path from git configuration. + + Returns: + path: git config's 'trace2.eventtarget' path if it exists, or None + """ + path = None + cmd = ['config', '--get', 'trace2.eventtarget'] + # TODO(https://crbug.com/gerrit/13706): Use GitConfig when it supports + # system git config variables. + p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, + bare=True) + retval = p.Wait() + if retval == 0: + # Strip trailing carriage-return in path. + path = p.stdout.rstrip('\n') + elif retval != 1: + # `git config --get` is documented to produce an exit status of `1` if + # the requested variable is not present in the configuration. Report any + # other return value as an error. + print("repo: error: 'git config --get' call failed with return code: %r, stderr: %r" % ( + retval, p.stderr), file=sys.stderr) + return path + def Write(self, path=None): """Writes the log out to a file. @@ -150,21 +174,11 @@ class EventLog(object): log_path = None # If no logging path is specified, get the path from 'trace2.eventtarget'. if path is None: - cmd = ['config', '--get', 'trace2.eventtarget'] - # TODO(https://crbug.com/gerrit/13706): Use GitConfig when it supports - # system git config variables. - p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, - bare=True) - retval = p.Wait() - if retval == 0: - # Strip trailing carriage-return in path. - path = p.stdout.rstrip('\n') - elif retval != 1: - # `git config --get` is documented to produce an exit status of `1` if - # the requested variable is not present in the configuration. Report any - # other return value as an error. - print("repo: error: 'git config --get' call failed with return code: %r, stderr: %r" % ( - retval, p.stderr), file=sys.stderr) + path = self._GetEventTargetPath() + + # If no logging path is specified, exit. + if path is None: + return None if isinstance(path, str): # Get absolute path. -- cgit v1.2.3-54-g00ecf