Skip to content

Commit d597f68

Browse files
authored
Merge pull request #981 from pbnj/feat/browse-file
feat(bin/git-browse): open website at file & line number or range
2 parents fdf89cd + 7acc1de commit d597f68

File tree

4 files changed

+146
-38
lines changed

4 files changed

+146
-38
lines changed

bin/git-browse

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,97 @@
11
#!/usr/bin/env bash
22

3-
if [[ $1 == "" ]]
4-
then
5-
branch=$(git rev-parse --abbrev-ref HEAD 2&> /dev/null)
3+
remote=${1:-""}
4+
branch=""
5+
filename=${2:-""}
6+
line1=${3:-""}
7+
line2=${4:-""}
8+
9+
# get remote name
10+
if [[ $remote == "" ]]; then
11+
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
612
remote=$(git config branch."${branch}".remote || echo "origin")
7-
else
8-
remote=$1
913
fi
1014

11-
if [[ $remote == "" ]]
12-
then
15+
if [[ $remote == "" ]]; then
1316
echo "Remote not found"
1417
exit 1
1518
fi
1619

20+
# get remote url
1721
remote_url=$(git remote get-url $remote)
1822

19-
if [[ $? -ne 0 ]]
20-
then
23+
if [[ $? -ne 0 ]]; then
2124
exit $?
2225
fi
2326

24-
if [[ $remote_url = git@* ]]
25-
then
27+
if [[ $remote_url = git@* ]]; then
2628
url=$(echo $remote_url | sed -E -e 's/:/\//' -e 's/\.git$//' -e 's/.*@(.*)/http:\/\/\1/')
27-
elif [[ $remote_url = http* ]]
28-
then
29+
elif [[ $remote_url = http* ]]; then
2930
url=${remote_url%.git}
3031
fi
3132

32-
case "$OSTYPE" in
33-
darwin*)
34-
# MacOS
35-
open $url
36-
;;
37-
msys)
38-
# Git-Bash on Windows
39-
start $url
40-
;;
41-
linux*)
42-
# Handle WSL on Windows
43-
if uname -a | grep -i -q Microsoft && command -v powershell.exe
44-
then
45-
powershell.exe -NoProfile start $url
46-
else
47-
xdg-open $url
33+
# construct urls
34+
commit_hash=$(git rev-parse HEAD 2>/dev/null)
35+
commit_or_branch=${commit_hash:-${branch}}
36+
37+
if [[ $remote_url =~ gitlab ]]; then
38+
# construct gitlab urls
39+
# https://gitlab.com/<user_or_group>/<repo>/-/blob/<commit_or_branch>/<filename>#L<line1>-<line2>
40+
if [[ -n ${filename} ]]; then
41+
url="${url}/-/blob/${commit_or_branch}/${filename}"
42+
if [[ -n "${line1}" ]]; then
43+
url="${url}#L${line1}"
44+
if [[ -n "${line2}" ]]; then
45+
url="${url}-${line2}"
46+
fi
47+
fi
48+
fi
49+
elif [[ $remote_url =~ github ]]; then
50+
# construct github urls
51+
# https://github.com/<user_or_org>/<repo>/blob/<commit_or_branch>/<filename>#L<line1>-L<line2>
52+
if [[ -n "${filename}" ]]; then
53+
url="${url}/blob/${commit_or_branch}/${filename}"
54+
if [[ -n "${line1}" ]]; then
55+
url="${url}#L${line1}"
56+
if [[ -n "${line2}" ]]; then
57+
url="${url}-L${line2}"
58+
fi
4859
fi
49-
;;
50-
*)
51-
# fall back to xdg-open for BSDs, etc.
60+
fi
61+
elif [[ $remote_url =~ bitbucket ]]; then
62+
# construct bitbucket urls
63+
# https://bitbucket.org/<user_or_org>/<repo>/src/<commit_or_branch>/<filename>#lines-<line1>:<line2>
64+
if [[ -n ${filename} ]]; then
65+
url=${url}/src/${commit_or_branch}/${filename}
66+
if [[ -n "${line1}" ]]; then
67+
url="${url}#lines-${line1}"
68+
if [[ -n "${line2}" ]]; then
69+
url="${url}:${line2}"
70+
fi
71+
fi
72+
fi
73+
fi
74+
75+
# open url
76+
case "$OSTYPE" in
77+
darwin*)
78+
# MacOS
79+
open $url
80+
;;
81+
msys)
82+
# Git-Bash on Windows
83+
start $url
84+
;;
85+
linux*)
86+
# Handle WSL on Windows
87+
if uname -a | grep -i -q Microsoft && command -v powershell.exe; then
88+
powershell.exe -NoProfile start $url
89+
else
5290
xdg-open $url
53-
;;
91+
fi
92+
;;
93+
*)
94+
# fall back to xdg-open for BSDs, etc.
95+
xdg-open $url
96+
;;
5497
esac

