#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
Added an event-driven Git auto-sync feature, and attempted NOT to conflict with the timed sync feature NOR manual sync command, NOR to loop and trigger itself: https://gist.github.com/ChenZhu-Xie/1eb4b764474a2b0cd94a351969d2e5bb