-
Notifications
You must be signed in to change notification settings - Fork 12
/
git-open-pr.sh
38 lines (36 loc) · 1.13 KB
/
git-open-pr.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh
_get_repo() {
echo "$1" | sed -e "s/.git$//" -e "s/.*github.com[:/]\(.*\)/\1/"
}
_build_url() {
# shellcheck disable=SC2039
local upstream origin branch repo pr_url target
upstream="$(git config --get remote.upstream.url)"
origin="$(git config --get remote.origin.url)"
branch="$(git symbolic-ref --short HEAD)"
repo="$(_get_repo "$origin")"
pr_url="https://github.com/$repo/pull/new"
target="$1"
test -z "$target" && target=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)" | cut -d '/' -f 2)
test -z "$target" && target="master"
if [ -z "$upstream" ]; then
echo "$pr_url/$target...$branch"
else
# shellcheck disable=SC2039
local origin_name upstream_name
origin_name="$(echo "$repo" | cut -f1 -d'/')"
upstream_name="$(_get_repo "$upstream" | cut -f1 -d'/')"
echo "$pr_url/$upstream_name:$target...$origin_name:$branch"
fi
}
# shellcheck disable=SC2039
open-pr() {
# shellcheck disable=SC2039
local url
url="$(_build_url "$*")"
if [ "$(uname -s)" = "Darwin" ]; then
open "$url" 2> /dev/null
else
xdg-open "$url" > /dev/null 2> /dev/null
fi
}