2012-05-20 15:01:34 +02:00
|
|
|
;;; git-auto-commit-mode.el --- Emacs Minor mode to automatically commit and push
|
2012-01-10 21:44:05 +01:00
|
|
|
|
|
|
|
;; Copyright (C) 2012 Tom Willemsen <tom@ryuslash.org>
|
|
|
|
|
2012-05-20 14:14:59 +02:00
|
|
|
;; Author: Tom Willemsen <tom@ryuslash.org>
|
2012-01-10 21:44:05 +01:00
|
|
|
;; Created: Jan 9, 2012
|
2012-05-20 15:17:21 +02:00
|
|
|
;; Version: 3
|
2012-05-20 14:14:59 +02:00
|
|
|
;; Keywords: vc
|
|
|
|
;; URL: http://ryuslash.org/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-05-20 15:01:34 +02:00
|
|
|
;; When `git-automatically-push' is non-nil, it also tries to push to
|
|
|
|
;; the current upstream.
|
|
|
|
|
2012-05-20 14:14:59 +02:00
|
|
|
;;; Change Log:
|
|
|
|
|
|
|
|
;; 1 - Initial release.
|
2012-01-10 21:44:05 +01:00
|
|
|
|
2012-05-20 15:01:34 +02:00
|
|
|
;; 2 - Add ability to automatically push.
|
|
|
|
|
2012-05-20 15:17:21 +02:00
|
|
|
;; 3 - Shows the status when push finishes.
|
|
|
|
|
2012-01-10 21:44:05 +01:00
|
|
|
;;; Code:
|
|
|
|
|
2012-05-20 15:01:34 +02:00
|
|
|
(defvar gac-automatically-push nil
|
|
|
|
"Control whether or not `git-auto-commit-mode' should also
|
|
|
|
automatically push the changes.")
|
|
|
|
|
2012-05-20 14:18:03 +02:00
|
|
|
(defun gac-relative-file-name (filename)
|
2012-01-10 21:32:50 +01:00
|
|
|
"Find the path to the filename relative to the git directory"
|
|
|
|
(let* ((git-dir
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\n+$" "" (shell-command-to-string
|
|
|
|
"git rev-parse --show-toplevel")))
|
|
|
|
(relative-file-name
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"^/" "" (replace-regexp-in-string
|
|
|
|
git-dir "" filename))))
|
|
|
|
relative-file-name))
|
|
|
|
|
2012-05-20 15:01:34 +02:00
|
|
|
(defun gac-password (proc string)
|
|
|
|
"Ask the user for a password when necessary."
|
|
|
|
(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)
|
|
|
|
"Checks if the process is asking for a password and asks the
|
|
|
|
user for one when it does."
|
|
|
|
(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)
|
|
|
|
"Report the process' status change."
|
|
|
|
(message "git %s" (substring status 0 -1)))
|
|
|
|
|
2012-05-20 14:18:03 +02:00
|
|
|
(defun gac-commit ()
|
2012-05-20 14:15:46 +02:00
|
|
|
"Commit `buffer-file-name' to git"
|
2012-01-10 21:32:50 +01:00
|
|
|
(let* ((filename (buffer-file-name))
|
|
|
|
(relative-filename
|
2012-05-20 14:18:03 +02:00
|
|
|
(gac-relative-file-name filename)))
|
2012-01-10 21:32:50 +01:00
|
|
|
(shell-command
|
|
|
|
(concat "git add " filename
|
|
|
|
" && git commit -m '" relative-filename "'"))))
|
2012-01-09 23:58:28 +01:00
|
|
|
|
2012-05-20 15:01:34 +02:00
|
|
|
(defun gac-push ()
|
|
|
|
"Push changes to the repository to the current upstream. This
|
|
|
|
doesn't check or ask for a remote, so the correct remote should
|
|
|
|
already have been set up."
|
|
|
|
(let ((proc (start-process "git" "*git-auto-push*" "git" "push")))
|
2012-05-20 15:17:21 +02:00
|
|
|
(set-process-sentinel proc 'gac-process-sentinel)
|
2012-05-20 15:01:34 +02:00
|
|
|
(set-process-filter proc 'gac-process-filter)))
|
|
|
|
|
|
|
|
(defun gac-after-save-func ()
|
|
|
|
"Commit the changes to the current file, and when
|
|
|
|
`gac-automatically-push' is not `nil', push."
|
|
|
|
(gac-commit)
|
2012-05-20 23:02:35 +02:00
|
|
|
(when gac-automatically-push
|
2012-05-20 15:01:34 +02:00
|
|
|
(gac-push)))
|
|
|
|
|
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
|
|
|
|
|
|
|
;;; git-auto-commit-mode.el ends here
|