From 4bbf264f24077c872ff2a0ba31da6f9d84c0e942 Mon Sep 17 00:00:00 2001 From: Matthew Cengia Date: Thu, 8 Aug 2024 19:00:42 +1000 Subject: [PATCH 1/3] fix: Use absolute (relative to repo root) path to supplied filename If a file is referenced from the subdir of a repository, the script confirms it exists and tacks the filename onto the URL, but doesn't check if the filename provided is relative to the repo root. --- bin/git-browse | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/git-browse b/bin/git-browse index 98f315bb..8e87665b 100755 --- a/bin/git-browse +++ b/bin/git-browse @@ -33,7 +33,7 @@ if [[ $remote_url =~ gitlab ]]; then # construct gitlab urls # https://gitlab.com///-/blob//#L- if [[ -n ${filename} ]]; then - url="${url}/-/blob/${commit_or_branch}/${filename}" + url="${url}/-/blob/${commit_or_branch}/$(git ls-files --full-name "${filename}")" if [[ -n "${line1}" ]]; then url="${url}#L${line1}" if [[ -n "${line2}" ]]; then @@ -45,7 +45,7 @@ elif [[ $remote_url =~ github ]]; then # construct github urls # https://github.com///blob//#L-L if [[ -n "${filename}" ]]; then - url="${url}/blob/${commit_or_branch}/${filename}" + url="${url}/blob/${commit_or_branch}/$(git ls-files --full-name "${filename}")" if [[ -n "${line1}" ]]; then url="${url}#L${line1}" if [[ -n "${line2}" ]]; then @@ -57,7 +57,7 @@ elif [[ $remote_url =~ bitbucket ]]; then # construct bitbucket urls # https://bitbucket.org///src//#lines-: if [[ -n ${filename} ]]; then - url=${url}/src/${commit_or_branch}/${filename} + url=${url}/src/${commit_or_branch}/$(git ls-files --full-name "${filename}") if [[ -n "${line1}" ]]; then url="${url}#lines-${line1}" if [[ -n "${line2}" ]]; then From f26412eeaebff45a8802daa8edfa2e45277af392 Mon Sep 17 00:00:00 2001 From: Matt Cengia Date: Wed, 13 May 2026 22:00:45 +1000 Subject: [PATCH 2/3] Refactor to remove duplication --- bin/git-browse | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/git-browse b/bin/git-browse index 8e87665b..da75f5fc 100755 --- a/bin/git-browse +++ b/bin/git-browse @@ -28,12 +28,13 @@ fi # construct urls commit_hash=$(git rev-parse HEAD 2>/dev/null) commit_or_branch=${commit_hash:-${branch}} +full_filename=$(git ls-files --full-name "${filename}") if [[ $remote_url =~ gitlab ]]; then # construct gitlab urls # https://gitlab.com///-/blob//#L- if [[ -n ${filename} ]]; then - url="${url}/-/blob/${commit_or_branch}/$(git ls-files --full-name "${filename}")" + url="${url}/-/blob/${commit_or_branch}/${full_filename}" if [[ -n "${line1}" ]]; then url="${url}#L${line1}" if [[ -n "${line2}" ]]; then @@ -45,7 +46,7 @@ elif [[ $remote_url =~ github ]]; then # construct github urls # https://github.com///blob//#L-L if [[ -n "${filename}" ]]; then - url="${url}/blob/${commit_or_branch}/$(git ls-files --full-name "${filename}")" + url="${url}/blob/${commit_or_branch}/${full_filename}" if [[ -n "${line1}" ]]; then url="${url}#L${line1}" if [[ -n "${line2}" ]]; then @@ -57,7 +58,7 @@ elif [[ $remote_url =~ bitbucket ]]; then # construct bitbucket urls # https://bitbucket.org///src//#lines-: if [[ -n ${filename} ]]; then - url=${url}/src/${commit_or_branch}/$(git ls-files --full-name "${filename}") + url=${url}/src/${commit_or_branch}/${full_filename} if [[ -n "${line1}" ]]; then url="${url}#lines-${line1}" if [[ -n "${line2}" ]]; then From 8c8d1559e604dbcb95e2efebc46315ab3f9d9ae0 Mon Sep 17 00:00:00 2001 From: Matt Cengia Date: Wed, 13 May 2026 22:20:34 +1000 Subject: [PATCH 3/3] Fix path discrepancy in bats tests --- tests/git-browse.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/git-browse.bats b/tests/git-browse.bats index 190ef004..954e4916 100644 --- a/tests/git-browse.bats +++ b/tests/git-browse.bats @@ -24,11 +24,11 @@ get_file_uri() { local commit_hash= commit_hash=$(git rev-parse HEAD) if [ "$mode" = 'github' ]; then - REPLY="https://github.com/tj/git-extras/blob/$commit_hash/${filename}" + REPLY="https://github.com/tj/git-extras/blob/$commit_hash/${filename#./}" elif [ "$mode" = 'gitlab' ]; then - REPLY="https://gitlab.com/tj/git-extras/-/blob/${commit_hash}/${filename}" + REPLY="https://gitlab.com/tj/git-extras/-/blob/${commit_hash}/${filename#./}" elif [ "$mode" = 'bitbucket' ]; then - REPLY="https://bitbucket.org/tj/git-extras/src/${commit_hash}/${filename}" + REPLY="https://bitbucket.org/tj/git-extras/src/${commit_hash}/${filename#./}" fi }