diff --git a/autoload/magit/git.vim b/autoload/magit/git.vim index 0389b90ce4ac29cee3ff38353050c185c59a503d..563a0550b6f3600cf7e617609dbc62cad8f4f871 100644 --- a/autoload/magit/git.vim +++ b/autoload/magit/git.vim @@ -1,3 +1,5 @@ +let s:git_cmd="git" + " magit#git#get_status: this function returns the git status output formated " into a List of Dict as " [ {staged', 'unstaged', 'filename'}, ... ] @@ -8,7 +10,7 @@ function! magit#git#get_status() " we can't use git status -z here, because system doesn't make the " difference between NUL and NL. -status z terminate entries with NUL, " instead of NF - let status_list=magit#utils#systemlist("git status --porcelain") + let status_list=magit#utils#systemlist(s:git_cmd . " status --porcelain") for file_status_line in status_list let line_match = matchlist(file_status_line, '\(.\)\(.\) \%(.\{-\} -> \)\?"\?\(.\{-\}\)"\?$') let filename = line_match[3] @@ -17,3 +19,113 @@ function! magit#git#get_status() return file_list endfunction +" s:magit_top_dir: top directory of git tree +" it is evaluated only once +" FIXME: it won't work when playing with multiple git directories wihtin one +" vim session +let s:magit_top_dir='' +" magit#git#top_dir: return the absolute path of current git worktree +" return top directory +function! magit#git#top_dir() + if ( s:magit_top_dir == '' ) + let s:magit_top_dir=magit#utils#strip( + \ system(s:git_cmd . " rev-parse --show-toplevel")) . "/" + if ( v:shell_error != 0 ) + echoerr "Git error: " . s:magit_top_dir + endif + endif + return s:magit_top_dir +endfunction + +" s:magit_git_dir: git directory +" it is evaluated only once +" FIXME: it won't work when playing with multiple git directories wihtin one +" vim session +let s:magit_git_dir='' +" magit#git#git_dir: return the absolute path of current git worktree +" return git directory +function! magit#git#git_dir() + if ( s:magit_git_dir == '' ) + let s:magit_git_dir=magit#utils#strip(system(s:git_cmd . " rev-parse --git-dir")) . "/" + if ( v:shell_error != 0 ) + echoerr "Git error: " . s:magit_git_dir + endif + endif + return s:magit_git_dir +endfunction + +" magit#git#git_add: helper function to add a whole file +" nota: when git fail (due to misformated patch for example), an error +" message is raised. +" param[in] filemane: it must be quoted if it contains spaces +function! magit#git#git_add(filename) + let git_cmd=s:git_cmd . " add -- " . a:filename + silent let git_result=magit#utils#system(git_cmd) + if ( v:shell_error != 0 ) + echoerr "Git error: " . git_result + echoerr "Git cmd: " . git_cmd + endif +endfunction + +" magit#git#git_reset: helper function to add a whole file +" nota: when git fail (due to misformated patch for example), an error +" message is raised. +" param[in] filemane: it must be quoted if it contains spaces +function! magit#git#git_reset(filename) + let git_cmd=s:git_cmd . " reset -- " . a:filename + silent let git_result=magit#utils#system(git_cmd) + if ( v:shell_error != 0 ) + echoerr "Git error: " . git_result + echoerr "Git cmd: " . git_cmd + endif +endfunction + +" magit#git#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#git#git_apply(header, selection) + let selection = magit#utils#flatten(a:header + a:selection) + if ( selection[-1] !~ '^$' ) + let selection += [ '' ] + endif + let git_cmd=s:git_cmd . " 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#git#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#git#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( + \ s:git_cmd . " 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 + +function! magit#git#submodule_status() + return system(s:git_cmd . " submodule status") +endfunction diff --git a/autoload/magit/state.vim b/autoload/magit/state.vim index d6557acf2dffb393bf025f0f51d09a75086febac..032416eacd461d1ef8b82abd57206c605157e2a8 100644 --- a/autoload/magit/state.vim +++ b/autoload/magit/state.vim @@ -216,7 +216,7 @@ function! magit#state#update() dict let dir = getcwd() try - call magit#utils#lcd(magit#utils#top_dir()) + call magit#utils#lcd(magit#git#top_dir()) call magit#utils#refresh_submodule_list() for [mode, diff_dict_mode] in items(self.dict) let status_list = magit#git#get_status() diff --git a/autoload/magit/utils.vim b/autoload/magit/utils.vim index 767aef80c495400c4874fceaa1fbb0d0f7fadd05..87c4da6ed19c8a2ed1b94ee0954b00b14688f1ea 100644 --- a/autoload/magit/utils.vim +++ b/autoload/magit/utils.vim @@ -1,110 +1,3 @@ -" s:magit_top_dir: top directory of git tree -" it is evaluated only once -" FIXME: it won't work when playing with multiple git directories wihtin one -" vim session -let s:magit_top_dir='' -" magit#utils#top_dir: return the absolute path of current git worktree -" return top directory -function! magit#utils#top_dir() - if ( s:magit_top_dir == '' ) - let s:magit_top_dir=magit#utils#strip( - \ system("git rev-parse --show-toplevel")) . "/" - if ( v:shell_error != 0 ) - echoerr "Git error: " . s:magit_top_dir - endif - endif - return s:magit_top_dir -endfunction - -" s:magit_git_dir: git directory -" it is evaluated only once -" FIXME: it won't work when playing with multiple git directories wihtin one -" vim session -let s:magit_git_dir='' -" magit#utils#git_dir: return the absolute path of current git worktree -" return git directory -function! magit#utils#git_dir() - if ( s:magit_git_dir == '' ) - let s:magit_git_dir=magit#utils#strip(system("git rev-parse --git-dir")) . "/" - if ( v:shell_error != 0 ) - echoerr "Git error: " . s:magit_git_dir - endif - endif - return s:magit_git_dir -endfunction - -" magit#utils#git_add: helper function to add a whole file -" nota: when git fail (due to misformated patch for example), an error -" message is raised. -" param[in] filemane: it must be quoted if it contains spaces -function! magit#utils#git_add(filename) - let git_cmd="git add -- " . a:filename - silent let git_result=magit#utils#system(git_cmd) - if ( v:shell_error != 0 ) - echoerr "Git error: " . git_result - echoerr "Git cmd: " . git_cmd - endif -endfunction - -" magit#utils#git_reset: helper function to add a whole file -" nota: when git fail (due to misformated patch for example), an error -" message is raised. -" param[in] filemane: it must be quoted if it contains spaces -function! magit#utils#git_reset(filename) - let git_cmd="git reset -- " . a:filename - silent let git_result=magit#utils#system(git_cmd) - if ( v:shell_error != 0 ) - echoerr "Git error: " . git_result - echoerr "Git cmd: " . git_cmd - endif -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 " param[in] filename: the file path. it must quoted if it contains spaces @@ -124,7 +17,7 @@ let s:submodule_list = [] " magit#utils#refresh_submodule_list: this function refresh the List s:submodule_list " magit#utils#is_submodule() is using s:submodule_list function! magit#utils#refresh_submodule_list() - let s:submodule_list = map(split(system("git submodule status"), "\n"), 'split(v:val)[1]') + let s:submodule_list = map(split(magit#git#submodule_status(), "\n"), 'split(v:val)[1]') endfunction " magit#utils#is_submodule search if dirname is in s:submodule_list @@ -151,7 +44,7 @@ endfunction function! magit#utils#system(...) let dir = getcwd() try - execute s:magit_cd_cmd . magit#utils#top_dir() + execute s:magit_cd_cmd . magit#git#top_dir() " List as system() input is since v7.4.247, it is safe to check " systemlist, which is sine v7.4.248 if exists('*systemlist') @@ -184,7 +77,7 @@ endfunction function! magit#utils#systemlist(...) let dir = getcwd() try - execute s:magit_cd_cmd . magit#utils#top_dir() + execute s:magit_cd_cmd . magit#git#top_dir() " systemlist since v7.4.248 if exists('*systemlist') return call('systemlist', a:000) diff --git a/plugin/magit.vim b/plugin/magit.vim index 44fc02bce46b528b91b8bbccd4f905bd62e46b07..773bf590d8eed867b360f7d64d86ebc6d9b56dd3 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -240,7 +240,7 @@ function! s:mg_get_commit_section() silent put =magit#utils#underline(g:magit_sections.commit_start) silent put ='' - let git_dir=magit#utils#git_dir() + let git_dir=magit#git#git_dir() " refresh the COMMIT_EDITMSG file if ( s:magit_commit_mode == 'CC' ) silent! call magit#utils#system("GIT_EDITOR=/bin/false git commit -e 2> /dev/null") @@ -677,15 +677,15 @@ function! magit#stage_block(selection, discard) abort if ( a:discard == 0 ) if ( section == 'unstaged' ) if ( file.must_be_added() ) - call magit#utils#git_add(magit#utils#add_quotes(filename)) + call magit#git#git_add(magit#utils#add_quotes(filename)) else - call magit#utils#git_apply(header, a:selection) + call magit#git#git_apply(header, a:selection) endif elseif ( section == 'staged' ) if ( file.must_be_added() ) - call magit#utils#git_reset(magit#utils#add_quotes(filename)) + call magit#git#git_resetmagit#utils#add_quotes(filename)) else - call magit#utils#git_unapply(header, a:selection, 'staged') + call magit#git#git_unapply(header, a:selection, 'staged') endif else echoerr "Must be in \"" . @@ -697,7 +697,7 @@ function! magit#stage_block(selection, discard) abort if ( file.must_be_added() ) call delete(filename) else - call magit#utils#git_unapply(header, a:selection, 'unstaged') + call magit#git#git_unapply(header, a:selection, 'unstaged') endif else echoerr "Must be in \"" . @@ -780,7 +780,7 @@ endfunction " FIXME: git diff adds some strange characters to end of line function! magit#ignore_file() abort let ignore_file=<SID>mg_get_filename() - call magit#utils#append_file(magit#utils#top_dir() . ".gitignore", + call magit#utils#append_file(magit#git#top_dir() . ".gitignore", \ [ ignore_file ] ) call magit#update_buffer() endfunction