git-pr: add -m|--merge option to check out a merge commit

Close #757.
This commit is contained in:
spacewander 2019-06-17 11:13:12 +08:00
parent 1712db2146
commit eae05bbb98
4 changed files with 106 additions and 19 deletions

View file

@ -1,13 +1,20 @@
#!/usr/bin/env bash
# Based on https://gist.github.com/gnarf/5406589 and https://gist.github.com/jhnns/d654d9d6da6d3b749986
TO_MERGE=
pull() {
local remote="$1"
local id="$2"
local branch="$3"
git fetch -fu $remote refs/pull/$id/head:$branch && \
local ref="refs/pull/$id/head"
if [ -n "$TO_MERGE" ]; then
ref="refs/pull/$id/merge"
fi
git fetch -fu $remote $ref:$branch && \
git checkout $branch && \
git config --local --replace branch.$branch.merge refs/pull/$id/head && \
git config --local --replace branch.$branch.merge $ref && \
git config --local --replace branch.$branch.remote $remote;
}
@ -30,6 +37,20 @@ pull_pr_if_matched() {
return 1
}
for arg in "$@"; do
case "$arg" in
-m|--merge)
TO_MERGE=1
;;
*)
# set the argument back
set -- "$@" "$arg"
;;
esac
shift
done
test -z "$1" && echo "pr number required." 1>&2 && exit 1
if test "$1" = "clean"; then

View file

@ -1,22 +1,16 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-PR" "1" "February 2019" "" "Git Extras"
.TH "GIT\-PR" "1" "June 2019" "" "Git Extras"
.
.SH "NAME"
\fBgit\-pr\fR \- Checks out a pull request locally
.
.SH "SYNOPSIS"
\fBgit\-pr\fR <number> [<remote>]
\fBgit\-pr\fR[\-m|\-\-merge] <number> [<remote>]
.
.br
\fBgit\-pr\fR <[remote]:number>\.\.\.
.
.br
\fBgit\-pr\fR <url>\.\.\.
.
.br
\fBgit\-pr clean\fR
\fBgit\-pr [\-m|\-\-merge] &lt;[remote]:number&gt;\.\.\.<br>\fRgit\-pr\fB[\-m|\-\-merge] &lt;url&gt;\.\.\.<br>\fRgit\-pr clean`
.
.SH "DESCRIPTION"
Creates a local branch based on a GitHub pull request number or URL, and switch to that branch afterwards\.
@ -33,6 +27,12 @@ The name of the remote to fetch from\. Defaults to \fBorigin\fR\.
.P
GitHub pull request URL in the format \fBhttps://github\.com/tj/git\-extras/pull/453\fR\.
.
.P
\-m | \-\-merge
.
.P
Checkout a merge commit against the branch the pull request is targeting\.
.
.SH "EXAMPLES"
This checks out the pull request \fB226\fR from \fBorigin\fR:
.
@ -130,6 +130,30 @@ $ 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\.
.
.P
With \fB\-\-merge\fR option, you could check out a merge commit:
.
.IP "" 4
.
.nf
$ git pr origin:755 \-\-merge
remote: Enumerating objects: 3, done\.
remote: Counting objects: 100% (3/3), done\.
remote: Compressing objects: 100% (3/3), done\.
remote: Total 3 (delta 0), reused 0 (delta 0), pack\-reused 0
Unpacking objects: 100% (3/3), done\.
From github\.com:tj/git\-extras
bf7dd69\.\.de6e51c refs/pull/755/merge \-> pr/755
Switched to branch \'pr/775\'
$ git log pr/775 \-\-oneline
de6e51c (pr/755) Merge bf7dd6\.\.\.
.
.fi
.
.IP "" 0
.
.P
To clean up old branches:
.
.IP "" 4
@ -140,6 +164,7 @@ $ git pr clean
Deleted branch pr/226 (was b96a8c2)\.
Deleted branch pr/220 (was d34dc0f)\.
Deleted branch pr/775 (was de6e51c)\.
.
.fi
.

View file

@ -76,10 +76,10 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-pr</code> &lt;number&gt; [&lt;remote&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>
<p><code>git-pr</code>[-m|--merge] &lt;number&gt; [&lt;remote&gt;]<br />
<code>git-pr [-m|--merge] &amp;lt;[remote]:number&amp;gt;...&lt;br&gt;
</code>git-pr<code>[-m|--merge] &amp;lt;url&amp;gt;...&lt;br&gt;
</code>git-pr clean`</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
@ -96,6 +96,10 @@
<p> GitHub pull request URL in the format <code>https://github.com/tj/git-extras/pull/453</code>.</p>
<p> -m | --merge</p>
<p> Checkout a merge commit against the branch the pull request is targeting.</p>
<h2 id="EXAMPLES">EXAMPLES</h2>
<p>This checks out the pull request <code>226</code> from <code>origin</code>:</p>
@ -146,12 +150,29 @@ $ git pr PR-B
<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>With <code>--merge</code> option, you could check out a merge commit:</p>
<pre><code>$ git pr origin:755 --merge
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:tj/git-extras
bf7dd69..de6e51c refs/pull/755/merge -&gt; pr/755
Switched to branch 'pr/775'
$ git log pr/775 --oneline
de6e51c (pr/755) Merge bf7dd6...
</code></pre>
<p>To clean up old branches:</p>
<pre><code>$ git pr clean
Deleted branch pr/226 (was b96a8c2).
Deleted branch pr/220 (was d34dc0f).
Deleted branch pr/775 (was de6e51c).
</code></pre>
<h2 id="AUTHOR">AUTHOR</h2>
@ -169,7 +190,7 @@ Deleted branch pr/220 (was d34dc0f).
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>February 2019</li>
<li class='tc'>June 2019</li>
<li class='tr'>git-pr(1)</li>
</ol>

View file

@ -3,9 +3,9 @@ git-pr(1) -- Checks out a pull request locally
## SYNOPSIS
`git-pr` &lt;number&gt; [&lt;remote&gt;]<br>
`git-pr` &lt;[remote]:number&gt;...<br>
`git-pr` &lt;url&gt;...<br>
`git-pr`[-m|--merge] &lt;number&gt; [&lt;remote&gt;]<br>
`git-pr [-m|--merge] &lt;[remote]:number&gt;...<br>
`git-pr`[-m|--merge] &lt;url&gt;...<br>
`git-pr clean`
## DESCRIPTION
@ -23,6 +23,10 @@ git-pr(1) -- Checks out a pull request locally
GitHub pull request URL in the format `https://github.com/tj/git-extras/pull/453`.
-m | --merge
Checkout a merge commit against the branch the pull request is targeting.
## EXAMPLES
This checks out the pull request `226` from `origin`:
@ -67,12 +71,28 @@ Note that `git pr PR-A PR-B` is equal to:
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.
With `--merge` option, you could check out a merge commit:
$ git pr origin:755 --merge
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:tj/git-extras
bf7dd69..de6e51c refs/pull/755/merge -> pr/755
Switched to branch 'pr/775'
$ git log pr/775 --oneline
de6e51c (pr/755) Merge bf7dd6...
To clean up old branches:
$ git pr clean
Deleted branch pr/226 (was b96a8c2).
Deleted branch pr/220 (was d34dc0f).
Deleted branch pr/775 (was de6e51c).
## AUTHOR