Skip to content

Commit

Permalink
subcmds: Use repo logger
Browse files Browse the repository at this point in the history
Bug: b/292704435
Change-Id: Ia3a45d87fc0bf0d4a1ba53050d9c3cd2dba20e55
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/386236
Reviewed-by: Jason Chang <[email protected]>
Commit-Queue: Aravind Vasudevan <[email protected]>
Tested-by: Aravind Vasudevan <[email protected]>
  • Loading branch information
Aravind Vasudevan authored and LUCI committed Sep 14, 2023
1 parent c3d7c85 commit c993c50
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 115 deletions.
17 changes: 7 additions & 10 deletions subcmds/abandon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import collections
import functools
import itertools
import sys

from command import Command
from command import DEFAULT_LOCAL_JOBS
from error import RepoError
from error import RepoExitError
from git_command import git
from progress import Progress
from repo_logging import RepoLogger


logger = RepoLogger(__file__)


class AbandonError(RepoExitError):
Expand Down Expand Up @@ -126,18 +129,12 @@ def _ProcessResults(_pool, pm, states):
if err:
for br in err.keys():
err_msg = "error: cannot abandon %s" % br
print(err_msg, file=sys.stderr)
logger.error(err_msg)
for proj in err[br]:
print(
" " * len(err_msg) + " | %s" % _RelPath(proj),
file=sys.stderr,
)
logger.error(" " * len(err_msg) + " | %s", _RelPath(proj))
raise AbandonError(aggregate_errors=aggregate_errors)
elif not success:
print(
"error: no project has local branch(es) : %s" % nb,
file=sys.stderr,
)
logger.error("error: no project has local branch(es) : %s", nb)
raise AbandonError(aggregate_errors=aggregate_errors)
else:
# Everything below here is displaying status.
Expand Down
12 changes: 6 additions & 6 deletions subcmds/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import functools
import sys
from typing import NamedTuple

from command import Command
Expand All @@ -22,6 +21,10 @@
from error import RepoExitError
from progress import Progress
from project import Project
from repo_logging import RepoLogger


logger = RepoLogger(__file__)


class CheckoutBranchResult(NamedTuple):
Expand Down Expand Up @@ -99,12 +102,9 @@ def _ProcessResults(_pool, pm, results):

if err_projects:
for p in err_projects:
print(
"error: %s/: cannot checkout %s" % (p.relpath, nb),
file=sys.stderr,
)
logger.error("error: %s/: cannot checkout %s", p.relpath, nb)
raise CheckoutCommandError(aggregate_errors=err)
elif not success:
msg = f"error: no project has branch {nb}"
print(msg, file=sys.stderr)
logger.error(msg)
raise MissingBranchError(msg)
22 changes: 9 additions & 13 deletions subcmds/cherry_pick.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
from command import Command
from error import GitError
from git_command import GitCommand
from repo_logging import RepoLogger


CHANGE_ID_RE = re.compile(r"^\s*Change-Id: I([0-9a-f]{40})\s*$")
logger = RepoLogger(__file__)


class CherryPick(Command):
Expand Down Expand Up @@ -52,7 +54,7 @@ def Execute(self, opt, args):
try:
p.Wait()
except GitError:
print(p.stderr, file=sys.stderr)
logger.error(p.stderr)
raise

sha1 = p.stdout.strip()
Expand All @@ -67,9 +69,7 @@ def Execute(self, opt, args):
try:
p.Wait()
except GitError:
print(
"error: Failed to retrieve old commit message", file=sys.stderr
)
logger.error("error: Failed to retrieve old commit message")
raise

