Skip to content

Commit

Permalink
Ruff format.
Browse files Browse the repository at this point in the history
stack-info: PR: #61, branch: ZolotukhinM/stack/3
  • Loading branch information
ZolotukhinM committed Feb 4, 2025
1 parent efc1d22 commit 5219b55
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 79 deletions.
102 changes: 32 additions & 70 deletions src/stack_pr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ def commit_id(self) -> str:
return self._search_group(RE_RAW_COMMIT_ID, "commit")

def parents(self) -> List[str]:
return [
m.group("commit") for m in RE_RAW_PARENT.finditer(self.raw_header)
]
return [m.group("commit") for m in RE_RAW_PARENT.finditer(self.raw_header)]

def author(self) -> str:
return self._search_group(RE_RAW_AUTHOR, "author")
Expand All @@ -225,8 +223,7 @@ def author_email(self) -> str:

def commit_msg(self) -> str:
return "\n".join(
m.group("line")
for m in RE_RAW_COMMIT_MSG_LINE.finditer(self.raw_header)
m.group("line") for m in RE_RAW_COMMIT_MSG_LINE.finditer(self.raw_header)
)


Expand Down Expand Up @@ -437,9 +434,7 @@ def get_stack(base: str, head: str, verbose: bool) -> List[StackEntry]:
st: List[StackEntry] = []
stack = (
split_header(
get_command_output(
["git", "rev-list", "--header", "^" + base, head]
)
get_command_output(["git", "rev-list", "--header", "^" + base, head])
)
)[::-1]

Expand Down Expand Up @@ -506,7 +501,11 @@ def verify(st: List[StackEntry], check_base: bool = False):
raise RuntimeError

# The first entry on the stack needs to be actually mergeable on GitHub.
if check_base and index == 0 and d["mergeStateStatus"] not in ["CLEAN", "UNKNOWN", "UNSTABLE"]:
if (
check_base
and index == 0
and d["mergeStateStatus"] not in ["CLEAN", "UNKNOWN", "UNSTABLE"]
):
error(ERROR_STACKINFO_PR_NOT_MERGEABLE.format(**locals()))
raise RuntimeError

