-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-annex.lua
192 lines (169 loc) · 5.08 KB
/
git-annex.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
dt = require "darktable"
json = require ("dkjson")
-- add
dt.register_event("shortcut", function()
git_annex("add", dt.gui.action_images, "adding")
end, "git annex: add images")
-- get
dt.register_event("shortcut", function()
git_annex("get", dt.gui.action_images, "getting")
end, "git annex: get images")
-- drop
dt.register_event("shortcut", function()
git_annex("drop", dt.gui.action_images, "dropping")
end, "git annex: drop images")
-- status
dt.register_event("shortcut", function()
--git_annex("status", dt.gui.action_images, "dropping")
get_status(dt.gui.action_images)
end, "git annex: status")
dt.register_lib(
"gitAnnex", -- Module name
"git annex", -- Name
true, -- Expandable
false, -- Resetable
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 20}}, -- containers
dt.new_widget("box")
{
orientation = "vertical",
dt.new_widget("button")
{
label = "add",
tooltip = "Add to git annex",
clicked_callback = function() git_annex("add", dt.gui.action_images, "adding") end
},
dt.new_widget("button")
{
label = "get",
tooltip = "Get from remote repo",
clicked_callback = function() git_annex("get", dt.gui.action_images, "getting") end
},
dt.new_widget("button")
{
label = "drop",
tooltip = "Drop from local repo",
clicked_callback = function() git_annex("drop", dt.gui.action_images, "dropping") end
},
dt.new_widget("button")
{
label = "status",
tooltip = "Update status tags",
clicked_callback = function() get_status(dt.gui.action_images) end
},
},
nil, -- view enter
nil -- view leave
)
-- executes git annex with the given subcommand on the selected files
-- cmd - string, the git annex subcommand
-- images - table, of dt_lua_image_t
-- msg - string, the verb to be displayed to the user
function git_annex(cmd, images, msg)
for _,image in pairs(images) do
notice = msg.." " .. image.filename .. " from git annex"
dt.print(notice)
result = call_git_annex(image.path, cmd, image.filename)
if result then
dt.print("finished "..notice)
if cmd == "get" or cmd == "add" then
set_tags(image, true)
elseif cmd == "drop" then
set_tags(image, false)
end
else
dt.print("failed "..notice)
end
end
end
function set_tags(image, here)
if here then
dt.tags.attach(dt.tags.create("git-annex|here"), image)
dt.tags.detach(dt.tags.create("git-annex|dropped"), image)
-- update the thumbnail here
else
dt.tags.detach(dt.tags.create("git-annex|here"), image)
dt.tags.attach(dt.tags.create("git-annex|dropped"), image)
-- save the thumbnail here
end
dt.tags.attach(dt.tags.create("git-annex|annexed"), image)
end
-- borrowed from http://lua-users.org/lists/lua-l/2010-07/msg00087.html
shell = {}
function shell.escape(...)
local command = type(...) == 'table' and ... or { ... }
for i, s in ipairs(command) do
s = (tostring(s) or ''):gsub('"', '\\"')
if s:find '[^A-Za-z0-9_."/-]' then
s = '"' .. s .. '"'
elseif s == '' then
s = '""'
end
command[i] = s
end
return table.concat(command, ' ')
end
function shell.execute(...)
cmd = shell.escape(...)
print(cmd)
--return os.execute(shell.escape(...))
return os.execute(cmd)
end
function shell.popen(...)
cmd = shell.escape(...)
print(cmd)
--return os.execute(shell.escape(...))
return io.popen(cmd)
end
-- end borrowed
function call_git_annex(annex_path, cmd, ...)
command = { "git", "-C", annex_path, "annex", cmd, ... }
return shell.execute(command)
end
function call_git_annex_p(annex_path, cmd, ...)
command = { "git", "-C", annex_path, "annex", cmd, ... }
return shell.popen(command)
end
-- borrowed from http://en.wikibooks.org/wiki/Lua_Functional_Programming/Functions
function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end
-- end borrowed
function get_status(images)
paths = {}
for _, image in ipairs(images) do
if not paths[image.path] then
paths[image.path] = {}
end
paths[image.path][image.filename] = image
end
for path, path_images in pairs(paths) do
print(path)
filenames = {}
for _, image in pairs(path_images) do
table.insert(filenames, image.filename)
end
-- If there are more than 25 files, it's probably quicker to just
-- load everything.
if #filenames > 25 then
filenames = {}
end
out=call_git_annex_p(path, "whereis", "-j", table.unpack(filenames))
for line in out:lines() do
status = json.decode(line)
whereis = status["whereis"]
here = false
for _, location in ipairs(whereis) do
if location["here"] then
here = true
end
end
if path_images[status["file"]] then
set_tags(path_images[status["file"]], here)
end
end
end
end