Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
[![Issue Count](https://codeclimate.com/github/unixorn/git-extra-commands/badges/issue_count.svg)](https://codeclimate.com/github/unixorn/git-extra-commands)
[![GitHub stars](https://img.shields.io/github/stars/unixorn/git-extra-commands.svg)](https://github.com/unixorn/git-extra-commands/stargazers)

**git-extra-commands** is a ZSH plugin that packages some extra git helper scripts I've found. This collection (and the scripts that I wrote in it) is licensed with the Apache Version 2 license.

Some of the scripts in this collection came from other sources and may have different licensing - if they do, their license information is included inline in the script source.

This collection doesn't actually require ZSH, but packaging it as a ZSH plugin makes it more convenient for people using a ZSH framework to use this collection.

If you wrote one of these scripts and want it removed from this collection, please either make a PR and/or file an issue against the repo and I will remove it.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**
## Table of Contents

- [Installing](#installing)
- [Pre-requisites](#pre-requisites)
Expand All @@ -36,6 +28,14 @@ If you wrote one of these scripts and want it removed from this collection, plea

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

**git-extra-commands** is a ZSH plugin that packages some extra `git` helper scripts I've found. This collection (and the scripts that I wrote in it) is licensed with the Apache Version 2 license.

However, some of the scripts in this collection came from other sources and may have different licensing - if they do, their license information is included inline in the script source.

This collection doesn't actually require ZSH, but packaging it as a ZSH plugin makes it more convenient for people using a ZSH framework to use this collection.

If you wrote one of these scripts and want it removed from this collection, please either make a PR and/or file an issue against the repo and I will remove it.

## Installing

### Pre-requisites
Expand Down Expand Up @@ -117,6 +117,7 @@ If you aren't using any ZSH frameworks, or if you're a `bash` user, do the follo
| `git-forest` | Jan Engelhardt | Prints a text-based tree visualisation of your repository. Requires [Git.pm](https://metacpan.org/release/Git) |
| `git-git` | Joe Block <jpb@unixorn.net> | Typing `git git foo` will make git do a `git foo` instead of complaining. |
| `git-github-open` | ? | Open GitHub file/blob page for FILE on LINE. |
| `git-gitlab-mr` | Noel Cower's [gist](https://gist.github.com/nilium/ac808ee3729cdce01ec0f3c0a499f099) | Open a merge request on GitLab |
| `git-ignored` | ? | Show files being ignored by git in the repo. |
| `git-improved-merge` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Sophisticated git merge with integrated CI check and automatic cleanup. |
| `git-incoming-commits` | Ryan Tomayko's [dotfiles](https://github.com/rtomayko/dotfiles) | Adds a remote for the current repository for the given github username. |
Expand Down Expand Up @@ -170,6 +171,7 @@ If you aren't using any ZSH frameworks, or if you're a `bash` user, do the follo
| `git-where` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Shows where a particular commit falls between releases. |
| `git-whoami` | Peter Eisentraut | Shows what username & email you have configured for the repo you're in |
| `git-winner` | Garry Dolley [https://github.com/up_the_irons/git-winner](https://github.com/up_the_irons/git-winner) | Shows what authors have made the most commits, both by number of commits and by number of lines changed. |
| `git-wordiness` | Noel Cower | Shows how wordy people's commit messages are. Useful for shaming the folks who commit atrocities like `git commit -m fixup` |
| `git-wtf` | William Morgan <wmorgan-git-wt-add@masanjin.net> | `git-wtf` displays the state of your repository in a readable, easy-to-scan format. It's useful for getting a summary of how a branch relates to a remote server, and for wrangling many topic branches. |
| `github-open` | Ryan Tomayko's [dotfiles](http://github.com/rtomayko/dotfiles) | |

Expand Down
98 changes: 98 additions & 0 deletions bin/git-gitlab-mr
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
# Copyright 2018 Noel Cower
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# git-mr: Simple script to open the URL for a new merge request for a GitLab
# repository.
#
# Note: Has no way of knowing something isn't a GitLab repository.

set -e

havecmd() {
[ -n "$(command -v "$@" 2>/dev/null)" ]
}

err() {
echo "ERR $*" 1>&2
exit 1
}

uriencode() {
jq -Rrns --arg in "$*" \
'$in|split("\n")|if last == "" then .[:-1] else . end|join(" ")|@uri'
}

if ! havecmd jq; then
err 'Required program missing: jq'
fi

ref="$(git rev-parse --abbrev-ref --symbolic-full-name @{u})"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This { is literal. Check expression (missing ;/\n?) or quote it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This } is literal. Check expression (missing ;/\n?) or quote it.

if [ -z "$ref" ]; then
err "No upstream for branch: $(git rev-parse --abbrev-ref --symbolic-full-name @)"
fi
repo="${ref%%/*}"
if [ -z "$repo" ]; then
err "Unable to determine repository for ref: $ref"
fi
branch="${ref#$repo/}"
if [ -z "$branch" ]; then
err "Unable to determine ref in repository of ref: $ref"
fi
uri="$(git remote get-url "$repo")"
if [ -z "$uri" ]; then
err "Unable to determine URI of repository: $repo"
fi

scheme=https
suffix="/merge_requests/new?$(uriencode 'merge_request[source_branch]')=$(uriencode "${branch}")"
case "$uri" in
*@*:*) # SSH
uri="${uri#*@}"
domain="${uri%:*}"
path="/${uri#${domain}:}"
;;
https://*) # HTTPS
uri="${uri#https://}"
uri="${uri#*@}"
domain="${uri%%/*}"
path="${uri#${domain}}"
;;
http://*) # HTTP?
scheme=http # Why? Why do this?
uri="${uri#https://}"
uri="${uri#*@}"
domain="${uri%%/*}"
path="${uri#${domain}}"
;;
*)
err "Unrecognized URI: $uri"
;;
esac

: "${mrurl:=${scheme}://${domain}${path%.git}${suffix}}"
if havecmd open; then
open "$mrurl"
elif havecmd xdg-open; then
xdg-open "$mrurl" >/dev/null 2>&1
else
# Print URL if we can't open it
echo "$mrurl"
fi