From fb6c589645dcf7859dd28fe818acaca179512453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tina=20M=C3=BCller?= Date: Wed, 10 Aug 2022 23:28:59 +0200 Subject: [PATCH] Add a pr-fetch-fork command This will add the remote url of the PR author to the list of local remotes and fetch the branch of the PR. If the author allows edits from repo owners, then you can then directly push to this branch. --- doc/git-hub.swim | 3 +++ lib/git-hub.d/git-hub-pr | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/doc/git-hub.swim b/doc/git-hub.swim index e64721c..37adfc3 100644 --- a/doc/git-hub.swim +++ b/doc/git-hub.swim @@ -473,6 +473,9 @@ interactions from the command line. They are installed by default. - `pr-fetch [/] ` Fetches a pull request to a local `review/$number` branch +- `pr-fetch-fork [/] ` + Adds the remote url from the PR author and fetches the corresponding branch + - `pr-merge [/] ` Merge and close a pull request. diff --git a/lib/git-hub.d/git-hub-pr b/lib/git-hub.d/git-hub-pr index 7538dd1..756b4cb 100644 --- a/lib/git-hub.d/git-hub-pr +++ b/lib/git-hub.d/git-hub-pr @@ -161,6 +161,33 @@ command:pr-fetch() { msg_ok=0 } +command:pr-fetch-fork() { + local pr_user pr_branch fork_url + get-args '?owner:get-owner/repo:get-repo' number + assert-inside-git-repo + api-get "/repos/$owner/$repo/pulls/$number" + pr_user=$(JSON.get -s /user/login -) + pr_branch=$(JSON.get -s /head/ref -) + fork_url=$(JSON.get -s /head/repo/ssh_url -) + + if [[ -n $(git remote | grep -E "^$pr_user$") ]]; then + echo "Remote $pr_user already exists" + else + git remote add "$pr_user" "$fork_url" || + error "can't add remote $pr_user $fork_url" + fi + + full_branch=$pr_user/$pr_branch + if [[ -n $(git branch | grep -E "^$full_branch$") ]]; then + echo "Branch $full_branch already exists" + else + git fetch "$pr_user" "$pr_branch" || + error "can't fetch PR $full_branch" + fi + say "Fetched PR $number into $full_branch" + msg_ok=0 +} + command:pr-merge() { get-args '?owner:get-owner/repo:get-repo' number '?message' local json_list=()