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

2.7 KiB

name, tags, share.uri, share.hash, share.mode
name tags share.uri share.hash share.mode
Library/zefhemel/Git meta/library https://github.com/zefhemel/silverbullet-libraries/blob/main/Git.md 8b51f35b pull

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 currently 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 server
  • git pushes 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:

config.set("git.autoSync", 5)

Implementation

The full implementation of this integration follows.

Configuration

-- priority: 100
config.define("git", {
  type = "object",
  properties = {
    autoSync = schema.number()
  }
})

Commands

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
}

-- 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