diff --git a/bundle/git.vim/autoload/git/reflog.vim b/bundle/git.vim/autoload/git/reflog.vim index 2c68c659b..aa6dc40db 100644 --- a/bundle/git.vim/autoload/git/reflog.vim +++ b/bundle/git.vim/autoload/git/reflog.vim @@ -1,6 +1,22 @@ +"============================================================================= +" reflog.vim --- git-reflog +" Copyright (c) 2016-2019 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg@outlook.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + let s:JOB = SpaceVim#api#import('job') let s:BUFFER = SpaceVim#api#import('vim#buffer') -" @todo rewrite Git reflog in lua + +"" +" @section git-reflog, reflog +" @parentsection commands +" This commands is to view git reflog. +" > +" :Git reflog +" < + function! git#reflog#run(args) let cmd = ['git', 'reflog'] + a:args let s:bufnr = s:openRefLogBuffer() diff --git a/bundle/git.vim/doc/git.txt b/bundle/git.vim/doc/git.txt index 732e3f710..f6e832aff 100644 --- a/bundle/git.vim/doc/git.txt +++ b/bundle/git.vim/doc/git.txt @@ -10,8 +10,9 @@ CONTENTS *git-contents* 3. git-cherry-pick.....................................|git-cherry-pick| 4. git-clean.................................................|git-clean| 5. git-mv.......................................................|git-mv| - 6. git-rm.......................................................|git-rm| - 7. git-stash.................................................|git-stash| + 6. git-reflog...............................................|git-reflog| + 7. git-rm.......................................................|git-rm| + 8. git-stash.................................................|git-stash| 3. Functions.................................................|git-functions| ============================================================================== @@ -68,6 +69,14 @@ the index. For example, rename current file. :Git mv % new_file.txt < +============================================================================== +GIT-REFLOG *git-reflog* + +This commands is to view git reflog. +> + :Git reflog +< + ============================================================================== GIT-RM *git-rm* diff --git a/bundle/git.vim/lua/git/command/reflog.lua b/bundle/git.vim/lua/git/command/reflog.lua new file mode 100644 index 000000000..9144373a4 --- /dev/null +++ b/bundle/git.vim/lua/git/command/reflog.lua @@ -0,0 +1,98 @@ +--============================================================================= +-- reflog.lua --- git-reflog +-- Copyright (c) 2016-2022 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +local M = {} + +local reflog_stdout = {} +local reflog_stderr = {} +local jobid = -1 +local reflog_bufid = -1 + +local job = require('spacevim.api.job') +local nt = require('spacevim.api.notify') + +local log = require('git.log') + +local function on_stdout(id, data) + if id ~= jobid then + return + end + + for _, v in ipairs(data) do + table.insert(reflog_stdout, v) + end +end + +local function on_stderr(id, data) + if id ~= jobid then + return + end + + for _, v in ipairs(data) do + table.insert(reflog_stderr, v) + end +end + +local function open_reflog_buffer() + local previous_buf = vim.api.nvim_get_current_buf() + vim.cmd('edit git://reflog') + vim.cmd([[ + normal! "_dd + setl nobuflisted + setl nomodifiable + setl nonumber norelativenumber + setl buftype=nofile + setl bufhidden=wipe + setf git-reflog + ]]) + vim.api.nvim_buf_set_keymap(vim.api.nvim_get_current_buf(), 'n', 'q', '', { + callback = function () + if vim.api.nvim_buf_is_valid(previous_buf) then + vim.api.nvim_set_current_buf(previous_buf) + else + vim.cmd('bd') + end + end, + }) + return vim.api.nvim_get_current_buf() +end + +local function on_exit(id, code, signal) + log.debug(string.format('git-reflog exit code %d, signal %d', code, signal)) + + if code == 0 and signal == 0 then + reflog_bufid = open_reflog_buffer() + + vim.api.nvim_buf_set_option(reflog_bufid, 'modifiable', true) + vim.api.nvim_buf_set_lines(reflog_bufid, 0, -1, false, reflog_stdout) + vim.api.nvim_buf_set_option(reflog_bufid, 'modifiable', false) + else + nt.notify(string.format('failed to run git reflog, exit code: %d', code), 'WaringMsg') + end + + jobid = -1 +end + +function M.run(argv) + local cmd = { 'git', 'reflog' } + + for _, v in ipairs(argv) do + table.insert(cmd, v) + end + + log.debug('git-reflog cmd:' .. vim.inspect(cmd)) + reflog_stdout = {} + reflog_stderr = {} + jobid = job.start(cmd, { + on_stderr = on_stderr, + on_stdout = on_stdout, + on_exit = on_exit, + }) +end + +return M diff --git a/bundle/git.vim/lua/git/init.lua b/bundle/git.vim/lua/git/init.lua index 9eb0de60d..318d15e72 100644 --- a/bundle/git.vim/lua/git/init.lua +++ b/bundle/git.vim/lua/git/init.lua @@ -21,6 +21,7 @@ local cmds = { 'remote', 'reset', 'rm', + 'reflog', 'shortlog', 'status', 'tag', diff --git a/docs/roadmap.md b/docs/roadmap.md index 5268ce51a..a32099598 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -37,7 +37,7 @@ If you have any suggestions , please checkout feedback section on [community](.. - [x] `:Git pull` - [x] `:Git push` - [ ] `:Git rebase` - - [ ] `:Git reflog` + - [x] `:Git reflog` - [x] `:Git remote` - [x] `:Git reset` - [x] `:Git rm`