From d4c7581835c13e9bd493e1fa8efffccc03e7c175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20BERSAC?= Date: Tue, 13 Jun 2017 17:01:14 +0200 Subject: [PATCH] Add git-mr to checkout GitLab merge requests --- Commands.md | 25 +++++++ bin/git-mr | 28 ++++++++ etc/git-extras-completion.zsh | 1 + man/git-extras.1 | 3 + man/git-extras.html | 1 + man/git-extras.md | 1 + man/git-mr.1 | 56 +++++++++++++++ man/git-mr.html | 130 ++++++++++++++++++++++++++++++++++ man/git-mr.md | 45 ++++++++++++ man/index.txt | 2 + 10 files changed, 292 insertions(+) create mode 100755 bin/git-mr create mode 100644 man/git-mr.1 create mode 100644 man/git-mr.html create mode 100644 man/git-mr.md diff --git a/Commands.md b/Commands.md index 228df36..95dc9ed 100644 --- a/Commands.md +++ b/Commands.md @@ -37,6 +37,7 @@ - [`git merge-into`](#git-merge-into) - [`git merge-repo`](#git-merge-repo) - [`git missing`](#git-missing) + - [`git mr`](#git-mr) - [`git obliterate`](#git-obliterate) - [`git pr`](#git-pr) - [`git psykorebase`](#git-psykorebase) @@ -1201,6 +1202,30 @@ or reset one file to certain commit $ git reset-file .htaccess dc82b19 ``` +## git mr + +Checks out a merge request from GitLab. Usage: `git mr [REMOTE]`. +Default remote is `origin`. + +``` bash +$ git mr 51 +From gitlab.com:owner/repository + * [new ref] refs/merge-requests/51/head -> mr/51 +Switched to branch 'mr/51' +``` + +With full URL, the head is fetched from a temporary remote pointing to the base URL. + +``` bash +$ git mr https://gitlab.com/owner/repository/merge_requests/51 +From gitlab.com:owner/repository + * [new ref] refs/merge-requests/51/head -> mr/51 +Switched to branch 'mr/51' +``` + +Just like [git pr](#git-pr), `git mr` accepts a `clean` argument to trash all +`mr/` branches. Ensure current branch is not one. + ## git pr Checks out a pull request from GitHub diff --git a/bin/git-mr b/bin/git-mr new file mode 100755 index 0000000..34d0fa4 --- /dev/null +++ b/bin/git-mr @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +set -eu + +if [ -z "${1-}" ] ; then + echo "mr number or URL required. See --help for usage." 1>&2 + exit 1 +fi + +if test "$1" = "clean"; then + git for-each-ref refs/heads/mr/* --format='%(refname)' | while read ref; do + git branch -D ${ref#refs/heads/} + done + exit 0 +elif [[ $1 =~ ^(https?://[^/]+/(.+))/merge_requests/([0-9]+).*$ ]]; then + remote=${BASH_REMATCH[1]}.git + id=${BASH_REMATCH[3]} +else + id=$1 + remote=${2:-origin} +fi + +branch=mr/$id +remote_ref=refs/merge-requests/$id/head +git fetch -fu $remote $remote_ref:$branch +git checkout $branch +git config --local --replace branch.$branch.merge $remote_ref +git config --local --replace branch.$branch.remote $remote diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index 2fc84c8..8701ac6 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -404,6 +404,7 @@ zstyle ':completion:*:*:git:*' user-commands \ merge-into:'merge one branch into another' \ merge-repo:'merge two repo histories' \ missing:'show commits missing from another branch' \ + mr:'checks out a merge request locally' \ obliterate:'rewrite past commits to remove some files' \ pr:'checks out a pull request locally' \ psykorebase:'rebase a branch with a merge commit' \ diff --git a/man/git-extras.1 b/man/git-extras.1 index 1c72076..03195d8 100644 --- a/man/git-extras.1 +++ b/man/git-extras.1 @@ -132,6 +132,9 @@ Self update\. \fBgit\-missing(1)\fR Show commits missing from another branch . .IP "\(bu" 4 +\fB# git\-mr(1)\fR Checks out a merge request locally +. +.IP "\(bu" 4 \fBgit\-pr(1)\fR Checks out a pull request locally . .IP "\(bu" 4 diff --git a/man/git-extras.html b/man/git-extras.html index 05e46e4..2bce5e4 100644 --- a/man/git-extras.html +++ b/man/git-extras.html @@ -128,6 +128,7 @@
  • git-merge-into(1) Merge one branch into another
  • git-merge-repo(1) Merge two repo histories
  • git-missing(1) Show commits missing from another branch
  • +
  • git-mr(1) Checks out a merge request locally
  • git-pr(1) Checks out a pull request locally
  • git-psykorebase(1) Rebase a branch with a merge commit
  • git-reauthor(1) Rewrite history to change author's identity
  • diff --git a/man/git-extras.md b/man/git-extras.md index 9ff7221..76ad030 100644 --- a/man/git-extras.md +++ b/man/git-extras.md @@ -55,6 +55,7 @@ git-extras(1) -- Awesome GIT utilities - **git-merge-into(1)** Merge one branch into another - **git-merge-repo(1)** Merge two repo histories - **git-missing(1)** Show commits missing from another branch + - **git-mr(1)** Checks out a merge request locally - **git-pr(1)** Checks out a pull request locally - **git-psykorebase(1)** Rebase a branch with a merge commit - **git-reauthor(1)** Rewrite history to change author's identity diff --git a/man/git-mr.1 b/man/git-mr.1 new file mode 100644 index 0000000..6808476 --- /dev/null +++ b/man/git-mr.1 @@ -0,0 +1,56 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-MR" "1" "June 2017" "" "" +. +.SH "NAME" +\fBgit\-mr\fR \- Checks out a merge request locally +. +.SH "SYNOPSIS" +\fBgit\-mr\fR [] +. +.br +\fBgit\-mr\fR +. +.br +\fBgit\-mr clean\fR +. +.SH "DESCRIPTION" +Fetches merge request head by its number or URl and check it out in a branch named with merge request number\. +. +.SH "OPTIONS" + +. +.P +The name of the remote to fetch from\. Defaults to \fBorigin\fR\. +. +.P + +. +.P +GitLab merge request URL in the format \fBhttps://gitlab\.tld/owner/repository/merge_requests/453\fR\. +. +.SH "EXAMPLES" +This checks out merge request \fB!51\fR from remote \fBorigin\fR to branch \fBmr/51\fR\. +. +.IP "" 4 +. +.nf + +$ git mr 51 +From gitlab\.com:owner/repository + * [new ref] refs/merge\-requests/51/head \-> mr/51 +Switched to branch \'mr/51\' +. +.fi +. +.IP "" 0 +. +.SH "AUTHOR" +Written by Étienne BERSAC \fIbersace03@gmail\.com\fR from git\-pr(1)\. +. +.SH "REPORTING BUGS" +<\fIhttps://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-mr.html b/man/git-mr.html new file mode 100644 index 0000000..83545cf --- /dev/null +++ b/man/git-mr.html @@ -0,0 +1,130 @@ + + + + + + git-mr(1) - Checks out a merge request locally + + + + +
    + + + +
      +
    1. git-mr(1)
    2. +
    3. +
    4. git-mr(1)
    5. +
    + +

    NAME

    +

    + git-mr - Checks out a merge request locally +

    + +

    SYNOPSIS

    + +

    git-mr <number> [<remote>]
    +git-mr <url>
    +git-mr clean

    + +

    DESCRIPTION

    + +

    Fetches merge request head by its number or URl and check it out in a branch + named with merge request number.

    + +

    OPTIONS

    + +

    <remote>

    + +

    The name of the remote to fetch from. Defaults to origin.

    + +

    <url>

    + +

    GitLab merge request URL in the format + https://gitlab.tld/owner/repository/merge_requests/453.

    + +

    EXAMPLES

    + +

    This checks out merge request !51 from remote origin to branch mr/51.

    + +
    $ git mr 51
    +From gitlab.com:owner/repository
    + * [new ref]         refs/merge-requests/51/head -> mr/51
    +Switched to branch 'mr/51'
    +
    + +

    AUTHOR

    + +

    Written by Étienne BERSAC bersace03@gmail.com from git-pr(1).

    + +

    REPORTING BUGS

    + +

    <https://github.com/tj/git-extras/issues>

    + +

    SEE ALSO

    + +

    <https://github.com/tj/git-extras>

    + + +
      +
    1. +
    2. June 2017
    3. +
    4. git-mr(1)
    5. +
    + +
    + + diff --git a/man/git-mr.md b/man/git-mr.md new file mode 100644 index 0000000..d908a60 --- /dev/null +++ b/man/git-mr.md @@ -0,0 +1,45 @@ +# git-mr(1) -- Checks out a merge request locally + +## SYNOPSIS + +`git-mr` <number> [<remote>]
    +`git-mr` <url>
    +`git-mr clean` + +## DESCRIPTION + + Fetches merge request head by its number or URl and check it out in a branch + named with merge request number. + +## OPTIONS + + <remote> + + The name of the remote to fetch from. Defaults to `origin`. + + <url> + + GitLab merge request URL in the format + `https://gitlab.tld/owner/repository/merge_requests/453`. + + +## EXAMPLES + +This checks out merge request `!51` from remote `origin` to branch `mr/51`. + + $ git mr 51 + From gitlab.com:owner/repository + * [new ref] refs/merge-requests/51/head -> mr/51 + Switched to branch 'mr/51' + +## AUTHOR + +Written by Étienne BERSAC from git-pr(1). + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> diff --git a/man/index.txt b/man/index.txt index 06b0363..3a2d9fb 100644 --- a/man/index.txt +++ b/man/index.txt @@ -34,6 +34,8 @@ git-locked(1) git-locked git-merge-into(1) git-merge-into git-merge-repo(1) git-merge-repo git-missing(1) git-missing +git-mr(1) git-mr +git-obliterate(1) git-obliterate git-pr(1) git-pr git-psykorebase(1) git-psykorebase git-reauthor(1) git-reauthor