Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
* next:
  doc,README: new help mapping
  plugin/magit.vim: handle empty commit (without staged file) and empty commit message (fix #79)
  autoload/magit/utils.vim: fix strip_array out of bound
  plugin/magit.vim: add magit#get_staged_files() and magit#get_unstaged_files() functions
  plugin/magit.vim: change inline help toggling mapping from 'h' to '?' (fix #80)
  • Loading branch information
jreybert committed Apr 14, 2016
2 parents ce7c781 + 2569fad commit 7d5f6a0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ E means 'edit'.
##### q
* Close the magit buffer

##### h
##### ?
* Toggle help showing in magit buffer

#### Autocommand events
Expand Down
7 changes: 4 additions & 3 deletions autoload/magit/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ endfunction
" param[in] array: array to strop
" return: stripped array
function! magit#utils#strip_array(array)
let array_len = len(a:array)
let start = 0
while ( a:array[start] == '' )
while ( start < array_len && a:array[start] == '' )
let start += 1
endwhile
let end = len(a:array) - 1
while ( a:array[end] == '' )
let end = array_len - 1
while ( end >= 0 && a:array[end] == '' )
let end -= 1
endwhile
return a:array[ start : end ]
Expand Down
4 changes: 2 additions & 2 deletions doc/vimagit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ Following mappings are set locally, for magit buffer only, in normal mode.
*vimagit-g:magit_close_mapping*
q close magit buffer.

*vimagit-h* *magit#toggle_help()*
*vimagit-?* *magit#toggle_help()*
*vimagit-g:magit_toggle_help_mapping*
h Toggle help showing in magit buffer
? Toggle help showing in magit buffer

AUTOCMD *vimagit-autocmd*

Expand Down
55 changes: 47 additions & 8 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let g:magit_reload_mapping = get(g:, 'magit_reload_mapping',
let g:magit_edit_mapping = get(g:, 'magit_edit_mapping', 'E' )
let g:magit_ignore_mapping = get(g:, 'magit_ignore_mapping', 'I' )
let g:magit_close_mapping = get(g:, 'magit_close_mapping', 'q' )
let g:magit_toggle_help_mapping = get(g:, 'magit_toggle_help_mapping', 'h' )
let g:magit_toggle_help_mapping = get(g:, 'magit_toggle_help_mapping', '?' )

let g:magit_folding_toggle_mapping = get(g:, 'magit_folding_toggle_mapping', [ '<CR>' ])
let g:magit_folding_open_mapping = get(g:, 'magit_folding_open_mapping', [ 'zo', 'zO' ])
Expand Down Expand Up @@ -372,16 +372,44 @@ function! s:mg_git_commit(mode) abort
silent let git_result=magit#utils#system(g:magit_git_cmd .
\ " commit --amend -C HEAD")
else
let commit_flag=""
if ( empty( magit#get_staged_files() ) )
let choice = confirm(
\ "Do you really want to commit without any staged files?",
\ "&Yes\n&No", 2)
if ( choice != 1 )
return
else
let commit_flag.=" --allow-empty "
endif
endif

let commit_msg=s:mg_get_commit_msg()
let amend_flag=""
if ( empty( commit_msg ) )
let choice = confirm(
\ "Do you really want to commit with an empty message?",
\ "&Yes\n&No", 2)
if ( choice != 1 )
return
else
let commit_flag.=" --allow-empty-message "
endif
endif

if ( a:mode == 'CA' )
let amend_flag=" --amend "
let commit_flag.=" --amend "
endif
silent! let git_result=magit#utils#system(g:magit_git_cmd .
\ " commit " . amend_flag . " --file - ", commit_msg)
let commit_cmd=g:magit_git_cmd . " commit " . commit_flag .
\ " --file - "
silent! let git_result=magit#utils#system(commit_cmd, commit_msg)
let b:magit_current_commit_mode=''
let b:magit_current_commit_msg=[]
endif
if ( v:shell_error != 0 )
echoerr "Git error: " . git_result
echohl ErrorMsg
echom "Git error: " . git_result
echom "Git cmd: " . commit_cmd
echohl None
endif
endfunction

Expand Down Expand Up @@ -1048,8 +1076,6 @@ function! magit#commit_command(mode)
" when we do commit, it is prefered ot commit the way we prepared it
" (.i.e normal or amend), whatever we commit with CC or CA.
call <SID>mg_git_commit(b:magit_current_commit_mode)
let b:magit_current_commit_mode=''
let b:magit_current_commit_msg=[]
else
let b:magit_current_commit_mode=a:mode
let b:magit_commit_newly_open=1
Expand Down Expand Up @@ -1101,6 +1127,19 @@ function! magit#jump_hunk(dir)
endif
endfunction

" magit#get_staged_files: function returning an array with staged files names
" return: an array with staged files names
function! magit#get_staged_files()
return keys(b:state.dict.staged)
endfunction

" magit#get_staged_files: function returning an array with unstaged files
" names
" return: an array with unstaged files names
function! magit#get_unstaged_files()
return keys(b:state.dict.unstaged)
endfunction

" magit#jump_to: function to move cursor to the file location of the current
" hunk
" if this file is already displayed in a window, jump to the window, if not,
Expand Down

0 comments on commit 7d5f6a0

Please sign in to comment.