{"thread":{"id":"7286","subject":"[PATCH] git.el: Add support for commit hooks.","startedAt":"2007-03-17T19:40:12Z","lastAt":"2007-03-17T19:40:12Z","messageCount":1,"participants":["Alexandre Julliard"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"37326","messageId":"87mz2by7z7.fsf@wine.dyndns.org","threadId":"7286","inReplyTo":null,"subject":"[PATCH] git.el: Add support for commit hooks.","fromName":"Alexandre Julliard","fromEmail":"julliard@winehq.org","sentAt":"2007-03-17T19:40:12Z","receivedAt":"2007-03-17T19:40:12Z","isPatch":true,"sender":{"key":"julliard@winehq.org","avatar":null},"body":"Run the pre-commit and post-commit hooks at appropriate places, and\ndisplay their output if any.\n\nSigned-off-by: Alexandre Julliard <julliard@winehq.org>\n---\n contrib/emacs/git.el |   81 +++++++++++++++++++++++++++++++++++---------------\n 1 files changed, 57 insertions(+), 24 deletions(-)\n\ndiff --git a/contrib/emacs/git.el b/contrib/emacs/git.el\nindex db87a37..5f22dec 100644\n--- a/contrib/emacs/git.el\n+++ b/contrib/emacs/git.el\n@@ -1,6 +1,6 @@\n ;;; git.el --- A user interface for git\n \n-;; Copyright (C) 2005, 2006 Alexandre Julliard <julliard@winehq.org>\n+;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org>\n \n ;; Version: 1.0\n \n@@ -213,6 +213,23 @@ and returns the process output as a string.\"\n     (error \"Failed to run \\\"git %s\\\":\\n%s\" (mapconcat (lambda (x) x) args \" \") (buffer-string)))\n   (message \"Running git %s...done\" (car args)))\n \n+(defun git-run-hook (hook env &rest args)\n+  \"Run a git hook and display its output if any.\"\n+  (let ((dir default-directory)\n+        (hook-name (expand-file-name (concat \".git/hooks/\" hook))))\n+    (or (not (file-executable-p hook-name))\n+        (let (status (buffer (get-buffer-create \"*Git Hook Output*\")))\n+          (with-current-buffer buffer\n+            (erase-buffer)\n+            (cd dir)\n+            (setq status\n+                  (if env\n+                      (apply #'call-process \"env\" nil (list buffer t) nil\n+                             (append (git-get-env-strings env) (list hook-name) args))\n+                    (apply #'call-process hook-name nil (list buffer t) nil args))))\n+          (display-message-or-buffer buffer)\n+          (eq 0 status)))))\n+\n (defun git-get-string-sha1 (string)\n   \"Read a SHA1 from the specified string.\"\n   (and string\n@@ -590,6 +607,20 @@ and returns the process output as a string.\"\n     (when modified\n       (apply #'git-run-command nil env \"update-index\" \"--\" (git-get-filenames modified)))))\n \n+(defun git-run-pre-commit-hook ()\n+  \"Run the pre-commit hook if any.\"\n+  (unless git-status (error \"Not in git-status buffer.\"))\n+  (let ((files (git-marked-files-state 'added 'deleted 'modified)))\n+    (or (not files)\n+        (not (file-executable-p \".git/hooks/pre-commit\"))\n+        (let ((index-file (make-temp-file \"gitidx\")))\n+          (unwind-protect\n+            (let ((head-tree (unless (git-empty-db-p) (git-rev-parse \"HEAD^{tree}\"))))\n+              (git-read-tree head-tree index-file)\n+              (git-update-index index-file files)\n+              (git-run-hook \"pre-commit\" `((\"GIT_INDEX_FILE\" . ,index-file))))\n+          (delete-file index-file))))))\n+\n (defun git-do-commit ()\n   \"Perform the actual commit using the current buffer as log message.\"\n   (interactive)\n@@ -622,7 +653,8 @@ and returns the process output as a string.\"\n                               (git-run-command nil nil \"rerere\"))\n                             (git-refresh-files)\n                             (git-refresh-ewoc-hf git-status)\n-                            (message \"Committed %s.\" commit))\n+                            (message \"Committed %s.\" commit)\n+                            (git-run-hook \"post-commit\" nil))\n                         (message \"Commit aborted.\"))))\n                 (message \"No files to commit.\")))\n           (delete-file index-file))))))\n@@ -944,28 +976,29 @@ and returns the process output as a string.\"\n   \"Commit the marked file(s), asking for a commit message.\"\n   (interactive)\n   (unless git-status (error \"Not in git-status buffer.\"))\n-  (let ((buffer (get-buffer-create \"*git-commit*\"))\n-        (coding-system (git-get-commits-coding-system))\n-        author-name author-email subject date)\n-    (when (eq 0 (buffer-size buffer))\n-      (when (file-readable-p \".dotest/info\")\n-        (with-temp-buffer\n-          (insert-file-contents \".dotest/info\")\n-          (goto-char (point-min))\n-          (when (re-search-forward \"^Author: \\\\(.*\\\\)\\nEmail: \\\\(.*\\\\)$\" nil t)\n-            (setq author-name (match-string 1))\n-            (setq author-email (match-string 2)))\n-          (goto-char (point-min))\n-          (when (re-search-forward \"^Subject: \\\\(.*\\\\)$\" nil t)\n-            (setq subject (match-string 1)))\n-          (goto-char (point-min))\n-          (when (re-search-forward \"^Date: \\\\(.*\\\\)$\" nil t)\n-            (setq date (match-string 1)))))\n-      (git-setup-log-buffer buffer author-name author-email subject date))\n-    (log-edit #'git-do-commit nil #'git-log-edit-files buffer)\n-    (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))\n-    (setq buffer-file-coding-system coding-system)\n-    (re-search-forward (regexp-quote (concat git-log-msg-separator \"\\n\")) nil t)))\n+  (when (git-run-pre-commit-hook)\n+    (let ((buffer (get-buffer-create \"*git-commit*\"))\n+          (coding-system (git-get-commits-coding-system))\n+          author-name author-email subject date)\n+      (when (eq 0 (buffer-size buffer))\n+        (when (file-readable-p \".dotest/info\")\n+          (with-temp-buffer\n+            (insert-file-contents \".dotest/info\")\n+            (goto-char (point-min))\n+            (when (re-search-forward \"^Author: \\\\(.*\\\\)\\nEmail: \\\\(.*\\\\)$\" nil t)\n+              (setq author-name (match-string 1))\n+              (setq author-email (match-string 2)))\n+            (goto-char (point-min))\n+            (when (re-search-forward \"^Subject: \\\\(.*\\\\)$\" nil t)\n+              (setq subject (match-string 1)))\n+            (goto-char (point-min))\n+            (when (re-search-forward \"^Date: \\\\(.*\\\\)$\" nil t)\n+              (setq date (match-string 1)))))\n+        (git-setup-log-buffer buffer author-name author-email subject date))\n+      (log-edit #'git-do-commit nil #'git-log-edit-files buffer)\n+      (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))\n+      (setq buffer-file-coding-system coding-system)\n+      (re-search-forward (regexp-quote (concat git-log-msg-separator \"\\n\")) nil t))))\n \n (defun git-find-file ()\n   \"Visit the current file in its own buffer.\"\n-- \n1.5.0.4.408.gca3d7\n\n-- \nAlexandre Julliard\njulliard@winehq.org\n"}]}