2

I´m new to git and I´ve a little problem using git the way I want to. I administrate a bunch of servers. What I want to have is a local repository of the config files I edit. In case of missconfiguratin I could then easily jump back to a working version.

For example: I edit a file that is not in the repository. After editing I would like to add it to the repo automatically including the absolute path and do the commit. The commit message should be asked automatically on quit.

Until now I tried it using this additional line in vimrc.local:

autocmd BufWritePost * let message = input('Message? ', 'Auto-commit: saved ' . 
expand('%')) | execute ':silent ! if git rev-parse --git-dir > /dev/null 2>&1 ; 
then git add % ; git commit -m ' . shellescape(message, 1) . '; fi > /dev/null 2>&1'

Source: stackoverflow

This does not solve my problem as the files are not included with their absolute path and sometimes are not added at all.

I heard about fugitive which could do that but I don´t get it working this way.

I think I am not the first one having this problem. Could anyone explain me how to set this up step by step. Please consider that I am new to this topic.

Thanks in advance Chris

Community
  • 1
  • 1
chris4jahn
  • 21
  • 2
  • 1
    Making a commit every time you save a file is _not_ a good idea. With configuration files, it is desirable to have each commit represent a _correctly working_ state of the configuration. So in general, you want to commit changes only _after_ you've tested them. Additionally, it is customary to give every commit an sensible commit message that summarizes the changes. – Roland Smith Mar 23 '14 at 20:58

1 Answers1

1

In the long run, I think you will be more productive if you get fugitive to work. I find :Gdiff invaluable.

I do not like the style of long one-liners in vim. If I want to get dizzy, I will stare at regular expressions. I would break up your autocommand as follows to make it easier to understand and update:

autocmd BufWritePost * call AutoGitCommit()

function! AutoGitCommit()
  let message = input('Message? ', 'Auto-commit: saved ' . expand('%'))
  let message = shellescape(message, 1)
  let shellcommand = 'if git rev-parse --git-dir > /dev/null 2>&1 ; '
  let shellcommand .= 'then git add ' . expand('%:p') . ' ; '
  let shellcommand .= 'git commit -m ' . message . '; '
  let shellcommand .= 'fi > /dev/null 2>&1'
  execute ':silent!' shellcommand
endfun

Since the file name is now in a String context, % will no longer be expanded automatically, so I use escape(). I added the :p modifier since you asked for the absolute path. See :help expand() and :help filename-modifiers.

In fact, my preference would be to let vim handle the conditional, like so:

function! AutoGitCommit()
  call system('git rev-parse --git-dir > /dev/null 2>&1')
  if v:shell_error
    return
  endif
  let message = input('Message? ', 'Auto-commit: saved ' . expand('%'))
  call system('git add ' . expand('%:p'))
  call system('git commit -m ' . shellescape(message, 1))
endfun
benjifisher
  • 5,054
  • 16
  • 18
  • Thank you. Could you explain why in my case fugitive would be the better way? Tomorow I will have a try with both ways and let you know about my results. – chris4jahn Mar 23 '14 at 18:07
  • I do not know that fugitive will help with this particular task. I do know that fugitive is well thought out. It is better to take advantage of the work Tim Pope has already put into it than to start from scratch yourself. – benjifisher Mar 23 '14 at 19:22