@matheusbsbs / matheus-bagliano / commits / d55552f6e3
Fork 0

git.el: Add support for commit hooks.

Run the pre-commit and post-commit hooks at appropriate places, and display their output if any. Signed-off-by: Alexandre Julliard <julliard@winehq.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Alexandre Julliard committed Mar 17, 2007 at 20:40 UTC d55552f6e3b63ab6f33dd61071760bee42b9bc5e
1 file changed +57 -24
contrib/emacs/git.el
+57 -24
@@ -1,6 +1,6 @@
1 ;;; git.el --- A user interface for git
2
3 -;; Copyright (C) 2005, 2006 Alexandre Julliard <julliard@winehq.org>
3 +;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org>
4
5 ;; Version: 1.0
6
@@ -213,6 +213,23 @@ and returns the process output as a string."
213 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))
214 (message "Running git %s...done" (car args)))
215
216 +(defun git-run-hook (hook env &rest args)
217 + "Run a git hook and display its output if any."
218 + (let ((dir default-directory)
219 + (hook-name (expand-file-name (concat ".git/hooks/" hook))))
220 + (or (not (file-executable-p hook-name))
221 + (let (status (buffer (get-buffer-create "*Git Hook Output*")))
222 + (with-current-buffer buffer
223 + (erase-buffer)
224 + (cd dir)
225 + (setq status
226 + (if env
227 + (apply #'call-process "env" nil (list buffer t) nil
228 + (append (git-get-env-strings env) (list hook-name) args))
229 + (apply #'call-process hook-name nil (list buffer t) nil args))))
230 + (display-message-or-buffer buffer)
231 + (eq 0 status)))))
232 +
233 (defun git-get-string-sha1 (string)
234 "Read a SHA1 from the specified string."
235 (and string
@@ -590,6 +607,20 @@ and returns the process output as a string."
607 (when modified
608 (apply #'git-run-command nil env "update-index" "--" (git-get-filenames modified)))))
609
610 +(defun git-run-pre-commit-hook ()
611 + "Run the pre-commit hook if any."
612 + (unless git-status (error "Not in git-status buffer."))
613 + (let ((files (git-marked-files-state 'added 'deleted 'modified)))
614 + (or (not files)
615 + (not (file-executable-p ".git/hooks/pre-commit"))
616 + (let ((index-file (make-temp-file "gitidx")))
617 + (unwind-protect
618 + (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
619 + (git-read-tree head-tree index-file)
620 + (git-update-index index-file files)
621 + (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" . ,index-file))))
622 + (delete-file index-file))))))
623 +
624 (defun git-do-commit ()
625 "Perform the actual commit using the current buffer as log message."
626 (interactive)
@@ -622,7 +653,8 @@ and returns the process output as a string."
653 (git-run-command nil nil "rerere"))
654 (git-refresh-files)
655 (git-refresh-ewoc-hf git-status)
625 - (message "Committed %s." commit))
656 + (message "Committed %s." commit)
657 + (git-run-hook "post-commit" nil))
658 (message "Commit aborted."))))
659 (message "No files to commit.")))
660 (delete-file index-file))))))
@@ -944,28 +976,29 @@ and returns the process output as a string."
976 "Commit the marked file(s), asking for a commit message."
977 (interactive)
978 (unless git-status (error "Not in git-status buffer."))
947 - (let ((buffer (get-buffer-create "*git-commit*"))
948 - (coding-system (git-get-commits-coding-system))
949 - author-name author-email subject date)
950 - (when (eq 0 (buffer-size buffer))
951 - (when (file-readable-p ".dotest/info")
952 - (with-temp-buffer
953 - (insert-file-contents ".dotest/info")
954 - (goto-char (point-min))
955 - (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t)
956 - (setq author-name (match-string 1))
957 - (setq author-email (match-string 2)))
958 - (goto-char (point-min))
959 - (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
960 - (setq subject (match-string 1)))
961 - (goto-char (point-min))
962 - (when (re-search-forward "^Date: \\(.*\\)$" nil t)
963 - (setq date (match-string 1)))))
964 - (git-setup-log-buffer buffer author-name author-email subject date))
965 - (log-edit #'git-do-commit nil #'git-log-edit-files buffer)
966 - (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))
967 - (setq buffer-file-coding-system coding-system)
968 - (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t)))
979 + (when (git-run-pre-commit-hook)
980 + (let ((buffer (get-buffer-create "*git-commit*"))
981 + (coding-system (git-get-commits-coding-system))
982 + author-name author-email subject date)
983 + (when (eq 0 (buffer-size buffer))
984 + (when (file-readable-p ".dotest/info")
985 + (with-temp-buffer
986 + (insert-file-contents ".dotest/info")
987 + (goto-char (point-min))
988 + (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t)
989 + (setq author-name (match-string 1))
990 + (setq author-email (match-string 2)))
991 + (goto-char (point-min))
992 + (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
993 + (setq subject (match-string 1)))
994 + (goto-char (point-min))
995 + (when (re-search-forward "^Date: \\(.*\\)$" nil t)
996 + (setq date (match-string 1)))))
997 + (git-setup-log-buffer buffer author-name author-email subject date))
998 + (log-edit #'git-do-commit nil #'git-log-edit-files buffer)
999 + (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))
1000 + (setq buffer-file-coding-system coding-system)
1001 + (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t))))
1002
1003 (defun git-find-file ()
1004 "Visit the current file in its own buffer."