Merge pull request #743 from spacewander/allow_multiple_pr

git-pr supports pulling multiple pr at once
This commit is contained in:
罗泽轩 2019-02-08 19:57:39 +07:00 committed by GitHub
commit 99238bdf0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 135 additions and 16 deletions

View file

@ -1,22 +1,55 @@
#!/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;
}
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 =~ ^(https?://[^/]+/(.+))/pull/([0-9]+).*$ ]]; then
remote=${BASH_REMATCH[1]}.git
id=${BASH_REMATCH[3]}
branch=pr/$id
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
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;

View file

@ -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,7 +10,10 @@
\fBgit\-pr\fR <number> [<remote>]
.
.br
\fBgit\-pr\fR <url>
\fBgit\-pr\fR <[remote]:number>\.\.\.
.
.br
\fBgit\-pr\fR <url>\.\.\.
.
.br
\fBgit\-pr clean\fR
@ -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
@ -83,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

View file

@ -77,7 +77,8 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-pr</code> &lt;number&gt; [&lt;remote&gt;]<br />
<code>git-pr</code> &lt;url&gt;<br />
<code>git-pr</code> &lt;[remote]:number&gt;...<br />
<code>git-pr</code> &lt;url&gt;...<br />
<code>git-pr clean</code></p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
@ -116,6 +117,11 @@ Switched to branch 'pr/226'
<pre><code>$ git pr 226 upstream
</code></pre>
<p>This does the same thing as the command above:</p>
<pre><code>$ git pr upstream:226
</code></pre>
<p>You can also checkout a pull request based on a GitHub URL:</p>
<pre><code>$ git pr https://github.com/tj/git-extras/pull/453
@ -125,6 +131,21 @@ From https://github.com/tj/git-extras
Switched to branch 'pr/453'
</code></pre>
<p>You could even pull multiple pull requests via the GitHub URL or the ID with remote:</p>
<pre><code>$ git pr upstream:226 upstream:443
$ git pr upstream:443 https://github.com/tj/git-extras/pull/453
</code></pre>
<p>Note that <code>git pr PR-A PR-B</code> is equal to:</p>
<pre><code>$ git pr PR-A
$ git pr PR-B
</code></pre>
<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>
<p>To clean up old branches:</p>
<pre><code>$ git pr clean
@ -148,7 +169,7 @@ Deleted branch pr/220 (was d34dc0f).
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>October 2017</li>
<li class='tc'>February 2019</li>
<li class='tr'>git-pr(1)</li>
</ol>

View file

@ -4,7 +4,8 @@ git-pr(1) -- Checks out a pull request locally
## SYNOPSIS
`git-pr` &lt;number&gt; [&lt;remote&gt;]<br>
`git-pr` &lt;url&gt;<br>
`git-pr` &lt;[remote]:number&gt;...<br>
`git-pr` &lt;url&gt;...<br>
`git-pr clean`
## DESCRIPTION
@ -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
@ -49,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