Skip to content

Commit 0cdef5f

Browse files
committed
improve Gadd with current file
1 parent 3b4bd86 commit 0cdef5f

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

autoload/easygit.vim

+45-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
" Version: 0.2.1
66
" Last Modified: Dec 18, 2016
77
" ============================================================================
8+
let s:nomodeline = (v:version > 703 || (v:version == 703 && has('patch442'))) ? '<nomodeline>' : ''
89
let s:is_win = has("win32") || has('win64')
910

1011
" Extract git directory by path
@@ -31,8 +32,8 @@ function! s:FindGitdir(path)
3132
return substitute(root, '\r\?\n', '', '') . '/.git'
3233
else
3334
let dir = finddir('.git', expand(a:path).';')
34-
if dir | return fnamemodify(dir, ':p:h') | endif
35-
return ''
35+
if empty(dir) | return '' | endif
36+
return fnamemodify(dir, ':p:h')
3637
endif
3738
endfunction
3839

@@ -617,14 +618,18 @@ endfunction
617618

618619
" Run git add with files in smartRoot
619620
function! easygit#add(...) abort
620-
if a:0 == 0 | return | endif
621621
let root = easygit#smartRoot()
622622
if empty(root) | return | endif
623623
let cwd = getcwd()
624624
execute 'lcd ' . root
625-
let args = join(map(copy(a:000), 'shellescape(v:val)'), ' ')
626-
let command = 'git add ' . join(a:000, ' ')
625+
if empty(a:000)
626+
let l:args = expand('%')
627+
else
628+
let l:args = join(map(copy(a:000), 'shellescape(v:val)'), ' ')
629+
endif
630+
let command = 'git add ' . l:args
627631
call s:system(command)
632+
call s:ResetGutter(bufnr('%'))
628633
execute 'lcd ' . cwd
629634
endfunction
630635

@@ -634,13 +639,40 @@ function! easygit#status()
634639
if empty(root) | return | endif
635640
let cwd = getcwd()
636641
execute 'lcd ' . root
637-
call s:execute('git status --long -b', {
642+
call s:execute('git --no-pager status --long -b', {
638643
\ 'edit': 'edit',
639644
\ 'title': '__easygit_status__',
640645
\})
641646
execute 'lcd ' . cwd
642647
endfunction
643648

649+
function! easygit#read(args)
650+
let root = easygit#smartRoot()
651+
if empty(root) | return | endif
652+
let old_cwd = getcwd()
653+
execute 'lcd ' . root
654+
if empty(a:args)
655+
let path = expand('%')
656+
else
657+
let path = a:args
658+
endif
659+
if empty(path) | return | endif
660+
let output = system('git --no-pager show :'.path)
661+
if v:shell_error && output !=# ""
662+
echohl Error | echon output | echohl None
663+
return -1
664+
endif
665+
execute 'edit ' . path
666+
execute '%d'
667+
let eol = s:is_win ? '\v\n' : '\v\r?\n'
668+
let list = split(output, eol)
669+
if len(list)
670+
call setline(1, list[0])
671+
silent! call append(1, list[1:])
672+
endif
673+
execute 'lcd ' . old_cwd
674+
endfunction
675+
644676
function! easygit#merge(args)
645677
if a:0 == 0 | return | endif
646678
let root = easygit#smartRoot()
@@ -678,7 +710,7 @@ function! s:execute(cmd, option) abort
678710
let edit = get(a:option, 'edit', 'edit')
679711
let pipe = get(a:option, 'pipe', 0)
680712
let bnr = bufnr('%')
681-
if edit !~# 'keepalt'
713+
if edit !~# 'keepalt' && !get(a:option, 'nokeep', 0)
682714
let edit = 'keepalt ' . edit
683715
endif
684716
if pipe
@@ -789,4 +821,10 @@ function! s:escape(str)
789821
endif
790822
return a:str
791823
endfunction
824+
825+
function! s:ResetGutter(bufnr)
826+
if exists('*gitgutter#process_buffer')
827+
call gitgutter#process_buffer(a:bufnr, 1)
828+
endif
829+
endfunction
792830
" vim:set et sw=2 ts=2 tw=78:

doc/easygit.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ All commands are disabed by default to avoid conflict, you have to use
192192
added for git rm. If no files specified, current file is used,
193193
{files} would also be removed from vim's buffer list.
194194

195-
:Gadd {files}... *:Gadd*
196-
Run git add with {files}
195+
:Gadd [{files}]... *:Gadd*
196+
Run git add with {files}, use current file when no argument
197+
provided.
197198

198199
:Gstatus *:Gstatus*
199200
Show git status in a temporary buffer
@@ -214,6 +215,12 @@ All commands are disabed by default to avoid conflict, you have to use
214215
:Grevert {args} *:Grevert*
215216
Run git revert with {args}
216217

218+
:Gread [{file}]
219+
a variant of git checkout -- filename that operates on the
220+
buffer rather than the filename. This means you can use u to
221+
undo it and you never get any warnings about the file changing
222+
outside Vim.
223+
217224
==============================================================================
218225
VARIABLES *easygit-variables*
219226

@@ -386,6 +393,8 @@ CHANGELOG *easygit-changelog*
386393

387394
- Use $GIT_DIR or `git rev-parse --show-toplevel` to find git root
388395
directory by default.
396+
- Add :Gread command
397+
- :Gadd works with current file by default
389398

390399

391400
0.5.1 2017-06-14

plugin/easygit.vim

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ if get(g:, 'easygit_enable_command', 0)
155155
command! -nargs=* -complete=custom,easygit#listRemotes Gpush :call easygit#dispatch('push', <q-args>)
156156
command! -nargs=* -complete=custom,easygit#listRemotes Gfetch :call easygit#dispatch('fetch', <q-args>)
157157
command! -nargs=* -complete=custom,easygit#listRemotes Gpull :call easygit#dispatch('pull', <q-args>)
158-
command! -nargs=+ -complete=custom,easygit#completeAdd Gadd :call easygit#add(<f-args>)
158+
command! -nargs=* -complete=custom,easygit#completeAdd Gadd :call easygit#add(<f-args>)
159159
command! -nargs=+ -complete=custom,s:CompleteBranch Gmerge :call easygit#merge(<q-args>)
160160
command! -nargs=+ -complete=custom,s:GitFiles Ggrep :call easygit#grep(<q-args>)
161161
command! -nargs=+ -complete=customlist,easygit#completeRevert Grevert :call easygit#revert(<q-args>)
162162
command! -nargs=+ -complete=customlist,easygit#completeReset Greset :call easygit#reset(<q-args>)
163163
command! -nargs=+ -complete=customlist,easygit#completeCommit Gcommit :call easygit#commit(<q-args>)
164+
command! -nargs=? -complete=custom,easygit#completeAdd Gread :call easygit#read(<q-args>)
164165
endif
165166

166167
" enable auto lcd

0 commit comments

Comments
 (0)