Skip to content
Snippets Groups Projects
Commit c10c9cad authored by Jerome Reybert's avatar Jerome Reybert
Browse files

plugin/magit.vim: move git_(un)apply helper functions to utils.vim

parent 87cbc4f5
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,54 @@ function! magit#utils#git_dir() ...@@ -33,6 +33,54 @@ function! magit#utils#git_dir()
return s:magit_git_dir return s:magit_git_dir
endfunction endfunction
" magit#utils#git_apply: helper function to stage a selection
" nota: when git fail (due to misformated patch for example), an error
" message is raised.
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
" header plus one or more hunks
" return: no
function! magit#utils#git_apply(header, selection)
let selection = magit#utils#flatten(a:header + a:selection)
if ( selection[-1] !~ '^$' )
let selection += [ '' ]
endif
let git_cmd="git apply --recount --no-index --cached -"
silent let git_result=magit#utils#system(git_cmd, selection)
if ( v:shell_error != 0 )
echoerr "Git error: " . git_result
echoerr "Git cmd: " . git_cmd
echoerr "Tried to aply this"
echoerr string(selection)
endif
endfunction
" magit#utils#git_unapply: helper function to unstage a selection
" nota: when git fail (due to misformated patch for example), an error
" message is raised.
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
" header plus one or more hunks
" return: no
function! magit#utils#git_unapply(header, selection, mode)
let cached_flag=''
if ( a:mode == 'staged' )
let cached_flag=' --cached '
endif
let selection = magit#utils#flatten(a:header + a:selection)
if ( selection[-1] !~ '^$' )
let selection += [ '' ]
endif
silent let git_result=magit#utils#system(
\ "git apply --recount --no-index " . cached_flag . " --reverse - ",
\ selection)
if ( v:shell_error != 0 )
echoerr "Git error: " . git_result
echoerr "Tried to unaply this"
echoerr string(selection)
endif
endfunction
" s:magit#utils#is_binary: check if file is a binary file " s:magit#utils#is_binary: check if file is a binary file
" param[in] filename: the file path. it must quoted if it contains spaces " param[in] filename: the file path. it must quoted if it contains spaces
function! magit#utils#is_binary(filename) function! magit#utils#is_binary(filename)
......
...@@ -380,52 +380,6 @@ function! s:mg_select_hunk_block() ...@@ -380,52 +380,6 @@ function! s:mg_select_hunk_block()
\ g:magit_file_re) \ g:magit_file_re)
endfunction endfunction
" s:mg_git_apply: helper function to stage a selection
" nota: when git fail (due to misformated patch for example), an error
" message is raised.
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
" header plus one or more hunks
" return: no
function! s:mg_git_apply(header, selection)
let selection = magit#utils#flatten(a:header + a:selection)
if ( selection[-1] !~ '^$' )
let selection += [ '' ]
endif
let git_cmd="git apply --recount --no-index --cached -"
silent let git_result=magit#utils#system(git_cmd, selection)
if ( v:shell_error != 0 )
echoerr "Git error: " . git_result
echoerr "Git cmd: " . git_cmd
echoerr "Tried to aply this"
echoerr string(selection)
endif
endfunction
" s:mg_git_unapply: helper function to unstage a selection
" nota: when git fail (due to misformated patch for example), an error
" message is raised.
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
" header plus one or more hunks
" return: no
function! s:mg_git_unapply(header, selection, mode)
let cached_flag=''
if ( a:mode == 'staged' )
let cached_flag=' --cached '
endif
let selection = magit#utils#flatten(a:header + a:selection)
if ( selection[-1] !~ '^$' )
let selection += [ '' ]
endif
silent let git_result=magit#utils#system(
\ "git apply --recount --no-index " . cached_flag . " --reverse - ",
\ selection)
if ( v:shell_error != 0 )
echoerr "Git error: " . git_result
echoerr "Tried to unaply this"
echoerr string(selection)
endif
endfunction
" s:mg_create_diff_from_select: craft the diff to apply from a selection " s:mg_create_diff_from_select: craft the diff to apply from a selection
" in a chunk " in a chunk
" remarks: it works with full lines, and can not span over multiple chunks " remarks: it works with full lines, and can not span over multiple chunks
...@@ -726,14 +680,14 @@ function! magit#stage_block(selection, discard) abort ...@@ -726,14 +680,14 @@ function! magit#stage_block(selection, discard) abort
call magit#utils#system('git add ' . call magit#utils#system('git add ' .
\ magit#utils#add_quotes(filename)) \ magit#utils#add_quotes(filename))
else else
call <SID>mg_git_apply(header, a:selection) call magit#utils#git_apply(header, a:selection)
endif endif
elseif ( section == 'staged' ) elseif ( section == 'staged' )
if ( file.must_be_added() ) if ( file.must_be_added() )
call magit#utils#system('git reset ' . call magit#utils#system('git reset ' .
\ magit#utils#add_quotes(filename)) \ magit#utils#add_quotes(filename))
else else
call <SID>mg_git_unapply(header, a:selection, 'staged') call magit#utils#git_unapply(header, a:selection, 'staged')
endif endif
else else
echoerr "Must be in \"" . echoerr "Must be in \"" .
...@@ -745,7 +699,7 @@ function! magit#stage_block(selection, discard) abort ...@@ -745,7 +699,7 @@ function! magit#stage_block(selection, discard) abort
if ( file.must_be_added() ) if ( file.must_be_added() )
call delete(filename) call delete(filename)
else else
call <SID>mg_git_unapply(header, a:selection, 'unstaged') call magit#utils#git_unapply(header, a:selection, 'unstaged')
endif endif
else else
echoerr "Must be in \"" . echoerr "Must be in \"" .
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment