Git blame implementation

Screen Recording 2025-03-18 at 14.24.02

git_blame.kak

# name: kakoune_git_blame
# version: 0.1.0
# description: This script provides support for the “git blame” command-line utility.
# authors: ["Mathieu Ablasou <alexherbo2@gmail.com>"]
# kakoune: 2023-12-12
# license: MIT
# dependencies: ["fifo"]
# doc: no
# tests: no
def git_blame %{
  eval -save-regs 'a' %{
    reg a -s '--pretty=tformat:%h %as “%an” %s'
    eval -draft %{
      exec 'x<a-_>'
      eval -itersel %{
        eval %exp{
          exec '<a-;>'
          reg a %%reg{a} -L "%%val{cursor_line},%val{cursor_line}:%%val{buffile}"
        }
      }
    }
    fifo -name '*git_blame*' -- git log %reg{a}
  }
}

compl git_blame file

def -hidden git_blame_show_patches %{
  eval -draft %{
    exec 'x<a-s><a-K>^\n<ret>Hs^[0-9a-f]{7,40}<ret>'
    eval -itersel %{
      eval -client %val{client} -verbatim fifo -name "%val{selection}.patch" -- git show -p %val{selection}
    }
  }
}

addhl shared/git_blame regex '^([0-9a-f]{7,40}) (\d{4}-\d{2}-\d{2}) (“.+?”) (.+?)$' 1:constant 2:value 3:string 4:comment

hook global BufCreate '\*git_blame\*' %{
  set buffer filetype git_blame
}

hook global BufSetOption filetype=git_blame %{
  addhl buffer/git_blame ref git_blame
  map buffer normal <ret> ':git_blame_show_patches<ret>'
}
4 Likes

this is similar to the stdlib integration shipped with Kakoune.
That one uses line-flags; your screencast suggests that :fifo git blame %val{buffile} is occasionally handy.

The NIH syndrome is the greatest strength and weakness of the Kakoune community :slight_smile:

1 Like