Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache location of session file #10

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions plugin/gitsessions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ if !exists('s:session_exist')
let s:session_exist = 0
endif

" Cache session file,
" Pros: performance gain (x100) on large repositories
" Cons: switch between git branches will be missed from GitSessionUpdate()
" You are advised to save it manually by calling to GitSessionSave()
" Default - cache disabled
let g:cache_session_file = 0

" HELPER FUNCTIONS

function! s:replace_bad_chars(string)
Expand Down Expand Up @@ -102,17 +109,24 @@ function! s:session_dir()
endif
endfunction

function! s:session_file()
function! s:session_file(invalidate_cache)
if g:cache_session_file && !a:invalidate_cache && exists('s:cached_session_file')
return s:cached_session_file
endif
let l:dir = s:session_dir()
let l:branch = s:git_branch_name()
return (empty(l:branch)) ? l:dir . '/master' : l:dir . '/' . l:branch
if exists('s:cached_session_file')
unlet s:cached_session_file
endif
let s:cached_session_file = (empty(l:branch)) ? l:dir . '/master' : l:dir . '/' . l:branch
return s:cached_session_file
endfunction

" PUBLIC FUNCTIONS

function! g:GitSessionSave()
let l:dir = s:session_dir()
let l:file = s:session_file()
let l:file = s:session_file(1)

if !isdirectory(l:dir)
call mkdir(l:dir, 'p')
Expand Down Expand Up @@ -141,7 +155,7 @@ endfunction

function! g:GitSessionUpdate(...)
let l:show_msg = a:0 > 0 ? a:1 : 1
let l:file = s:session_file()
let l:file = s:session_file(0)

if s:session_exist && filereadable(l:file)
execute 'mksession!' l:file
Expand All @@ -157,7 +171,7 @@ function! g:GitSessionLoad(...)
endif

let l:show_msg = a:0 > 0 ? a:1 : 0
let l:file = s:session_file()
let l:file = s:session_file(1)

if filereadable(l:file)
let s:session_exist = 1
Expand All @@ -170,8 +184,14 @@ function! g:GitSessionLoad(...)
endfunction

function! g:GitSessionDelete()
let l:file = s:session_file()
" Delete is a tricky case, we still need to use cached version if any.
" This version was used and saved by GitSessionUpdate(), however
" we should ensure that session cached variable is cleared.
let l:file = s:session_file(1)
let s:session_exist = 0
if exists('s:cached_session_file')
unlet s:cached_session_file
endif
if filereadable(l:file)
call delete(l:file)
echom "session deleted:" l:file
Expand Down