|
| 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