Skip to content

Commit

Permalink
Track expected git errors in logs
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Gavin Mak <[email protected]>
Tested-by: Jason Chang <[email protected]>
  • Loading branch information
Jason Chang authored and LUCI committed Sep 27, 2023
1 parent b564416 commit 87058c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions git_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def __init__(
objdir=None,
verify_command=False,
add_event_log=True,
log_as_error=True,
):
if project:
if not cwd:
Expand Down Expand Up @@ -362,6 +363,7 @@ def __init__(
"ReturnCode": str(e.git_rc)
if e.git_rc is not None
else None,
"IsError": log_as_error,
}
)
event_log.ErrorEvent(
Expand Down
31 changes: 24 additions & 7 deletions project.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ def commits(self):
"--",
)
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
Expand Down Expand Up @@ -1593,7 +1595,9 @@ def _dosubmodules():
# 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:
Expand Down Expand Up @@ -2304,15 +2308,26 @@ def _CheckForImmutableRevision(self):
# 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:
Expand Down Expand Up @@ -3612,7 +3627,7 @@ def DeleteRef(self, name, old=None):
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:
Expand All @@ -3626,6 +3641,7 @@ def rev_list(self, *args, **kw):
capture_stdout=True,
capture_stderr=True,
verify_command=True,
log_as_error=log_as_error,
)
p.Wait()
return p.stdout.splitlines()
Expand Down Expand Up @@ -3653,7 +3669,7 @@ def __getattr__(self, name):
"""
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:
Expand All @@ -3674,6 +3690,7 @@ def runner(*args, **kwargs):
capture_stdout=True,
capture_stderr=True,
verify_command=True,
log_as_error=log_as_error,
)
p.Wait()
r = p.stdout
Expand Down

0 comments on commit 87058c6

Please sign in to comment.