This commit is contained in:
Kyle I S Harrington 2022-12-28 09:26:24 +03:00 committed by GitHub
commit d231eb1216
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 10 deletions

View file

@ -3,7 +3,7 @@
* NAME * NAME
git-auto-commit-mode - Emacs minor mode to automatically commit (and git-auto-commit-mode - Emacs minor mode to automatically commit (and pull and
push) a git repository. push) a git repository.
[[http://melpa.org/#/git-auto-commit-mode][file:http://melpa.org/packages/git-auto-commit-mode-badge.svg]] [[http://melpa.org/#/git-auto-commit-mode][file:http://melpa.org/packages/git-auto-commit-mode-badge.svg]]
@ -26,6 +26,8 @@
When enabled, git-auto-commit-mode uses the =after-save-hook= to When enabled, git-auto-commit-mode uses the =after-save-hook= to
commit changes to git each time. If =gac-automatically-push-p= is commit changes to git each time. If =gac-automatically-push-p= is
non-nil it also tries to push the ~HEAD~ to the current upstream. non-nil it also tries to push the ~HEAD~ to the current upstream.
If =gac-automatically-pull-p= is
non-nil it also tries to pull the ~HEAD~ to the current upstream.
Making sure that upstream is properly set is the responsibility of Making sure that upstream is properly set is the responsibility of
the user. the user.
@ -82,6 +84,11 @@
push the git repository's ~HEAD~ to its default upstream. Setting up the push the git repository's ~HEAD~ to its default upstream. Setting up the
upstream is the user's responsibility. upstream is the user's responsibility.
- =gac-automatically-pull-p= ::
A boolean value indicating whether or not git-auto-commit-mode should try to
pull the git repository's ~HEAD~ from its default upstream. Setting up the
upstream is the user's responsibility.
- =gac-automatically-add-new-files-p= :: - =gac-automatically-add-new-files-p= ::
A boolean value indicating whether or not git-auto-commit-mode should add A boolean value indicating whether or not git-auto-commit-mode should add
new (untracked) files to the repository. new (untracked) files to the repository.

View file

@ -8,6 +8,9 @@
;; Keywords: vc ;; Keywords: vc
;; URL: https://github.com/ryuslash/git-auto-commit-mode ;; 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 ;; This file is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License ;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3 ;; as published by the Free Software Foundation; either version 3
@ -29,6 +32,9 @@
;; When `gac-automatically-push-p' is non-nil, it also tries to push ;; When `gac-automatically-push-p' is non-nil, it also tries to push
;; to the current upstream. ;; to the current upstream.
;; When `gac-automatically-pull-p' is non-nil, it also tries to pull
;; from the current upstream.
;; When `gac-debounce-interval' is non-nil and set to a number ;; When `gac-debounce-interval' is non-nil and set to a number
;; representing seconds, it will only perform Git actions at that ;; representing seconds, it will only perform Git actions at that
;; interval. That way, repeatedly saving a file will not hammer the ;; interval. That way, repeatedly saving a file will not hammer the
@ -52,6 +58,16 @@ If non-nil a git push will be executed after each commit."
:risky t) :risky t)
(make-variable-buffer-local 'gac-automatically-push-p) (make-variable-buffer-local 'gac-automatically-push-p)
(defcustom gac-automatically-pull-p nil
"Automatically pull before each commit.
If non-nil a git pull will be executed before each commit."
:tag "Automatically pull"
:group 'git-auto-commit-mode
:type 'boolean
:risky t)
(make-variable-buffer-local 'gac-automatically-pull-p)
(defcustom gac-automatically-add-new-files-p t (defcustom gac-automatically-add-new-files-p t
"Should new (untracked) files automatically be committed to the repo?" "Should new (untracked) files automatically be committed to the repo?"
:tag "Automatically add new files" :tag "Automatically add new files"
@ -193,9 +209,21 @@ should already have been set up."
;; default-directory. The explicit binding here is defensive, in case gac-push ;; default-directory. The explicit binding here is defensive, in case gac-push
;; starts being used elsewhere. ;; starts being used elsewhere.
(let ((default-directory (file-name-directory (buffer-file-name buffer)))) (let ((default-directory (file-name-directory (buffer-file-name buffer))))
(let ((proc (start-process "git" "*git-auto-push*" "git" "push"))) (call-process "git" nil "*git-auto-push*" nil "push")))
(set-process-sentinel proc 'gac-process-sentinel)
(set-process-filter proc 'gac-process-filter)))) (defun gac-pull (buffer)
"Pull commits from the current upstream.
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
;; 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.
(let ((default-directory (file-name-directory (buffer-file-name buffer))))
(call-process "git" nil "*git-auto-pull*" nil "merge" "--squash")
(call-process "git" nil "*git-auto-pull*" nil "commit" "--no-edit")
(call-process "git" nil "*git-auto-pull*" nil "pull" "--rebase")))
(defvar gac--debounce-timers (make-hash-table :test #'equal)) (defvar gac--debounce-timers (make-hash-table :test #'equal))
@ -234,11 +262,17 @@ should already have been set up."
(not (gac--buffer-is-tracked buffer))) (not (gac--buffer-is-tracked buffer)))
(gac--buffer-has-changes buffer))) (gac--buffer-has-changes buffer)))
(gac-commit buffer) (gac-commit buffer)
(make-thread
(lambda ()
(with-current-buffer 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))
;; with-current-buffer required here because gac-automatically-push-p ;; with-current-buffer required here because gac-automatically-push-p
;; is buffer-local ;; is buffer-local
(when gac-automatically-push-p (when gac-automatically-push-p
(gac-push buffer)))) (gac-push buffer))))))
(remhash buffer gac--debounce-timers))) (remhash buffer gac--debounce-timers)))
(defun gac-kill-buffer-hook () (defun gac-kill-buffer-hook ()
@ -260,7 +294,7 @@ When `gac-automatically-push-p' is non-nil also push."
;;;###autoload ;;;###autoload
(define-minor-mode git-auto-commit-mode (define-minor-mode git-auto-commit-mode
"Automatically commit any changes made when saving with this "Automatically commit any changes made when saving with this
mode turned on and optionally push them too." mode turned on, optionally pull and push them too."
:lighter " ga" :lighter " ga"
(if git-auto-commit-mode (if git-auto-commit-mode
(add-hook 'after-save-hook 'gac-after-save-func t t) (add-hook 'after-save-hook 'gac-after-save-func t t)