-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghwd.sh
More file actions
executable file
·62 lines (46 loc) · 1.9 KB
/
ghwd.sh
File metadata and controls
executable file
·62 lines (46 loc) · 1.9 KB
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
#!/usr/bin/env bash
# 'Open the GitHub page or website for a repository in your browser.'
# Figure out github repo base URL
base_url=$(git config --get remote.origin.url)
base_url=${base_url%\.git} # remove .git from end of string
# Fix git@github.com: URLs
base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
# Fix git://github.com URLS
base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
# Fix git@bitbucket.org: URLs
base_url=${base_url//git@bitbucket.org:/https:\/\/bitbucket\.org\/}
# Fix git@gitlab.com: URLs
base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
# Validate that this folder is a git folder
git branch 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
echo Not a git repo!
exit $?
fi
# Find current directory relative to .git parent
full_path=$(pwd)
git_base_path=$(cd ./$(git rev-parse --show-cdup); pwd)
relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
# If filename argument is present, append it
if [ "$1" ]; then
relative_path="$relative_path/$1"
fi
# Figure out current git branch
# git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
# Remove cruft from branchname
branch="${git_where#refs\/heads\/}"
branch="${branch#tags\/}"
branch="${branch%^0}"
[[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
url="$base_url/$tree/$branch$relative_path"
echo "$url"
# Check for various OS openers. Quit as soon as we find one that works.
# Don't assume this will work, provide a helpful diagnostic if it fails.
for opener in xdg-open open cygstart "start"; {
if command -v $opener; then
open=$opener;
break;
fi
}
$open "$url" || (echo "Unrecognized OS: Expected to find one of the following launch commands: xdg-open, open, cygstart, start" && exit 1);