Skip to content

Commit

Permalink
feat(blame): support extra options
Browse files Browse the repository at this point in the history
Closes #953
Resolves #959
  • Loading branch information
lewis6991 committed Apr 4, 2024
1 parent 1389134 commit 77117b7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ blame_line({opts}, {callback?}) *gitsigns.blame_line()*
Ignore whitespace when running blame.
{rev}: (string)
Revision to blame against.
• {extra_opts}: (string[])
Extra options passed to `git-blame`.

get_hunks({bufnr}) *gitsigns.get_hunks()*
Get hunk array for specified buffer.
Expand Down Expand Up @@ -773,6 +775,8 @@ current_line_blame_opts *gitsigns-config-current_line_blame_opts*
Ignore whitespace when running blame.
• virt_text_priority: integer
Priority of virtual text.
• extra_opts: string[]
Extra options passed to `git-blame`.

current_line_blame_formatter_opts
*gitsigns-config-current_line_blame_formatter_opts*
Expand Down
2 changes: 2 additions & 0 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ end
--- Ignore whitespace when running blame.
--- • {rev}: (string)
--- Revision to blame against.
--- • {extra_opts}: (string[])
--- Extra options passed to `git-blame`.
M.blame_line = async.create(1, function(opts)
if popup.focus_open('blame') then
return
Expand Down
4 changes: 2 additions & 2 deletions lua/gitsigns/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function CacheEntry:run_blame(lnum, opts)
local tick = vim.b[bufnr].changedtick
local lnum0 = #buftext > BLAME_THRESHOLD_LEN and lnum or nil
-- TODO(lewis6991): Cancel blame on changedtick
blame_cache = self.git_obj:run_blame(buftext, lnum0, opts.ignore_whitespace)
blame_cache = self.git_obj:run_blame(buftext, lnum0, opts)
async.scheduler()
if not vim.api.nvim_buf_is_valid(bufnr) then
return
Expand Down Expand Up @@ -129,7 +129,7 @@ end
function CacheEntry:get_blame(lnum, opts)
if opts.rev then
local buftext = util.buf_lines(self.bufnr)
return self.git_obj:run_blame(buftext, lnum, opts.ignore_whitespace)[lnum]
return self.git_obj:run_blame(buftext, lnum, opts)[lnum]
end

local blame_cache = self.blame
Expand Down
3 changes: 3 additions & 0 deletions lua/gitsigns/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
--- @class (exact) Gitsigns.BlameOpts
--- @field ignore_whitespace? boolean
--- @field rev? string
--- @field extra_opts? string[]

--- @class (exact) Gitsigns.LineBlameOpts : Gitsigns.BlameOpts
--- @field full? boolean
Expand Down Expand Up @@ -644,6 +645,8 @@ M.schema = {
Ignore whitespace when running blame.
• virt_text_priority: integer
Priority of virtual text.
• extra_opts: string[]
Extra options passed to `git-blame`.
]],
},

Expand Down
20 changes: 14 additions & 6 deletions lua/gitsigns/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ end

--- @param lines string[]
--- @param lnum? integer
--- @param ignore_whitespace? boolean
--- @param opts? Gitsigns.BlameOpts
--- @return table<integer,Gitsigns.BlameInfo?>?
function Obj:run_blame(lines, lnum, ignore_whitespace)
function Obj:run_blame(lines, lnum, opts)
local ret = {} --- @type table<integer,Gitsigns.BlameInfo>

if not self.object_name or self.repo.abbrev_head == '' then
Expand All @@ -576,16 +576,24 @@ function Obj:run_blame(lines, lnum, ignore_whitespace)

local args = { 'blame', '--contents', '-', '--incremental' }

opts = opts or {}

if opts.ignore_whitespace then
args[#args + 1] = '-w'
end

if lnum then
vim.list_extend(args, { '-L', lnum .. ',+1' })
end

args[#args + 1] = self.file

if ignore_whitespace then
args[#args + 1] = '-w'
if opts.extra_opts then
vim.list_extend(args, opts.extra_opts)
end

args[#args + 1] = opts.rev
args[#args + 1] = '--'
args[#args + 1] = self.file

local ignore_file = self.repo.toplevel .. '/.git-blame-ignore-revs'
if uv.fs_stat(ignore_file) then
vim.list_extend(args, { '--ignore-revs-file', ignore_file })
Expand Down

0 comments on commit 77117b7

Please sign in to comment.