2019-07-11 18:24:58 +02:00
|
|
|
|
;;; git-auto-commit-mode.el --- Emacs Minor mode to automatically commit and push -*- lexical-binding: t -*-
|
2012-01-10 21:44:05 +01:00
|
|
|
|
|
2015-04-04 16:49:15 +02:00
|
|
|
|
;; Copyright (C) 2012, 2013, 2014, 2015 Tom Willemse <tom@ryuslash.org>
|
2012-01-10 21:44:05 +01:00
|
|
|
|
|
2013-10-07 23:59:05 +02:00
|
|
|
|
;; Author: Tom Willemse <tom@ryuslash.org>
|
2012-01-10 21:44:05 +01:00
|
|
|
|
;; Created: Jan 9, 2012
|
2020-08-28 08:53:52 +02:00
|
|
|
|
;; Version: 4.7.0
|
2012-05-20 14:14:59 +02:00
|
|
|
|
;; Keywords: vc
|
2020-03-22 21:07:48 +01:00
|
|
|
|
;; URL: https://github.com/ryuslash/git-auto-commit-mode
|
2012-01-10 21:44:05 +01:00
|
|
|
|
|
|
|
|
|
;; 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
|
|
|
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; This file is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2012-05-20 14:14:59 +02:00
|
|
|
|
;; along with this file; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; git-auto-commit-mode is an Emacs minor mode that tries to commit
|
|
|
|
|
;; changes to a file after every save.
|
|
|
|
|
|
2012-06-23 11:45:24 +02:00
|
|
|
|
;; When `gac-automatically-push-p' is non-nil, it also tries to push
|
|
|
|
|
;; to the current upstream.
|
2012-05-20 15:01:34 +02:00
|
|
|
|
|
2018-06-23 06:57:16 +02:00
|
|
|
|
;; When `gac-debounce-interval' is non-nil and set to a number
|
|
|
|
|
;; representing seconds, it will only perform Git actions at that
|
|
|
|
|
;; interval. That way, repeatedly saving a file will not hammer the
|
|
|
|
|
;; Git repository.
|
|
|
|
|
|
2012-01-10 21:44:05 +01:00
|
|
|
|
;;; Code:
|
|
|
|
|
|
2020-08-01 09:46:42 +02:00
|
|
|
|
(require 'subr-x)
|
|
|
|
|
|
2013-04-09 02:29:00 +02:00
|
|
|
|
(defgroup git-auto-commit-mode nil
|
|
|
|
|
"Customization options for `git-auto-commit-mode'."
|
|
|
|
|
:group 'external)
|
|
|
|
|
|
|
|
|
|
(defcustom gac-automatically-push-p nil
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Automatically push after each commit.
|
|
|
|
|
|
|
|
|
|
If non-nil a git push will be executed after each commit."
|
2013-04-09 02:29:00 +02:00
|
|
|
|
:tag "Automatically push"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:risky t)
|
2012-06-23 11:41:27 +02:00
|
|
|
|
(make-variable-buffer-local 'gac-automatically-push-p)
|
2012-05-20 15:01:34 +02:00
|
|
|
|
|
2019-12-11 06:39:06 +01:00
|
|
|
|
(defcustom gac-automatically-add-new-files-p t
|
|
|
|
|
"Should new (untracked) files automatically be committed to the repo?"
|
|
|
|
|
:tag "Automatically add new files"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'boolean)
|
|
|
|
|
|
2014-12-29 20:33:50 +01:00
|
|
|
|
(defcustom gac-ask-for-summary-p nil
|
|
|
|
|
"Ask the user for a short summary each time a file is committed?"
|
|
|
|
|
:tag "Ask for a summary on each commit"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'boolean)
|
|
|
|
|
|
2016-12-29 17:17:29 +01:00
|
|
|
|
(defcustom gac-shell-and " && "
|
|
|
|
|
"How to join commands together in the shell. For fish shell,
|
|
|
|
|
you want to customise this to: \" ; and \" instead of the default."
|
|
|
|
|
:tag "Join shell commands"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'string)
|
|
|
|
|
|
2019-12-14 11:10:38 +01:00
|
|
|
|
(defcustom gac-add-additional-flag ""
|
|
|
|
|
"Flag to add to the git add command."
|
|
|
|
|
:tag "git add flag"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'string)
|
|
|
|
|
|
2020-07-05 16:23:45 +02:00
|
|
|
|
(defcustom gac-commit-additional-flag ""
|
|
|
|
|
"Flag to add to the git commit command."
|
|
|
|
|
:tag "git commit flag"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'string)
|
|
|
|
|
|
2020-07-05 16:28:19 +02:00
|
|
|
|
(defcustom gac-silent-message-p nil
|
|
|
|
|
"Should git output be output to the message area?"
|
|
|
|
|
:tag "Quiet message output"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type 'boolean)
|
|
|
|
|
|
2019-12-14 11:10:38 +01:00
|
|
|
|
|
2018-06-23 06:57:16 +02:00
|
|
|
|
(defcustom gac-debounce-interval nil
|
|
|
|
|
"Debounce automatic commits to avoid hammering Git.
|
|
|
|
|
|
2019-07-11 18:24:58 +02:00
|
|
|
|
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."
|
|
|
|
|
:tag "Debounce interval"
|
2018-06-23 06:57:16 +02:00
|
|
|
|
:group 'git-auto-commit-mode
|
2019-07-11 18:24:58 +02:00
|
|
|
|
:type '(choice (number :tag "Interval in seconds")
|
|
|
|
|
(const :tag "Off" nil)))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
(make-variable-buffer-local 'gac-debounce-interval)
|
|
|
|
|
|
2019-11-21 13:02:03 +01:00
|
|
|
|
(defcustom gac-default-message nil
|
|
|
|
|
"Default message for automatic commits.
|
|
|
|
|
|
|
|
|
|
It can be:
|
|
|
|
|
- nil to use the default FILENAME
|
|
|
|
|
- a string which is used
|
2019-11-21 13:08:30 +01:00
|
|
|
|
- a function returning a string, called with FILENAME as
|
2019-11-21 13:08:47 +01:00
|
|
|
|
argument, in which case the result is used as commit message
|
2019-11-21 13:02:03 +01:00
|
|
|
|
"
|
|
|
|
|
:tag "Default commit message"
|
|
|
|
|
:group 'git-auto-commit-mode
|
|
|
|
|
:type '(choice (string :tag "Commit message")
|
|
|
|
|
(const :tag "Default: FILENAME" nil)
|
|
|
|
|
(function :tag "Function")))
|
|
|
|
|
|
2012-05-20 14:18:03 +02:00
|
|
|
|
(defun gac-relative-file-name (filename)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Find the path to FILENAME relative to the git directory."
|
2012-01-10 21:32:50 +01:00
|
|
|
|
(let* ((git-dir
|
2020-08-01 09:46:42 +02:00
|
|
|
|
(string-trim-right
|
|
|
|
|
(shell-command-to-string "git rev-parse --show-toplevel"))))
|
|
|
|
|
(file-relative-name filename git-dir)))
|
2012-01-10 21:32:50 +01:00
|
|
|
|
|
2012-05-20 15:01:34 +02:00
|
|
|
|
(defun gac-password (proc string)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Ask the user for a password when necessary.
|
|
|
|
|
|
|
|
|
|
PROC is the process running git. STRING is the line that was
|
|
|
|
|
output by PROC."
|
2012-05-20 15:01:34 +02:00
|
|
|
|
(let (ask)
|
|
|
|
|
(cond
|
|
|
|
|
((or
|
|
|
|
|
(string-match "^Enter passphrase for key '\\\(.*\\\)': $" string)
|
|
|
|
|
(string-match "^\\\(.*\\\)'s password:" string))
|
|
|
|
|
(setq ask (format "Password for '%s': " (match-string 1 string))))
|
|
|
|
|
((string-match "^[pP]assword:" string)
|
|
|
|
|
(setq ask "Password:")))
|
|
|
|
|
|
|
|
|
|
(when ask
|
|
|
|
|
(process-send-string proc (concat (read-passwd ask nil) "\n")))))
|
|
|
|
|
|
|
|
|
|
(defun gac-process-filter (proc string)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Check if PROC is asking for a password and promps the user if so.
|
|
|
|
|
|
|
|
|
|
STRING is the output line from PROC."
|
2012-05-20 15:01:34 +02:00
|
|
|
|
(save-current-buffer
|
|
|
|
|
(set-buffer (process-buffer proc))
|
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
|
(gac-password proc string))))
|
|
|
|
|
|
2012-05-20 15:17:21 +02:00
|
|
|
|
(defun gac-process-sentinel (proc status)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Report PROC change to STATUS."
|
2012-05-20 15:17:21 +02:00
|
|
|
|
(message "git %s" (substring status 0 -1)))
|
|
|
|
|
|
2014-12-29 20:33:50 +01:00
|
|
|
|
(defun gac--commit-msg (filename)
|
|
|
|
|
"Get a commit message.
|
|
|
|
|
|
|
|
|
|
Default to FILENAME."
|
|
|
|
|
(let ((relative-filename (gac-relative-file-name filename)))
|
|
|
|
|
(if (not gac-ask-for-summary-p)
|
2019-11-21 13:04:33 +01:00
|
|
|
|
(if gac-default-message
|
|
|
|
|
(if (functionp gac-default-message)
|
2019-11-21 13:07:48 +01:00
|
|
|
|
(funcall gac-default-message filename)
|
2019-11-21 13:04:33 +01:00
|
|
|
|
gac-default-message)
|
|
|
|
|
relative-filename)
|
2019-11-21 13:02:03 +01:00
|
|
|
|
(or gac-default-message relative-filename)
|
2014-12-29 20:33:50 +01:00
|
|
|
|
(read-string "Summary: " nil nil relative-filename))))
|
|
|
|
|
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(defun gac-commit (buffer)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Commit the current buffer's file to git."
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(let* ((buffer-file (buffer-file-name buffer))
|
|
|
|
|
(filename (convert-standard-filename
|
|
|
|
|
(file-name-nondirectory buffer-file)))
|
|
|
|
|
(commit-msg (gac--commit-msg buffer-file))
|
|
|
|
|
(default-directory (file-name-directory buffer-file)))
|
2020-07-05 16:28:19 +02:00
|
|
|
|
(funcall (if gac-silent-message-p
|
|
|
|
|
#'call-process-shell-command
|
|
|
|
|
#'shell-command)
|
2019-12-14 11:10:38 +01:00
|
|
|
|
(concat "git add " gac-add-additional-flag " " (shell-quote-argument filename)
|
2019-07-11 18:24:58 +02:00
|
|
|
|
gac-shell-and
|
2020-07-05 16:23:45 +02:00
|
|
|
|
"git commit -m " (shell-quote-argument commit-msg)
|
|
|
|
|
" " gac-commit-additional-flag))))
|
2019-07-11 18:24:58 +02:00
|
|
|
|
|
|
|
|
|
(defun gac-push (buffer)
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Push commits to the current upstream.
|
|
|
|
|
|
|
|
|
|
This doesn't check or ask for a remote, so the correct remote
|
|
|
|
|
should already have been set up."
|
2019-07-16 21:36:37 +02:00
|
|
|
|
;; gac-push is currently only called from gac--after-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-push
|
|
|
|
|
;; starts being used elsewhere.
|
|
|
|
|
(let ((default-directory (file-name-directory (buffer-file-name buffer))))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
(let ((proc (start-process "git" "*git-auto-push*" "git" "push")))
|
|
|
|
|
(set-process-sentinel proc 'gac-process-sentinel)
|
|
|
|
|
(set-process-filter proc 'gac-process-filter))))
|
|
|
|
|
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(defvar gac--debounce-timers (make-hash-table :test #'equal))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
|
|
|
|
|
(defun gac--debounced-save ()
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(let* ((actual-buffer (current-buffer))
|
|
|
|
|
(current-buffer-debounce-timer (gethash actual-buffer gac--debounce-timers)))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
(unless current-buffer-debounce-timer
|
|
|
|
|
(puthash actual-buffer
|
|
|
|
|
(run-at-time gac-debounce-interval nil
|
2019-07-11 18:24:58 +02:00
|
|
|
|
#'gac--after-save
|
2018-06-23 06:57:16 +02:00
|
|
|
|
actual-buffer)
|
|
|
|
|
gac--debounce-timers))))
|
|
|
|
|
|
2019-12-11 06:18:17 +01:00
|
|
|
|
(defun gac--buffer-is-tracked (buffer)
|
|
|
|
|
"Check to see if BUFFER’s file is tracked in git."
|
|
|
|
|
(let ((file-name (convert-standard-filename
|
|
|
|
|
(file-name-nondirectory
|
|
|
|
|
(buffer-file-name buffer)))))
|
|
|
|
|
(not (string=
|
|
|
|
|
(shell-command-to-string (concat "git ls-files " file-name))
|
|
|
|
|
""))))
|
|
|
|
|
|
2019-10-08 06:29:12 +02:00
|
|
|
|
(defun gac--buffer-has-changes (buffer)
|
|
|
|
|
"Check to see if there is any change in BUFFER."
|
|
|
|
|
(let ((file-name (convert-standard-filename
|
|
|
|
|
(file-name-nondirectory
|
|
|
|
|
(buffer-file-name buffer)))))
|
|
|
|
|
(not (string=
|
|
|
|
|
(shell-command-to-string (concat "git diff " file-name))
|
|
|
|
|
""))))
|
|
|
|
|
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(defun gac--after-save (buffer)
|
|
|
|
|
(unwind-protect
|
2019-10-08 06:29:12 +02:00
|
|
|
|
(when (and (buffer-live-p buffer)
|
2019-12-11 06:39:06 +01:00
|
|
|
|
(or (and gac-automatically-add-new-files-p
|
|
|
|
|
(not (gac--buffer-is-tracked buffer)))
|
2019-12-11 06:18:17 +01:00
|
|
|
|
(gac--buffer-has-changes buffer)))
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(gac-commit buffer)
|
2019-07-16 21:36:37 +02:00
|
|
|
|
(with-current-buffer buffer
|
|
|
|
|
;; with-current-buffer required here because gac-automatically-push-p
|
|
|
|
|
;; is buffer-local
|
|
|
|
|
(when gac-automatically-push-p
|
|
|
|
|
(gac-push buffer))))
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(remhash buffer gac--debounce-timers)))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
|
|
|
|
|
(defun gac-kill-buffer-hook ()
|
|
|
|
|
(when (and gac-debounce-interval
|
|
|
|
|
gac--debounce-timers
|
|
|
|
|
(gethash (current-buffer) gac--debounce-timers))
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(gac--after-save (current-buffer))))
|
2018-06-23 06:57:16 +02:00
|
|
|
|
|
|
|
|
|
(add-hook 'kill-buffer-hook #'gac-kill-buffer-hook)
|
2012-05-20 15:01:34 +02:00
|
|
|
|
|
|
|
|
|
(defun gac-after-save-func ()
|
2013-10-07 23:59:21 +02:00
|
|
|
|
"Commit the current file.
|
|
|
|
|
|
|
|
|
|
When `gac-automatically-push-p' is non-nil also push."
|
2018-06-23 06:57:16 +02:00
|
|
|
|
(if gac-debounce-interval
|
|
|
|
|
(gac--debounced-save)
|
2019-07-11 18:24:58 +02:00
|
|
|
|
(gac--after-save (current-buffer))))
|
2012-05-20 15:01:34 +02:00
|
|
|
|
|
2012-05-27 11:51:11 +02:00
|
|
|
|
;;;###autoload
|
2012-01-09 23:58:28 +01:00
|
|
|
|
(define-minor-mode git-auto-commit-mode
|
2012-05-20 14:15:46 +02:00
|
|
|
|
"Automatically commit any changes made when saving with this
|
2012-05-20 15:01:34 +02:00
|
|
|
|
mode turned on and optionally push them too."
|
2012-01-10 00:08:10 +01:00
|
|
|
|
:lighter " ga"
|
2012-01-09 23:58:28 +01:00
|
|
|
|
(if git-auto-commit-mode
|
2012-05-20 15:01:34 +02:00
|
|
|
|
(add-hook 'after-save-hook 'gac-after-save-func t t)
|
|
|
|
|
(remove-hook 'after-save-hook 'gac-after-save-func t)))
|
2012-05-20 14:14:59 +02:00
|
|
|
|
|
2013-10-07 21:03:19 +02:00
|
|
|
|
(provide 'git-auto-commit-mode)
|
|
|
|
|
|
2012-05-20 14:14:59 +02:00
|
|
|
|
;;; git-auto-commit-mode.el ends here
|