From cfebd1313557a47730a78d562207998da1451c76 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 11 Sep 2019 01:20:27 +0200 Subject: github-url: Fix URL generation when `:pwd` is below repository root If the `:pwd` was a subdirectory of the repository root, the path in the constructed GitHub URL would not include any subdirectories up to and including the current working directory. This created an incorrect path in the URL, resulting in a 404. --- bundle/github-url/autoload/github_url.vim | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'bundle/github-url') diff --git a/bundle/github-url/autoload/github_url.vim b/bundle/github-url/autoload/github_url.vim index e090681..713ba89 100644 --- a/bundle/github-url/autoload/github_url.vim +++ b/bundle/github-url/autoload/github_url.vim @@ -16,7 +16,7 @@ endfunction function! s:FileURL(include_lines, start_line, end_line) let current_sha = system('git show --no-patch --format="format:%H"') let current_sha = substitute(current_sha, '\n$', '', '') - let current_filename = expand('%') + let current_filename = s:FilePathRelativeToRepoRoot() let lines = '' if a:include_lines @@ -27,7 +27,7 @@ function! s:FileURL(include_lines, start_line, end_line) endif endif - return s:BaseRepoURL() . '/blob/' . current_sha . '/' . current_filename . lines + return s:BaseRepoURL() . '/blob/' . current_sha . current_filename . lines endfunction " Copy the GitHub URL to the clipboard and echo it to the command line. @@ -37,3 +37,13 @@ function! github_url#GitHubURL(include_lines, start_line, end_line) call system('pbcopy', url) echo url endfunction + +" Get the path of the current file relative to the repository root. Gives us +" the correct path to the file even if `:pwd` is below the Git root. The +" returned path begins with a `/`. +function! s:FilePathRelativeToRepoRoot() + let repo_root = system('git rev-parse --show-toplevel')[:-2] + let current_file_absolute = expand('%:p') + + return substitute(current_file_absolute, repo_root, '', '') +endfunction -- cgit v1.2.3