Track expected git errors in logs

Sometimes it is expected that a GitCommand executed in repo fails. In
such cases indicate in trace logs that the error was expected.

Bug: b/293344017
Change-Id: If137fae9ef9769258246f5b4494e070345db4a71
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/387714
Commit-Queue: Jason Chang <jasonnc@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Tested-by: Jason Chang <jasonnc@google.com>
This commit is contained in:
Jason Chang 2023-09-27 11:34:43 -07:00 committed by LUCI
parent b5644160b7
commit 87058c6ca5
2 changed files with 26 additions and 7 deletions

View File

@ -286,6 +286,7 @@ class GitCommand(object):
objdir=None,
verify_command=False,
add_event_log=True,
log_as_error=True,
):
if project:
if not cwd:
@ -362,6 +363,7 @@ class GitCommand(object):
"ReturnCode": str(e.git_rc)
if e.git_rc is not None
else None,
"IsError": log_as_error,
}
)
event_log.ErrorEvent(

View File

@ -205,7 +205,9 @@ class ReviewableBranch(object):
"--",
)
try:
self._commit_cache = self.project.bare_git.rev_list(*args)
self._commit_cache = self.project.bare_git.rev_list(
*args, log_as_error=self.base_exists
)
except GitError:
# We weren't able to probe the commits for this branch. Was it
# tracking a branch that no longer exists? If so, return no
@ -1593,7 +1595,9 @@ class Project(object):
# See if we can perform a fast forward merge. This can happen if our
# branch isn't in the exact same state as we last published.
try:
self.work_git.merge_base("--is-ancestor", HEAD, revid)
self.work_git.merge_base(
"--is-ancestor", HEAD, revid, log_as_error=False
)
# Skip the published logic.
pub = False
except GitError:
@ -2304,15 +2308,26 @@ class Project(object):
# if revision (sha or tag) is not present then following function
# throws an error.
self.bare_git.rev_list(
"-1", "--missing=allow-any", "%s^0" % self.revisionExpr, "--"
"-1",
"--missing=allow-any",
"%s^0" % self.revisionExpr,
"--",
log_as_error=False,
)
if self.upstream:
rev = self.GetRemote().ToLocal(self.upstream)
self.bare_git.rev_list(
"-1", "--missing=allow-any", "%s^0" % rev, "--"
"-1",
"--missing=allow-any",
"%s^0" % rev,
"--",
log_as_error=False,
)
self.bare_git.merge_base(
"--is-ancestor", self.revisionExpr, rev
"--is-ancestor",
self.revisionExpr,
rev,
log_as_error=False,
)
return True
except GitError:
@ -3612,7 +3627,7 @@ class Project(object):
self.update_ref("-d", name, old)
self._project.bare_ref.deleted(name)
def rev_list(self, *args, **kw):
def rev_list(self, *args, log_as_error=True, **kw):
if "format" in kw:
cmdv = ["log", "--pretty=format:%s" % kw["format"]]
else:
@ -3626,6 +3641,7 @@ class Project(object):
capture_stdout=True,
capture_stderr=True,
verify_command=True,
log_as_error=log_as_error,
)
p.Wait()
return p.stdout.splitlines()
@ -3653,7 +3669,7 @@ class Project(object):
"""
name = name.replace("_", "-")
def runner(*args, **kwargs):
def runner(*args, log_as_error=True, **kwargs):
cmdv = []
config = kwargs.pop("config", None)
for k in kwargs:
@ -3674,6 +3690,7 @@ class Project(object):
capture_stdout=True,
capture_stderr=True,
verify_command=True,
log_as_error=log_as_error,
)
p.Wait()
r = p.stdout