Skip to content

Commit

Permalink
feat: git commits (#97)
Browse files Browse the repository at this point in the history
* WIP: add context

* WIP: add context to previewer

* WIP

* todo: test

* todo: test

* fix: git show

* todo: fix switch

* fix: invalid current buffer

* fix: swithc

* perf: set --date=short

* doc: git commits

* doc: keys

* doc: git
  • Loading branch information
linrongbin16 authored Aug 24, 2023
1 parent c36e378 commit ded9623
Show file tree
Hide file tree
Showing 16 changed files with 548 additions and 115 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This is the next generation of [fzfx.vim](https://github.com/linrongbin16/fzfx.v
- [rg](https://github.com/BurntSushi/ripgrep) (optional for **live grep**).
- [fd](https://github.com/sharkdp/fd) (optional for **files**).
- [bat](https://github.com/sharkdp/bat) (optional for preview files, e.g. the right side of **live grep**, **files**).
- [git](https://git-scm.com/) (optional for **git** commands).

> Note: **live grep**, **files** and preview files will use builtin unix/linux commands ([grep](https://man7.org/linux/man-pages/man1/grep.1.html), [find](https://man7.org/linux/man-pages/man1/find.1.html), [cat](https://man7.org/linux/man-pages/man1/cat.1.html)) if `rg`, `fd`, `bat` not installed.
>
Expand Down Expand Up @@ -232,6 +233,7 @@ Commands are named following below rules:
- The visual select variant is named with `V` suffix.
- The cursor word variant is named with `W` suffix.
- The yank text variant is named with `P` suffix (just like press the `p` key).
- The current buffer only variant is named with `B` suffix.

Especially for git commands:

Expand Down Expand Up @@ -361,6 +363,32 @@ Especially for git commands:
<td>FzfxGBranches(R)P</td>
<td>N</td>
</tr>
<tr>
<td rowspan="4">Git Commits</td>
<td>FzfxGCommits(B)</td>
<td>N</td>
<td>No</td>
<td>Yes</td>
<td rowspan="4">1. Use `enter` to copy commit SHA</td>
</tr>
<tr>
<td>FzfxGCommits(B)V</td>
<td>V</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>FzfxGCommits(B)W</td>
<td>N</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>FzfxGCommits(B)P</td>
<td>N</td>
<td>No</td>
<td>Yes</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -451,6 +479,26 @@ nnoremap <space>rwbr :\<C-U>FzfxGBranchesRW<CR>
nnoremap <space>pbr :\<C-U>FzfxGBranchesP<CR>
" remote branches by yank text
nnoremap <space>rpbr :\<C-U>FzfxGBranchesRP<CR>
" ======== git commits ========
" git commits
nnoremap <space>gc :\<C-U>FzfxGCommits<CR>
" by visual select
xnoremap <space>gc :\<C-U>FzfxGCommitsV<CR>
" only current buffer
nnoremap <space>bgc :\<C-U>FzfxGCommitsB<CR>
" only current buffer by visual select
xnoremap <space>bgc :\<C-U>FzfxGCommitsBV<CR>
" by cursor word
nnoremap <space>wgc :\<C-U>FzfxGCommitsW<CR>
" only current buffer by cursor word
nnoremap <space>bwgc :\<C-U>FzfxGCommitsBW<CR>
" by yank text
nnoremap <space>pgc :\<C-U>FzfxGCommitsP<CR>
" only current buffer by yank text
nnoremap <space>bpgc :\<C-U>FzfxGCommitsBP<CR>
```

### Lua
Expand Down Expand Up @@ -590,6 +638,36 @@ vim.keymap.set('n', '<space>pbr', '<cmd>FzfxGBranchesP<cr>',
-- remote branches by yank text
vim.keymap.set('n', '<space>rwbr', '<cmd>FzfxGBranchesRP<cr>',
{silent=true, noremap=true, desc="Search remote git branches by yank text"})

-- ======== git commits ========

-- git commits
vim.keymap.set('n', '<space>gc', '<cmd>FzfxGCommits<cr>',
{silent=true, noremap=true, desc="Search git commits"})
-- by visual select
vim.keymap.set('x', '<space>gc', '<cmd>FzfxGCommitsV<CR>',
{silent=true, noremap=true, desc="Search git commits"})
-- only current buffer
vim.keymap.set('n', '<space>bgc',
'<cmd>FzfxGCommitsB<cr>',
{silent=true, noremap=true, desc="Search git commits only on current buffer"})
-- only current buffer by visual select
vim.keymap.set('x', '<space>bgc',
'<cmd>FzfxGCommitsBV<CR>',
{silent=true, noremap=true, desc="Search git commits only on current buffer"})

-- cursor word
vim.keymap.set('n', '<space>wgc', '<cmd>FzfxGCommitsW<cr>',
{silent=true, noremap=true, desc="Search git commits by cursor word"})
-- only current buffer by cursor word
vim.keymap.set('n', '<space>bwgc', '<cmd>FzfxGCommitsBW<cr>',
{silent=true, noremap=true, desc="Search git commits only on current buffer by cursor word"})
-- yank text
vim.keymap.set('n', '<space>pgc', '<cmd>FzfxGCommitsP<cr>',
{silent=true, noremap=true, desc="Search git commits by yank text"})
-- only current buffer by yank text
vim.keymap.set('n', '<space>bpgc', '<cmd>FzfxGCommitsBP<cr>',
{silent=true, noremap=true, desc="Search git commits only on current buffer by yank text"})
```

## Configuration
Expand Down
Empty file added bin/general/previewer.lua
Empty file.
Empty file added bin/general/provider.lua
Empty file.
24 changes: 12 additions & 12 deletions bin/git_branches/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ local cmd = shell_helpers.read_provider_command(provider) --[[@as string]]
shell_helpers.log_debug("cmd:[%s]", cmd)

local git_root_cmd = shell_helpers.GitRootCommand:run()
shell_helpers.log_debug(
"git_root_cmd.result.stdout:%s",
vim.inspect(git_root_cmd.result.stdout)
)
shell_helpers.log_debug(
"git_root_cmd.result.stderr:%s",
vim.inspect(git_root_cmd.result.stderr)
)
shell_helpers.log_debug(
"git_root_cmd.result.exitcode:%s",
vim.inspect(git_root_cmd.result.exitcode)
)
-- shell_helpers.log_debug(
-- "git_root_cmd.result.stdout:%s",
-- vim.inspect(git_root_cmd.result.stdout)
-- )
-- shell_helpers.log_debug(
-- "git_root_cmd.result.stderr:%s",
-- vim.inspect(git_root_cmd.result.stderr)
-- )
-- shell_helpers.log_debug(
-- "git_root_cmd.result.exitcode:%s",
-- vim.inspect(git_root_cmd.result.exitcode)
-- )
if git_root_cmd:wrong() then
return
end
Expand Down
23 changes: 23 additions & 0 deletions bin/git_commits/previewer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
io.write(
string.format(
"|fzfx.bin.git_commits.previewer| error! SELF_PATH is empty!"
)
)
end
vim.opt.runtimepath:append(SELF_PATH)
local shell_helpers = require("fzfx.shell_helpers")

local previewer = _G.arg[1]
local commit = _G.arg[2]

shell_helpers.log_debug("previewer:[%s]", vim.inspect(previewer))
shell_helpers.log_debug("branch:[%s]", vim.inspect(commit))

commit = vim.fn.split(commit)[1]
local cmd = shell_helpers.read_provider_command(previewer) --[[@as string]]
cmd = string.format("%s %s", cmd, commit)

shell_helpers.log_debug("cmd:[%s]", cmd)
os.execute(cmd)
48 changes: 48 additions & 0 deletions bin/git_commits/provider.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
io.write(
string.format(
"|fzfx.bin.git_commits.provider| error! SELF_PATH is empty!"
)
)
end
vim.opt.runtimepath:append(SELF_PATH)
local shell_helpers = require("fzfx.shell_helpers")

local provider = _G.arg[1]

shell_helpers.log_debug("provider:[%s]", provider)

local cmd = shell_helpers.read_provider_command(provider) --[[@as string]]
shell_helpers.log_debug("cmd:[%s]", cmd)

local git_root_cmd = shell_helpers.GitRootCommand:run()
-- shell_helpers.log_debug(
-- "git_root_cmd.result.stdout:%s",
-- vim.inspect(git_root_cmd.result.stdout)
-- )
-- shell_helpers.log_debug(
-- "git_root_cmd.result.stderr:%s",
-- vim.inspect(git_root_cmd.result.stderr)
-- )
-- shell_helpers.log_debug(
-- "git_root_cmd.result.exitcode:%s",
-- vim.inspect(git_root_cmd.result.exitcode)
-- )
if git_root_cmd:wrong() then
return
end

local p = io.popen(cmd)
shell_helpers.log_ensure(
p ~= nil,
"error! failed to open pipe on cmd! %s",
vim.inspect(cmd)
)
--- @diagnostic disable-next-line: need-check-nil
for line in p:lines("*line") do
-- shell_helpers.log_debug("line:%s", vim.inspect(line))
io.write(string.format("%s\n", line))
end
--- @diagnostic disable-next-line: need-check-nil
p:close()
3 changes: 3 additions & 0 deletions lua/fzfx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ local function setup(options)
-- git branches
require("fzfx.git_branches").setup()

-- git commits
require("fzfx.git_commits").setup()

-- yank history
require("fzfx.yank_history").setup()
end
Expand Down
9 changes: 9 additions & 0 deletions lua/fzfx/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ local function git_checkout(lines)
end
end

local function yank_git_commit(lines)
if type(lines) == "table" and #lines > 0 then
local line = lines[#lines]
local git_commit = vim.fn.split(line)[1]
vim.api.nvim_command("let @+ = '" .. git_commit .. "'")
end
end

local M = {
nop = nop,
edit = edit,
Expand All @@ -170,6 +178,7 @@ local M = {
buffer_rg = buffer_rg,
bdelete = bdelete,
git_checkout = git_checkout,
yank_git_commit = yank_git_commit,
}

return M
8 changes: 1 addition & 7 deletions lua/fzfx/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ end
--- @param bufnr integer
--- @return boolean
local function buf_valid(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
return vim.api.nvim_buf_is_valid(bufnr)
and vim.api.nvim_buf_is_loaded(bufnr)
and vim.fn.buflisted(bufnr) > 0
and type(bufname) == "string"
and string.len(bufname) > 0
and not buf_exclude(bufnr)
return utils.is_buf_valid(bufnr) and not buf_exclude(bufnr)
end

--- @param bufnr integer
Expand Down
12 changes: 12 additions & 0 deletions lua/fzfx/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,16 @@ M.git_local_branches_header = function(action)
return render(":: Press %s to local mode", M.magenta, string.upper(action))
end

--- @param action string
--- @return string
M.git_all_commits_header = function(action)
return render(":: Press %s to all mode", M.magenta, string.upper(action))
end

--- @param action string
--- @return string
M.git_buffer_commits_header = function(action)
return render(":: Press %s to buffer mode", M.magenta, string.upper(action))
end

return M
Loading

0 comments on commit ded9623

Please sign in to comment.