Skip to content

Commit 3c3baa3

Browse files
committed
Merge branch 'dev/fix_add_bin'
* 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()
2 parents ce83343 + a874be6 commit 3c3baa3

13 files changed

+255
-160
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ branches:
44
only:
55
- master
66
- next
7-
- dev
7+
- /^dev\/.*$/
88

99
os:
1010
- linux
@@ -62,7 +62,7 @@ install:
6262

6363
before_script:
6464
- git clone https://github.com/jreybert/djooks
65-
- git clone https://github.com/junegunn/vader.vim
65+
- git clone https://github.com/jreybert/vader.vim
6666

6767
script:
6868
- ./test/run.sh . vader.vim djooks $VIM_VERSION

autoload/magit/git.vim

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let s:git_cmd="GIT_CONFIG=/dev/null GIT_CONFIG_NOSYSTEM=1 XDG_CONFIG_HOME=/ git"
2+
13
" magit#git#get_status: this function returns the git status output formated
24
" into a List of Dict as
35
" [ {staged', 'unstaged', 'filename'}, ... ]
@@ -8,7 +10,7 @@ function! magit#git#get_status()
810
" we can't use git status -z here, because system doesn't make the
911
" difference between NUL and NL. -status z terminate entries with NUL,
1012
" instead of NF
11-
let status_list=magit#utils#systemlist("git status --porcelain")
13+
let status_list=magit#utils#systemlist(s:git_cmd . " status --porcelain")
1214
for file_status_line in status_list
1315
let line_match = matchlist(file_status_line, '\(.\)\(.\) \%(.\{-\} -> \)\?"\?\(.\{-\}\)"\?$')
1416
let filename = line_match[3]
@@ -17,3 +19,126 @@ function! magit#git#get_status()
1719
return file_list
1820
endfunction
1921

