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
```
This commit is contained in:
Peter Benjamin 2022-06-09 19:44:02 -07:00
parent 8a6e39d589
commit 7acc1de7fa
No known key found for this signature in database

View file

@ -8,7 +8,7 @@ line2=${4:-""}
# get remote name
if [[ $remote == "" ]]; then
branch=$(git rev-parse --abbrev-ref HEAD 2 &>/dev/null)
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
remote=$(git config branch."${branch}".remote || echo "origin")
fi
@ -31,14 +31,14 @@ elif [[ $remote_url = http* ]]; then
fi
# construct urls
commit_hash=$(git log -n1 --format=format:"%H" "${filename}" 2>/dev/null)
commit_hash=$(git rev-parse HEAD 2>/dev/null)
commit_or_branch=${commit_hash:-${branch}}
if [[ $remote_url =~ gitlab ]]; then
# construct gitlab urls
# https://gitlab.com/<user_or_group>/<repo>/-/blob/<commit_or_branch>/<filename>#L<line1>-<line2>
if [[ -n ${filename} ]]; then
url="${url}/-/blob/${commit_hash:-${branch}}/${filename}"
url="${url}/-/blob/${commit_or_branch}/${filename}"
if [[ -n "${line1}" ]]; then
url="${url}#L${line1}"
if [[ -n "${line2}" ]]; then
@ -50,7 +50,7 @@ elif [[ $remote_url =~ github ]]; then
# construct github urls
# https://github.com/<user_or_org>/<repo>/blob/<commit_or_branch>/<filename>#L<line1>-L<line2>
if [[ -n "${filename}" ]]; then
url="${url}/blob/${commit_hash:-${branch}}/${filename}"
url="${url}/blob/${commit_or_branch}/${filename}"
if [[ -n "${line1}" ]]; then
url="${url}#L${line1}"
if [[ -n "${line2}" ]]; then
@ -62,7 +62,7 @@ elif [[ $remote_url =~ bitbucket ]]; then
# construct bitbucket urls
# https://bitbucket.org/<user_or_org>/<repo>/src/<commit_or_branch>/<filename>#lines-<line1>:<line2>
if [[ -n ${filename} ]]; then
url=${url}/src/${commit_hash:-${branch}}/${filename}
url=${url}/src/${commit_or_branch}/${filename}
if [[ -n "${line1}" ]]; then
url="${url}#lines-${line1}"
if [[ -n "${line2}" ]]; then