如何自定义cider-repl-prompt-render-function以高亮显示当前Git分支
解读
在国内 Clojure 岗位面试中,这道题考察的是“对 CIDER 深度定制能力”与“工程化意识”。
CIDER 的 REPL prompt 默认只显示 ns 名,而一线互联网公司(如阿里、头条、美团)的微服务代码库往往同时维护多个分支,开发者在 REPL 里频繁切换分支打 hot-fix。若能一眼看出当前分支,可显著降低“在错误分支上修 bug”的事故率。
面试官想确认:
- 你是否熟悉 CIDER 的渲染钩子机制;
- 你是否能把 Emacs Lisp、Clojure、Shell 三方数据无缝串起来;
- 你是否兼顾性能(不卡 REPL)与可维护性(不硬编码路径)。
知识点
- cider-repl-prompt-render-function 的签名
(defun my-render (namespace) -> "string"),返回的字符串最终插入 REPL。 - CIDER 加载顺序
cider-repl-init-hook 在 jack-in 后、首个 prompt 渲染前运行,是注入自定义渲染函数的安全时机。 - 获取 Git 分支的三种办法
a) 调用 magit-get-current-branch(依赖 Magit,最快);
b) 调用 (vc-working-revision root) 内置 vc 库;
c) 直接 shell-command-to-string "git branch --show-current"。
国内很多公司用内网 GitLab,Magit 不一定能直连,因此方案 b/c 更稳。 - 高亮手段
使用 propertize 给分支名加 face,推荐用 cider-repl-prompt-face 的变体,保证与主题一致。 - 性能与缓存
每次渲染都起进程会卡 REPL;用缓存 + file-notify 监听 .git/HEAD 变化,国内 SSD 笔记本 1 ms 内完成更新。 - 工程化细节
把配置拆到独立文件(lisp/cider-custom.el),并在 dir-locals 里为老项目关闭该特性,避免 monorepo 分支过多导致 prompt 过长。
答案
下面给出一份可直接落地到国内项目的最小可用配置,兼顾“无 Magit 依赖”“不卡 REPL”“颜色区分分支状态”。
;; 放入 ~/.emacs.d/lisp/cider-git-prompt.el
(require 'cider)
(defvar cider-git-prompt--branch-cache nil
"Cons cell: (file-mod-tick . branch-name)")
(defun cider-git-prompt--current-branch ()
"Return current git branch, cached until .git/HEAD changes."
(let* ((root (locate-dominating-file default-directory ".git"))
(head (and root (expand-file-name ".git/HEAD" root))))
(if (and head (file-exists-p head))
(let ((tick (file-attribute-modification-time (file-attributes head))))
(if (and cider-git-prompt--branch-cache
(equal tick (car cider-git-prompt--branch-cache)))
(cdr cider-git-prompt--branch-cache)
(let ((branch (string-trim
(with-temp-buffer
(insert-file-contents head)
(if (looking-p "ref: refs/heads/")
(buffer-substring-no-properties 16 (line-end-position))
(buffer-string))))))
(setq cider-git-prompt--branch-cache (cons tick branch))
branch)))
"")))
(defun cider-git-prompt--face-for-branch (branch)
"Return a face for BRANCH: green=main/master, yellow=feature/*, red=hotfix/*"
(cond
((member branch '("main" "master")) 'success)
((string-prefix-p "feature/" branch) 'warning)
((string-prefix-p "hotfix/" branch) 'error)
(t 'font-lock-comment-face)))
(defun cider-git-prompt-render-with-git (namespace)
"Prompt renderer that appends highlighted git branch."
(let* ((branch (cider-git-prompt--current-branch))
(face (cider-git-prompt--face-for-branch branch))
(branch-str (if (string-empty-p branch) ""
(propertize (format " %s" branch)
'face face))))
(concat (propertize (format "%s" namespace) 'face 'cider-repl-prompt-face)
branch-str)))
;; 安全注入:只在 cider-repl-mode 里生效
(with-eval-after-load 'cider
(add-hook 'cider-repl-init-hook
(lambda ()
(setq-local cider-repl-prompt-render-function
#'cider-git-prompt-render-with-git))))
使用方式
- 把文件加入 load-path,(require 'cider-git-prompt)。
- M-x cider-jack-in 后,REPL prompt 立即出现 ns 名 + 高亮分支。
- 切换分支后,下次 prompt 更新自动感知,无需重启 REPL。
拓展思考
- 多仓库场景
国内很多团队用 lein-monolith 或 polylith,一个 Emacs 可能同时打开多个 Git 仓库。可以把缓存改成哈希表,key 为 (projectile-project-root),value 为分支 tick,避免跨项目污染。 - CI 集成
在 prompt 里追加 “[CI ✅]” 或 “[CI ❌]”:监听公司内网 Jenkins 的 badge.svg,解析 status 字段,用 emacs-request 异步拉取,渲染前更新。让开发者“本地 REPL 一眼看全局”。 - 安全合规
金融类公司要求“不落盘缓存分支信息”,可把缓存改为弱哈希 + 定时清空,确保敏感特性分支名不留在磁盘。 - 性能极限优化
当仓库在 NFS 或 SSHFS 上时,file-attributes 仍可能阻塞。可改用 (file-notify-add-watch ".git/HEAD" …) 事件驱动,只在文件变动时清缓存,渲染阶段纯内存读取,保证 60 fps 的 REPL 体验。