man/git-browse.1

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "GIT\-BROWSE" "1" "March 2020" "" "Git Extras"
4+
.TH "GIT\-BROWSE" "1" "June 2022" "" "Git Extras"
55
.
66
.SH "NAME"
77
\fBgit\-browse\fR \-
88
.
99
.SH "SYNOPSIS"
10-
\fBgit\-browse\fR [remote_name]
10+
\fBgit\-browse\fR [remote_name] [file_name] [line_1] [line_2]
1111
.
1212
.SH "DESCRIPTION"
1313
Opens the current git repository website in your default web browser\.
@@ -18,12 +18,39 @@ Opens the current git repository website in your default web browser\.
1818
.P
1919
The name of the remote you wish to browse to\. Defaults to the first remote if not specified\.
2020
.
21+
.P
22+
<file_name>
23+
.
24+
.P
25+
The name of the file you wish to browse to\.
26+
.
27+
.P
28+
<line_1>
29+
.
30+
.P
31+
The line number of the file you wish to browse to\.
32+
.
33+
.P
34+
<line_2>
35+
.
36+
.P
37+
The line range (from <line_1> to <line_2>) of the file you wish to browse to\.
38+
.
2139
.SH "EXAMPLES"
2240
$ git browse
2341
.
2442
.P
2543
$ git browse upstream
2644
.
45+
.P
46+
$ git browse upstream bin/git\-browse
47+
.
48+
.P
49+
$ git browse upstream bin/git\-browse 42
50+
.
51+
.P
52+
$ git browse upstream bin/git\-browse 1 42
53+
.
2754
.SH "AUTHOR"
2855
Written by Mark Pitman <\fIhttps://github\.com/mapitman\fR>
2956
.

man/git-browse.html

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-browse.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ git-browse(1) -- <View the web page for the current repository>
33

44
## SYNOPSIS
55

6-
`git-browse` [remote_name]
6+
`git-browse` [remote_name] [file_name] [line_1] [line_2]
77

88
## DESCRIPTION
99

@@ -16,12 +16,31 @@ Opens the current git repository website in your default web browser.
1616
The name of the remote you wish to browse to. Defaults to
1717
the first remote if not specified.
1818

19+
&lt;file_name&gt;
20+
21+
The name of the file you wish to browse to.
22+
23+
&lt;line_1&gt;
24+
25+
The line number of the file you wish to browse to.
26+
27+
&lt;line_2&gt;
28+
29+
The line range (from &lt;line_1&gt; to &lt;line_2&gt;) of the file you wish to
30+
browse to.
31+
1932
## EXAMPLES
2033

2134
$ git browse
2235

2336
$ git browse upstream
2437

38+
$ git browse upstream bin/git-browse
39+
40+
$ git browse upstream bin/git-browse 42
41+
42+
$ git browse upstream bin/git-browse 1 42
43+
2544
## AUTHOR
2645

2746
Written by Mark Pitman &lt;<https://github.com/mapitman>&gt;

0 commit comments

Comments
 (0)