Skip to content

Commit

Permalink
feat #66 - hunk_stage
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 28, 2021
1 parent 3dbaa72 commit e1c0697
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 253 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<br />
<img src="https://user-images.githubusercontent.com/25164326/119602598-79808f80-bdb9-11eb-974c-aaa2a4445313.gif" alt="reset_hunk" />
</details>
<details>
<summary>Stage a hunk</summary>
<br />
<img src="https://user-images.githubusercontent.com/25164326/127396247-a3e16213-5865-455e-9f72-72da1315abd6.gif" alt="hunk_stage" />
</details>
<details>
<summary>Navigate through hunks</summary>
<br />
Expand Down Expand Up @@ -72,6 +77,13 @@
<img src="https://user-images.githubusercontent.com/25164326/121595739-95687000-ca0c-11eb-8f1d-9b5b398e3b0d.gif" alt="diff_preference" />
</details>

## Supported Neovim versions:
- Neovim > 0.5

## Supported Opperating System:
- linux-gnu*
- Darwin

## Prerequisites
- [Git](https://git-scm.com/)
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)
Expand Down Expand Up @@ -374,8 +386,9 @@ vim.api.nvim_set_keymap('n', '<leader>gq', ':VGit hunks_quickfix_list<CR>', {
| setup | Sets up the plugin for success |
| toggle_buffer_hunks | Shows hunk signs on buffers/Hides hunk signs on buffers |
| toggle_buffer_blames | Enables blames feature on buffers /Disables blames feature on buffers |
| hunk_preview | Opens a VGit view of a hunk, if cursor is on a line with a git change |
| hunk_reset | Removes the hunk from the buffer |
| hunk_preview | Opens a VGit view of a hunk, if cursor is on a hunk |
| hunk_reset | Removes the hunk from the buffer, if cursor is on a hunk |
| hunk_stage | Stages a hunk, if cursor is on a hunk |
| hunk_down | Navigate downward through a hunk |
| hunk_up | Navigate upwards through a hunk |
| buffer_preview | Shows the current differences in lines in the current buffer |
Expand All @@ -397,5 +410,4 @@ vim.api.nvim_set_keymap('n', '<leader>gq', ':VGit hunks_quickfix_list<CR>', {
## Similar Git Plugins
- [vim-fugitive](https://github.com/tpope/vim-fugitive)
- [vim-gitgutter](https://github.com/airblade/vim-gitgutter)
- [gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim)
- [neogit](https://github.com/TimUntersberger/neogit)
- [vim-signify](https://github.com/mhinz/vim-signify)
175 changes: 0 additions & 175 deletions lua/vgit/algorithms.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/vgit/defer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ M.throttle_leading = function(fn, ms)
end
end

function M.debounce_trailing(fn, ms)
M.debounce_trailing = function(fn, ms)
local timer = vim.loop.new_timer()
return function(...)
local argv = {...}
Expand Down
39 changes: 29 additions & 10 deletions lua/vgit/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local vim = vim

local M = {}

M.relative_path = function(filepath)
M.relative_filename = function(filepath)
assert(type(filepath) == 'string', 'type error :: expected string')
local cwd = vim.loop.cwd()
if not cwd or not filepath then return filepath end
Expand All @@ -19,13 +19,26 @@ M.relative_path = function(filepath)
return filepath
end

M.short_filename = function(filepath)
assert(type(filepath) == 'string', 'type error :: expected string')
local filename = ''
for i = #filepath, 1, -1 do
local letter = filepath:sub(i, i)
if letter == '/' then
break
end
filename = letter .. filename
end
return filename
end

M.project_relative_filename = function(filepath, project_files)
assert(type(filepath) == 'string', 'type error :: expected string')
assert(vim.tbl_islist(project_files), 'type error :: expected table of type list')
table.sort(project_files)
if filepath == '' then
return filepath
return nil
end
table.sort(project_files)
for i = #filepath, 1, -1 do
local letter = filepath:sub(i, i)
local new_project_files = {}
Expand All @@ -39,7 +52,19 @@ M.project_relative_filename = function(filepath, project_files)
end
project_files = new_project_files
end
return project_files[1]
local project_filename = project_files[1]
if project_filename then
local short_filename = M.short_filename(filepath)
local project_short_filename = M.short_filename(project_filename)
return (short_filename == project_short_filename and project_filename) or nil
end
return nil
end

M.filename = function(buf)
assert(type(buf) == 'number', 'type error :: expected number')
local filepath = vim.api.nvim_buf_get_name(buf)
return M.relative_filename(filepath)
end

M.filetype = function(buf)
Expand All @@ -49,12 +74,6 @@ end

M.detect_filetype = pfiletype.detect

M.filename = function(buf)
assert(type(buf) == 'number', 'type error :: expected number')
local filepath = vim.api.nvim_buf_get_name(buf)
return M.relative_path(filepath)
end

M.tmpname = function()
local length = 6
local res = ''
Expand Down
45 changes: 44 additions & 1 deletion lua/vgit/git.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local fs = require('vgit.fs')
local Job = require('plenary.job')
local State = require('vgit.State')
local a = require('plenary.async')
Expand Down Expand Up @@ -111,6 +112,7 @@ end
M.create_hunk = function(header)
local previous, current = parse_hunk_header(header)
local hunk = {
header = header,
start = current[1],
finish = current[1] + current[2] - 1,
type = nil,
Expand All @@ -128,6 +130,20 @@ M.create_hunk = function(header)
return hunk
end

M.create_patch = function(filename, hunk)
local patch = {
string.format('diff --git a/%s b/%s', filename, filename),
'index 000000..000000',
string.format('--- a/%s', filename),
string.format('+++ a/%s', filename),
hunk.header,
}
for i = 1, #hunk.diff do
patch[#patch + 1] = hunk.diff[i]
end
return patch
end

M.create_blame = function(info)
local function split_by_whitespace(str)
return vim.split(str, ' ')
Expand Down Expand Up @@ -516,13 +532,40 @@ M.show = wrap(function(filename, commit_hash, callback)
job:start()
end, 3)

M.stage_hunk = wrap(function(filename, hunk, callback)
local patch = M.create_patch(filename, hunk)
local patch_filename = fs.tmpname()
fs.write_file(patch_filename, patch)
local err = {}
local job = Job:new({
command = 'git',
args = {
'apply',
'--cached',
'--unidiff-zero',
patch_filename,
},
on_stderr = function(_, data, _)
err[#err + 1] = data
end,
on_exit = function()
fs.remove_file(patch_filename)
if #err ~= 0 then
return callback(err)
end
callback(nil)
end,
})
job:start()
end, 3)

M.reset = wrap(function(filename, callback)
local err = {}
local job = Job:new({
command = 'git',
args = {
'checkout',
'HEAD',
M.get_diff_base(),
'--',
filename,
},
Expand Down
Loading

0 comments on commit e1c0697

Please sign in to comment.