2
\$\begingroup\$

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 22, 2013 at 19:24
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

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.

answered Aug 23, 2013 at 6:46
\$\endgroup\$
1
  • \$\begingroup\$ Hi, thank you for your reply, I will these changes actually for remote it was a bad idea. \$\endgroup\$ Commented Aug 23, 2013 at 9:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.