-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-browse-file
executable file
·100 lines (79 loc) · 2.09 KB
/
git-browse-file
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -euo pipefail
run() {
if [ -z "$quiet" ]; then
echo >&2 "+ $*"
fi
"$@"
}
usage() {
cat >&2 <<EOM
usage: $(basename "$0") [OPTIONS...] FILE
Open a web browser to look at the named FILE on Github on the current branch.
This is like \`hub browse\`, but actually knows where to find files on Github,
and handles relative paths correctly.
Options:
-u, --url-only Print URL rather than opening browser
-h, --help Show this help message
-q, --quiet Be less verbose
EOM
}
quiet=
url_only=
while [ $# -gt 0 ] && [[ "$1" = -* ]]; do
case "$1" in
-h|--help)
usage
exit
;;
-u|--url-only)
url_only=1
;;
-q|--quiet)
quiet=1
;;
*)
echo >&2 "Error: unexpected option $1"
usage
exit 1
;;
esac
shift
done
if [ $# -ne 1 ]; then
echo "FILE is required"
usage
exit 1
fi
filename="$1"
repo_url="$(run hub browse -u)"
# Get the current branch name, in a symbolic format if one is available.
# The first command will return a branch name like `master` if we're on a
# branch. It will exit nonzero if HEAD is detached. The second command will
# always return the actual SHA1 that HEAD refers to.
current_git_ref_name="$(run git symbolic-ref --short -q HEAD || run git rev-parse HEAD)"
# Find the path to the requested file
full_path="$(run git ls-tree --full-name --name-only HEAD "$filename")"
# If the requested file is a directory, then ls-tree will have returned a list
# of filenames. Call dirname on the first one.
if [ -d "$filename" ]; then
full_path="$(dirname "$(head -1 <<< "$full_path")")"
thing=tree
else
thing=blob
fi
full_url="$repo_url/$thing/$current_git_ref_name/$full_path"
if [ -n "$url_only" ]; then
echo "$full_url"
exit
fi
if type -t xdg-open >/dev/null; then
run xdg-open "$full_url"
exit
fi
if type -t open >/dev/null; then
run open "$full_url"
exit
fi
echo >&2 "Error: couldn't find open or xdg-open to load a browser with"
exit 2