-2

I have also posted this question here on Reddit

I'm not a version control or Git expert and it is very cumbersome for me to do all the steps manually. I want to have a text editor which upon closing the file if any changes were applied, runs these commands:

  • checks if the file has been edited
  • git add <filename>
  • opens a popup or tab to edit the commit message
  • git commit -m "the commit message entered in the popup"

I would appreciate it if you could help me know if there is such an editor to have this feature built-in or via a plugin/addon. Or if it is possible to write such a plugin.

P.S.1. It doesn't really have to be git, mercurial or other versions control systems are also fine.

P.S.2. I think this should be doable in Vim script and/or emacs lisp, If I knew those languages.

P.S.3. here and here different ideas have been discussed. It is apparently possible to git commit upon closing, but I want to write the message manually.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 1
    [vim-fugitive](http://vimcasts.org/episodes/fugitive-vim---a-complement-to-command-line-git/) – phd Jul 05 '18 at 15:24
  • 1
    If Michal's answer helps you solve the problem, you can mark the answer by clicking √ symbol on the left of the answer? And it will also benefit other members who meet similar questions. – Marina Liu Jul 16 '18 at 09:02

1 Answers1

5

It should definitely be doable in any reasonably programmable editor.

Here, for example, is a definitely very buggy and incomplete first stab at a hook for Emacs + magit:

(defun my/auto-commit-on-kill ()
  (when buffer-file-name
    (let* ((current (magit-file-relative-name))
           (choices (nconc (magit-modified-files) (magit-untracked-files)))
           (to-stage (car (member current choices))))
      (when to-stage
        (magit-stage-file to-stage)
        (magit-commit)))))

(add-hook 'kill-buffer-hook 'my/auto-commit-on-kill)

But actually if you were to go the Emacs/magit route I would strongly urge you to just use magit "manually". It's really not that complicated and yet it would teach you way more about git than relying on a even more simplified interface.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41
  • I haven't used emacs for a long long time now. would you be so kind to explain where this code should go? – Foad S. Farimani Jul 05 '18 at 10:55
  • 1
    If you really wanted to test it, your Emacs initialization file - probably ~/.emacs.d/init.el - is the place for setting this up. You would also need to install magit. But I bet the current version of my example is not good enough for real use. I'm something of an Emacs user, but definitely not an elisp expert. – Michał Politowski Jul 05 '18 at 11:00