Skip to content

Commit

Permalink
fix(project_commits_preview): fix incorrect diff
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jun 17, 2024
1 parent f17f8d6 commit cf6d8d0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 293 deletions.
76 changes: 28 additions & 48 deletions lua/vgit/features/screens/ProjectCommitsScreen/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ function Store:fetch(shape, commits, opts)
return nil, self.err
end

-- We will use the parent_hash and the commit_hash inside
-- the log object to list all the files associated with the log.
loop.free_textlock()
local statuses, status_err = git_status.tree(reponame, {
commit_hash = log.commit_hash,
Expand All @@ -86,19 +84,15 @@ function Store:fetch(shape, commits, opts)
end

data[commit] = utils.list.map(statuses, function(status)
-- Log contains the metadata about parent_hash and commit_hash
-- File contains the name of the file in that particular working tree.
-- Using commit_hash, parent_hash and the name of the file we can easily get the lines
-- and hunks to recreate the diffs by feeding this info into our algorithm.
local datum = {
id = utils.math.uuid(),
commit = commit,
local id = utils.math.uuid()
local entry = {
id = id,
log = log,
file = status,
status = status,
}
self.state.commits[datum.id] = datum
self.state.commits[id] = entry

return datum
return entry
end)
end

Expand All @@ -119,59 +113,45 @@ function Store:get(id)
if self.err then return nil, self.err end
if id then self.id = id end

local datum = self.state.commits[self.id]
if not datum then return nil, { 'Item not found' } end
local entry = self.state.commits[self.id]
if not entry then return nil, { 'Item not found' } end

return datum
return entry
end

function Store:get_diff()
local datum, err = self:get()
local entry, err = self:get()
if err then return nil, err end
if not datum then return nil, { 'no data found' } end
if not entry then return nil, { 'no data found' } end

local file = datum.file
local file = entry.status
if not file then return nil, { 'No file found in item' } end

local log = datum.log
local log = entry.log
if not log then return nil, { 'No log found in item' } end

local id = file.id
local filename = file.filename
local parent_hash = log.parent_hash
local commit_hash = log.commit_hash
local current_commit = datum.commit

if self.state.commits[id] then return self.state.commits[id] end

local lines_err, lines
local is_deleted = false

loop.free_textlock()
local is_deleted = file:has_either('DD')
local reponame = git_repo.discover()
if not git_repo.has(reponame, filename, commit_hash) then
if not git_repo.has(reponame, filename, parent_hash) then
lines, lines_err = git_show.lines(reponame, filename, current_commit)
else
is_deleted = true
lines, lines_err = git_show.lines(reponame, filename, parent_hash)
end
local lines_err, lines
if is_deleted then
lines, lines_err = git_show.lines(reponame, filename, parent_hash)
else
lines, lines_err = git_show.lines(reponame, filename, commit_hash)
end
loop.free_textlock()

if lines_err then return nil, lines_err end

local hunks_err, hunks
if is_deleted then
hunks = git_hunks.custom(lines, { deleted = true })
else
hunks, hunks_err = git_hunks.list(reponame, filename, {
parent = parent_hash,
current = commit_hash,
})
end
local hunks, hunks_err = git_hunks.list(reponame, filename, {
parent = parent_hash,
current = commit_hash,
})
loop.free_textlock()
if hunks_err then return nil, hunks_err end

Expand All @@ -183,31 +163,31 @@ function Store:get_diff()
end

function Store:get_filename()
local datum, err = self:get()
local entry, err = self:get()
if err then return nil, err end

return datum.file.filename
return entry.status.filename
end

function Store:get_filetype()
local datum, err = self:get()
local entry, err = self:get()
if err then return nil, err end

return datum.file.filetype
return entry.status.filetype
end

function Store:get_lnum()
return self.state.lnum
end

function Store:get_parent_commit()
local datum, err = self:get()
local entry, err = self:get()
if err then return nil, err end

local file = datum.file
local file = entry.status
if not file then return nil, { 'No file found in item' } end

local log = datum.log
local log = entry.log
if not log then return nil, { 'No log found in item' } end

return log.parent_hash
Expand Down
4 changes: 2 additions & 2 deletions lua/vgit/features/screens/ProjectCommitsScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local console = require('vgit.core.console')
local git_repo = require('vgit.git.git_repo')
local git_show = require('vgit.git.git_show')
local DiffView = require('vgit.ui.views.DiffView')
local FSListGenerator = require('vgit.ui.FSListGenerator')
local StatusListGenerator = require('vgit.ui.StatusListGenerator')
local FoldableListView = require('vgit.ui.views.FoldableListView')
local Store = require('vgit.features.screens.ProjectCommitsScreen.Store')

Expand Down Expand Up @@ -46,7 +46,7 @@ function ProjectCommitsScreen:constructor(opts)
foldable_list[#foldable_list + 1] = {
open = true,
value = commit_hash,
items = FSListGenerator(files):generate(),
items = StatusListGenerator(files):generate(),
}
end

Expand Down
8 changes: 4 additions & 4 deletions lua/vgit/features/screens/ProjectDiffScreen/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function Store:conflict_status()
return git_conflict.status(reponame)
end

function Store:get_file_lines(status, type)
function Store:get_lines(status, type)
local filename = status.filename
local reponame = git_repo.discover()

Expand All @@ -169,7 +169,7 @@ function Store:get_file_lines(status, type)
return fs.read_file(filename)
end

function Store:get_file_hunks(status, type, lines)
function Store:get_hunks(status, type, lines)
local filename = status.filename
local reponame = git_repo.discover()

Expand Down Expand Up @@ -207,10 +207,10 @@ function Store:get_diff()
local cache_key = string.format('%s-%s-%s', id, type, status.id)
if self.state.diffs[cache_key] then return self.state.diffs[cache_key] end

local lines, lines_err = self:get_file_lines(status, type)
local lines, lines_err = self:get_lines(status, type)
if lines_err then return nil, lines_err end

local hunks, hunks_err = self:get_file_hunks(status, type, lines)
local hunks, hunks_err = self:get_hunks(status, type, lines)
if hunks_err then return nil, hunks_err end

loop.free_textlock()
Expand Down
2 changes: 1 addition & 1 deletion lua/vgit/features/screens/ProjectLogsScreen/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Store:fetch()
return self.data, self.err
end

function Store:get_data()
function Store:get_logs()
return self.data, self.err
end

Expand Down
2 changes: 1 addition & 1 deletion lua/vgit/features/screens/ProjectStashScreen/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Store:fetch(opts)
return self.data, self.err
end

function Store:get_data()
function Store:get_logs()
return self.data, self.err
end

Expand Down
8 changes: 4 additions & 4 deletions lua/vgit/git/git_status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ function git_status.tree(reponame, opts)
'--no-pager',
'diff-tree',
'--no-commit-id',
'--name-only',
'--name-status',
'-r',
commit_hash,
parent_hash == '' and empty_hash or parent_hash,
commit_hash,
})
if err then return nil, err end

local files = {}
for i = 1, #result do
local line = result[i]
files[#files + 1] = GitStatus(string.format('%s %s', '--', line))
local status, path = result[i]:match("(%w+)%s+(.+)")
files[#files + 1] = GitStatus(string.format('%s %s', status, path))
end

return files
Expand Down
Loading

0 comments on commit cf6d8d0

Please sign in to comment.