Skip to content

Commit 7acc1de

Browse files
committed
fix(bin/git-browse): fix commit hash
Retrieving commit hashes with the following command is not very reliable for users who sign their commits with gpg/ssh keys: ``` git log -n1 --format=format:"%H" "${filename}" 2>/dev/null ```
1 parent 8a6e39d commit 7acc1de

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

bin/git-browse

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ line2=${4:-""}
88

99
# get remote name
1010
if [[ $remote == "" ]]; then
11-
branch=$(git rev-parse --abbrev-ref HEAD 2 &>/dev/null)
11+
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
1212
remote=$(git config branch."${branch}".remote || echo "origin")
1313
fi
1414

@@ -31,14 +31,14 @@ elif [[ $remote_url = http* ]]; then
3131
fi
3232

3333
# construct urls
34-
commit_hash=$(git log -n1 --format=format:"%H" "${filename}" 2>/dev/null)
34+
commit_hash=$(git rev-parse HEAD 2>/dev/null)
3535
commit_or_branch=${commit_hash:-${branch}}
3636

3737
if [[ $remote_url =~ gitlab ]]; then
3838
# construct gitlab urls
3939
# https://gitlab.com/<user_or_group>/<repo>/-/blob/<commit_or_branch>/<filename>#L<line1>-<line2>
4040
if [[ -n ${filename} ]]; then
41-
url="${url}/-/blob/${commit_hash:-${branch}}/${filename}"
41+
url="${url}/-/blob/${commit_or_branch}/${filename}"
4242
if [[ -n "${line1}" ]]; then
4343
url="${url}#L${line1}"
4444
if [[ -n "${line2}" ]]; then
@@ -50,7 +50,7 @@ elif [[ $remote_url =~ github ]]; then
5050
# construct github urls
5151
# https://github.com/<user_or_org>/<repo>/blob/<commit_or_branch>/<filename>#L<line1>-L<line2>
5252
if [[ -n "${filename}" ]]; then
53-
url="${url}/blob/${commit_hash:-${branch}}/${filename}"
53+
url="${url}/blob/${commit_or_branch}/${filename}"
5454
if [[ -n "${line1}" ]]; then
5555
url="${url}#L${line1}"
5656
if [[ -n "${line2}" ]]; then
@@ -62,7 +62,7 @@ elif [[ $remote_url =~ bitbucket ]]; then
6262
# construct bitbucket urls
6363
# https://bitbucket.org/<user_or_org>/<repo>/src/<commit_or_branch>/<filename>#lines-<line1>:<line2>
6464
if [[ -n ${filename} ]]; then
65-
url=${url}/src/${commit_hash:-${branch}}/${filename}
65+
url=${url}/src/${commit_or_branch}/${filename}
6666
if [[ -n "${line1}" ]]; then
6767
url="${url}#lines-${line1}"
6868
if [[ -n "${line2}" ]]; then

0 commit comments

Comments
 (0)