Running the SilverBullet Git Plug-in with Version 2

I recently discovered SilverBullet and am just about to start using it. In the future, I plan to migrate all of my thousands of notes currently stored in Evernote to SilverBullet.

After trying it out a bit, I became completely hooked on SilverBullet. I’ve decided to go with version 2 instead of v1. I’m self-hosting it using Docker and have already created a few pages.

Now, the issue I’m facing is that the AutoSync feature of the SilverBullet Git plug-in doesn’t seem to work.

I added the following lines to the CONFIG page, then ran Plugs: Update, System: Reload, and also reloaded the page in my browser:

config.set("plugs", {
  "github:silverbulletmd/silverbullet-git/git.plug.js"
})

config.set("git", {
  autoCommitMinutes = 1,
  autoSync = true,
})

The Git: Sync command works perfectly — it commits and pushes to my GitHub repository just as I want, and changes are correctly pulled from another device. Manual operations seem to work fine.

However, judging from the Docker logs, AutoSync doesn’t appear to be running at all. In fact, no automatic push or pull actions seem to be happening.

I’m not sure whether this issue is related to changes introduced in v2, a problem with the plug-in itself, or perhaps something wrong in my configuration. But I wanted to report the issue and share my experience in hopes it may help with future improvements.

I love open-source software, and I hope to contribute and be involved in the community.
Thank you very much — I’m looking forward to what’s ahead!

I believe this functionality has been removed in v2. In the changelog:

  • You’ve entered the brave new world of SilverBullet 2.0. Welcome
  • The following features have been removed:
    • Cron hook (plug feature)

And the autocommit function in the git plug relies on a cron hook:

name: git
requiredPermissions:
  - shell
functions:
  autoCommit:
    path: git.ts:autoCommit
    env: server
    cron: "* * * * *"  <----

I haven’t seen any information about reimplementing the cron hook. There is an event dispatched called cron:secondPassed that could probably be used to implement a similar feature locally. I also so a discussion somewhere that included code to sync when leaving a page. I’ll try to find that and see if it works in v2.

Edit: This was the post I had seen. I haven’t tested it.

I wrote something using cron:secondPassed to attempt to sync every autoCommitMinutes:

-- put this in a space-lua code block
gitConfig = config.get("git", {})
autoSyncEnabled = gitConfig.autoSync or false
autoCommitMinutes = gitConfig.autoCommitMinutes or 5
print("autoSyncEnabled: ", autoSyncEnabled)
print("autoCommitMinutes: ", autoCommitMinutes)

-- This event triggers often enough that two or more subsequent 
-- dispatches can both try to sync. We can imitate cron '* * * * *'
-- by trying every minute or so. Two or more subsequent events can
-- still race if event handling has slowed down, but this is close enough.
delayBetweenAttempts = 60

if autoSyncEnabled then
  event.listen {
    name = "cron:secondPassed",
    run = function(e)
      currentTime = os.time()

      if currentTime - previousCheck > delayBetweenAttempts then
        previousCheck = currentTime
        if (currentTime - previousCommit) / 60 > autoCommitMinutes then
          print('"autoSyncing" git')
          previousCommit = os.time()
          system.invokeCommand('Git: Sync')
        end
      end
    end
  }
else
  print("Git autoSync is not enabled.")
end

It reads the git config using the standard config options:

-- in a space-lua code block in CONFIG or wherever you want
config.set {
  plugs = {
    -- Add your plugs here (https://silverbullet.md/Plugs)
    -- Then run the `Plugs: Update` command to update them
    "github:silverbulletmd/silverbullet-git/git.plug.js"
  },
  git = {
    autoCommitMinutes = 15,
    autoSync = true,
  }
}

It runs client side instead of server side, so it won’t truly attempt every autoCommitMinutes. But trying every autoCommitMinutes while a client is active seems reasonable enough.

Thanks for figuring this out. My plan was to rebuild the git plug as a space lua script. This is what I’m using now for myself:

Indeed, what this is missing is the cron aspect of it, which you are implementing with your cron:secondPassed handler. I’ll integrate that with my version and then publish it somewhere. Or if you like you can fork my version and add this to it.

2 Likes

I added to your gist: git.md · GitHub

2 Likes

@wbh Nice, I wondered how to implement cron-like functionality. This should go to docs, if not there yet (with some simplest possible example, e.g. print something every 10 seconds to logs). :slight_smile: It also looks like the git plug is somewhat obsoleted now. :+1:

2 Likes

I’m glad it was helpful. I also added a bit to my gist to skip the initial sync (skipInitialSync in the git config) because I was spamming my repo with commits when using System: Reload while testing. I will look into adding a basic cron-like example, probably to the event API page.

1 Like

FYI on using gists as the way to distribute libraries like the Git one: I’m coming around to the idea of the practice of putting these in proper git(hub) repos again. Less messy.

I still need to build easier Import support for this, but likely I’ll encourage creating repos like this one to collect this stuff: GitHub - silverbulletmd/silverbullet-libraries: Awesome SilverBullet libraries that way people can also create issues, PRs etc. Not sure if it will be better to have one repo per library or not. We’ll see.

4 Likes