From eae05bbb987295bb3330e127da9be229fabfac86 Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 17 Jun 2019 11:13:12 +0800 Subject: [PATCH] git-pr: add -m|--merge option to check out a merge commit Close #757. --- bin/git-pr | 25 +++++++++++++++++++++++-- man/git-pr.1 | 43 ++++++++++++++++++++++++++++++++++--------- man/git-pr.html | 31 ++++++++++++++++++++++++++----- man/git-pr.md | 26 +++++++++++++++++++++++--- 4 files changed, 106 insertions(+), 19 deletions(-) diff --git a/bin/git-pr b/bin/git-pr index 3d5daad3b..622a40c86 100755 --- a/bin/git-pr +++ b/bin/git-pr @@ -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 diff --git a/man/git-pr.1 b/man/git-pr.1 index e55b5b67d..4c03d0049 100644 --- a/man/git-pr.1 +++ b/man/git-pr.1 @@ -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 [] +\fBgit\-pr\fR[\-m|\-\-merge] [] . .br -\fBgit\-pr\fR <[remote]:number>\.\.\. -. -.br -\fBgit\-pr\fR \.\.\. -. -.br -\fBgit\-pr clean\fR +\fBgit\-pr [\-m|\-\-merge] <[remote]:number>\.\.\.
\fRgit\-pr\fB[\-m|\-\-merge] <url>\.\.\.
\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 . diff --git a/man/git-pr.html b/man/git-pr.html index 1770c6d83..f4649ef31 100644 --- a/man/git-pr.html +++ b/man/git-pr.html @@ -76,10 +76,10 @@

NAME

SYNOPSIS

-

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

+

git-pr[-m|--merge] <number> [<remote>]
+git-pr [-m|--merge] &lt;[remote]:number&gt;...<br> +git-pr[-m|--merge] &lt;url&gt;...<br> +git-pr clean`

DESCRIPTION

@@ -96,6 +96,10 @@

OPTIONS

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:

@@ -146,12 +150,29 @@

EXAMPLES

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

@@ -169,7 +190,7 @@

SEE ALSO

  1. -
  2. February 2019
  3. +
  4. June 2019
  5. git-pr(1)
diff --git a/man/git-pr.md b/man/git-pr.md index aa66a9f5b..72c97fed9 100644 --- a/man/git-pr.md +++ b/man/git-pr.md @@ -3,9 +3,9 @@ git-pr(1) -- Checks out a pull request locally ## SYNOPSIS -`git-pr` <number> [<remote>]
-`git-pr` <[remote]:number>...
-`git-pr` <url>...
+`git-pr`[-m|--merge] <number> [<remote>]
+`git-pr [-m|--merge] <[remote]:number>...
+`git-pr`[-m|--merge] <url>...
`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