Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ return {

## Usage
The following commands are used:
- `BlameToggle [view]` - Toggle the blame with provided view. If no view is provided it opens the `default` (window) view
- `BlameToggle [view] [options]` - Toggle the blame with provided view and options.
- If no view is provided, it opens the `default` (window) view.
- If no options are provided, it fall back to `blame_options`.

There are two built-in views:
- `window` - fugitive style window to the left of the current window
Expand Down
22 changes: 16 additions & 6 deletions lua/blame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ local config = {
}

---@param blame_view BlameView
local function open(blame_view)
---@param opts string[] | nil
local function open(blame_view, opts)
local filename = vim.api.nvim_buf_get_name(0)
local cwd = vim.fn.expand("%:p:h")
local g = git:new(config)
g:blame(filename, cwd, nil, function(data)
g:blame(filename, cwd, nil, opts, function(data)
vim.schedule(function()
local parsed_blames = porcelain_parser.parse_porcelain(data)
blame_view:open(parsed_blames)
Expand Down Expand Up @@ -95,13 +96,22 @@ M.setup = function(setup_args)
M.last_opened_view:close(false)
M.last_opened_view = nil
else
local arg = args.args == "" and "default" or args.args
blame_view = config.views[arg]:new(config)
open(blame_view)
local fargs = args.fargs
local view_name = "default"
local opts = {}
for i = 1, #fargs do
if fargs[i]:sub(1, 1) == "-" then
table.insert(opts, fargs[i])
else
view_name = fargs[i]
end
end
blame_view = config.views[view_name]:new(config)
open(blame_view, opts)
M.last_opened_view = blame_view
end
end, {
nargs = "?",
nargs = "*",
complete = function()
local all_views = {}
for k, _ in pairs(config.views) do
Expand Down
10 changes: 8 additions & 2 deletions lua/blame/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ end
---@param filename string
---@param cwd any cwd where to execute the command
---@param commit string|nil
---@param opts string[]|nil
---@param callback fun(data: string[]) callback on exiting the command with output string
function Git:blame(filename, cwd, commit, callback, err_cb)
function Git:blame(filename, cwd, commit, opts, callback, err_cb)
local blame_command = {
"git",
"--no-pager",
"blame",
"--line-porcelain",
filename,
}
add_blame_options(blame_command, self.config.blame_options)

local blame_options = self.config.blame_options
if opts ~= nil and #opts > 0 then
blame_options = opts
end
add_blame_options(blame_command, blame_options)
if commit ~= nil then
table.insert(blame_command, #blame_command - 1, commit)
end
Expand Down