{"thread":{"id":"13177","subject":"[PATCH/RFC] git.el: show/refresh diff buffer from commit buffer","startedAt":"2008-04-19T06:53:21Z","lastAt":"2008-04-19T06:53:21Z","messageCount":1,"participants":["Clifford Caoile"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"74743","messageId":"1f748ec60804182353q45237c8esc8d05cb49a132676@mail.gmail.com","threadId":"13177","inReplyTo":null,"subject":"[PATCH/RFC] git.el: show/refresh diff buffer from commit buffer","fromName":"Clifford Caoile","fromEmail":"piyo@users.sourceforge.net","sentAt":"2008-04-19T06:53:21Z","receivedAt":"2008-04-19T06:53:21Z","isPatch":true,"sender":{"key":"piyo@users.sourceforge.net","avatar":null},"body":"This patch adds Emacs Lisp plumbing:\n  run-hooks 'git-log-edit-commit-hook to git-commit-file\n  run-hooks 'git-diff-setup-hook to git-setup-diff-buffer\n  a commit buffer reference to the diff buffer\n\nThis allows diff buffer refreshing:\n\nThe hooks git-log-edit-commit-hook and git-diff-setup-hook are given\nsample hooks that add keybindings \"\\C-c\\C-d\" and \"g\" to the\n*git-commit* and *git-diff* buffer, respectively. These keybindings\nallow refreshing the diff information shown in the *git-diff* buffer.\n\nSigned-off-by: Clifford Caoile <piyo@users.sourceforge.net>\n---\n contrib/emacs/git.el |   53 ++++++++++++++++++++++++++++++++++++++++++-------\n 1 files changed, 45 insertions(+), 8 deletions(-)\n\ndiff --git a/contrib/emacs/git.el b/contrib/emacs/git.el\nindex 4fa853f..6b23145 100644\n--- a/contrib/emacs/git.el\n+++ b/contrib/emacs/git.el\n@@ -179,6 +179,32 @@ if there is already one that displays the same directory.\"\n     (,(concat \"^\\\\(\" (regexp-quote git-log-msg-separator) \"\\\\)$\")\n      (1 font-lock-comment-face))))\n\n+;\n+\n+(defvar git-log-edit-commit-hook nil\n+  \"Run after the log edit buffer created `git-commit-file' is setup.\")\n+\n+(defvar git-diff-setup-hook nil\n+  \"Run after the diff buffer created by `git-diff-setup-buffer' is setup.\")\n+\n+(defun git-log-edit-diff-cmd ()\n+  \"The interactive version of `git-log-edit-diff', designed to be\n+called from define-key calls inside of `git-log-edit-commit-hook'\n+and `git-diff-setup-hook'.\"\n+  (interactive)\n+  (git-log-edit-diff))\n+\n+(add-hook 'git-log-edit-commit-hook 'git-log-commit-sample-hook)\n+(add-hook 'git-diff-setup-hook      'git-diff-setup-sample-hook)\n+\n+(defun git-log-commit-sample-hook ()\n+  (define-key (current-local-map) \"\\C-c\\C-d\" 'git-log-edit-diff-cmd))\n+\n+(defun git-diff-setup-sample-hook ()\n+  (define-key (current-local-map) \"g\"  'git-log-edit-diff-cmd))\n+\n+;\n+\n (defun git-get-env-strings (env)\n   \"Build a list of NAME=VALUE strings from a list of environment strings.\"\n   (mapcar (lambda (entry) (concat (car entry) \"=\" (cdr entry))) env))\n@@ -1124,25 +1150,34 @@ Return the list of files that haven't been handled.\"\n       (git-refresh-ewoc-hf git-status)\n       t)))\n\n-(defun git-setup-diff-buffer (buffer)\n+(defun git-setup-diff-buffer (buffer &optional parent-buffer)\n   \"Setup a buffer for displaying a diff.\"\n   (let ((dir default-directory))\n     (with-current-buffer buffer\n       (diff-mode)\n       (goto-char (point-min))\n       (setq default-directory dir)\n-      (setq buffer-read-only t)))\n+      (setq buffer-read-only t)\n+      ;; when called by (git-diff-file) via the git's log-edit buffer,\n+      ;; parent-buffer will be non-nil\n+      (when parent-buffer\n+        (set (make-local-variable 'log-edit-parent-buffer) parent-buffer))\n+      (run-hooks 'git-diff-setup-hook)))\n   (display-buffer buffer)\n   ; shrink window only if it displays the status buffer\n   (when (eq (window-buffer) (current-buffer))\n     (shrink-window-if-larger-than-buffer)))\n\n-(defun git-diff-file ()\n-  \"Diff the marked file(s) against HEAD.\"\n+(defun git-diff-file (&optional parent-buffer)\n+  \"Diff the marked file(s) against HEAD.\n+\n+PARENT-BUFFER is usually passed by `git-log-edit-diff' to be able\n+to revert the diff buffer.\"\n   (interactive)\n   (let ((files (git-marked-files)))\n     (git-setup-diff-buffer\n-     (apply #'git-run-command-buffer \"*git-diff*\" \"diff-index\" \"-p\"\n\"-M\" \"HEAD\" \"--\" (git-get-filenames files)))))\n+     (apply #'git-run-command-buffer \"*git-diff*\" \"diff-index\" \"-p\"\n\"-M\" \"HEAD\" \"--\" (git-get-filenames files))\n+     parent-buffer)))\n\n (defun git-diff-file-merge-head (arg)\n   \"Diff the marked file(s) against the first merge head (or the nth\none with a numeric prefix).\"\n@@ -1210,8 +1245,9 @@ Return the list of files that haven't been handled.\"\n\n (defun git-log-edit-diff ()\n   \"Run a diff of the current files being committed from a log-edit buffer.\"\n-  (with-current-buffer log-edit-parent-buffer\n-    (git-diff-file)))\n+  (let ((original-buffer log-edit-parent-buffer))\n+    (with-current-buffer log-edit-parent-buffer\n+      (git-diff-file original-buffer))))\n\n (defun git-append-sign-off (name email)\n   \"Append a Signed-off-by entry to the current buffer, avoiding duplicates.\"\n@@ -1292,7 +1328,8 @@ Return the list of files that haven't been handled.\"\n \t(log-edit 'git-do-commit nil 'git-log-edit-files buffer))\n       (setq font-lock-keywords (font-lock-compile-keywords\ngit-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\"\\n\")) nil t))))\n+      (re-search-forward (regexp-quote (concat git-log-msg-separator\n\"\\n\")) nil t)\n+      (run-hooks 'git-log-edit-commit-hook))))\n\n (defun git-setup-commit-buffer (commit)\n   \"Setup the commit buffer with the contents of COMMIT.\"\n-- \n1.5.5.1015.g9d258\n"}]}