-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalias
executable file
·69 lines (61 loc) · 2.92 KB
/
alias
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
git config --global alias.open '!f() {
local type="${1:-branch}"
local target="${2:-HEAD}"
local remote="origin"
if [ "$type" = "branch" -o "$type" = "pr" ]; then
# get full name (i.e. refs/heads/*; refs/remotes/*/*); src: https://stackoverflow.com/a/9753364
target="$(git rev-parse --symbolic-full-name "$target")"
if [ "$target" != "${target#"refs/remotes/"}" ]; then
# extract from remote branch reference
target="${target#"refs/remotes/"}"
else
# extract from local branch reference; src: https://stackoverflow.com/a/9753364
target="$(git for-each-ref --format="%(upstream:short)" "$target")"
fi
# split remote/branch
remote="${target%%/*}"
target="${target#"$remote/"}"
if [ -z "$remote" ]; then
echo "Branch ($2) does not point to a remote repository." >&2
return 2
fi
fi
local repo_url="$(git remote get-url "$remote" | sed -E -e "s/(\.(com|org|io))\:/\1\//" -e "s/git@/https:\/\//" -e "s/\.git$//")"
if [ -z "$repo_url" ]; then
echo "Cannot open: no remote repository configured under (origin)" >&2
return 1
fi
case "$(tr "[:upper:]" "[:lower:]" <<< "$repo_url")" in
*github*)
[ "$type" = "commit" ] && repo_url="$repo_url/commit/$(git rev-parse "$target")"
[ "$type" = "pr" ] && repo_url="$repo_url/compare/$target?expand=1"
[ "$type" = "tag" ] && repo_url="$repo_url/releases/tag/$target"
[ "$type" = "branch" ] && repo_url="$repo_url/tree/$target"
;;
*bitbucket*)
[ "$type" = "commit" ] && repo_url="$repo_url/commits/$(git rev-parse "$target")"
[ "$type" = "pr" ] && repo_url="$repo_url/pull-requests/new?source=$target"
[ "$type" = "tag" ] && repo_url="$repo_url/src/$target"
[ "$type" = "branch" ] && repo_url="$repo_url/src/$target"
;;
*gitlab*)
[ "$type" = "commit" ] && repo_url="$repo_url/-/commit/$(git rev-parse "$target")"
[ "$type" = "pr" ] && repo_url="$repo_url/-/merge_requests/new?merge_request[source_branch]=$target"
[ "$type" = "tag" ] && repo_url="$repo_url/-/tags/$target"
[ "$type" = "branch" ] && repo_url="$repo_url/-/tree/$target"
;;
*azure*)
[ "$type" = "commit" ] && repo_url="$repo_url/commit/$(git rev-parse "$target")"
[ "$type" = "pr" ] && repo_url="$repo_url/pullrequestcreate?sourceRef=$target"
[ "$type" = "tag" ] && repo_url="$repo_url?version=GT$target"
[ "$type" = "branch" ] && repo_url="$repo_url?version=GB$target"
;;
*)
echo "Cannot open: not a GitHub, GitLab, or Bitbucket repository" >&2
return 1
;;
esac
open "$repo_url"
}; f'
exit 0