如何设置 emacs 以便每次保存打开的文件时或定期自动执行 git commit?
答案1
如果您想在每次保存时提交,您可以这样做:
(add-hook 'after-save-hook 'my-commit-on-save)
(defun my-commit-on-save ()
"commit the buffer"
...your-code-goes-here...)
你可能只需要使用
(defun my-commit-on-save ()
"commit the buffer"
(call-interactively 'vc-next-action))
但是,您需要添加一些检查以确保它是您想要提交的文件集的一部分,否则您保存的每个缓冲区都将添加到存储库中。
答案2
答案3
这是我找到的一点 lisp。不是每次保存时都执行提交/推送,而是将其安排在不久的将来,因此如果您保存了一堆小编辑,则不会得到一堆小提交。
https://gist.github.com/defunkt/449668
;; Automatically add, commit, and push when files change.
(defvar autocommit-dir-set '()
"Set of directories for which there is a pending timer job")
(defun autocommit-schedule-commit (dn)
"Schedule an autocommit (and push) if one is not already scheduled for the given dir."
(if (null (member dn autocommit-dir-set))
(progn
(run-with-idle-timer
10 nil
(lambda (dn)
(setq autocommit-dir-set (remove dn autocommit-dir-set))
(message (concat "Committing org files in " dn))
(shell-command (concat "cd " dn " && git commit -m 'Updated org files.'"))
(shell-command (concat "cd " dn " && git push & /usr/bin/true")))
dn)
(setq autocommit-dir-set (cons dn autocommit-dir-set)))))
(defun autocommit-after-save-hook ()
"After-save-hook to 'git add' the modified file and schedule a commit and push in the idle loop."
(let ((fn (buffer-file-name)))
(message "git adding %s" fn)
(shell-command (concat "git add " fn))
(autocommit-schedule-commit (file-name-directory fn))))
(defun autocommit-setup-save-hook ()
"Set up the autocommit save hook for the current file."
(interactive)
(message "Set up autocommit save hook for this buffer.")
(add-hook 'after-save-hook 'autocommit-after-save-hook nil t))
答案4
您还可以使用我的git 监控实用程序,特别是如果你是 Haskell 用户。