Skip to content

Commit a641012

Browse files
committed
Add git-gitlab-mr
Noel posted a link to this in the Hangops slack.
1 parent 8fc1e53 commit a641012

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ If you aren't using any ZSH frameworks, or if you're a `bash` user, do the follo
117117
| `git-forest` | Jan Engelhardt | Prints a text-based tree visualisation of your repository. Requires [Git.pm](https://metacpan.org/release/Git) |
118118
| `git-git` | Joe Block <jpb@unixorn.net> | Typing `git git foo` will make git do a `git foo` instead of complaining. |
119119
| `git-github-open` | ? | Open GitHub file/blob page for FILE on LINE. |
120+
| `git-gitlab-mr` | Noel Cower's [gist](https://gist.github.com/nilium/ac808ee3729cdce01ec0f3c0a499f099) | Open a merge request on GitLab |
120121
| `git-ignored` | ? | Show files being ignored by git in the repo. |
121122
| `git-improved-merge` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Sophisticated git merge with integrated CI check and automatic cleanup. |
122123
| `git-incoming-commits` | Ryan Tomayko's [dotfiles](https://github.com/rtomayko/dotfiles) | Adds a remote for the current repository for the given github username. |

bin/git-gitlab-mr

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2018 Noel Cower
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in all
12+
# copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
# SOFTWARE.
21+
22+
# git-mr: Simple script to open the URL for a new merge request for a GitLab
23+
# repository.
24+
#
25+
# Note: Has no way of knowing something isn't a GitLab repository.
26+
27+
set -e
28+
29+
havecmd() {
30+
[ -n "$(command -v "$@" 2>/dev/null)" ]
31+
}
32+
33+
err() {
34+
echo "ERR $*" 1>&2
35+
exit 1
36+
}
37+
38+
uriencode() {
39+
jq -Rrns --arg in "$*" \
40+
'$in|split("\n")|if last == "" then .[:-1] else . end|join(" ")|@uri'
41+
}
42+
43+
if ! havecmd jq; then
44+
err 'Required program missing: jq'
45+
fi
46+
47+
ref="$(git rev-parse --abbrev-ref --symbolic-full-name @{u})"
48+
if [ -z "$ref" ]; then
49+
err "No upstream for branch: $(git rev-parse --abbrev-ref --symbolic-full-name @)"
50+
fi
51+
repo="${ref%%/*}"
52+
if [ -z "$repo" ]; then
53+
err "Unable to determine repository for ref: $ref"
54+
fi
55+
branch="${ref#$repo/}"
56+
if [ -z "$branch" ]; then
57+
err "Unable to determine ref in repository of ref: $ref"
58+
fi
59+
uri="$(git remote get-url "$repo")"
60+
if [ -z "$uri" ]; then
61+
err "Unable to determine URI of repository: $repo"
62+
fi
63+
64+
scheme=https
65+
suffix="/merge_requests/new?$(uriencode 'merge_request[source_branch]')=$(uriencode "${branch}")"
66+
case "$uri" in
67+
*@*:*) # SSH
68+
uri="${uri#*@}"
69+
domain="${uri%:*}"
70+
path="/${uri#${domain}:}"
71+
;;
72+
https://*) # HTTPS
73+
uri="${uri#https://}"
74+
uri="${uri#*@}"
75+
domain="${uri%%/*}"
76+
path="${uri#${domain}}"
77+
;;
78+
http://*) # HTTP?
79+
scheme=http # Why? Why do this?
80+
uri="${uri#https://}"
81+
uri="${uri#*@}"
82+
domain="${uri%%/*}"
83+
path="${uri#${domain}}"
84+
;;
85+
*)
86+
err "Unrecognized URI: $uri"
87+
;;
88+
esac
89+
90+
: "${mrurl=${scheme}://${domain}${path%.git}${suffix}}"
91+
if havecmd open; then
92+
open "$mrurl"
93+
elif havecmd xdg-open; then
94+
xdg-open "$mrurl" >/dev/null 2>&1
95+
else
96+
# Print URL if we can't open it
97+
echo "$mrurl"
98+
fi

0 commit comments

Comments
 (0)