22+
" s:magit_top_dir: top directory of git tree
23+
" it is evaluated only once
24+
" FIXME: it won't work when playing with multiple git directories wihtin one
25+
" vim session
26+
let s:magit_top_dir=''
27+
" magit#git#top_dir: return the absolute path of current git worktree
28+
" return top directory
29+
function! magit#git#top_dir()
30+
if ( s:magit_top_dir == '' )
31+
let s:magit_top_dir=magit#utils#strip(
32+
\ system(s:git_cmd . " rev-parse --show-toplevel")) . "/"
33+
if ( v:shell_error != 0 )
34+
echoerr "Git error: " . s:magit_top_dir
35+
endif
36+
endif
37+
return s:magit_top_dir
38+
endfunction
39+
40+
" s:magit_git_dir: git directory
41+
" it is evaluated only once
42+
" FIXME: it won't work when playing with multiple git directories wihtin one
43+
" vim session
44+
let s:magit_git_dir=''
45+
" magit#git#git_dir: return the absolute path of current git worktree
46+
" return git directory
47+
function! magit#git#git_dir()
48+
if ( s:magit_git_dir == '' )
49+
let s:magit_git_dir=magit#utils#strip(system(s:git_cmd . " rev-parse --git-dir")) . "/"
50+
if ( v:shell_error != 0 )
51+
echoerr "Git error: " . s:magit_git_dir
52+
endif
53+
endif
54+
return s:magit_git_dir
55+
endfunction
56+
57+
" magit#git#git_add: helper function to add a whole file
58+
" nota: when git fail (due to misformated patch for example), an error
59+
" message is raised.
60+
" param[in] filemane: it must be quoted if it contains spaces
61+
function! magit#git#git_add(filename)
62+
let git_cmd=s:git_cmd . " add --no-ignore-removal -- " . a:filename
63+
silent let git_result=magit#utils#system(git_cmd)
64+
if ( v:shell_error != 0 )
65+
echoerr "Git error: " . git_result
66+
echoerr "Git cmd: " . git_cmd
67+
endif
68+
endfunction
69+
70+
" magit#git#git_checkout: helper function to add a whole file
71+
" nota: when git fail (due to misformated patch for example), an error
72+
" message is raised.
73+
" param[in] filemane: it must be quoted if it contains spaces
74+
function! magit#git#git_checkout(filename)
75+
let git_cmd=s:git_cmd . " checkout -- " . a:filename
76+
silent let git_result=magit#utils#system(git_cmd)
77+
if ( v:shell_error != 0 )
78+
echoerr "Git error: " . git_result
79+
echoerr "Git cmd: " . git_cmd
80+
endif
81+
endfunction
82+
83+
" magit#git#git_reset: helper function to add a whole file
84+
" nota: when git fail (due to misformated patch for example), an error
85+
" message is raised.
86+
" param[in] filemane: it must be quoted if it contains spaces
87+
function! magit#git#git_reset(filename)
88+
let git_cmd=s:git_cmd . " reset HEAD -- " . a:filename
89+
silent let git_result=magit#utils#system(git_cmd)
90+
if ( v:shell_error != 0 )
91+
echoerr "Git error: " . git_result
92+
echoerr "Git cmd: " . git_cmd
93+
endif
94+
endfunction
95+
96+
" magit#git#git_apply: helper function to stage a selection
97+
" nota: when git fail (due to misformated patch for example), an error
98+
" message is raised.
99+
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
100+
" header plus one or more hunks
101+
" return: no
102+
function! magit#git#git_apply(header, selection)
103+
let selection = magit#utils#flatten(a:header + a:selection)
104+
if ( selection[-1] !~ '^$' )
105+
let selection += [ '' ]
106+
endif
107+
let git_cmd=s:git_cmd . " apply --recount --no-index --cached -"
108+
silent let git_result=magit#utils#system(git_cmd, selection)
109+
if ( v:shell_error != 0 )
110+
echoerr "Git error: " . git_result
111+
echoerr "Git cmd: " . git_cmd
112+
echoerr "Tried to aply this"
113+
echoerr string(selection)
114+
endif
115+
endfunction
116+
117+
" magit#git#git_unapply: helper function to unstage a selection
118+
" nota: when git fail (due to misformated patch for example), an error
119+
" message is raised.
120+
" param[in] selection: the text to stage. It must be a patch, i.e. a diff
121+
" header plus one or more hunks
122+
" return: no
123+
function! magit#git#git_unapply(header, selection, mode)
124+
let cached_flag=''
125+
if ( a:mode == 'staged' )
126+
let cached_flag=' --cached '
127+
endif
128+
let selection = magit#utils#flatten(a:header + a:selection)
129+
if ( selection[-1] !~ '^$' )
130+
let selection += [ '' ]
131+
endif
132+
silent let git_result=magit#utils#system(
133+
\ s:git_cmd . " apply --recount --no-index " . cached_flag . " --reverse - ",
134+
\ selection)
135+
if ( v:shell_error != 0 )
136+
echoerr "Git error: " . git_result
137+
echoerr "Tried to unaply this"
138+
echoerr string(selection)
139+
endif
140+
endfunction
141+
142+
function! magit#git#submodule_status()
143+
return system(s:git_cmd . " submodule status")
144+
endfunction

autoload/magit/state.vim

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1+
" magit#state#is_file_visible: file getter function
2+
" return if file is visible
13
function! magit#state#is_file_visible() dict
24
return self.visible
35
endfunction
46

7+
" magit#state#set_file_visible: file setter function
8+
" param[in] val: visible state to set to file
59
function! magit#state#set_file_visible(val) dict
610
let self.visible = a:val
711
endfunction
812

13+
" magit#state#toggle_file_visible: file setter function, toggle file visible
14+
" state
915
function! magit#state#toggle_file_visible() dict
1016
let self.visible = ( self.visible == 0 ) ? 1 : 0
1117
endfunction
1218

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

