Skip to content

Commit

Permalink
git-pr: add -m|--merge option to check out a merge commit
Browse files Browse the repository at this point in the history
Close tj#757.
  • Loading branch information
spacewander committed Jun 17, 2019
1 parent 1712db2 commit eae05bb
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
25 changes: 23 additions & 2 deletions bin/git-pr
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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
Expand Down
43 changes: 34 additions & 9 deletions man/git-pr.1
Original file line number Diff line number Diff line change
@@ -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\.
Expand All @@ -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:
.
Expand Down Expand Up @@ -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
Expand All @@ -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
.
Expand Down
31 changes: 26 additions & 5 deletions man/git-pr.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions man/git-pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit eae05bb

Please sign in to comment.