#meta
This library adds a basic git synchronization functionality to SilverBullet. It should be considered a successor to silverbullet-git implemented in Space Lua.
The following commands are implemented:
${widgets.commandButton("Git: Sync")}
- Adds all files in your folder to git
- Commits them with the default "Snapshot" commit message
git pulls changes from the remote servergit pushes changes to the remote server
${widgets.commandButton("Git: Commit")}
- Asks you for a commit message
- Commits
config.set("git", {
autoSync = 5
})-- priority: 100
config.define("git", {
type = "object",
properties = {
autoSync = schema.number()
}
})
git = {}
function git.commit(message)
message = message or "Snapshot"
print "Comitting..."
local ok, message = pcall(function()
shell.run("git", {"add", "./*"})
shell.run("git", {"commit", "-a", "-m", message})
end)
if not ok then
print("Git commit failed: " .. message)
end
end
function git.sync()
git.commit()
print "Pulling..."
shell.run("git", {"pull"})
print "Pushing..."
shell.run("git", {"push"})
end
command.define {
name = "Git: Commit",
run = function()
local message = editor.prompt "Commit message:"
git.commit(message)
end
}
command.define {
name = "Git: Sync",
run = function()
git.sync()
editor.flashNotification "Done!"
end
}
-- priority: -1
local autoSync = config.get("git.autoSync")
if autoSync then
print("Enabling git auto sync every " .. autoSync .. " minutes")
local lastSync = 0
event.listen {
name = "cron:secondPassed",
run = function()
print "Activating timer"
local now = os.time()
if (now - lastSync)/60 >= autoSync then
lastSync = now
git.sync()
end
end
}
end
It works! when imported as Github Repo file (automatically) into Library/git, see test.
And AutoSync1 = AutoSync2 works as well.
Maybe a space-lua version EventListener1 = EventListener2 command would be more user-friendly, for limited i/o consideration, especially when using fly.io.
However, since this space-lua version AutoSync will not prevent fly.io from automatically shutting down remote machine/server, thus io-friendly, alternative realizaton like EventListener, doesn't seem necessary...