17-
function! magit#state#get_files(mode) dict
18-
return self.dict[a:mode]
19-
endfunction
20-
25+
" magit#state#must_be_added: file helper function
26+
" there are some conditions where files must be widely added (git add), not
27+
" 'diff applied' (git apply)
28+
" return 1 if file must
2129
function! magit#state#must_be_added() dict
2230
return ( self.empty == 1 ||
2331
\ self.symlink != '' ||
@@ -208,7 +216,7 @@ function! magit#state#update() dict
208216

209217
let dir = getcwd()
210218
try
211-
call magit#utils#lcd(magit#utils#top_dir())
219+
call magit#utils#lcd(magit#git#top_dir())
212220
call magit#utils#refresh_submodule_list()
213221
for [mode, diff_dict_mode] in items(self.dict)
214222
let status_list = magit#git#get_status()
@@ -236,6 +244,9 @@ function! magit#state#update() dict
236244
endfor
237245
endfunction
238246

247+
" magit#state#set_files_visible: global dict setter function
248+
" update all files visible state
249+
" param[in] is_visible: boolean value to set to files
239250
function! magit#state#set_files_visible(is_visible) dict
240251
for diff_dict_mode in values(self.dict)
241252
for file in values(diff_dict_mode)
@@ -244,6 +255,14 @@ function! magit#state#set_files_visible(is_visible) dict
244255
endfor
245256
endfunction
246257

258+
" magit#state#get_files: global dict getter function
259+
" param[in] mode: mode to select, can be 'staged' or 'unstaged'
260+
" return all files belonging to mode
261+
function! magit#state#get_files(mode) dict
262+
return self.dict[a:mode]
263+
endfunction
264+
265+
247266
" dict: structure containing all diffs
248267
" It is formatted as follow
249268
" {

autoload/magit/utils.vim

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
1-
" s:magit_top_dir: top directory of git tree
2-
" it is evaluated only once
3-
" FIXME: it won't work when playing with multiple git directories wihtin one
4-
" vim session
5-
let s:magit_top_dir=''
6-
" magit#utils#top_dir: return the absolute path of current git worktree
7-
" return top directory
8-
function! magit#utils#top_dir()
9-
if ( s:magit_top_dir == '' )
10-
let s:magit_top_dir=magit#utils#strip(
11-
\ system("git rev-parse --show-toplevel")) . "/"
12-
if ( v:shell_error != 0 )
13-
echoerr "Git error: " . s:magit_top_dir
14-
endif
15-
endif
16-
return s:magit_top_dir
17-
endfunction
18-
19-
" s:magit_git_dir: git directory
20-
" it is evaluated only once
21-
" FIXME: it won't work when playing with multiple git directories wihtin one
22-
" vim session
23-
let s:magit_git_dir=''
24-
" magit#utils#git_dir: return the absolute path of current git worktree
25-
" return git directory
26-
function! magit#utils#git_dir()
27-
if ( s:magit_git_dir == '' )
28-
let s:magit_git_dir=magit#utils#strip(system("git rev-parse --git-dir")) . "/"
29-
if ( v:shell_error != 0 )
30-
echoerr "Git error: " . s:magit_git_dir
31-
endif
32-
endif
33-
return s:magit_git_dir
34-
endfunction
351

362
" s:magit#utils#is_binary: check if file is a binary file
373
" param[in] filename: the file path. it must quoted if it contains spaces
384
function! magit#utils#is_binary(filename)
395
return ( match(system("file --mime " . a:filename ),
40-
\ a:filename . ".*charset=binary") != -1 )
6+
\ ".*charset=binary") != -1 )
417
endfunction
428

439
" magit#utils#ls_all: list all files (including hidden ones) in a given path
@@ -51,7 +17,7 @@ let s:submodule_list = []
5117
" magit#utils#refresh_submodule_list: this function refresh the List s:submodule_list
5218
" magit#utils#is_submodule() is using s:submodule_list
5319
function! magit#utils#refresh_submodule_list()
54-
let s:submodule_list = map(split(system("git submodule status"), "\n"), 'split(v:val)[1]')
20+
let s:submodule_list = map(split(magit#git#submodule_status(), "\n"), 'split(v:val)[1]')
5521
endfunction
5622

5723
" magit#utils#is_submodule search if dirname is in s:submodule_list
@@ -78,7 +44,7 @@ endfunction
7844
function! magit#utils#system(...)
7945
let dir = getcwd()
8046
try
81-
execute s:magit_cd_cmd . magit#utils#top_dir()
47+
execute s:magit_cd_cmd . magit#git#top_dir()
8248
" List as system() input is since v7.4.247, it is safe to check
8349
" systemlist, which is sine v7.4.248
8450
if exists('*systemlist')
@@ -111,7 +77,7 @@ endfunction
11177
function! magit#utils#systemlist(...)
11278
let dir = getcwd()
11379
try
114-
execute s:magit_cd_cmd . magit#utils#top_dir()
80+
execute s:magit_cd_cmd . magit#git#top_dir()
11581
" systemlist since v7.4.248
11682
if exists('*systemlist')
11783
return call('systemlist', a:000)

0 commit comments

Comments
 (0)