I made a small Vim distribution that uses vundle. I decided to make a small function that checks if the repository must be updated.
""""""""""""""""""""""""""""""""""""""""""""""""""
"Update the repository
""""""""""""""""""""""""""""""""""""""""""""""""""
"A function to update if necessary
function! Get_update()
"Get the current path and change the directory to update the good
"repository
let l:current_path = expand("<sfile>:p:h")
let l:path = '~/.vim-mahewin-repository'
exec 'cd' l:path
let l:last_local_commit = system('git rev-parse HEAD')
let l:last_remote_comit = system('git ls-remote origin -h refs/heads/master |cut -f1')
if l:last_local_commit != l:last_remote_comit
echo 'Need to be updated, launches updated'
:!git pull `git remote` `git rev-parse --abbrev-ref HEAD`
endif
exec 'cd' l:current_path
endfunction
It's not very nice, but I have not found out how to do it better. I'm open to advice.
1 Answer 1
Your function has the side-effect of switching the directory to the file's path. To be side-effect-free, you should let l:current_path = getcwd()
instead. Better yet, use the --git-dir
and --work-tree
options to git(1)
to avoid having to cd
at all.
You always query the remote named origin
, but pull from `git remote`. That inconsistent looks like a bug.
I don't see any reason to use --abbrev-ref
when the result is not intended for human consumption. Just use the full commit id.
-
\$\begingroup\$ Hi, thank you for your reply, I will these changes actually for remote it was a bad idea. \$\endgroup\$Hobbestigrou– Hobbestigrou2013年08月23日 09:45:54 +00:00Commented Aug 23, 2013 at 9:45