Skip to content

Commit 9757dc1

Browse files
committed
Cleanup verbosity handling and make output less verbose
stack-info: PR: #109, branch: ZolotukhinM/stack/2
1 parent 82571b8 commit 9757dc1

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/stack_pr/cli.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@
7979

8080
logger = getLogger(__name__)
8181

82+
# Global verbose flag
83+
_verbose = False
84+
85+
86+
def set_verbose(verbose: bool) -> None: # noqa: FBT001
87+
"""Set the global verbose flag."""
88+
global _verbose # noqa: PLW0603
89+
_verbose = verbose
90+
91+
92+
def is_verbose() -> bool:
93+
"""Check if verbose mode is enabled."""
94+
return _verbose
95+
96+
8297
# A bunch of regexps for parsing commit messages and PR descriptions
8398
RE_RAW_COMMIT_ID = re.compile(r"^(?P<commit>[a-f0-9]+)$", re.MULTILINE)
8499
RE_RAW_AUTHOR = re.compile(
@@ -409,12 +424,14 @@ def error(msg: str) -> None:
409424

410425

411426
def log(msg: str, *, level: int = 1) -> None:
412-
if level <= 1:
427+
"""Log a message based on verbosity level.
428+
429+
Args:
430+
msg: Message to log
431+
level: 1 for essential messages (always shown), 2+ for verbose-only messages
432+
"""
433+
if level == 1 or (level >= 2 and is_verbose()): # noqa: PLR2004
413434
print(msg)
414-
elif level == 1:
415-
logger.info(msg)
416-
elif level >= 2: # noqa: PLR2004
417-
logger.debug(msg)
418435

419436

420437
# ===----------------------------------------------------------------------=== #
@@ -484,7 +501,7 @@ def set_base_branches(st: list[StackEntry], target: str) -> None:
484501

485502

486503
def verify(st: list[StackEntry], *, check_base: bool = False) -> None:
487-
log(h("Verifying stack info"))
504+
log(h("Verifying stack info"), level=2)
488505
for index, e in enumerate(st):
489506
if e.has_missing_info():
490507
error(ERROR_STACKINFO_MISSING.format(**locals()))
@@ -677,7 +694,7 @@ def set_head_branches(
677694
def init_local_branches(
678695
st: list[StackEntry], remote: str, *, verbose: bool, branch_name_template: str
679696
) -> None:
680-
log(h("Initializing local branches"))
697+
log(h("Initializing local branches"), level=2)
681698
set_head_branches(
682699
st, remote, verbose=verbose, branch_name_template=branch_name_template
683700
)
@@ -689,7 +706,7 @@ def init_local_branches(
689706

690707

691708
def push_branches(st: list[StackEntry], remote: str, *, verbose: bool) -> None:
692-
log(h("Updating remote branches"))
709+
log(h("Updating remote branches"), level=2)
693710
cmd = ["git", "push", "-f", remote]
694711
cmd.extend([f"{e.head}:{e.head}" for e in st])
695712
run_shell_command(cmd, quiet=not verbose)
@@ -840,7 +857,7 @@ def add_cross_links(st: list[StackEntry], *, keep_body: bool, verbose: bool) ->
840857
def reset_remote_base_branches(
841858
st: list[StackEntry], target: str, *, verbose: bool
842859
) -> None:
843-
log(h("Resetting remote base branches"), level=1)
860+
log(h("Resetting remote base branches"), level=2)
844861

845862
for e in filter(lambda e: e.has_pr(), st):
846863
run_shell_command(["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose)
@@ -1080,7 +1097,7 @@ def command_submit(
10801097
verify(st)
10811098

10821099
# Embed stack-info into commit messages
1083-
log(h("Updating commit messages with stack metadata"), level=1)
1100+
log(h("Updating commit messages with stack metadata"), level=2)
10841101
needs_rebase = False
10851102
for e in st:
10861103
try:
@@ -1097,7 +1114,7 @@ def command_submit(
10971114
add_cross_links(st, keep_body=keep_body, verbose=args.verbose)
10981115

10991116
if need_to_rebase_current:
1100-
log(h(f"Rebasing the original branch '{current_branch}'"), level=1)
1117+
log(h(f"Rebasing the original branch '{current_branch}'"), level=2)
11011118
run_shell_command(
11021119
[
11031120
"git",
@@ -1109,7 +1126,7 @@ def command_submit(
11091126
quiet=not args.verbose,
11101127
)
11111128
else:
1112-
log(h(f"Checking out the original branch '{current_branch}'"), level=1)
1129+
log(h(f"Checking out the original branch '{current_branch}'"), level=2)
11131130
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)
11141131

11151132
delete_local_branches(st, verbose=args.verbose)
@@ -1180,7 +1197,7 @@ def land_pr(e: StackEntry, remote: str, target: str, *, verbose: bool) -> None:
11801197

11811198

11821199
def delete_local_branches(st: list[StackEntry], *, verbose: bool) -> None:
1183-
log(h("Deleting local branches"), level=1)
1200+
log(h("Deleting local branches"), level=2)
11841201
# Delete local branches
11851202
cmd = ["git", "branch", "-D"]
11861203
cmd.extend([e.head for e in st if e.head])
@@ -1618,6 +1635,10 @@ def main() -> None: # noqa: PLR0912
16181635
parser = create_argparser(config)
16191636
args = parser.parse_args()
16201637

1638+
# Set global verbose flag (if present - config command doesn't have it)
1639+
if hasattr(args, "verbose"):
1640+
set_verbose(args.verbose)
1641+
16211642
if not args.command:
16221643
print(h(red("Invalid usage of the stack-pr command.")))
16231644
parser.print_help()

0 commit comments

Comments
 (0)