Files
2026-02-05 13:44:35 +00:00

125 lines
2.7 KiB
Markdown

---
name: Library/zefhemel/Git
tags: meta/library
share.uri: "https://github.com/zefhemel/silverbullet-libraries/blob/main/Git.md"
share.hash: 8b51f35b
share.mode: pull
---
This library adds a basic git synchronization functionality to SilverBullet. It should be considered a successor to [silverbullet-git](https://github.com/silverbulletmd/silverbullet-git) implemented in Space Lua.
The following commands are currently implemented:
${widgets.commandButton("Git: Sync")}
* Adds all files in your folder to git
* Commits them with the default "Snapshot" commit message
* `git pull`s changes from the remote server
* `git push`es changes to the remote server
${widgets.commandButton("Git: Commit")}
* Asks you for a commit message
* Commits
# Configuration
There is currently only a single configuration option: `git.autoSync`. When set, the `Git: Sync` command will be run every _x_ minutes.
Example configuration:
```lua
config.set("git.autoSync", 5)
```
# Implementation
The full implementation of this integration follows.
## Configuration
```space-lua
-- priority: 100
config.define("git", {
type = "object",
properties = {
autoSync = schema.number()
}
})
```
## Commands
```space-lua
git = {}
function checkedShellRun(cmd, args)
local r = shell.run(cmd, args)
if r.code != 0 then
error("Error: " .. r.stdout .. r.stderr, "error")
end
end
function git.localChanges()
local r = shell.run("git", {"diff", "--exit-code"})
return r.code != 0
end
function git.commit(message)
message = message or "Snapshot"
if git.localChanges() then
print "Comitting changes..."
local ok, message = pcall(function()
checkedShellRun("git", {"add", "./*"})
checkedShellRun("git", {"commit", "-a", "-m", message})
end)
if not ok then
print("Git commit failed: " .. message)
end
else
print "No local changes to commit"
end
end
function git.sync()
git.commit()
print "Pulling..."
checkedShellRun("git", {"pull"})
print "Pushing..."
checkedShellRun("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
}
```
```space-lua
-- 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()
local now = os.time()
if (now - lastSync)/60 >= autoSync then
lastSync = now
git.sync()
end
end
}
end
```