Skip to content

Commit 6001932

Browse files
committed
fix: git blame not working on renamed files
1 parent 662960a commit 6001932

15 files changed

Lines changed: 40 additions & 33 deletions

File tree

lua/vgit.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
local lazy = require('vgit.core.lazy')
2+
23
local env = lazy('vgit.core.env')
34
local sign = lazy('vgit.core.sign')
45
local libgit2 = lazy('vgit.libgit2')
56
local event = lazy('vgit.core.event')
7+
local router = lazy('vgit.cli.router')
68
local keymap = lazy('vgit.core.keymap')
79
local console = lazy('vgit.core.console')
810
local renderer = lazy('vgit.core.renderer')
@@ -13,19 +15,20 @@ local Hunks = lazy('vgit.features.buffer.Hunks')
1315
local hunks_setting = lazy('vgit.settings.hunks')
1416
local scene_setting = lazy('vgit.settings.scene')
1517
local signs_setting = lazy('vgit.settings.signs')
18+
local git_porcelain = lazy('vgit.cli.GitPorcelain')
1619
local symbols_setting = lazy('vgit.settings.symbols')
1720
local libgit2_setting = lazy('vgit.settings.libgit2')
1821
local display_service = lazy('vgit.ui.display_service')
1922
local Conflicts = lazy('vgit.features.buffer.Conflicts')
2023
local LiveBlame = lazy('vgit.features.buffer.LiveBlame')
21-
local status_diff_view_setting = lazy('vgit.settings.status_diff_view')
2224
local git_buffer_store = lazy('vgit.git.git_buffer_store')
2325
local LiveGutter = lazy('vgit.features.buffer.LiveGutter')
2426
local live_blame_setting = lazy('vgit.settings.live_blame')
2527
local live_gutter_setting = lazy('vgit.settings.live_gutter')
28+
local LiveConflict = lazy('vgit.features.buffer.LiveConflict')
2629
local file_diff_view_setting = lazy('vgit.settings.file_diff_view')
30+
local status_diff_view_setting = lazy('vgit.settings.status_diff_view')
2731
local project_diff_view_setting = lazy('vgit.settings.project_diff_view')
28-
local LiveConflict = lazy('vgit.features.buffer.LiveConflict')
2932

3033
local hunks = Hunks()
3134
local conflicts = Conflicts()
@@ -214,7 +217,6 @@ controller.execute_command = event.async(function(args)
214217
}
215218

216219
if porcelain_commands[cmd] then
217-
local router = require('vgit.cli.router')
218220
router.execute(args.fargs)
219221
return
220222
end
@@ -253,7 +255,6 @@ function controller.autocomplete(arg_lead, cmd_line, _)
253255
end
254256

255257
if cmd == 'diff' or cmd == 'blame' or cmd == 'hunk' then
256-
local git_porcelain = require('vgit.cli.GitPorcelain')
257258
local porcelain = git_porcelain()
258259
local command_def = porcelain:get_command(cmd)
259260
if command_def and command_def.options then

lua/vgit/cli/commands/blame.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ local function execute_lens_blame(opts, repo, repo_path, filename)
111111

