# [PATCH] . git.el: use commit.template if available

2 messages from 2013-06-14 to 2013-06-14. Participants: Curt Brune, Junio C Hamano.
Thread: https://gitlist.dev/t/34144

## Curt Brune, 2013-06-14 17:25

Subject: [PATCH] . git.el: use commit.template if available
Message-ID: <20130614172535.GA24983@higgs.cumulusnetworks.com>
URL: https://gitlist.dev/e/20130614172535.GA24983%40higgs.cumulusnetworks.com

```

If the user's git config defines commit.template then include the
contents of that file in the log buffer by default.

In git-setup-log-buffer, instead of supplying the default commit
message insert the user's commit.template.

Signed-off-by: Curt Brune <curt@brune.net>
---
 contrib/emacs/git.el |   47 ++++++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index 5ffc506..827578f 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -296,6 +296,11 @@ the process output as a string, or nil if the git command failed."
       (and (fboundp 'user-mail-address) (user-mail-address))
       (and (boundp 'user-mail-address) user-mail-address)))
 
+(defun git-get-commit-template ()
+  "Return the git commit template or nil"
+  ; copied from log-edit
+  (git-config "commit.template"))
+
 (defun git-get-commits-coding-system ()
   "Return the coding system to use for commits."
   (let ((repo-config (git-config "i18n.commitencoding")))
@@ -1280,29 +1285,33 @@ The FILES list must be sorted."
   (let ((dir default-directory)
         (committer-name (git-get-committer-name))
         (committer-email (git-get-committer-email))
+        (commit-template (git-get-commit-template))
         (sign-off git-append-signed-off-by))
     (with-current-buffer buffer
       (cd dir)
       (erase-buffer)
-      (insert
-       (propertize
-        (format "Author: %s <%s>\n%s%s"
-                (or author-name committer-name)
-                (or author-email committer-email)
-                (if date (format "Date: %s\n" date) "")
-                (if merge-heads
-                    (format "Merge: %s\n"
-                            (mapconcat 'identity merge-heads " "))
-                  ""))
-        'face 'git-header-face)
-       (propertize git-log-msg-separator 'face 'git-separator-face)
-       "\n")
-      (when subject (insert subject "\n\n"))
-      (cond (msg (insert msg "\n"))
-            ((file-readable-p ".git/rebase-apply/msg")
-             (insert-file-contents ".git/rebase-apply/msg"))
-            ((file-readable-p ".git/MERGE_MSG")
-             (insert-file-contents ".git/MERGE_MSG")))
+      (if commit-template
+	  (insert-file-contents commit-template)
+	(progn
+          (insert
+           (propertize
+            (format "Author: %s <%s>\n%s%s"
+                    (or author-name committer-name)
+                    (or author-email committer-email)
+                    (if date (format "Date: %s\n" date) "")
+                    (if merge-heads
+                        (format "Merge: %s\n"
+                                (mapconcat 'identity merge-heads " "))
+                      ""))
+            'face 'git-header-face)
+           (propertize git-log-msg-separator 'face 'git-separator-face)
+           "\n")
+          (when subject (insert subject "\n\n"))
+          (cond (msg (insert msg "\n"))
+                ((file-readable-p ".git/rebase-apply/msg")
+                 (insert-file-contents ".git/rebase-apply/msg"))
+                ((file-readable-p ".git/MERGE_MSG")
+                 (insert-file-contents ".git/MERGE_MSG")))))
       ; delete empty lines at end
       (goto-char (point-min))
       (when (re-search-forward "\n+\\'" nil t)
-- 
1.7.9.5

```

## Junio C Hamano, 2013-06-14 17:51

Subject: Re: [PATCH] . git.el: use commit.template if available
Message-ID: <7v1u84pomv.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1u84pomv.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20130614172535.GA24983@higgs.cumulusnetworks.com>

```
Curt Brune <curt@brune.net> writes:

> If the user's git config defines commit.template then include the
> contents of that file in the log buffer by default.
>
> In git-setup-log-buffer, instead of supplying the default commit
> message insert the user's commit.template.
>
> Signed-off-by: Curt Brune <curt@brune.net>
> ---

Interesting.

I wonder if git-do-commit still needs to reimplement the commit
logic using low-level plumbing in this era, which I think is the
cause of having to constantly catch up with what "git commit"
Porcelain does (like paying attention to configuration variables).

I often just do

	M-x compile<ENTER>git commit args...<ENTER>

while setting my EDITOR to 'emacsclient', and I have a suspicion
that the callchain from git-commit-file to git-setup-log-buffer can
be quite simplified if such an approach is used.  Enumerate the
marked file(s) to formulate the "git commit" command line and let
the underlying "git commit" do the heavy-lifting, that is.

```
