-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(git): add better git abstraction
- Loading branch information
Showing
14 changed files
with
652 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
local fs = require('vgit.core.fs') | ||
local Diff = require('vgit.core.Diff') | ||
local Object = require('vgit.core.Object') | ||
local git_repo = require('vgit.git.git_repo') | ||
local git_show = require('vgit.git.git_show') | ||
local git_hunks = require('vgit.git.git_hunks') | ||
local git_status = require('vgit.git.git_status') | ||
|
||
local GitFile = Object:extend() | ||
|
||
function GitFile:constructor(reponame, filename, revision) | ||
if not reponame then error('reponame is required') end | ||
if not filename then error('filename is required') end | ||
if not revision then error('revision is required') end | ||
|
||
return { | ||
reponame = reponame, | ||
filename = filename, | ||
revision = revision, | ||
state = { | ||
status = nil, | ||
is_tracked = nil | ||
} | ||
} | ||
end | ||
|
||
function GitFile:is_tracked() | ||
if self.state.is_tracked then return self.state.is_tracked end | ||
local is_tracked, err = git_repo.has(self.reponame, self.filename) | ||
if err then return nil, err end | ||
|
||
self.state.is_tracked = is_tracked | ||
|
||
return is_tracked, err | ||
end | ||
|
||
function GitFile:status() | ||
if self.state.status then return self.state.status end | ||
|
||
local status, err = git_status.get( | ||
self.reponame, | ||
self.filename, | ||
self.revision ~= 'INDEX' and self.revision or nil | ||
) | ||
if err then return nil, err end | ||
self.state.status = status | ||
|
||
return status | ||
end | ||
|
||
function GitFile:live_hunks(current_lines) | ||
if not current_lines then return nil, { 'lines is required' } end | ||
if self.revision ~= 'INDEX' then return nil, { 'invalid revision for live hunk' } end | ||
if not self:is_tracked() then return git_hunks.custom(current_lines, { untracked = true }) end | ||
|
||
local original_lines, err = fs.read_file(self.filename) | ||
if err then return nil, err end | ||
|
||
return git_hunks.live(original_lines, current_lines) | ||
end | ||
|
||
function GitFile:diff(shape) | ||
local reponame = self.reponame | ||
local filename = self.filename | ||
|
||
if self.revision == 'INDEX' then | ||
local status, err = self:status() | ||
if err then return nil, err end | ||
if not status then return nil, { 'status not found' } end | ||
|
||
local lines = nil | ||
local hunks = nil | ||
|
||
if status:unmerged() then | ||
if status:has_both('UD') then | ||
lines, err = git_show.lines(reponame, filename, ':2') | ||
if err then return nil, err end | ||
hunks, err = git_hunks.list_hunks(filename, { lines = lines, deleted = true }) | ||
if err then return nil, err end | ||
elseif status:has_both('DU') then | ||
lines, err = git_show.lines(reponame, filename, ':3') | ||
if err then return nil, err end | ||
hunks, err = git_hunks.list_hunks(filename, { lines = lines, untracked = true }) | ||
if err then return nil, err end | ||
else | ||
lines, err = git_show.lines(reponame, filename, 'HEAD') | ||
if err then return nil, err end | ||
if status:has_either('DD') then | ||
hunks, err = git_hunks.list_hunks(filename, { lines = lines, deleted = true }) | ||
else | ||
hunks, err = git_show.lines(reponame, filename, 'HEAD') | ||
end | ||
if err then return nil, err end | ||
end | ||
else | ||
local path = string.format('%s%s%s', self.reponame, fs.sep, self.filename) | ||
lines, err = fs.read_file(path) | ||
if err then return nil, err end | ||
hunks, err = self:live_hunks(lines) | ||
if err then return nil, err end | ||
end | ||
|
||
return Diff():generate(hunks, lines, shape) | ||
end | ||
end | ||
|
||
return GitFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
local fs = require('vgit.core.fs') | ||
local Object = require('vgit.core.Object') | ||
local git_log = require('vgit.git.git_log') | ||
local git_repo = require('vgit.git.git_repo') | ||
|
||
local GitRepository = Object:extend() | ||
|
||
function GitRepository:constructor(path) | ||
local name, err = git_repo.discover(path) | ||
if err then error(string.format('failed to access .git at "%s"', path)) end | ||
|
||
return { name = name } | ||
end | ||
|
||
function GitRepository.exists(path) | ||
return git_repo.exists(path) | ||
end | ||
|
||
function GitRepository:logs(opts) | ||
opts = opts or {} | ||
|
||
return git_log.list(self.name, { | ||
from = opts.from, | ||
count = opts.count, | ||
stashed = opts.stashed, | ||
}) | ||
end | ||
|
||
function GitRepository:state() | ||
local git_dir = string.format('%s/.git', self.name) | ||
|
||
if fs.exists(string.format('%s/rebase-apply/applying', git_dir)) then return 'APPLY-MAILBOX' end | ||
if fs.exists(string.format('%s/rebase-apply/rebasing', git_dir)) then return 'REBASE' end | ||
if fs.exists(string.format('%s/rebase-apply', git_dir)) then return 'APPLY-MAILBOX-REBASE' end | ||
if fs.exists(string.format('%s/rebase-merge/interactive', git_dir)) then return 'REBASE-INTERACTIVE' end | ||
if fs.exists(string.format('%s/rebase-merge', git_dir)) then return 'REBASE' end | ||
if fs.exists(string.format('%s/CHERRY_PICK_HEAD', git_dir)) then | ||
if fs.exists(string.format('%s/sequencer/todo', git_dir)) then return 'CHERRY-PICK-SEQUENCE' end | ||
return 'CHERRY-PICK' | ||
end | ||
if fs.exists(string.format('%s/MERGE_HEAD', git_dir)) then return 'MERGE' end | ||
if fs.exists(string.format('%s/BISECT_LOG', git_dir)) then return 'BISECT' end | ||
if fs.exists(string.format('%s/REVERT_HEAD', git_dir)) then | ||
if fs.exists(string.format('%s/sequencer/todo', git_dir)) then return 'REVERT-SEQUENCE' end | ||
return 'REVERT' | ||
end | ||
|
||
return nil | ||
end | ||
|
||
function GitRepository:commit(commit_hash) end | ||
|
||
function GitRepository:tree(commit_hash) end | ||
|
||
function GitRepository:file(commit_hash) end | ||
|
||
return GitRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
local Object = require('vgit.core.Object') | ||
|
||
local GitTree = Object:extend() | ||
|
||
function GitTree:constructor() | ||
return {} | ||
end | ||
|
||
function GitTree:entries(commit_hash) end | ||
|
||
return GitTree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.