Skip to content

Commit

Permalink
Allow :GV [git log options]
Browse files Browse the repository at this point in the history
Close #9
  • Loading branch information
junegunn committed Jan 23, 2016
1 parent 46976ec commit 84b61f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Usage
- `:GV` to open commit browser
- `:GV!` will only list commits for the current file

You can pass `git log` options to the command, e.g. `:GV -S foobar`.

### Mappings

- `o` or `<cr>` on a commit to display the content of it
Expand Down
30 changes: 26 additions & 4 deletions plugin/gv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ endfunction

function! s:list(fugitive_repo, log_opts)
let default_opts = ['--color=never', '--date=short', '--format=%cd %h%d %s (%an)']
let git_args = ['log'] + default_opts + a:log_opts
let git_args = ['log'] + a:log_opts + default_opts
let git_log_cmd = call(a:fugitive_repo.git_command, git_args, a:fugitive_repo)
call s:fill(git_log_cmd)
setlocal nowrap cursorline iskeyword+=#
Expand All @@ -207,7 +207,29 @@ function! s:list(fugitive_repo, log_opts)
echo 'o: open split / O: open tab / gb: Gbrowse / q: quit'
endfunction

function! s:gv(bang) abort
function! s:trim(arg)
let arg = substitute(a:arg, '\s*$', '', '')
return arg =~ "^'.*'$" ? substitute(arg[1:-2], "''", '', 'g')
\ : arg =~ '^".*"$' ? substitute(substitute(arg[1:-2], '""', '', 'g'), '\\"', '"', 'g')
\ : substitute(substitute(arg, '""\|''''', '', 'g'), '\\ ', ' ', 'g')
endfunction

function! gv#shellwords(arg)
let words = []
let contd = 0
for token in split(a:arg, '\%(\%(''\%([^'']\|''''\)\+''\)\|\%("\%(\\"\|[^"]\)\+"\)\|\%(\%(\\ \|\S\)\+\)\)\s*\zs')
let trimmed = s:trim(token)
if contd
let words[-1] .= trimmed
else
call add(words, trimmed)
endif
let contd = token !~ '\s\+$'
endfor
return words
endfunction

function! s:gv(bang, args) abort
if !exists('g:loaded_fugitive')
return s:warn('fugitive not found')
endif
Expand All @@ -221,12 +243,12 @@ function! s:gv(bang) abort
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
try
execute cd fugitive_repo.tree()
let log_opts = s:log_opts(fugitive_repo, a:bang)
let log_opts = extend(gv#shellwords(a:args), s:log_opts(fugitive_repo, a:bang))
call s:setup(git_dir, fugitive_repo.config('remote.origin.url'))
call s:list(fugitive_repo, log_opts)
finally
execute cd '-'
endtry
endfunction

command! -bang GV call s:gv(<bang>0)
command! -bang -nargs=* GV call s:gv(<bang>0, <q-args>)
13 changes: 13 additions & 0 deletions test/gv.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Execute (Test gv#shellwords):
AssertEqual [], gv#shellwords('')
AssertEqual ['foo', 'bar'], gv#shellwords('foo bar')
AssertEqual ['foo bar'], gv#shellwords("'foo bar'")
AssertEqual ['foo bar'], gv#shellwords('"foo bar"')
AssertEqual ['foo bar'], gv#shellwords('"foo "'' bar''')
AssertEqual ['foo bar'], gv#shellwords('''foo ''" bar"')
AssertEqual ['foo bar'], gv#shellwords("'foo '' bar'")
AssertEqual ['foo bar'], gv#shellwords('"foo "" bar"')
AssertEqual ['foo bar'], gv#shellwords('foo\ \ bar')
AssertEqual ['foo " bar'], gv#shellwords('"foo \" bar"')
AssertEqual ['foo " bar', 'baz'], gv#shellwords("'foo \" bar' baz")
AssertEqual ['foo', 'bar baz'], gv#shellwords("'foo' 'bar baz'")

0 comments on commit 84b61f0

Please sign in to comment.