From 3330f1f3d07928c07a2439bf131d652f28d203bc Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Mon, 21 Oct 2024 16:07:54 +0100 Subject: [PATCH 1/5] init --- README.md | 3 ++- ghstack/github_utils.py | 18 ++++++++++++++++-- ghstack/land.py | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 46b2098..6e182be 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,8 @@ branch, and start working on a fresh branch. **WARNING.** You will NOT be able to merge these commits using the normal GitHub UI, as their branch bases won't be master. Use -`ghstack land $PR_URL` to land a ghstack'ed pull request. +`ghstack land $PR_URL` (or alternatively `ghtstack land #PR_NUM`) to land +a ghstack'ed pull request. ## Structure of submitted pull requests diff --git a/ghstack/github_utils.py b/ghstack/github_utils.py index f8000ac..fd784ad 100644 --- a/ghstack/github_utils.py +++ b/ghstack/github_utils.py @@ -27,7 +27,7 @@ def get_github_repo_name_with_owner( # Grovel in remotes to figure it out remote_url = sh.git("remote", "get-url", remote_name) while True: - match = r"^git@{github_url}:/?([^/]+)/(.+?)(?:\.git)?$".format( + match = r"^git@{github_url}:([^/]+)/(.+?)(?:\.git)?$".format( github_url=github_url ) m = re.match(match, remote_url) @@ -115,9 +115,23 @@ def get_github_repo_info( ) -def parse_pull_request(pull_request: str) -> GitHubPullRequestParams: +def parse_pull_request( + pull_request: str, + *, + sh: Optional[ghstack.shell.Shell] = None, + remote_name: Optional[str] = None, +) -> GitHubPullRequestParams: m = RE_PR_URL.match(pull_request) if not m: + # We can reconstruct the URL if just a PR number is passed + if sh is not None and remote_name is not None: + remote_url = sh.git("remote", "get-url", remote_name) + # Do not pass the shell to avoid infinite loop + try: + return parse_pull_request(remote_url + "/pull/" + pull_request) + except RuntimeError: + # Fall back on original error message + pass raise RuntimeError("Did not understand PR argument. PR must be URL") github_url = m.group("github_url") diff --git a/ghstack/land.py b/ghstack/land.py index 56ee6a0..eb8ca39 100644 --- a/ghstack/land.py +++ b/ghstack/land.py @@ -57,7 +57,7 @@ def main( # Furthermore, the parent commits of PR are ignored: we always # take the canonical version of the patch from any given pr - params = ghstack.github_utils.parse_pull_request(pull_request) + params = ghstack.github_utils.parse_pull_request(pull_request, sh=sh, remote_name=remote_name) default_branch = ghstack.github_utils.get_github_repo_info( github=github, sh=sh, From 197ac2d61516eedef9daa30f303161e4ad909ee2 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Tue, 22 Oct 2024 07:47:07 +0100 Subject: [PATCH 2/5] lint --- ghstack/github_utils.py | 8 ++++---- ghstack/land.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ghstack/github_utils.py b/ghstack/github_utils.py index fd784ad..09c0a96 100644 --- a/ghstack/github_utils.py +++ b/ghstack/github_utils.py @@ -116,10 +116,10 @@ def get_github_repo_info( def parse_pull_request( - pull_request: str, - *, - sh: Optional[ghstack.shell.Shell] = None, - remote_name: Optional[str] = None, + pull_request: str, + *, + sh: Optional[ghstack.shell.Shell] = None, + remote_name: Optional[str] = None, ) -> GitHubPullRequestParams: m = RE_PR_URL.match(pull_request) if not m: diff --git a/ghstack/land.py b/ghstack/land.py index eb8ca39..73b61ae 100644 --- a/ghstack/land.py +++ b/ghstack/land.py @@ -57,7 +57,9 @@ def main( # Furthermore, the parent commits of PR are ignored: we always # take the canonical version of the patch from any given pr - params = ghstack.github_utils.parse_pull_request(pull_request, sh=sh, remote_name=remote_name) + params = ghstack.github_utils.parse_pull_request( + pull_request, sh=sh, remote_name=remote_name + ) default_branch = ghstack.github_utils.get_github_repo_info( github=github, sh=sh, From f7494f538c8d7cdf0a6127ec6e6d5fff9c58405a Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Tue, 22 Oct 2024 09:40:43 +0100 Subject: [PATCH 3/5] checkout PR number --- ghstack/checkout.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ghstack/checkout.py b/ghstack/checkout.py index 4e10366..4767297 100644 --- a/ghstack/checkout.py +++ b/ghstack/checkout.py @@ -15,7 +15,9 @@ def main( remote_name: str, ) -> None: - params = ghstack.github_utils.parse_pull_request(pull_request) + params = ghstack.github_utils.parse_pull_request( + pull_request, sh=sh, remote_name=remote_name + ) head_ref = github.get_head_ref(**params) orig_ref = re.sub(r"/head$", "/orig", head_ref) if orig_ref == head_ref: From f6edd44039571f1eed4aa4b42829239630aeed5c Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Wed, 23 Oct 2024 15:35:39 -0700 Subject: [PATCH 4/5] edit tests --- test/github_utils/get_repo_name_with_owner.py.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/github_utils/get_repo_name_with_owner.py.test b/test/github_utils/get_repo_name_with_owner.py.test index 9f5e3ef..8cf4e48 100644 --- a/test/github_utils/get_repo_name_with_owner.py.test +++ b/test/github_utils/get_repo_name_with_owner.py.test @@ -63,7 +63,7 @@ assert_eq( ), {"owner": "ezyang", "name": "ghstack.dotted"}, ) -git("remote", "add", "with-leading-slash", "git@github.com:/ezyang/ghstack.git") +git("remote", "add", "with-leading-slash", "git@github.com/ezyang/ghstack.git") assert_eq( ghstack.github_utils.get_github_repo_name_with_owner( sh=sh, github_url="github.com", remote_name="with-leading-slash" From b34f0f10ad75f3109d9d4c2c2514f40c3799bbf5 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 24 Oct 2024 07:55:52 -0700 Subject: [PATCH 5/5] revert --- ghstack/github_utils.py | 2 +- test/github_utils/get_repo_name_with_owner.py.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ghstack/github_utils.py b/ghstack/github_utils.py index 09c0a96..ecb4b70 100644 --- a/ghstack/github_utils.py +++ b/ghstack/github_utils.py @@ -27,7 +27,7 @@ def get_github_repo_name_with_owner( # Grovel in remotes to figure it out remote_url = sh.git("remote", "get-url", remote_name) while True: - match = r"^git@{github_url}:([^/]+)/(.+?)(?:\.git)?$".format( + match = r"^git@{github_url}:/?([^/]+)/(.+?)(?:\.git)?$".format( github_url=github_url ) m = re.match(match, remote_url) diff --git a/test/github_utils/get_repo_name_with_owner.py.test b/test/github_utils/get_repo_name_with_owner.py.test index 8cf4e48..9f5e3ef 100644 --- a/test/github_utils/get_repo_name_with_owner.py.test +++ b/test/github_utils/get_repo_name_with_owner.py.test @@ -63,7 +63,7 @@ assert_eq( ), {"owner": "ezyang", "name": "ghstack.dotted"}, ) -git("remote", "add", "with-leading-slash", "git@github.com/ezyang/ghstack.git") +git("remote", "add", "with-leading-slash", "git@github.com:/ezyang/ghstack.git") assert_eq( ghstack.github_utils.get_github_repo_name_with_owner( sh=sh, github_url="github.com", remote_name="with-leading-slash"