Setup auto pull to run in a before-save-hook

This commit is contained in:
Kyle Harrington 2021-09-22 11:12:22 +02:00
parent 6fc4b1e994
commit cc78719844

View file

@ -8,6 +8,9 @@
;; Keywords: vc
;; URL: https://github.com/ryuslash/git-auto-commit-mode
;; Updates by: Kyle Harrington <emacs@kyleharrington.com>
;; Updated: Sept 22, 2021
;; This file is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
@ -215,7 +218,7 @@ should already have been set up."
This doesn't check or ask for a remote, so the correct remote
should already have been set up."
;; gac-pull is currently only called from gac--after-save, where it is wrapped
;; gac-pull is currently only called from gac--before-save, where it is wrapped
;; in with-current-buffer, which should already take care of
;; default-directory. The explicit binding here is defensive, in case gac-pull
;; starts being used elsewhere.
@ -261,11 +264,6 @@ should already have been set up."
(or (and gac-automatically-add-new-files-p
(not (gac--buffer-is-tracked buffer)))
(gac--buffer-has-changes buffer)))
(with-current-buffer buffer
;; with-current-buffer required here because gac-automatically-pull-p
;; is buffer-local
(when gac-automatically-pull-p
(gac-pull buffer)))
(gac-commit buffer)
(with-current-buffer buffer
;; with-current-buffer required here because gac-automatically-push-p
@ -274,10 +272,24 @@ should already have been set up."
(gac-push buffer))))
(remhash buffer gac--debounce-timers)))
(defun gac--before-save (buffer)
(unwind-protect
(when (and (buffer-live-p buffer)
(or (and gac-automatically-add-new-files-p
(not (gac--buffer-is-tracked buffer)))
(gac--buffer-has-changes buffer)))
(with-current-buffer buffer
;; with-current-buffer required here because gac-automatically-pull-p
;; is buffer-local
(when gac-automatically-pull-p
(gac-pull buffer))))
(remhash buffer gac--debounce-timers)))
(defun gac-kill-buffer-hook ()
(when (and gac-debounce-interval
gac--debounce-timers
(gethash (current-buffer) gac--debounce-timers))
(gac--before-save (current-buffer))
(gac--after-save (current-buffer))))
(add-hook 'kill-buffer-hook #'gac-kill-buffer-hook)
@ -285,21 +297,29 @@ should already have been set up."
(defun gac-after-save-func ()
"Commit the current file.
When `gac-automatically-pull-p' is non-nil also pull.
When `gac-automatically-push-p' is non-nil also push."
(if gac-debounce-interval
(gac--debounced-save)
(gac--after-save (current-buffer))))
(defun gac-before-save-func ()
"Commit the current file.
When `gac-automatically-pull-p' is non-nil also pull."
(gac--before-save (current-buffer)))
;;;###autoload
(define-minor-mode git-auto-commit-mode
"Automatically commit any changes made when saving with this
mode turned on, optionally pull and push them too."
:lighter " ga"
(if git-auto-commit-mode
(add-hook 'after-save-hook 'gac-after-save-func t t)
(remove-hook 'after-save-hook 'gac-after-save-func t)))
(progn
(add-hook 'before-save-hook 'gac-before-save-func t t)
(add-hook 'after-save-hook 'gac-after-save-func t t))
(progn
(remove-hook 'before-save-hook 'gac-before-save-func t)
(remove-hook 'after-save-hook 'gac-after-save-func t))))
(provide 'git-auto-commit-mode)