From 87b7c959b498ea840c9fcb76a1cf216dc7a47018 Mon Sep 17 00:00:00 2001 From: spacewander Date: Sat, 2 Feb 2019 11:45:27 +0800 Subject: [PATCH 1/2] git-pr: added <[remote]:pr number> option. --- bin/git-pr | 30 +++++++++++++++++++++++------- man/git-pr.1 | 18 +++++++++++++++++- man/git-pr.html | 8 +++++++- man/git-pr.md | 5 +++++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/bin/git-pr b/bin/git-pr index 5f68638..f09bb3a 100755 --- a/bin/git-pr +++ b/bin/git-pr @@ -1,22 +1,38 @@ #!/usr/bin/env bash # Based on https://gist.github.com/gnarf/5406589 and https://gist.github.com/jhnns/d654d9d6da6d3b749986 +pull() { + local remote="$1" + local id="$2" + local branch="$3" + git fetch -fu $remote refs/pull/$id/head:$branch && \ + git checkout $branch && \ + git config --local --replace branch.$branch.merge refs/pull/$id/head && \ + git config --local --replace branch.$branch.remote $remote; +} + if test "$1" = "clean"; then git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do git branch -D ${ref#refs/heads/} done exit 0 -elif [[ $1 =~ ^(https?://[^/]+/(.+))/pull/([0-9]+).*$ ]]; then - remote=${BASH_REMATCH[1]}.git - id=${BASH_REMATCH[3]} + +elif [[ $1 =~ ^(.*):([0-9]+)|(https?://[^/]+/.+)/pull/([0-9]+).*$ ]]; then + if [[ -n ${BASH_REMATCH[2]} ]]; then + remote="${BASH_REMATCH[1]:-origin}" + id="${BASH_REMATCH[2]}" + else + remote="${BASH_REMATCH[3]}.git" + id="${BASH_REMATCH[4]}" + fi + branch=pr/$id + pull "$remote" "$id" "$branch" + else test -z $1 && echo "pr number required." 1>&2 && exit 1 remote=${2:-origin} id=$1 branch=pr/$id + pull "$remote" "$id" "$branch" fi -git fetch -fu $remote refs/pull/$id/head:$branch && \ -git checkout $branch && \ -git config --local --replace branch.$branch.merge refs/pull/$id/head && \ -git config --local --replace branch.$branch.remote $remote; diff --git a/man/git-pr.1 b/man/git-pr.1 index 34f10fe..2ed3a90 100644 --- a/man/git-pr.1 +++ b/man/git-pr.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "GIT\-PR" "1" "October 2017" "" "Git Extras" +.TH "GIT\-PR" "1" "February 2019" "" "Git Extras" . .SH "NAME" \fBgit\-pr\fR \- Checks out a pull request locally @@ -10,6 +10,9 @@ \fBgit\-pr\fR [] . .br +\fBgit\-pr\fR <[remote]:number> +. +.br \fBgit\-pr\fR . .br @@ -66,6 +69,19 @@ $ git pr 226 upstream .IP "" 0 . .P +This does the same thing as the command above: +. +.IP "" 4 +. +.nf + +$ git pr upstream:226 +. +.fi +. +.IP "" 0 +. +.P You can also checkout a pull request based on a GitHub URL: . .IP "" 4 diff --git a/man/git-pr.html b/man/git-pr.html index a7d6301..be1dedd 100644 --- a/man/git-pr.html +++ b/man/git-pr.html @@ -77,6 +77,7 @@

SYNOPSIS

git-pr <number> [<remote>]
+git-pr <[remote]:number>
git-pr <url>
git-pr clean

@@ -116,6 +117,11 @@ Switched to branch 'pr/226'
$ git pr 226 upstream
 
+

This does the same thing as the command above:

+ +
$ git pr upstream:226
+
+

You can also checkout a pull request based on a GitHub URL:

$ git pr https://github.com/tj/git-extras/pull/453
@@ -148,7 +154,7 @@ Deleted branch pr/220 (was d34dc0f).
 
   
  1. -
  2. October 2017
  3. +
  4. February 2019
  5. git-pr(1)
