-
Notifications
You must be signed in to change notification settings - Fork 4
/
git.vim
126 lines (113 loc) · 4.51 KB
/
git.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
vim9script
import autoload 'popup.vim'
import autoload 'os.vim'
var pack_jobs = []
# Update or install plugins listed in packs
export def PackUpdate()
if !reduce(pack_jobs, (acc, val) => acc && job_status(val) != 'run', true)
echow "Previous update is not finished yet!"
return
endif
pack_jobs = []
echow "Update plugins..."
var cwd = $MYVIMDIR
var pack_list = $'{cwd}/pack/packs'
var jobs = []
var msg_count = 2
def OutCb(ch: channel, msg: string)
if msg !~ '.*up to date.$' && msg !~ '^HEAD' && msg !~ '^Removing .*tags' && msg !~ '^Updating files'
msg_count += 1
echow msg
endif
enddef
if filereadable(pack_list)
var plugs = readfile(pack_list)
for pinfo in plugs
if pinfo =~ '^\s*#' || pinfo =~ '^\s*$'
continue
endif
var [name, url] = pinfo->split("\t")
if empty(name) || empty(url)
continue
endif
var path = $"{cwd}/pack/{name}"
if isdirectory(path)
var job = job_start([&shell, &shellcmdflag, 'git fetch && git reset --hard @{u} && git clean -dfx'],
{"cwd": path, "err_cb": OutCb, "out_cb": OutCb})
pack_jobs->add(job)
else
var job = job_start($'git clone {url} {path}',
{"cwd": cwd, "err_cb": OutCb, "out_cb": OutCb})
pack_jobs->add(job)
endif
endfor
endif
def TimerHandler(t: number)
if reduce(pack_jobs, (acc, val) => acc && job_status(val) != 'run', true)
timer_stop(t)
if msg_count == 2
echow "No updates available."
else
echow "Plugins are updated!"
endif
helptags ALL
endif
enddef
timer_start(2000, (t) => TimerHandler(t), {"repeat": 100})
enddef
# Show commit that introduced current(selected) line
# If a count was given, show full history
# Src: https://www.reddit.com/r/vim/comments/i50pce/how_to_show_commit_that_introduced_current_line/
# Usage:
# import autoload 'git.vim'
# nnoremap <silent> <space>gi <scriptcmd>git.ShowCommit(v:count)<CR>
# xnoremap <silent> <space>gi <scriptcmd>git.ShowCommit(v:count, line("v"), line("."))<CR>
export def ShowCommit(count: number, firstline: number = line("."), lastline: number = line("."))
if !executable('git')
echoerr "Git is not installed!"
return
endif
var depth = (count > 0 ? "" : "-n 1")
var git_output = systemlist(
"git -C " .. shellescape(fnamemodify(resolve(expand('%:p')), ":h")) ..
$" log --no-merges {depth} -L " ..
shellescape($'{firstline},{lastline}:{resolve(expand("%:p"))}')
)
popup.ShowAtCursor(git_output, (winid) => {
setbufvar(winbufnr(winid), "&filetype", "git")
})
enddef
# Blame current (selected) line.
# Usage:
# import autoload 'git.vim'
# nnoremap <silent> <space>gb <scriptcmd>git.Blame()<CR>
# xnoremap <silent> <space>gb <scriptcmd>git.Blame(line("v"), line("."))<CR>
export def Blame(firstline: number = line("."), lastline: number = line("."))
if !executable('git')
echoerr "Git is not installed!"
return
endif
var git_output = systemlist(
"git -C " .. shellescape(fnamemodify(resolve(expand('%:p')), ":h")) ..
$' blame -L {firstline},{lastline} {expand("%:t")}')
popup.ShowAtCursor(git_output, (winid) => {
setbufvar(winbufnr(winid), "&filetype", "fugitiveblame")
})
enddef
# Open current line/selection in Github.
# Usage:
# import autoload 'git.vim'
# nnoremap <silent> <space>gh <scriptcmd>git.GithubOpen()<CR>
# xnoremap <silent> <space>gh <scriptcmd>git.GithubOpen(line("v"), line("."))<CR>
export def GithubOpen(firstline: number = line("."), lastline: number = line("."))
var gitroot = systemlist("git rev-parse --show-toplevel")->join('')
var filename = strpart(expand('%:p'), len(gitroot) + 1)->tr('\', '/')
var branch = systemlist("git rev-parse --abbrev-ref HEAD")->join('')
var remote_url = systemlist("git remote get-url origin")->join('')
if remote_url =~ '^[email protected]'
remote_url = remote_url->substitute('^[email protected]:', 'https://github.com/', '')
endif
remote_url = remote_url->substitute('.git$', '', '')
var github_url = $'{remote_url}/blob/{branch}/{filename}#L{firstline}-L{lastline}'
os.Open(github_url)
enddef