Keep an eye on those revision descriptions.
1local M = {}
2
3M.update_jj_status = function(bufnr)
4 vim.api.nvim_buf_set_var(bufnr, "jj_desc", "")
5
6 local current_file = vim.api.nvim_buf_get_name(bufnr)
7 if current_file == "" or current_file == nil then
8 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
9 return
10 end
11
12 if not vim.fn.filereadable(current_file) then
13 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
14 return
15 end
16
17 local jj_root = vim.fs.find(
18 ".jj", {
19 upward = true,
20 stop = vim.loop.os_homedir(),
21 path = current_file,
22 type = "directory",
23 }
24 )[1]
25
26 if not jj_root then
27 vim.api.nvim_buf_set_var(bufnr, "jj_desc", "~no_jj_repo~")
28 return
29 end
30
31 local repo_root = vim.fs.dirname(jj_root)
32 local _cmd = string.format("jj log -R %s -r @ -T description --no-graph", repo_root)
33
34 local cmd = { "jj", "log", "-R", repo_root, "-r", "@", "-T", "description", "--no-graph", }
35
36 vim.system(cmd, { text = true, cwd = vim.fs.dirname(repo_root) }, function(obj)
37 if obj.code ~= 0 then
38 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
39 return
40 end
41
42 local output = vim.trim(obj.stdout or "")
43 output = output:gsub("\n", ""):gsub("^%s*", ""):gsub("%s*$", "")
44
45 if output == "" then output = "~no_desc~" end
46 if #output > 50 then output = string.sub(output, 1, 47) .. "..." end
47
48 vim.schedule(function()
49 if vim.api.nvim_buf_is_valid(bufnr) then
50 vim.api.nvim_buf_set_var(bufnr, "jj_desc", output)
51
52 if bufnr == vim.api.nvim_get_current_buf() then
53 vim.cmd.redrawstatus()
54 end
55 end
56 end)
57 end)
58end
59
60M.setup = function()
61 local group = vim.api.nvim_create_augroup("JJLogPlugin", { clear = true })
62
63 vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "BufWritePost", "FocusGained" }, {
64 group = group,
65 pattern = "*",
66 callback = function(args)
67 M.update_jj_status(args.buf)
68 end,
69 })
70end
71
72return M