1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:50:05 +08:00

feat(git): lua complete for git-fetch

This commit is contained in:
Eric Wong 2024-12-17 19:13:47 +08:00
parent 7959583115
commit eacd71c784
3 changed files with 87 additions and 35 deletions

View File

@ -1,6 +1,29 @@
let s:JOB = SpaceVim#api#import('job') "=============================================================================
" fetch.vim --- Git fetch command
" Copyright 2021 Eric Wong
" Author: Eric Wong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
function! git#fetch#run(args)
if has('nvim-0.9.0')
function! git#fetch#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.fetch").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
""
" @section git-fetch, fetch
" @parentsection commands
" This commands is to fetch remote repo.
" >
" :Git fetch --all
" <
let s:JOB = SpaceVim#api#import('job')
function! git#fetch#run(args)
let cmd = ['git', 'fetch'] + a:args let cmd = ['git', 'fetch'] + a:args
call git#logger#debug('git-fetch cmd:' . string(cmd)) call git#logger#debug('git-fetch cmd:' . string(cmd))
@ -10,18 +33,18 @@ function! git#fetch#run(args)
\ } \ }
\ ) \ )
endfunction endfunction
function! s:on_exit(id, data, event) abort function! s:on_exit(id, data, event) abort
call git#logger#debug('git-fetch exit data:' . string(a:data)) call git#logger#debug('git-fetch exit data:' . string(a:data))
if a:data ==# 0 if a:data ==# 0
echo 'fetch done!' echo 'fetch done!'
else else
echo 'fetch failed!' echo 'fetch failed!'
endif endif
endfunction endfunction
function! git#fetch#complete(ArgLead, CmdLine, CursorPos) function! git#fetch#complete(ArgLead, CmdLine, CursorPos)
if a:ArgLead =~# '^-' if a:ArgLead =~# '^-'
return s:options() return s:options()
@ -33,15 +56,16 @@ function! git#fetch#complete(ArgLead, CmdLine, CursorPos)
return '' return ''
endif endif
endfunction endfunction
function! s:remotes() abort function! s:remotes() abort
return map(systemlist('git remote'), 'trim(v:val)') return map(systemlist('git remote'), 'trim(v:val)')
endfunction endfunction
function! s:options() abort function! s:options() abort
return join([ return join([
\ '--all', \ '--all',
\ '--multiple', \ '--multiple',
\ ], "\n") \ ], "\n")
endfunction endfunction
endif

View File

@ -10,11 +10,12 @@ CONTENTS *git-contents*
3. git-checkout............................................ |git-checkout| 3. git-checkout............................................ |git-checkout|
4. git-cherry-pick...................................... |git-cherry-pick| 4. git-cherry-pick...................................... |git-cherry-pick|
5. git-clean.................................................. |git-clean| 5. git-clean.................................................. |git-clean|
6. git-mv........................................................ |git-mv| 6. git-fetch.................................................. |git-fetch|
7. git-reflog................................................ |git-reflog| 7. git-mv........................................................ |git-mv|
8. git-rm........................................................ |git-rm| 8. git-reflog................................................ |git-reflog|
9. git-stash.................................................. |git-stash| 9. git-rm........................................................ |git-rm|
10. git-tag..................................................... |git-tag| 10. git-stash................................................. |git-stash|
11. git-tag..................................................... |git-tag|
3. Functions.................................................. |git-functions| 3. Functions.................................................. |git-functions|
============================================================================== ==============================================================================
@ -70,6 +71,14 @@ This commands is to run `git clean`.
:Git clean -f :Git clean -f
< <
==============================================================================
GIT-FETCH *git-fetch*
This commands is to fetch remote repo.
>
:Git fetch --all
<
============================================================================== ==============================================================================
GIT-MV *git-mv* GIT-MV *git-mv*

View File

@ -1,12 +1,12 @@
--============================================================================= --=============================================================================
-- fetch.lua --- :Git fetch -- fetch.lua --- Git fetch
-- Copyright (c) 2016-2022 Wang Shidong & Contributors -- Copyright 2023 Eric Wong
-- Author: Wang Shidong < wsdjeg@outlook.com > -- Author: Eric Wong < wsdjeg@outlook.com >
-- URL: https://spacevim.org -- URL: https://spacevim.org
-- License: GPLv3 -- License: GPLv3
--============================================================================= --=============================================================================
local m = {} local M = {}
local job = require('spacevim.api.job') local job = require('spacevim.api.job')
local nt = require('spacevim.api.notify') local nt = require('spacevim.api.notify')
@ -28,7 +28,7 @@ local function on_std(id, data)
end end
end end
function m.run(argv) function M.run(argv)
local cmd = { 'git', 'fetch' } local cmd = { 'git', 'fetch' }
for _, v in ipairs(argv) do for _, v in ipairs(argv) do
table.insert(cmd, v) table.insert(cmd, v)
@ -38,8 +38,27 @@ function m.run(argv)
job.start(cmd, { job.start(cmd, {
on_exit = on_exit, on_exit = on_exit,
on_stdout = on_std, on_stdout = on_std,
on_stderr = on_std on_stderr = on_std,
}) })
end end
return m local function get_remotes()
return vim.tbl_map(function(t)
return vim.fn.trim(t)
end, vim.fn.systemlist('git remote'))
end
function M.complete(ArgLead, CmdLine, CursorPos)
if vim.startswith(ArgLead, '-') then
return table.concat({ '--all', '--multiple' }, '\n')
end
local str = string.sub(CmdLine, 1, CursorPos)
if vim.regex([[^Git\s\+fetch\s\+[^ ]*$]]):match_str(str) then
return table.concat(get_remotes(), '\n')
else
return ''
end
end
return M