Skip to content

Commit

Permalink
Merge branch 'dev/fix_add_bin'
Browse files Browse the repository at this point in the history
* dev/fix_add_bin:
  travis: reuse disable vader vim version output
  test/addFile/: add golden files for binary tests
  autoload/magit/utils.vim: fix is_binary function (fixes #27)
  .travis.yml: enable travis for all dev/* branches
  test/: add new tests with new and modified binary files
  plugin/magit.vim: fix stage_block discard
  plugin/magit.vim: stage file when closed (do not need vimagit cached data)
  autoload/magit/git.vim: add git_checkout(), fix git_add() and git_reset()
  plugin/magit.vim: add mg_stage_closed_file function
  plugin/magit.vim: fix big :%s
  autoload/magit/git.vim: git works in a clean environment (no config)
  autoload/magit/git.vim: move all git related functions to git.vim
  autoload/magit/utils.vim: add git_add and git_reset helper functions
  plugin/magit.vim: move git_(un)apply helper functions to utils.vim
  syntax,plugin: fix end of diff display (section title may be non highlighed)
  README.md: add asciinema example (fix #3)
  plugin/magit.vim, autoload/magit/state.vim: add some comments
  plugin/magit.vim: factorize some if statements
  plugin/magit.vim: remove useless window restore commands
  plugin/magit.vim: replace s:set() by get()
  • Loading branch information
jreybert committed Nov 1, 2015
2 parents ce83343 + a874be6 commit 3c3baa3
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 160 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ branches:
only:
- master
- next
- dev
- /^dev\/.*$/

os:
- linux
Expand Down Expand Up @@ -62,7 +62,7 @@ install:

before_script:
- git clone https://github.com/jreybert/djooks
- git clone https://github.com/junegunn/vader.vim
- git clone https://github.com/jreybert/vader.vim

script:
- ./test/run.sh . vader.vim djooks $VIM_VERSION
Expand Down
127 changes: 126 additions & 1 deletion autoload/magit/git.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let s:git_cmd="GIT_CONFIG=/dev/null GIT_CONFIG_NOSYSTEM=1 XDG_CONFIG_HOME=/ git"

" magit#git#get_status: this function returns the git status output formated
" into a List of Dict as
" [ {staged', 'unstaged', 'filename'}, ... ]
Expand All @@ -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]
Expand All @@ -17,3 +19,126 @@ 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 --no-ignore-removal -- " . 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_checkout: 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_checkout(filename)
let git_cmd=s:git_cmd . " checkout -- " . 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 HEAD -- " . 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
29 changes: 24 additions & 5 deletions autoload/magit/state.vim
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
" magit#state#is_file_visible: file getter function
" return if file is visible
function! magit#state#is_file_visible() dict
return self.visible
endfunction

" magit#state#set_file_visible: file setter function
" param[in] val: visible state to set to file
function! magit#state#set_file_visible(val) dict
let self.visible = a:val
endfunction

" magit#state#toggle_file_visible: file setter function, toggle file visible
" state
function! magit#state#toggle_file_visible() dict
let self.visible = ( self.visible == 0 ) ? 1 : 0
endfunction

" magit#state#is_file_dir: file getter function
" return 1 if current file is a directory, 0 otherwise
function! magit#state#is_file_dir() dict
return self.dir != 0
endfunction

function! magit#state#get_files(mode) dict
return self.dict[a:mode]
endfunction

" magit#state#must_be_added: file helper function
" there are some conditions where files must be widely added (git add), not
" 'diff applied' (git apply)
" return 1 if file must
function! magit#state#must_be_added() dict
return ( self.empty == 1 ||
\ self.symlink != '' ||
Expand Down Expand Up @@ -208,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()
Expand Down Expand Up @@ -236,6 +244,9 @@ function! magit#state#update() dict
endfor
endfunction

" magit#state#set_files_visible: global dict setter function
" update all files visible state
" param[in] is_visible: boolean value to set to files
function! magit#state#set_files_visible(is_visible) dict
for diff_dict_mode in values(self.dict)
for file in values(diff_dict_mode)
Expand All @@ -244,6 +255,14 @@ function! magit#state#set_files_visible(is_visible) dict
endfor
endfunction

" magit#state#get_files: global dict getter function
" param[in] mode: mode to select, can be 'staged' or 'unstaged'
" return all files belonging to mode
function! magit#state#get_files(mode) dict
return self.dict[a:mode]
endfunction


" dict: structure containing all diffs
" It is formatted as follow
" {
Expand Down
42 changes: 4 additions & 38 deletions autoload/magit/utils.vim
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
" 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

" 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
function! magit#utils#is_binary(filename)
return ( match(system("file --mime " . a:filename ),
\ a:filename . ".*charset=binary") != -1 )
\ ".*charset=binary") != -1 )
endfunction

" magit#utils#ls_all: list all files (including hidden ones) in a given path
Expand All @@ -51,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
Expand All @@ -78,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')
Expand Down Expand Up @@ -111,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)
Expand Down
Loading

0 comments on commit 3c3baa3

Please sign in to comment.