LoginSignup
11
12

More than 5 years have passed since last update.

magit でコミットログを書くときに helm で過去のコミットログを参照する

Posted at

Emacs の helm と magit を使っている人に便利だと思います。
過去のコミットログからログを挿入できます。

コミットログは過去に使った言い回しをそのまま使ったり、他の人が書いたのを参考にして書いたりすることが多いと思うので コミットメッセージをanythingで挿入できるようにする を参考にして helm 用に書き直しました。

元にしたものにあった複数のプロジェクトを切り替えながら使うと最初に読み込んだものしか参照できないことがあるというバグを修正してあります。

anything に移植してもうまく動かないかもしれません。。。

(defvar helm-c-source-git-commit-messages
  '((name . "Git Commit Messages")
    (init . helm-c-git-commit-messages-init)
    (action . (("Insert" . (lambda (candidate)
                             (insert
                              (replace-regexp-in-string "\0" "\n" candidate))))))
    (real-to-display . helm-c-git-commit-messages-real-to-display)
    (migemo)
    (multiline)
    (candidates-in-buffer)))

(defun helm-c-git-commit-messages-init ()
  (with-current-buffer (helm-candidate-buffer 'global)
    (call-process-shell-command
     "git log --format=\"%x00%B\" | tr '\\n\\000\' '\\000\\n' | sed -e '/^$/d' -e 's/\\x0\\+$//'"
     nil (current-buffer))))

(defun helm-git-commit-messages ()
  "`helm' for git commit messages."
  (interactive)
  (helm-other-buffer 'helm-c-source-git-commit-messages
                         "*helm commit messages*"))

(defun helm-c-git-commit-messages-real-to-display (candidate)
    (replace-regexp-in-string "\0" "\n" candidate))

(defun magit-enable-helm ()
  ;; 過去のコミットメッセージを挿入
  (define-key magit-log-edit-mode-map
    (kbd "C-c i") 'helm-git-commit-messages))
(add-hook 'magit-mode-hook
          'magit-enable-helm)

自分の設定は dotfiles で公開しています。

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12