Skip to content

Commit

Permalink
fix(bin/git-browse): fix commit hash
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
pbnj committed Jun 10, 2022
1 parent 8a6e39d commit 7acc1de
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bin/git-browse
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7acc1de

Please sign in to comment.