branch: elpa/git-commit commit dd14e0c3c6604f97a447967f0c1ace9947a09e66 Author: Magnar Sveen <magn...@gmail.com> Commit: Jonas Bernoulli <jo...@bernoul.li>
git-commit: Ignore leading comment when inserting headers It is suggested in several places on the internet to use a git commit template, for instance here [1]. These often include a header comment: # Title: Summary, imperative, start upper case, don't end with a period # No more than 50 chars. #### 50 chars is here: # [1]: https://gist.github.com/lisawolderiksen/a7b99d94c92c6671181611be1641c733 This change ensures that headers are inserted after this leading comment (meaning there are no empty lines before it), instead of above it. Headers are still inserted before the comments added by Git itself. --- lisp/git-commit.el | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lisp/git-commit.el b/lisp/git-commit.el index f44ff2233e..558f376b9c 100644 --- a/lisp/git-commit.el +++ b/lisp/git-commit.el @@ -943,17 +943,28 @@ have the form \"NAME <EMAIL>\"." (defun git-commit-insert-header (header name email) (setq header (format "%s: %s <%s>" header name email)) (save-excursion - (goto-char (point-max)) - (cond ((re-search-backward "^[-a-zA-Z]+: [^<\n]+? <[^>\n]+>" nil t) - (end-of-line) - (insert ?\n header) - (unless (= (char-after) ?\n) - (insert ?\n))) - (t - (while (re-search-backward (concat "^" comment-start) nil t)) - (unless (looking-back "\n\n" nil) - (insert ?\n)) - (insert header ?\n))) + (let ((leading-comment-end nil)) + ;; Make sure we skip forward past any leading comments. + (goto-char (point-min)) + (while (looking-at comment-start) + (forward-line)) + (setq leading-comment-end (point)) + (goto-char (point-max)) + (cond + ;; Look backwards for existing headers. + ((re-search-backward "^[-a-zA-Z]+: [^<\n]+? <[^>\n]+>" nil t) + (end-of-line) + (insert ?\n header) + (unless (= (char-after) ?\n) + (insert ?\n))) + ;; Or place the new header right before the first non-leading + ;; comments. + (t + (while (re-search-backward (concat "^" comment-start) + leading-comment-end t)) + (unless (looking-back "\n\n" nil) + (insert ?\n)) + (insert header ?\n)))) (unless (or (eobp) (= (char-after) ?\n)) (insert ?\n))))