112112
local diff = repo:diff({
113113
type = 'blame',
114-
filename = filename,
114+
filename = blame.filename or filename,
115+
old_filename = blame.old_filename,
115116
blame_commit = commit_hash,
116117
parent_commit = parent_hash,
117118
layout_type = layout_type,

lua/vgit/core/Buffer.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ function Buffer:get_lines(top, bot)
165165
end
166166

167167
function Buffer:get_option(key)
168-
return vim.api.nvim_buf_get_option(self.bufnr, key)
168+
return vim.api.nvim_get_option_value(key, { buf = self.bufnr })
169169
end
170170

171171
function Buffer:set_option(key, value)
172172
if key == 'modifiable' then self._modifiable = value end
173-
pcall(vim.api.nvim_buf_set_option, self.bufnr, key, value)
173+
pcall(vim.api.nvim_set_option_value, key, value, { buf = self.bufnr })
174174
return self
175175
end
176176

@@ -181,7 +181,7 @@ function Buffer:set_lines(lines, top, bot)
181181

182182
local modifiable = self._modifiable
183183
if modifiable == nil then
184-
modifiable = vim.api.nvim_buf_get_option(bufnr, 'modifiable')
184+
modifiable = vim.api.nvim_get_option_value('modifiable', { buf = bufnr })
185185
self._modifiable = modifiable
186186
end
187187

@@ -190,9 +190,9 @@ function Buffer:set_lines(lines, top, bot)
190190
return self
191191
end
192192

193-
vim.api.nvim_buf_set_option(bufnr, 'modifiable', true)
193+
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
194194
vim.api.nvim_buf_set_lines(bufnr, top, bot, false, lines)
195-
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
195+
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
196196

197197
return self
198198
end
@@ -202,7 +202,7 @@ function Buffer:assign_options(options)
202202

203203
for key, value in pairs(options) do
204204
if key == 'modifiable' then self._modifiable = value end
205-
vim.api.nvim_buf_set_option(bufnr, key, value)
205+
vim.api.nvim_set_option_value(key, value, { buf = bufnr })
206206
end
207207

208208
return self

lua/vgit/core/Buffer_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('Buffer:', function()
183183

184184
describe('set_option', function()
185185
it('should set buffer option', function()
186-
vim.api.nvim_buf_set_option(buffer.bufnr, 'ft', 'lua')
186+
vim.api.nvim_set_option_value('ft', 'lua', { buf = buffer.bufnr })
187187
assert.equals(buffer:get_option('ft'), 'lua')
188188
end)
189189

lua/vgit/core/Window.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Window.open_screen(buffer, config)
1818

1919
if config.win_options then
2020
for key, value in pairs(config.win_options) do
21-
pcall(vim.api.nvim_win_set_option, win_id, key, value)
21+
pcall(vim.api.nvim_set_option_value, key, value, { win = win_id })
2222
end
2323
end
2424

@@ -80,7 +80,7 @@ function Window:set_lnum(lnum)
8080
end
8181

8282
function Window:set_option(key, value)
83-
pcall(vim.api.nvim_win_set_option, self.win_id, key, value)
83+
pcall(vim.api.nvim_set_option_value, key, value, { win = self.win_id })
8484
return self
8585
end
8686

@@ -106,15 +106,15 @@ end
106106

107107
function Window:assign_options(options)
108108
for key, value in pairs(options) do
109-
vim.api.nvim_win_set_option(self.win_id, key, value)
109+
vim.api.nvim_set_option_value(key, value, { win = self.win_id })
110110
end
111111
return self
112112
end
113113

114114
function Window:get_options(option_names)
115115
local options = {}
116116
for _, name in ipairs(option_names) do
117-
local ok, value = pcall(vim.api.nvim_win_get_option, self.win_id, name)
117+
local ok, value = pcall(vim.api.nvim_get_option_value, name, { win = self.win_id })
118118
if ok then options[name] = value end
119119
end
120120
return options

lua/vgit/core/Window_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('Window:', function()
155155
win = Window:open(buffer, default_float_opts)
156156
win:set_option('wrap', false)
157157

158-
assert.is_false(vim.api.nvim_win_get_option(win.win_id, 'wrap'))
158+
assert.is_false(vim.api.nvim_get_option_value('wrap', { win = win.win_id }))
159159
end)
160160
end)
161161

@@ -196,8 +196,8 @@ describe('Window:', function()
196196
win = Window:open(buffer, default_float_opts)
197197
win:assign_options({ wrap = false, number = true })
198198

199-
assert.is_false(vim.api.nvim_win_get_option(win.win_id, 'wrap'))
200-
assert.is_true(vim.api.nvim_win_get_option(win.win_id, 'number'))
199+
assert.is_false(vim.api.nvim_get_option_value('wrap', { win = win.win_id }))
200+
assert.is_true(vim.api.nvim_get_option_value('number', { win = win.win_id }))
201201
end)
202202
end)
203203

lua/vgit/core/diff/DiffBuilder.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ function DiffBuilder:_get_blame_lines(spec)
2727
local blame_commit = spec.blame_commit
2828
local parent_commit = spec.parent_commit
2929
local filename = spec.filename
30+
local old_filename = spec.old_filename
3031

3132
assertion.assert(blame_commit, 'blame_commit is required').assert(filename, 'filename is required')
3233

3334
local effective_parent_commit = parent_commit or blame_commit .. '~1'
3435

35-
local original_lines = self._repository:file_lines(filename, effective_parent_commit)
36+
local original_lines = self._repository:file_lines(old_filename or filename, effective_parent_commit)
3637
local current_lines = self._repository:file_lines(filename, blame_commit)
3738

3839
return original_lines, current_lines
@@ -178,7 +179,8 @@ function DiffBuilder:build(spec)
178179
display_lines = original_lines
179180
end
180181

181-
local hunks = (spec.hunks and #spec.hunks > 0) and spec.hunks or hunk_generator:generate(original_lines, current_lines, spec)
182+
local hunks = (spec.hunks and #spec.hunks > 0) and spec.hunks
183+
or hunk_generator:generate(original_lines, current_lines, spec)
182184
assertion.assert(hunks, 'hunk generator returned nil')
183185

184186
local layout_generator = DiffLayoutGenerator()

lua/vgit/core/fs_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('fs:', function()
155155
local bufnr = vim.api.nvim_create_buf(true, true)
156156
local buffer = Buffer(bufnr)
157157

158-
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'bar')
158+
vim.api.nvim_set_option_value('filetype', 'bar', { buf = bufnr })
159159
eq(fs.filetype(buffer), 'bar')
160160
end)
161161

lua/vgit/core/utils/math.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
local jit_uuid = require('vgit.vendor.jit-uuid')
2+
13
local M = {}
24

35
function M.round(x)
46
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
57
end
68

79
function M.uuid()
8-
return require('vgit.vendor.jit-uuid').generate_v4()
10+
return jit_uuid.generate_v4()
911
end
1012

1113
function M.scale_unit_up(unit, percent)

lua/vgit/features/screens/BlameView.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ function BlameView:enter_parent()
474474
})
475475

476476
local reponame = self._opts.reponame
477-
local parent_filename = blame.previous_filename or self._opts.filename
477+
local parent_filename = blame.old_filename or self._opts.filename
478478

479479
local new_blames, blame_err = git_blame.list(reponame, parent_filename, parent_hash)
480480
if blame_err or not new_blames or #new_blames == 0 then
@@ -550,7 +550,7 @@ function BlameView:show_commit_diff()
550550

551551
local commit_hash = blame.commit_hash or blame.hash
552552
local parent_hash = blame.parent_hash or blame._parent_hash or (commit_hash .. '~1')
553-
local filename = blame.previous_filename or self._opts.filename
553+
local filename = blame.filename or self._opts.filename
554554

555555
local repo, repo_err = repository.current()
556556
if repo_err then
@@ -563,6 +563,7 @@ function BlameView:show_commit_diff()
563563
local diff, diff_err = repo:diff({
564564
type = 'blame',
565565
filename = filename,
566+
old_filename = blame.old_filename,
566567
blame_commit = commit_hash,
567568
parent_commit = parent_hash,
568569
layout_type = layout_type,

0 commit comments

Comments
 (0)