diff --git a/man/git-pr.md b/man/git-pr.md index 93c6fd0..be2e823 100644 --- a/man/git-pr.md +++ b/man/git-pr.md @@ -4,6 +4,7 @@ git-pr(1) -- Checks out a pull request locally ## SYNOPSIS `git-pr` <number> [<remote>]
+`git-pr` <[remote]:number>
`git-pr` <url>
`git-pr clean` @@ -41,6 +42,10 @@ This pulls from a different remote: $ git pr 226 upstream +This does the same thing as the command above: + + $ git pr upstream:226 + You can also checkout a pull request based on a GitHub URL: $ git pr https://github.com/tj/git-extras/pull/453 From f29dad042cc3c057b6820b910f36d63d28606558 Mon Sep 17 00:00:00 2001 From: spacewander Date: Sat, 2 Feb 2019 12:29:41 +0800 Subject: [PATCH 2/2] git-pr: accpeted multiple GitHub URL or ID with remote. --- bin/git-pr | 47 ++++++++++++++++++++++++++++++++--------------- man/git-pr.1 | 35 +++++++++++++++++++++++++++++++++-- man/git-pr.html | 19 +++++++++++++++++-- man/git-pr.md | 17 +++++++++++++++-- 4 files changed, 97 insertions(+), 21 deletions(-) diff --git a/bin/git-pr b/bin/git-pr index f09bb3a..3d5daad 100755 --- a/bin/git-pr +++ b/bin/git-pr @@ -11,28 +11,45 @@ pull() { git config --local --replace branch.$branch.remote $remote; } +pull_pr_if_matched() { + if [[ $1 =~ ^(.*):([0-9]+)|(https?://[^/]+/.+)/pull/([0-9]+).*$ ]]; then + if [[ -n ${BASH_REMATCH[2]} ]]; then + remote="${BASH_REMATCH[1]:-origin}" + id="${BASH_REMATCH[2]}" + else + remote="${BASH_REMATCH[3]}.git" + id="${BASH_REMATCH[4]}" + fi + + branch=pr/$id + pull "$remote" "$id" "$branch" + return $? + fi + + echo "$1 doesn't match the pr id pattern." + return 1 +} + +test -z "$1" && echo "pr number required." 1>&2 && exit 1 + if test "$1" = "clean"; then git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do git branch -D ${ref#refs/heads/} done - exit 0 -elif [[ $1 =~ ^(.*):([0-9]+)|(https?://[^/]+/.+)/pull/([0-9]+).*$ ]]; then - if [[ -n ${BASH_REMATCH[2]} ]]; then - remote="${BASH_REMATCH[1]:-origin}" - id="${BASH_REMATCH[2]}" - else - remote="${BASH_REMATCH[3]}.git" - id="${BASH_REMATCH[4]}" - fi - - branch=pr/$id - pull "$remote" "$id" "$branch" - -else - test -z $1 && echo "pr number required." 1>&2 && exit 1 +elif [[ "$1" =~ ^[0-9]+$ ]]; then remote=${2:-origin} id=$1 branch=pr/$id pull "$remote" "$id" "$branch" + +else + rc=1 + while [ "$1" != "" ]; do + pull_pr_if_matched "$1" + rc=$? + shift + done + + exit "$rc" fi diff --git a/man/git-pr.1 b/man/git-pr.1 index 2ed3a90..1a5fba9 100644 --- a/man/git-pr.1 +++ b/man/git-pr.1 @@ -10,10 +10,10 @@ \fBgit\-pr\fR [] . .br -\fBgit\-pr\fR <[remote]:number> +\fBgit\-pr\fR <[remote]:number>\.\.\. . .br -\fBgit\-pr\fR +\fBgit\-pr\fR \.\.\. . .br \fBgit\-pr clean\fR @@ -99,6 +99,37 @@ Switched to branch \'pr/453\' .IP "" 0 . .P +You could even pull multiple pull requests via the GitHub URL or the ID with remote: +. +.IP "" 4 +. +.nf + +$ git pr upstream:226 upstream:443 +$ git pr upstream:443 https://github\.com/tj/git\-extras/pull/453 +. +.fi +. +.IP "" 0 +. +.P +Note that \fBgit pr PR\-A PR\-B\fR is equal to: +. +.IP "" 4 +. +.nf + +$ git pr PR\-A +$ git pr PR\-B +. +.fi +. +.IP "" 0 +. +.P +Therefore, if one of the pull request is failed to pull, this command will still go ahead and pull the others\. The final exit code will be decided by the result of the final pulling\. +. +.P To clean up old branches: . .IP "" 4 diff --git a/man/git-pr.html b/man/git-pr.html index be1dedd..38fde1e 100644 --- a/man/git-pr.html +++ b/man/git-pr.html @@ -77,8 +77,8 @@

SYNOPSIS

git-pr <number> [<remote>]
-git-pr <[remote]:number>
-git-pr <url>
+git-pr <[remote]:number>...
+git-pr <url>...
git-pr clean

DESCRIPTION

@@ -131,6 +131,21 @@ From https://github.com/tj/git-extras Switched to branch 'pr/453'
+

You could even pull multiple pull requests via the GitHub URL or the ID with remote:

+ +
$ git pr upstream:226 upstream:443
+$ git pr upstream:443 https://github.com/tj/git-extras/pull/453
+
+ +

Note that git pr PR-A PR-B is equal to:

+ +
$ git pr PR-A
+$ git pr PR-B
+
+ +

Therefore, if one of the pull request is failed to pull, this command will still go ahead and pull +the others. The final exit code will be decided by the result of the final pulling.

+

To clean up old branches:

$ git pr clean
diff --git a/man/git-pr.md b/man/git-pr.md
index be2e823..00adf87 100644
--- a/man/git-pr.md
+++ b/man/git-pr.md
@@ -4,8 +4,8 @@ git-pr(1) -- Checks out a pull request locally
 ## SYNOPSIS
 
 `git-pr` <number> [<remote>]
-`git-pr` <[remote]:number>
-`git-pr` <url>
+`git-pr` <[remote]:number>...
+`git-pr` <url>...
`git-pr clean` ## DESCRIPTION @@ -54,6 +54,19 @@ You can also checkout a pull request based on a GitHub URL: * [new ref] refs/pull/453/head -> pr/453 Switched to branch 'pr/453' +You could even pull multiple pull requests via the GitHub URL or the ID with remote: + + $ git pr upstream:226 upstream:443 + $ git pr upstream:443 https://github.com/tj/git-extras/pull/453 + +Note that `git pr PR-A PR-B` is equal to: + + $ git pr PR-A + $ git pr PR-B + +Therefore, if one of the pull request is failed to pull, this command will still go ahead and pull +the others. The final exit code will be decided by the result of the final pulling. + To clean up old branches: $ git pr clean