-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
init.lua
114 lines (103 loc) · 2.49 KB
/
init.lua
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
local function git_exec(cmd)
local out = vim.fn.system(cmd)
if vim.v.shell_error ~= 0 then
return nil
end
return out
end
local M = {}
function M.checkout(remote, commitish)
local cmd
if remote == nil then
cmd = string.format([[git checkout %s]], commitish)
else
cmd = string.format([[git checkout %s/%s]], remote, commitish)
end
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.pull(remote, branch)
local cmd = string.format([[git pull %s %s]], remote, branch)
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.fetch(remote, branch)
local cmd = string.format([[git fetch %s %s]], remote, branch)
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.add_remote(name, remote_url)
local cmd = string.format([[git remote add %s %s]], name, remote_url)
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.remove_remote(name)
local cmd = string.format([[git remote remove %s]], name)
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
function M.list_remotes()
local cmd = string.format([[git remote]])
local out = git_exec(cmd)
if out == nil then
return nil
end
return vim.fn.split(out, "\n")
end
function M.repo_dirty()
local cmd = [[git status --porcelain]]
local out = git_exec(cmd)
if out == nil then
return nil
end
if vim.fn.strlen(out) > 0 then
return true
end
return false
end
function M.remote_exists(remote_url)
local cmd = [[git remote -v]]
local out = git_exec(cmd)
if out == nil then
return nil
end
local bool = vim.fn.match(out, remote_url)
if bool == -1 then
return false
end
local lines = vim.fn.split(out, "\n")
local remote = ""
for _, line in ipairs(lines) do
bool = vim.fn.match(line, remote_url)
if bool ~= -1 then
local idx = vim.fn.stridx(line, "\t", 0)
remote = vim.fn.strpart(line, 0, idx)
break
end
end
return true, remote
end
function M.git_show_and_write(commit, file, write_to)
local cmd = string.format([[git show %s:%s > %s]], commit, file, write_to)
local out = git_exec(cmd)
if out == nil then
return nil
end
return out
end
return M