Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CommitHeader use cached_property #199

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions ghstack/git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import re
from functools import cached_property
from typing import List, Pattern

import ghstack.diff
Expand Down Expand Up @@ -32,30 +33,38 @@ def _search_group(self, regex: Pattern[str], group: str) -> str:
assert m
return m.group(group)

@cached_property
def tree(self) -> GitTreeHash:
return GitTreeHash(self._search_group(RE_RAW_TREE, "tree"))

@cached_property
def title(self) -> str:
return self._search_group(RE_RAW_COMMIT_MSG_LINE, "line")

@cached_property
def commit_id(self) -> GitCommitHash:
return GitCommitHash(self._search_group(RE_RAW_COMMIT_ID, "commit"))

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

@cached_property
def author(self) -> str:
return self._search_group(RE_RAW_AUTHOR, "author")

@cached_property
def author_name(self) -> str:
return self._search_group(RE_RAW_AUTHOR, "name")

@cached_property
def author_email(self) -> str:
return self._search_group(RE_RAW_AUTHOR, "email")

@cached_property
def commit_msg(self) -> str:
return "\n".join(
m.group("line") for m in RE_RAW_COMMIT_MSG_LINE.finditer(self.raw_header)
Expand All @@ -68,25 +77,25 @@ def split_header(s: str) -> List[CommitHeader]:

def parse_header(s: str, github_url: str) -> List[ghstack.diff.Diff]:
def convert(h: CommitHeader) -> ghstack.diff.Diff:
parents = h.parents()
parents = h.parents
if len(parents) != 1:
raise RuntimeError(
"The commit {} has {} parents, which makes my head explode. "
"`git rebase -i` your diffs into a stack, then try again.".format(
h.commit_id(), len(parents)
h.commit_id, len(parents)
)
)
return ghstack.diff.Diff(
title=h.title(),
summary=h.commit_msg(),
oid=h.commit_id(),
source_id=h.tree(),
title=h.title,
summary=h.commit_msg,
oid=h.commit_id,
source_id=h.tree,
pull_request_resolved=ghstack.diff.PullRequestResolved.search(
h.raw_header, github_url
),
tree=h.tree(),
author_name=h.author_name(),
author_email=h.author_email(),
tree=h.tree,
author_name=h.author_name,
author_email=h.author_email,
)

return list(map(convert, split_header(s)))
4 changes: 2 additions & 2 deletions ghstack/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def main(
repo_name=repo_name_nonopt,
repo_id=repo_id,
base_commit=base,
base_tree=base_obj.tree(),
base_tree=base_obj.tree,
stack_base=base,
stack_header=stack_header,
update_fields=update_fields,
Expand Down Expand Up @@ -566,7 +566,7 @@ def elaborate_diff(
) from e
raise
remote_summary = ghstack.git.split_header(rev_list)[0]
m_remote_source_id = RE_GHSTACK_SOURCE_ID.search(remote_summary.commit_msg())
m_remote_source_id = RE_GHSTACK_SOURCE_ID.search(remote_summary.commit_msg)
remote_source_id = m_remote_source_id.group(1) if m_remote_source_id else None

return DiffWithGitHubMetadata(
Expand Down
10 changes: 5 additions & 5 deletions ghstack/unlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def main(
if parsed_commits is not None:
stack_commits = set()
for s in stack:
stack_commits.add(s.commit_id())
stack_commits.add(s.commit_id)
invalid_commits = parsed_commits - stack_commits
if invalid_commits:
raise RuntimeError(
Expand All @@ -82,15 +82,15 @@ def main(
rewriting = False

for s in stack:
commit_id = s.commit_id()
commit_id = s.commit_id
should_unlink = parsed_commits is None or commit_id in parsed_commits
if not rewriting and not should_unlink:
# Advance HEAD without reconstructing commit
head = commit_id
continue

rewriting = True
commit_msg = s.commit_msg()
commit_msg = s.commit_msg
logging.debug("-- commit_msg:\n{}".format(textwrap.indent(commit_msg, " ")))
if should_unlink:
commit_msg = RE_GHSTACK_SOURCE_ID.sub(
Expand All @@ -106,7 +106,7 @@ def main(
sh.git(
"commit-tree",
*ghstack.gpg_sign.gpg_args_if_necessary(sh),
s.tree(),
s.tree,
"-p",
head,
input=commit_msg,
Expand All @@ -123,7 +123,7 @@ def main(

git reset --soft {}
""".format(
s.commit_id()
s.commit_id
)
)

Expand Down