Skip to content

Commit f6aa5ae

Browse files
authored
Run black on sources. (#31)
1 parent a2af242 commit f6aa5ae

File tree

3 files changed

+142
-51
lines changed

3 files changed

+142
-51
lines changed

src/stack_pr/cli.py

Lines changed: 120 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def commit_id(self) -> str:
204204
return self._search_group(RE_RAW_COMMIT_ID, "commit")
205205

206206
def parents(self) -> List[str]:
207-
return [m.group("commit") for m in RE_RAW_PARENT.finditer(self.raw_header)]
207+
return [
208+
m.group("commit") for m in RE_RAW_PARENT.finditer(self.raw_header)
209+
]
208210

209211
def author(self) -> str:
210212
return self._search_group(RE_RAW_AUTHOR, "author")
@@ -217,7 +219,8 @@ def author_email(self) -> str:
217219

218220
def commit_msg(self) -> str:
219221
return "\n".join(
220-
m.group("line") for m in RE_RAW_COMMIT_MSG_LINE.finditer(self.raw_header)
222+
m.group("line")
223+
for m in RE_RAW_COMMIT_MSG_LINE.finditer(self.raw_header)
221224
)
222225

223226

@@ -398,7 +401,9 @@ def is_ancestor(commit1: str, commit2: str, verbose: bool) -> bool:
398401
# TODO: We need to check returncode of this command more carefully, as the
399402
# command simply might fail (rc != 0 and rc != 1).
400403
p = run_shell_command(
401-
["git", "merge-base", "--is-ancestor", commit1, commit2], check=False, quiet=not verbose
404+
["git", "merge-base", "--is-ancestor", commit1, commit2],
405+
check=False,
406+
quiet=not verbose,
402407
)
403408
return p.returncode == 0
404409

@@ -424,7 +429,9 @@ def get_stack(base: str, head: str, verbose: bool) -> List[StackEntry]:
424429
st: List[StackEntry] = []
425430
stack = (
426431
split_header(
427-
get_command_output(["git", "rev-list", "--header", "^" + base, head])
432+
get_command_output(
433+
["git", "rev-list", "--header", "^" + base, head]
434+
)
428435
)
429436
)[::-1]
430437

@@ -514,7 +521,9 @@ def draft_bitmask_type(value: str) -> List[bool]:
514521
# ===----------------------------------------------------------------------=== #
515522
# SUBMIT
516523
# ===----------------------------------------------------------------------=== #
517-
def add_or_update_metadata(e: StackEntry, needs_rebase: bool, verbose: bool) -> bool:
524+
def add_or_update_metadata(
525+
e: StackEntry, needs_rebase: bool, verbose: bool
526+
) -> bool:
518527
if needs_rebase:
519528
run_shell_command(
520529
[
@@ -538,7 +547,9 @@ def add_or_update_metadata(e: StackEntry, needs_rebase: bool, verbose: bool) ->
538547
# Add the stack info metadata to the commit message
539548
commit_msg += f"\n\nstack-info: PR: {e.pr}, branch: {e.head}"
540549
run_shell_command(
541-
["git", "commit", "--amend", "-F", "-"], input=commit_msg.encode(), quiet=not verbose
550+
["git", "commit", "--amend", "-F", "-"],
551+
input=commit_msg.encode(),
552+
quiet=not verbose,
542553
)
543554
return True
544555

@@ -581,7 +592,10 @@ def init_local_branches(st: List[StackEntry], remote: str, verbose: bool):
581592
log(h("Initializing local branches"), level=1)
582593
set_head_branches(st, remote, verbose)
583594
for e in st:
584-
run_shell_command(["git", "checkout", e.commit.commit_id(), "-B", e.head], quiet=not verbose)
595+
run_shell_command(
596+
["git", "checkout", e.commit.commit_id(), "-B", e.head],
597+
quiet=not verbose,
598+
)
585599

586600

587601
def push_branches(st: List[StackEntry], remote, verbose: bool):
@@ -593,14 +607,14 @@ def push_branches(st: List[StackEntry], remote, verbose: bool):
593607

594608
def print_cmd_failure_details(exc: SubprocessError):
595609
cmd_stdout = (
596-
exc.stdout.decode("utf-8").replace("\\n", "\n").replace("\\t", "\t")
597-
if exc.stdout
598-
else None
610+
exc.stdout.decode("utf-8")
611+
.replace("\\n", "\n")
612+
.replace("\\t", "\t") if exc.stdout else None
599613
)
600614
cmd_stderr = (
601-
exc.stderr.decode("utf-8").replace("\\n", "\n").replace("\\t", "\t")
602-
if exc.stderr
603-
else None
615+
exc.stderr.decode("utf-8")
616+
.replace("\\n", "\n")
617+
.replace("\\t", "\t") if exc.stderr else None
604618
)
605619
print(f"Exitcode: {exc.returncode}")
606620
print(f"Stdout: {cmd_stdout}")
@@ -678,7 +692,9 @@ def add_cross_links(st: List[StackEntry], keep_body: bool, verbose: bool):
678692
if keep_body:
679693
# Keep current body of the PR after the cross links component
680694
current_pr_body = get_current_pr_body(e)
681-
pr_body.append(current_pr_body.split(CROSS_LINKS_DELIMETER, 1)[-1].lstrip())
695+
pr_body.append(
696+
current_pr_body.split(CROSS_LINKS_DELIMETER, 1)[-1].lstrip()
697+
)
682698
else:
683699
pr_body.extend(
684700
[
@@ -718,11 +734,15 @@ def add_cross_links(st: List[StackEntry], keep_body: bool, verbose: bool):
718734
#
719735
# To avoid this, we temporarily set all base branches to point to 'main' - once
720736
# all the branches are pushed we can set the actual base branches.
721-
def reset_remote_base_branches(st: List[StackEntry], target: str, verbose: bool):
737+
def reset_remote_base_branches(
738+
st: List[StackEntry], target: str, verbose: bool
739+
):
722740
log(h("Resetting remote base branches"), level=1)
723741

724742
for e in filter(lambda e: e.has_pr(), st):
725-
run_shell_command(["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose)
743+
run_shell_command(
744+
["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose
745+
)
726746

727747

728748
# If local 'main' lags behind 'origin/main', and 'head' contains all commits
@@ -736,7 +756,9 @@ def reset_remote_base_branches(st: List[StackEntry], target: str, verbose: bool)
736756
# already in remote into their stack, they can use a different notation for the
737757
# base (e.g. explicit hash of the commit) - but most probably nobody ever would
738758
# need that.
739-
def should_update_local_base(head: str, base: str, remote: str, target: str, verbose: bool):
759+
def should_update_local_base(
760+
head: str, base: str, remote: str, target: str, verbose: bool
761+
):
740762
base_hash = get_command_output(["git", "rev-parse", base])
741763
target_hash = get_command_output(["git", "rev-parse", f"{remote}/{target}"])
742764
return (
@@ -748,7 +770,9 @@ def should_update_local_base(head: str, base: str, remote: str, target: str, ver
748770

749771
def update_local_base(base: str, remote: str, target: str, verbose: bool):
750772
log(h(f"Updating local branch {base} to {remote}/{target}"), level=1)
751-
run_shell_command(["git", "rebase", f"{remote}/{target}", base], quiet=not verbose)
773+
run_shell_command(
774+
["git", "rebase", f"{remote}/{target}", base], quiet=not verbose
775+
)
752776

753777

754778
class CommonArgs(NamedTuple):
@@ -763,7 +787,14 @@ class CommonArgs(NamedTuple):
763787

764788
@classmethod
765789
def from_args(cls, args: argparse.Namespace) -> "CommonArgs":
766-
return cls(args.base, args.head, args.remote, args.target, args.hyperlinks, args.verbose)
790+
return cls(
791+
args.base,
792+
args.head,
793+
args.remote,
794+
args.target,
795+
args.hyperlinks,
796+
args.verbose,
797+
)
767798

768799

769800
# If the base isn't explicitly specified, find the merge base between
@@ -784,7 +815,12 @@ def deduce_base(args: CommonArgs) -> CommonArgs:
784815
["git", "merge-base", args.head, f"{args.remote}/{args.target}"]
785816
)
786817
return CommonArgs(
787-
deduced_base, args.head, args.remote, args.target, args.hyperlinks, args.verbose
818+
deduced_base,
819+
args.head,
820+
args.remote,
821+
args.target,
822+
args.hyperlinks,
823+
args.verbose,
788824
)
789825

790826

@@ -815,9 +851,13 @@ def command_submit(
815851

816852
current_branch = get_current_branch_name()
817853

818-
if should_update_local_base(args.head, args.base, args.remote, args.target, args.verbose):
854+
if should_update_local_base(
855+
args.head, args.base, args.remote, args.target, args.verbose
856+
):
819857
update_local_base(args.base, args.remote, args.target, args.verbose)
820-
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)
858+
run_shell_command(
859+
["git", "checkout", current_branch], quiet=not args.verbose
860+
)
821861

822862
# Determine what commits belong to the stack
823863
st = get_stack(args.base, args.head, args.verbose)
@@ -842,7 +882,9 @@ def command_submit(
842882
# If the current branch contains commits from the stack, we will need to
843883
# rebase it in the end since the commits will be modified.
844884
top_branch = st[-1].head
845-
need_to_rebase_current = is_ancestor(top_branch, current_branch, args.verbose)
885+
need_to_rebase_current = is_ancestor(
886+
top_branch, current_branch, args.verbose
887+
)
846888

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

@@ -852,7 +894,9 @@ def command_submit(
852894
# Now we have all the branches, so we can create the corresponding PRs
853895
log(h("Submitting PRs"), level=1)
854896
for e_idx, e in enumerate(st):
855-
is_pr_draft = draft or ((draft_bitmask is not None) and draft_bitmask[e_idx])
897+
is_pr_draft = draft or (
898+
(draft_bitmask is not None) and draft_bitmask[e_idx]
899+
)
856900
create_pr(e, is_pr_draft, reviewer)
857901

858902
# Verify consistency in everything we have so far
@@ -883,11 +927,13 @@ def command_submit(
883927
current_branch,
884928
"--committer-date-is-author-date",
885929
],
886-
quiet=not args.verbose
930+
quiet=not args.verbose,
887931
)
888932
else:
889933
log(h(f"Checking out the original branch '{current_branch}'"), level=1)
890-
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)
934+
run_shell_command(
935+
["git", "checkout", current_branch], quiet=not args.verbose
936+
)
891937

892938
delete_local_branches(st, args.verbose)
893939
print_tips_after_export(st, args)
@@ -920,7 +966,9 @@ def rebase_pr(e: StackEntry, remote: str, target: str, verbose: bool):
920966
except Exception:
921967
error(ERROR_CANT_REBASE.format(**locals()))
922968
raise
923-
run_shell_command(["git", "push", remote, "-f", f"{e.head}:{e.head}"], quiet=not verbose)
969+
run_shell_command(
970+
["git", "push", remote, "-f", f"{e.head}:{e.head}"], quiet=not verbose
971+
)
924972

925973

926974
def land_pr(e: StackEntry, remote: str, target: str, verbose: bool):
@@ -935,7 +983,9 @@ def land_pr(e: StackEntry, remote: str, target: str, verbose: bool):
935983
raise
936984

937985
# Switch PR base branch to 'main'
938-
run_shell_command(["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose)
986+
run_shell_command(
987+
["gh", "pr", "edit", e.pr, "-B", target], quiet=not verbose
988+
)
939989

940990
# Form the commit message: it should contain the original commit message
941991
# and nothing else.
@@ -950,7 +1000,7 @@ def land_pr(e: StackEntry, remote: str, target: str, verbose: bool):
9501000
run_shell_command(
9511001
["gh", "pr", "merge", e.pr, "--squash", "-t", title, "-F", "-"],
9521002
input=pr_body.encode(),
953-
quiet=not verbose
1003+
quiet=not verbose,
9541004
)
9551005

9561006

@@ -992,9 +1042,13 @@ def command_land(args: CommonArgs):
9921042

9931043
current_branch = get_current_branch_name()
9941044

995-
if should_update_local_base(args.head, args.base, args.remote, args.target, args.verbose):
1045+
if should_update_local_base(
1046+
args.head, args.base, args.remote, args.target, args.verbose
1047+
):
9961048
update_local_base(args.base, args.remote, args.target, args.verbose)
997-
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)
1049+
run_shell_command(
1050+
["git", "checkout", current_branch], quiet=not args.verbose
1051+
)
9981052

9991053
# Determine what commits belong to the stack
10001054
st = get_stack(args.base, args.head, args.verbose)
@@ -1023,10 +1077,15 @@ def command_land(args: CommonArgs):
10231077
for e in prs_to_rebase:
10241078
rebase_pr(e, args.remote, args.target, args.verbose)
10251079
# Change the target of the new bottom-most PR in the stack to 'target'
1026-
run_shell_command(["gh", "pr", "edit", prs_to_rebase[0].pr, "-B", args.target], quiet=not args.verbose)
1080+
run_shell_command(
1081+
["gh", "pr", "edit", prs_to_rebase[0].pr, "-B", args.target],
1082+
quiet=not args.verbose,
1083+
)
10271084

10281085
# Delete local and remote stack branches
1029-
run_shell_command(["git", "checkout", current_branch], quiet=not args.verbose)
1086+
run_shell_command(
1087+
["git", "checkout", current_branch], quiet=not args.verbose
1088+
)
10301089

10311090
delete_local_branches(st, args.verbose)
10321091
delete_remote_branches(st[:1], args.remote, args.verbose)
@@ -1035,9 +1094,12 @@ def command_land(args: CommonArgs):
10351094
if branch_exists(args.target):
10361095
run_shell_command(
10371096
["git", "rebase", f"{args.remote}/{args.target}", args.target],
1038-
quiet=not args.verbose
1097+
quiet=not args.verbose,
10391098
)
1040-
run_shell_command(["git", "rebase", f"{args.remote}/{args.target}", current_branch], quiet=not args.verbose)
1099+
run_shell_command(
1100+
["git", "rebase", f"{args.remote}/{args.target}", current_branch],
1101+
quiet=not args.verbose,
1102+
)
10411103

10421104
log(h(blue("SUCCESS!")), level=1)
10431105

@@ -1051,9 +1113,13 @@ def strip_metadata(e: StackEntry, verbose: bool) -> str:
10511113
m = RE_STACK_INFO_LINE.sub("", m)
10521114
run_shell_command(
10531115
["git", "rebase", e.base, e.head, "--committer-date-is-author-date"],
1054-
quiet=not verbose
1116+
quiet=not verbose,
1117+
)
1118+
run_shell_command(
1119+
["git", "commit", "--amend", "-F", "-"],
1120+
input=m.encode(),
1121+
quiet=not verbose,
10551122
)
1056-
run_shell_command(["git", "commit", "--amend", "-F", "-"], input=m.encode(), quiet=not verbose)
10571123

10581124
return get_command_output(["git", "rev-parse", e.head])
10591125

@@ -1081,7 +1147,9 @@ def command_abandon(args: CommonArgs):
10811147
last_hash = strip_metadata(e, args.verbose)
10821148

10831149
log(h("Rebasing the current branch on top of updated top branch"), level=1)
1084-
run_shell_command(["git", "rebase", last_hash, current_branch], quiet=not args.verbose)
1150+
run_shell_command(
1151+
["git", "rebase", last_hash, current_branch], quiet=not args.verbose
1152+
)
10851153

10861154
delete_local_branches(st, args.verbose)
10871155
delete_remote_branches(st, args.remote, args.verbose)
@@ -1122,7 +1190,9 @@ def print_tips_after_view(st: List[StackEntry], args: CommonArgs):
11221190
def command_view(args: CommonArgs):
11231191
log(h("VIEW"), level=1)
11241192

1125-
if should_update_local_base(args.head, args.base, args.remote, args.target, args.verbose):
1193+
if should_update_local_base(
1194+
args.head, args.base, args.remote, args.target, args.verbose
1195+
):
11261196
log(
11271197
red(
11281198
f"\nWarning: Local '{args.base}' is behind"
@@ -1166,9 +1236,13 @@ def create_argparser() -> argparse.ArgumentParser:
11661236
subparsers = parser.add_subparsers(help="sub-command help", dest="command")
11671237

11681238
common_parser = argparse.ArgumentParser(add_help=False)
1169-
common_parser.add_argument("-R", "--remote", default="origin", help="Remote name")
1239+
common_parser.add_argument(
1240+
"-R", "--remote", default="origin", help="Remote name"
1241+
)
11701242
common_parser.add_argument("-B", "--base", help="Local base branch")
1171-
common_parser.add_argument("-H", "--head", default="HEAD", help="Local head branch")
1243+
common_parser.add_argument(
1244+
"-H", "--head", default="HEAD", help="Local head branch"
1245+
)
11721246
common_parser.add_argument(
11731247
"-T", "--target", default="main", help="Remote target branch"
11741248
)
@@ -1179,7 +1253,8 @@ def create_argparser() -> argparse.ArgumentParser:
11791253
help="Enable or disable hyperlink support.",
11801254
)
11811255
common_parser.add_argument(
1182-
"-V", "--verbose",
1256+
"-V",
1257+
"--verbose",
11831258
action="store_true",
11841259
default=False,
11851260
help="Enable verbose output from Git subcommands.",
@@ -1273,7 +1348,9 @@ def main():
12731348
raise Exception(f"Unknown command {args.command}")
12741349
except Exception as exc:
12751350
# If something failed, checkout the original branch
1276-
run_shell_command(["git", "checkout", current_branch], quiet=not common_args.verbose)
1351+
run_shell_command(
1352+
["git", "checkout", current_branch], quiet=not common_args.verbose
1353+
)
12771354
if isinstance(exc, SubprocessError):
12781355
print_cmd_failure_details(exc)
12791356
raise

0 commit comments

Comments
 (0)