old_msg = self._StripHeader(p.stdout)
Expand All @@ -85,14 +85,13 @@ def Execute(self, opt, args):
try:
p.Wait()
except GitError as e:
print(str(e))
print(
logger.error(e)
logger.warn(
"NOTE: When committing (please see above) and editing the "
"commit message, please remove the old Change-Id-line and "
"add:"
"add:\n%s",
self._GetReference(sha1),
)
print(self._GetReference(sha1), file=sys.stderr)
print(file=sys.stderr)
raise

if p.stdout:
Expand All @@ -115,10 +114,7 @@ def Execute(self, opt, args):
try:
p.Wait()
except GitError:
print(
"error: Failed to update commit message",
file=sys.stderr,
)
logger.error("error: Failed to update commit message")
raise

def _IsChangeId(self, line):
Expand Down
49 changes: 25 additions & 24 deletions subcmds/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
from error import GitError
from error import NoSuchProjectError
from error import RepoExitError
from repo_logging import RepoLogger


CHANGE_RE = re.compile(r"^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$")
logger = RepoLogger(__file__)


class DownloadCommandError(RepoExitError):
Expand Down Expand Up @@ -109,21 +111,16 @@ def _ParseChangeIds(self, opt, args):
except NoSuchProjectError:
project = None
if project not in projects:
print(
logger.error(
"error: %s matches too many projects; please "
"re-run inside the project checkout." % (a,),
file=sys.stderr,
"re-run inside the project checkout.",
a,
)
for project in projects:
print(
" %s/ @ %s"
% (
project.RelPath(
local=opt.this_manifest_only
),
project.revisionExpr,
),
file=sys.stderr,
logger.error(
" %s/ @ %s",
project.RelPath(local=opt.this_manifest_only),
project.revisionExpr,
)
raise NoSuchProjectError()
else:
Expand Down Expand Up @@ -156,18 +153,21 @@ def _ExecuteHelper(self, opt, args):
dl = project.DownloadPatchSet(change_id, ps_id)

if not opt.revert and not dl.commits:
print(
"[%s] change %d/%d has already been merged"
% (project.name, change_id, ps_id),
file=sys.stderr,
logger.error(
"[%s] change %d/%d has already been merged",
project.name,
change_id,
ps_id,
)
continue

if len(dl.commits) > 1:
print(
"[%s] %d/%d depends on %d unmerged changes:"
% (project.name, change_id, ps_id, len(dl.commits)),
file=sys.stderr,
logger.error(
"[%s] %d/%d depends on %d unmerged changes:",
project.name,
change_id,
ps_id,
len(dl.commits),
)
for c in dl.commits:
print(" %s" % (c), file=sys.stderr)
Expand Down Expand Up @@ -204,9 +204,10 @@ def _ExecuteHelper(self, opt, args):
project._Checkout(dl.commit)

except GitError:
print(
"[%s] Could not complete the %s of %s"
% (project.name, mode, dl.commit),
file=sys.stderr,
logger.error(
"[%s] Could not complete the %s of %s",
project.name,
mode,
dl.commit,
)
raise
10 changes: 6 additions & 4 deletions subcmds/forall.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
from command import MirrorSafeCommand
from command import WORKER_BATCH_SIZE
from error import ManifestInvalidRevisionError
from repo_logging import RepoLogger


logger = RepoLogger(__file__)
_CAN_COLOR = [
"branch",
"diff",
Expand Down Expand Up @@ -293,10 +295,10 @@ def __init__(self, config, cmd):
rc = rc or errno.EINTR
except Exception as e:
# Catch any other exceptions raised
print(
"forall: unhandled error, terminating the pool: %s: %s"
% (type(e).__name__, e),
file=sys.stderr,
logger.error(
"forall: unhandled error, terminating the pool: %s: %s",
type(e).__name__,
e,
)
rc = rc or getattr(e, "errno", 1)
if rc != 0:
Expand Down
8 changes: 6 additions & 2 deletions subcmds/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
from error import SilentRepoExitError
from git_command import GitCommand
from project import Project
from repo_logging import RepoLogger


logger = RepoLogger(__file__)


class GrepColoring(Coloring):
Expand Down Expand Up @@ -371,7 +375,7 @@ def Execute(self, opt, args):
if opt.revision:
if "--cached" in cmd_argv:
msg = "fatal: cannot combine --cached and --revision"
print(msg, file=sys.stderr)
logger.error(msg)
raise InvalidArgumentsError(msg)
have_rev = True
cmd_argv.extend(opt.revision)
Expand All @@ -396,5 +400,5 @@ def Execute(self, opt, args):
sys.exit(0)
elif have_rev and bad_rev:
for r in opt.revision:
print("error: can't search revision %s" % r, file=sys.stderr)
logger.error("error: can't search revision %s", r)
raise GrepCommandError(aggregate_errors=errors)
18 changes: 9 additions & 9 deletions subcmds/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
from git_command import git_require
from git_command import MIN_GIT_VERSION_HARD
from git_command import MIN_GIT_VERSION_SOFT
from repo_logging import RepoLogger
from wrapper import Wrapper


logger = RepoLogger(__file__)

_REPO_ALLOW_SHALLOW = os.environ.get("REPO_ALLOW_SHALLOW")


Expand Down Expand Up @@ -330,11 +333,11 @@ def ValidateOptions(self, opt, args):
def Execute(self, opt, args):
git_require(MIN_GIT_VERSION_HARD, fail=True)
if not git_require(MIN_GIT_VERSION_SOFT):
print(
"repo: warning: git-%s+ will soon be required; please upgrade "
"your version of git to maintain support."
% (".".join(str(x) for x in MIN_GIT_VERSION_SOFT),),
file=sys.stderr,
logger.warning(
"repo: warning: git-%s+ will soon be required; "
"please upgrade your version of git to maintain "
"support.",
".".join(str(x) for x in MIN_GIT_VERSION_SOFT),
)

rp = self.manifest.repoProject
Expand All @@ -357,10 +360,7 @@ def Execute(self, opt, args):
)
except wrapper.CloneFailure as e:
err_msg = "fatal: double check your --repo-rev setting."
print(
err_msg,
file=sys.stderr,
)
logger.error(err_msg)
self.git_event_log.ErrorEvent(err_msg)
raise RepoUnhandledExceptionError(e)

Expand Down
16 changes: 10 additions & 6 deletions subcmds/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import sys

from command import PagedCommand
from repo_logging import RepoLogger


logger = RepoLogger(__file__)


class Manifest(PagedCommand):
Expand Down Expand Up @@ -132,7 +136,7 @@ def _Output(self, opt):
manifest.SetUseLocalManifests(not opt.ignore_local_manifests)

if opt.json:
print("warning: --json is experimental!", file=sys.stderr)
logger.warn("warning: --json is experimental!")
doc = manifest.ToDict(
peg_rev=opt.peg_rev,
peg_rev_upstream=opt.peg_rev_upstream,
Expand All @@ -159,13 +163,13 @@ def _Output(self, opt):
if output_file != "-":
fd.close()
if manifest.path_prefix:
print(
f"Saved {manifest.path_prefix} submanifest to "
f"{output_file}",
file=sys.stderr,
logger.warn(
"Saved %s submanifest to %s",
manifest.path_prefix,
output_file,
)
else:
print(f"Saved manifest to {output_file}", file=sys.stderr)
logger.warn("Saved manifest to %s", output_file)

def ValidateOptions(self, opt, args):
if args:
Expand Down
Loading

0 comments on commit c993c50

Please sign in to comment.