Expand All @@ -529,9 +528,7 @@ def draft_bitmask_type(value: str) -> List[bool]:
# ===----------------------------------------------------------------------=== #
# SUBMIT
# ===----------------------------------------------------------------------=== #
def add_or_update_metadata(
e: StackEntry, needs_rebase: bool, verbose: bool
) -> bool:
def add_or_update_metadata(e: StackEntry, needs_rebase: bool, verbose: bool) -> bool:
if needs_rebase:
run_shell_command(
[
Expand Down Expand Up @@ -631,14 +628,14 @@ def push_branches(st: List[StackEntry], remote, verbose: bool):

def print_cmd_failure_details(exc: SubprocessError):
cmd_stdout = (
exc.stdout.decode("utf-8")
.replace("\\n", "\n")
.replace("\\t", "\t") if exc.stdout else None
exc.stdout.decode("utf-8").replace("\\n", "\n").replace("\\t", "\t")
if exc.stdout
else None
)
cmd_stderr = (
exc.stderr.decode("utf-8")
.replace("\\n", "\n")
.replace("\\t", "\t") if exc.stderr else None
exc.stderr.decode("utf-8").replace("\\n", "\n").replace("\\t", "\t")
if exc.stderr
else None
)
print(f"Exitcode: {exc.returncode}")
print(f"Stdout: {cmd_stdout}")
Expand Down Expand Up @@ -716,9 +713,7 @@ def add_cross_links(st: List[StackEntry], keep_body: bool, verbose: bool):
if keep_body:
# Keep current body of the PR after the cross links component
current_pr_body = get_current_pr_body(e)
pr_body.append(
current_pr_body.split(CROSS_LINKS_DELIMETER, 1)[-1].lstrip()
)
pr_body.append(current_pr_body.split(CROSS_LINKS_DELIMETER, 1)[-1].lstrip())
else:
pr_body.extend(
[
Expand Down Expand Up @@ -758,15 +753,11 @@ def add_cross_links(st: List[StackEntry], keep_body: bool, verbose: bool):
#
# To avoid this, we temporarily set all base branches to point to 'main' - once
# all the branches are pushed we can set the actual base branches.
def reset_remote_base_branches(
st: List[StackEntry], target: str, verbose: bool
):
def reset_remote_base_branches(st: List[StackEntry], target: str, verbose: bool):
log(h("Resetting remote base branches"), level=1)

for e in filter(lambda e: e.has_pr(), st):
run_shell_command(
["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose
)
run_shell_command(["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose)


# If local 'main' lags behind 'origin/main', and 'head' contains all commits
Expand Down Expand Up @@ -794,9 +785,7 @@ def should_update_local_base(

def update_local_base(base: str, remote: str, target: str, verbose: bool):
log(h(f"Updating local branch {base} to {remote}/{target}"), level=1)
run_shell_command(
["git", "rebase", f"{remote}/{target}", base], quiet=not verbose
)
run_shell_command(["git", "rebase", f"{remote}/{target}", base], quiet=not verbose)


class CommonArgs(NamedTuple):
Expand Down Expand Up @@ -882,9 +871,7 @@ def command_submit(
args.head, args.base, args.remote, args.target, args.verbose
):
update_local_base(args.base, args.remote, args.target, args.verbose)
run_shell_command(
["git", "checkout", current_branch], quiet=not args.verbose
)
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)

# Determine what commits belong to the stack
st = get_stack(args.base, args.head, args.verbose)
Expand All @@ -902,18 +889,14 @@ def command_submit(

# Create local branches and initialize base and head fields in the stack
# elements
init_local_branches(
st, args.remote, args.verbose, args.branch_name_template
)
init_local_branches(st, args.remote, args.verbose, args.branch_name_template)
set_base_branches(st, args.target)
print_stack(st, args.hyperlinks)

# If the current branch contains commits from the stack, we will need to
# rebase it in the end since the commits will be modified.
top_branch = st[-1].head
need_to_rebase_current = is_ancestor(
top_branch, current_branch, args.verbose
)
need_to_rebase_current = is_ancestor(top_branch, current_branch, args.verbose)

reset_remote_base_branches(st, args.target, args.verbose)

Expand All @@ -923,9 +906,7 @@ def command_submit(
# Now we have all the branches, so we can create the corresponding PRs
log(h("Submitting PRs"), level=1)
for e_idx, e in enumerate(st):
is_pr_draft = draft or (
(draft_bitmask is not None) and draft_bitmask[e_idx]
)
is_pr_draft = draft or ((draft_bitmask is not None) and draft_bitmask[e_idx])
create_pr(e, is_pr_draft, reviewer)

# Verify consistency in everything we have so far
Expand Down Expand Up @@ -960,9 +941,7 @@ def command_submit(
)
else:
log(h(f"Checking out the original branch '{current_branch}'"), level=1)
run_shell_command(
["git", "checkout", current_branch], quiet=not args.verbose
)
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)

delete_local_branches(st, args.verbose)
print_tips_after_export(st, args)
Expand Down Expand Up @@ -1012,9 +991,7 @@ def land_pr(e: StackEntry, remote: str, target: str, verbose: bool):
raise

# Switch PR base branch to 'main'
run_shell_command(
["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose
)
run_shell_command(["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose)

# Form the commit message: it should contain the original commit message
# and nothing else.
Expand Down Expand Up @@ -1077,9 +1054,7 @@ def command_land(args: CommonArgs):
args.head, args.base, args.remote, args.target, args.verbose
):
update_local_base(args.base, args.remote, args.target, args.verbose)
run_shell_command(
["git", "checkout", current_branch], quiet=not args.verbose
)
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)

# Determine what commits belong to the stack
st = get_stack(args.base, args.head, args.verbose)
Expand Down Expand Up @@ -1114,9 +1089,7 @@ def command_land(args: CommonArgs):
)

# Delete local and remote stack branches
run_shell_command(
["git", "checkout", current_branch], quiet=not args.verbose
)
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)

delete_local_branches(st, args.verbose)

Expand Down Expand Up @@ -1166,9 +1139,7 @@ def command_abandon(args: CommonArgs):
return
current_branch = get_current_branch_name()

init_local_branches(
st, args.remote, args.verbose, args.branch_name_template
)
init_local_branches(st, args.remote, args.verbose, args.branch_name_template)
set_base_branches(st, args.target)
print_stack(st, args.hyperlinks)

Expand All @@ -1184,9 +1155,7 @@ def command_abandon(args: CommonArgs):
)

delete_local_branches(st, args.verbose)
delete_remote_branches(
st, args.remote, args.verbose, args.branch_name_template
)
delete_remote_branches(st, args.remote, args.verbose, args.branch_name_template)
log(h(blue("SUCCESS!")), level=1)


Expand Down Expand Up @@ -1235,10 +1204,7 @@ def command_view(args: CommonArgs):
level=1,
)
log(
(
"Consider updating your local branch by"
" running the following commands:"
),
("Consider updating your local branch by running the following commands:"),
level=1,
)
log(
Expand Down Expand Up @@ -1279,9 +1245,7 @@ def create_argparser(
help="Remote name",
)
common_parser.add_argument("-B", "--base", help="Local base branch")
common_parser.add_argument(
"-H", "--head", default="HEAD", help="Local head branch"
)
common_parser.add_argument("-H", "--head", default="HEAD", help="Local head branch")
common_parser.add_argument(
"-T",
"--target",
Expand All @@ -1303,9 +1267,7 @@ def create_argparser(
)
common_parser.add_argument(
"--branch-name-template",
default=config.get(
"repo", "branch_name_template", fallback="$USERNAME/stack"
),
default=config.get("repo", "branch_name_template", fallback="$USERNAME/stack"),
help="A template for names of the branches stack-pr would use.",
)

Expand Down
4 changes: 1 addition & 3 deletions src/stack_pr/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def fetch_checkout_commit(
run_shell_command(
["git", "fetch", "--depth=1", remote, ref], cwd=repo_dir, quiet=quiet
)
run_shell_command(
["git", "checkout", "FETCH_HEAD"], cwd=repo_dir, quiet=quiet
)
run_shell_command(["git", "checkout", "FETCH_HEAD"], cwd=repo_dir, quiet=quiet)


def is_full_git_sha(s: str) -> bool:
Expand Down
8 changes: 2 additions & 6 deletions src/stack_pr/shell_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def run_shell_command(
_ = subprocess.list2cmdline(cmd)
kwargs.update({"check": check})
if quiet:
kwargs.update(
{"stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
)
kwargs.update({"stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL})
if SHOW_COMMANDS:
print(f"Running: {cmd}")
return subprocess.run(list(map(str, cmd)), **kwargs)
Expand All @@ -57,8 +55,6 @@ def get_command_output(cmd: ShellCommand, **kwargs: Any) -> str:
ValueError: if the capture_output keyword argument is specified.
"""
if "capture_output" in kwargs:
raise ValueError(
"Cannot pass capture_output when using get_command_output"
)
raise ValueError("Cannot pass capture_output when using get_command_output")
proc = run_shell_command(cmd, capture_output=True, quiet=False, **kwargs)
return proc.stdout.decode("utf-8").rstrip()

0 comments on commit 5219b55

Please sign in to comment.