1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-04 03:40:05 +08:00
SpaceVim/bundle/git.vim/autoload/git/checkout.vim

70 lines
1.9 KiB
VimL
Raw Normal View History

2024-12-09 22:29:20 +08:00
"=============================================================================
" checkout.vim --- checkout command
" Copyright (c) 2016 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
""
" @section git-checkout, checkout
" @parentsection commands
" This comamnd is to switch branches or restore working tree files.
" >
" :Git checkout -b new_branch_name
" <
if has('nvim-0.9.0')
function! git#checkout#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.checkout").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
let s:JOB = SpaceVim#api#import('job')
let s:NOTI = SpaceVim#api#import('notify')
function! git#checkout#run(args)
2021-01-02 18:15:08 +08:00
let cmd = ['git', 'checkout'] + a:args
call git#logger#debug('git-checkout cmd:' . string(cmd))
2021-01-02 18:15:08 +08:00
call s:JOB.start(cmd,
2024-12-09 22:29:20 +08:00
\ {
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
endfunction
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
function! s:on_exit(id, data, event) abort
call git#logger#debug('git-checkout exit data:' . string(a:data))
2021-01-02 18:15:08 +08:00
if a:data ==# 0
2024-12-09 22:29:20 +08:00
silent! checktime
call s:NOTI.notify('checkout done.')
call git#branch#detect()
2021-01-02 18:15:08 +08:00
else
2024-12-09 22:29:20 +08:00
call s:NOTI.notify('checkout failed.', 'WarningMsg')
2021-01-02 18:15:08 +08:00
endif
2024-12-09 22:29:20 +08:00
endfunction
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
function! s:options() abort
2021-01-02 18:15:08 +08:00
return join([
2024-12-09 22:29:20 +08:00
\ '-m',
\ '-b',
\ ], "\n")
endfunction
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
function! git#checkout#complete(ArgLead, CmdLine, CursorPos)
2021-01-02 18:15:08 +08:00
if a:ArgLead =~# '^-'
2024-12-09 22:29:20 +08:00
return s:options()
2021-01-02 18:15:08 +08:00
endif
let branchs = systemlist('git branch')
if v:shell_error
2024-12-09 22:29:20 +08:00
return ''
2021-01-02 18:15:08 +08:00
else
2024-12-09 22:29:20 +08:00
let branchs = join(map(filter(branchs, 'v:val !~ "^*"'), 'trim(v:val)'), "\n")
return branchs
2021-01-02 18:15:08 +08:00
endif
2024-12-09 22:29:20 +08:00
endfunction
2021-01-02 18:15:08 +08:00
2024-12-09 22:29:20 +08:00
endif