From ceeae9b5e2625e23e6e3792d06a6c8122a36d18b Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 16 May 2026 04:26:20 -0500 Subject: feat(gptel-tools): wire git_status / git_log / git_diff as local tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three read-only git context tools so gptel can see what's changed without me pasting `git status` / `git log` / `git diff` output into every chat turn. Builds the first batch from the ADOPT bucket in `docs/design/gptel-tools-shortlist.org`. Shape per tool: - `gptel-tools/git_status.el` — `git status --short --branch` for a directory inside a git working tree under HOME. Returns the porcelain output, or a "Clean working tree" marker when only the branch line is present. - `gptel-tools/git_log.el` — `git log --oneline -nN` with an optional `--since` filter. N defaults to 20, capped at 100; nil / non- integer / out-of-range N falls back to the default. - `gptel-tools/git_diff.el` — `git diff [REF1 [REF2]] [-- FILE]`. Output capped at ~500KB so a runaway diff can't blow up context; truncation is reported inline. Validation is uniform: path must resolve under HOME, must be a directory, must be inside a git working tree (verified via `git rev-parse --is-inside-work-tree`). Color is disabled via `-c color.ui=false` at the git level (`git status` doesn't accept `--no-color` directly). Tests run against real temp git repos created via `process-file`, not mocked — there's nothing in gptel-tools/git_*.el that's process-mockable in a meaningful way, and a real `git init` + a couple of commits is cheaper than building a fake. 31 tests total: 7 for git_status, 11 for git_log, 13 for git_diff. Wired into `cj/gptel-local-tool-features` so gptel exposes the three tools on next restart. --- gptel-tools/git_log.el | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 gptel-tools/git_log.el (limited to 'gptel-tools/git_log.el') diff --git a/gptel-tools/git_log.el b/gptel-tools/git_log.el new file mode 100644 index 00000000..9cfae263 --- /dev/null +++ b/gptel-tools/git_log.el @@ -0,0 +1,94 @@ +;;; git_log.el --- Read-only git log tool for gptel -*- coding: utf-8; lexical-binding: t; -*- + +;; Author: Craig Jennings +;; Keywords: convenience, tools, git + +;; This file is not part of GNU Emacs. + +;;; Commentary: + +;; Gptel tool returning `git log --oneline -n N' for a path under the +;; user's home directory. Read-only. N is capped to keep the model's +;; context budget predictable. + +;;; Code: + +(require 'gptel) + +(defconst cj/gptel-git-log--max-count 100 + "Hard cap on the number of commits `git_log' will return.") + +(defconst cj/gptel-git-log--default-count 20 + "Default commit count when the caller doesn't specify one.") + +(defun cj/gptel-git-log--validate-path (path) + "Validate PATH for a git log call. Return the expanded path on success. +Same contract as the git_status validator: must be under HOME, must +be a directory, must be inside a git working tree." + (let ((full (expand-file-name (or path "~") "~"))) + (unless (string-prefix-p (expand-file-name "~") full) + (error "Path must be within home directory: %s" path)) + (unless (file-directory-p full) + (error "Not a directory: %s" full)) + (let ((default-directory full)) + (unless (zerop (process-file "git" nil nil nil + "rev-parse" "--is-inside-work-tree")) + (error "Not a git working tree: %s" full))) + full)) + +(defun cj/gptel-git-log--effective-count (n) + "Return the commit count to use given caller-supplied N. +Nil / non-integer N → `cj/gptel-git-log--default-count'. +Values above `cj/gptel-git-log--max-count' get capped." + (cond + ((not (integerp n)) cj/gptel-git-log--default-count) + ((< n 1) cj/gptel-git-log--default-count) + ((> n cj/gptel-git-log--max-count) cj/gptel-git-log--max-count) + (t n))) + +(defun cj/gptel-git-log--run (path &optional n since) + "Run `git log --oneline -n N' in PATH. Return the output as a string. +SINCE, if a non-empty string, is passed as `--since=SINCE'." + (let* ((dir (cj/gptel-git-log--validate-path path)) + (count (cj/gptel-git-log--effective-count n)) + (args (list "-c" "color.ui=false" + "log" "--oneline" + (format "-n%d" count))) + (args (if (and (stringp since) (not (string-empty-p since))) + (append args (list (format "--since=%s" since))) + args)) + (default-directory dir)) + (with-temp-buffer + (let ((exit (apply #'process-file "git" nil t nil args))) + (unless (zerop exit) + (error "git log exited with %d: %s" exit (buffer-string))) + (let ((out (buffer-string))) + (if (string-empty-p out) + (format "No commits in %s matching the filter" dir) + out)))))) + +(with-eval-after-load 'gptel + (gptel-make-tool + :name "git_log" + :function (lambda (path &optional n since) + (cj/gptel-git-log--run path n since)) + :description "Return the output of `git log --oneline -n N' for a directory in the user's home tree. Read-only. N defaults to 20 and is capped at 100. Use SINCE to filter commits more recent than a date expression git understands (e.g. '2 weeks ago', '2026-05-01')." + :args (list '(:name "path" + :type string + :description "Directory inside a git working tree. Either an absolute path under the user's home directory or a path relative to it (e.g. 'code/myproject').") + '(:name "n" + :type integer + :description "Number of commits to return. Defaults to 20; capped at 100." + :optional t) + '(:name "since" + :type string + :description "Optional date expression for `git log --since='; e.g. '2 weeks ago' or '2026-05-01'." + :optional t)) + :category "git" + :confirm nil + :include t) + + (add-to-list 'gptel-tools (gptel-get-tool '("git" "git_log")))) + +(provide 'git_log) +;;; git_log.el ends here -- cgit v1.2.3