aboutsummaryrefslogtreecommitdiffstats
path: root/bundle
diff options
context:
space:
mode:
authorTeddy Wing2019-09-11 01:20:27 +0200
committerTeddy Wing2019-09-11 01:44:00 +0200
commitcfebd1313557a47730a78d562207998da1451c76 (patch)
treedea24ed171cf34dadca5c29918b89fc2f64be2e4 /bundle
parentaa9fadda4b29a33ccc150d687a9b9eed21704e31 (diff)
downloaddotvim-cfebd1313557a47730a78d562207998da1451c76.tar.bz2
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.
Diffstat (limited to 'bundle')
-rw-r--r--bundle/github-url/autoload/github_url.vim14
1 files changed, 12 insertions, 2 deletions
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