Skip to content

Instantly share code, notes, and snippets.

@jwiegley
Created June 8, 2025 22:46
Show Gist options
  • Save jwiegley/99a2509d707e06b2dc0bdea247f7b373 to your computer and use it in GitHub Desktop.
Save jwiegley/99a2509d707e06b2dc0bdea247f7b373 to your computer and use it in GitHub Desktop.
(use-package gptel-commit
:no-require t
:after (gptel git-commit)
:preface
(defun my/gptel-commit-summary ()
"Insert a commit message header line in the format I use, followed by a
standard magit (GNU style) changelog.
Don't get the LLM to write the commit message itself, because it's bad
at inferring my intent.
Intended to be placed in `git-commit-setup-hook'."
(gptel-with-preset 'commit-summary
(let ((commit-buffer (current-buffer))) ;commit message buffer
(with-temp-buffer
(vc-git-command ;insert diff
(current-buffer) 1 nil
"diff-index" "--exit-code" "--patch"
(and (magit-anything-staged-p) "--cached")
"HEAD" "--")
(gptel-request nil ;Run request on diff buffer contents
:context commit-buffer
:callback
(lambda (resp info)
(if (not (stringp resp))
(message "Git commit summary generation failed")
(with-current-buffer (plist-get info :context)
(save-excursion
(goto-char (point-min))
(insert resp "\n\n")
(magit-generate-changelog))))))))))
:config
(gptel-make-preset 'commit-summary
:system 'commit-summary
:parents 'rewrite
:tools nil)
(add-hook 'git-commit-setup-hook #'my/gptel-commit-summary 50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment