From 660c799e12c433d10d3816c275bd793c9f3227fc Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Tue, 5 Dec 2023 16:57:07 +0800 Subject: [PATCH] Make CommitHeader use cached_property Signed-off-by: Edward Z. Yang [ghstack-poisoned] --- ghstack/git.py | 27 ++++++++++++++++++--------- ghstack/submit.py | 4 ++-- ghstack/unlink.py | 10 +++++----- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ghstack/git.py b/ghstack/git.py index 08791c8..f0f0bf4 100644 --- a/ghstack/git.py +++ b/ghstack/git.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import re +from functools import cached_property from typing import List, Pattern import ghstack.diff @@ -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) @@ -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))) diff --git a/ghstack/submit.py b/ghstack/submit.py index 3421d73..d56d8f3 100644 --- a/ghstack/submit.py +++ b/ghstack/submit.py @@ -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, @@ -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( diff --git a/ghstack/unlink.py b/ghstack/unlink.py index 2195566..611f2e6 100644 --- a/ghstack/unlink.py +++ b/ghstack/unlink.py @@ -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( @@ -82,7 +82,7 @@ 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 @@ -90,7 +90,7 @@ def main( 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( @@ -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, @@ -123,7 +123,7 @@ def main( git reset --soft {} """.format( - s.commit_id() + s.commit_id ) )