Skip to content

Commit

Permalink
Add utility functions for getting/splitting remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
rbong committed Sep 7, 2024
1 parent e7a0e9f commit 6158cbe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
20 changes: 20 additions & 0 deletions autoload/flog/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ function! flog#git#GetRefs() abort

return flog#shell#Run(l:cmd) + ['HEAD', 'FETCH_HEAD', 'ORIG_HEAD']
endfunction

function! flog#git#GetRemotes() abort
let l:cmd = flog#fugitive#GetGitCommand()
let l:cmd .= ' remote -v'

let l:remotes = flog#shell#Run(l:cmd)

return uniq(sort(map(l:remotes, 'substitute(v:val, "\t.*$", "", "")')))
endfunction

function! flog#git#SplitRemote(ref, remotes) abort
for l:remote in a:remotes
let l:len = len(l:remote)
if a:ref[ : l:len - 1] ==# l:remote
return [l:remote, a:ref[l:len + 1 : ]]
endif
endfor

return ['', a:ref]
endfunction
15 changes: 2 additions & 13 deletions autoload/flog/state.vim
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,13 @@ endfunction
function! flog#state#GetCommitRefs(commit) abort
let l:refs = []

let l:remotes = flog#shell#Run(flog#fugitive#GetGitCommand() . ' remote -v')
let l:remotes = uniq(map(l:remotes, 'substitute(v:val, "\t.*$", "", "")'))
let l:remotes = flog#git#GetRemotes()

for l:ref in split(a:commit.refs, ', ')
let l:match = matchlist(l:ref, '\v^(([^ ]+) -\> )?(tag: )?((refs/.{-}/)?(.*))')

let l:path = l:match[6]
let l:tail = l:path
let l:remote = ''

" Match path with remote
for l:remote_match in l:remotes
if l:path[ : len(l:remote_match) - 1] ==# l:remote_match
let l:remote = l:remote_match
let l:tail = l:path[len(l:remote_match) + 1 : ]
break
endif
endfor
let [l:remote, l:tail] = flog#git#SplitRemote(l:path, l:remotes)

" orig: The name of the original path, ex. "HEAD"
" tag: Whether the ref is a tag
Expand Down

0 comments on commit 6158cbe

Please sign in to comment.