Skip to content

Instantly share code, notes, and snippets.

@zefhemel
Last active October 28, 2025 07:32
Show Gist options
  • Select an option

  • Save zefhemel/df94468428db3fe7049612cefbb8628a to your computer and use it in GitHub Desktop.

Select an option

Save zefhemel/df94468428db3fe7049612cefbb8628a to your computer and use it in GitHub Desktop.

#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 server
  • git pushes changes to the remote server

${widgets.commandButton("Git: Commit")}

  • Asks you for a commit message
  • Commits

Configuration

config.set("git", {
  autoSync = 5
})

Implementation

Configuration

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

Commands

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

@ChenZhu-Xie
Copy link

ChenZhu-Xie commented Sep 9, 2025

How to use this git.md?

It seems to be different from the installation method of https://github.com/silverbulletmd/silverbullet-git/blob/main/git.plug.js.

I can't put the above git.md under the 'real' Library/ even if I modify the name of the page to Library/git.

I see that almost all other #meta objects are placed under Library/std/ so that these objects can be interpreted as auto-executed scripts?

A single normal git.md in SB_space cannot generate the expected git commands.

PS: I am a new SB 2.0.0 crew :)

@zefhemel
Copy link
Author

zefhemel commented Sep 9, 2025

Hey! The way you do is as you tried, except that Library/Std is "baked in" and therefore read only, you can put it e.g. under Library/git. A more convenient way to this is to use the Import: URL command.

Distributing these with Gists became a bit messy, so I've now put it in a repo that's more up to date: https://github.com/silverbulletmd/silverbullet-libraries/blob/main/Git.md

Same thing applies there, you can just import it with Import: URL wit the URL of that github page.

@ChenZhu-Xie
Copy link

ChenZhu-Xie commented Sep 9, 2025

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

@ChenZhu-Xie
Copy link

ChenZhu-Xie commented Sep 10, 2025

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment