Code review fixes.

This commit is contained in:
Constantine Vetoshev 2019-07-11 09:24:58 -07:00
parent 074739d78f
commit 0ac5025766

View file

@ -1,4 +1,4 @@
;;; git-auto-commit-mode.el --- Emacs Minor mode to automatically commit and push ;;; git-auto-commit-mode.el --- Emacs Minor mode to automatically commit and push -*- lexical-binding: t -*-
;; Copyright (C) 2012, 2013, 2014, 2015 Tom Willemse <tom@ryuslash.org> ;; Copyright (C) 2012, 2013, 2014, 2015 Tom Willemse <tom@ryuslash.org>
@ -36,8 +36,6 @@
;;; Code: ;;; Code:
(require 'cl)
(defgroup git-auto-commit-mode nil (defgroup git-auto-commit-mode nil
"Customization options for `git-auto-commit-mode'." "Customization options for `git-auto-commit-mode'."
:group 'external) :group 'external)
@ -68,10 +66,13 @@ If non-nil a git push will be executed after each commit."
(defcustom gac-debounce-interval nil (defcustom gac-debounce-interval nil
"Debounce automatic commits to avoid hammering Git. "Debounce automatic commits to avoid hammering Git.
If non-nil a commit will be scheduled to occur that many seconds in the future. Note that this uses Emacs timer functionality, and is subject to its limitations." If non-nil a commit will be scheduled to occur that many seconds
:tag "Debounce to only make one commit this many seconds." in the future. Note that this uses Emacs timer functionality, and
is subject to its limitations."
:tag "Debounce interval"
:group 'git-auto-commit-mode :group 'git-auto-commit-mode
:type 'fixnum) :type '(choice (number :tag "Interval in seconds")
(const :tag "Off" nil)))
(make-variable-buffer-local 'gac-debounce-interval) (make-variable-buffer-local 'gac-debounce-interval)
(defun gac-relative-file-name (filename) (defun gac-relative-file-name (filename)
@ -125,55 +126,53 @@ Default to FILENAME."
relative-filename relative-filename
(read-string "Summary: " nil nil relative-filename)))) (read-string "Summary: " nil nil relative-filename))))
(defun gac-commit (actual-buffer) (defun gac-commit (buffer)
"Commit the current buffer's file to git." "Commit the current buffer's file to git."
(with-current-buffer (or actual-buffer (current-buffer)) (let* ((buffer-file (buffer-file-name buffer))
(let* ((buffer-file (buffer-file-name)) (filename (convert-standard-filename
(filename (convert-standard-filename (file-name-nondirectory buffer-file)))
(file-name-nondirectory buffer-file))) (commit-msg (gac--commit-msg buffer-file))
(commit-msg (gac--commit-msg buffer-file)) (default-directory (file-name-directory buffer-file)))
(default-directory (file-name-directory buffer-file))) (shell-command
(shell-command (concat "git add " (shell-quote-argument filename)
(concat "git add " (shell-quote-argument filename) gac-shell-and
" && git commit -m " (shell-quote-argument commit-msg)))))) "git commit -m " (shell-quote-argument commit-msg)))))
(defun gac-push (actual-buffer) (defun gac-push (buffer)
"Push commits to the current upstream. "Push commits to the current upstream.
This doesn't check or ask for a remote, so the correct remote This doesn't check or ask for a remote, so the correct remote
should already have been set up." should already have been set up."
(with-current-buffer (or actual-buffer (current-buffer)) (with-current-buffer buffer
(let ((proc (start-process "git" "*git-auto-push*" "git" "push"))) (let ((proc (start-process "git" "*git-auto-push*" "git" "push")))
(set-process-sentinel proc 'gac-process-sentinel) (set-process-sentinel proc 'gac-process-sentinel)
(set-process-filter proc 'gac-process-filter)))) (set-process-filter proc 'gac-process-filter))))
(setq gac--debounce-timers (make-hash-table :test #'equal)) (defvar gac--debounce-timers (make-hash-table :test #'equal))
(defun gac--debounced-save () (defun gac--debounced-save ()
(lexical-let* ((actual-buffer (current-buffer)) (let* ((actual-buffer (current-buffer))
(current-buffer-debounce-timer (gethash actual-buffer gac--debounce-timers))) (current-buffer-debounce-timer (gethash actual-buffer gac--debounce-timers)))
(unless current-buffer-debounce-timer (unless current-buffer-debounce-timer
(puthash actual-buffer (puthash actual-buffer
(run-at-time gac-debounce-interval nil (run-at-time gac-debounce-interval nil
(lambda (actual-buffer) #'gac--after-save
(gac--after-save actual-buffer))
actual-buffer) actual-buffer)
gac--debounce-timers)))) gac--debounce-timers))))
(defun gac--after-save (&optional actual-buffer) (defun gac--after-save (buffer)
(let ((actual-buffer (or actual-buffer (current-buffer)))) (unwind-protect
(unwind-protect (when (buffer-live-p buffer)
(when (buffer-live-p actual-buffer) (gac-commit buffer)
(gac-commit actual-buffer) (when gac-automatically-push-p
(when gac-automatically-push-p (gac-push buffer)))
(gac-push actual-buffer))) (remhash buffer gac--debounce-timers)))
(remhash actual-buffer gac--debounce-timers))))
(defun gac-kill-buffer-hook () (defun gac-kill-buffer-hook ()
(when (and gac-debounce-interval (when (and gac-debounce-interval
gac--debounce-timers gac--debounce-timers
(gethash (current-buffer) gac--debounce-timers)) (gethash (current-buffer) gac--debounce-timers))
(gac--after-save))) (gac--after-save (current-buffer))))
(add-hook 'kill-buffer-hook #'gac-kill-buffer-hook) (add-hook 'kill-buffer-hook #'gac-kill-buffer-hook)
@ -184,7 +183,7 @@ When `gac-automatically-push-p' is non-nil also push."
(if gac-debounce-interval (if gac-debounce-interval
(gac--debounced-save) (gac--debounced-save)
(gac--after-save))) (gac--after-save (current-buffer))))
;;;###autoload ;;;###autoload
(define-minor-mode git-auto-commit-mode (define-minor-mode git-auto-commit-mode