基于gptel分析git commit的命令

使用,

  • 第一步,调用 my-gptel-add-project-context (可选)
  • 第二步, 调用 my-gptel-analyze-commit

用本地的llm(ollama+qwen3-coder)测了一下,执行第一步会使得第二步输出有些微小的不同。

在copilot的帮助下写的代码,

(require 'gptel)

(defun my-gptel-add-project-context ()
  "Add the current Git project as context."
  (interactive)
  (let ((project-root (locate-dominating-file default-directory ".git")))
    (when project-root
      (gptel-add-file project-root)
      (message "Project context added to GPTel session."))))


(defun my-gptel-analyze-commit ()
  "Analyze code commit in the current git project using GPTel.
If current buffer is in diff format, buffer context is regarded as one commit;
Or else latest git commit is used.,
Adds project context and explicitly specifies the project root in the prompt.
Displays the response in Org mode."
  (interactive)
  (let* ((project-root (locate-dominating-file "." ".git"))
         (default-directory project-root)
         (diff-output (cond
                       ((derived-mode-p 'diff-mode)
                        (buffer-string))
                       (t
                        (shell-command-to-string "git show HEAD"))))
         (prompt (concat
                  "You are analyzing code from a Git project located at: " project-root "\n\n"
                  "Please analyze the following code changes from the Git commit. "
                  "Consider the overall project context and provide insights on logic improvements, "
                  "potential issues, or optimization suggestions. "
                  "Format your response using Org mode syntax:\n\n"
                  diff-output)))
    (gptel-request prompt
      :callback (lambda (response info)
                  (ignore info)
                  (cond
                   (response
                    (with-current-buffer (get-buffer-create "*GPTel Commit Analysis*")
                      (erase-buffer)
                      (org-mode)
                      (insert response)
                      (pop-to-buffer (current-buffer))))
                   (t
                    (message "No response is given.")))))))
3 个赞