brainsteam.co.uk/brainsteam/content/posts/2023/02/git-sync-foam/index.md

77 lines
4.6 KiB
Markdown
Raw Normal View History

2023-02-05 12:44:06 +00:00
---
2023-07-09 11:34:44 +01:00
date: 2023-02-05 08:01:11+00:00
description: Using git-sync to synchronise my markdown notes across multiple machines
and git hosts
post_meta:
- date
2024-10-28 20:59:46 +00:00
preview: /social/c81f2237c2cd9482750e1085e02ad5de16f81ce9e0beec5878ffbcfcc509cdca.png
2023-02-05 12:44:06 +00:00
resources:
2023-07-09 11:34:44 +01:00
- name: feature
src: images/git-sync.drawio.png
2023-02-05 12:44:06 +00:00
tags:
2023-07-09 11:34:44 +01:00
- pkm
- work
- git
title: Multi-Repo FOAM Git Sync
toc: false
type: posts
url: /2022/02/05/multi-repo-foam-git-sync
2023-02-05 12:44:06 +00:00
---
Over the last few days, I've been working out how to get [FOAM](https://foambubble.github.io/) to automatically sync my notes to their respective Git repositories when I save them in the editor. Here are my findings.
### Why?
I keep notes on different areas of my life siloed from each other. This allows me to completely prevent commercially sensitive stuff from my work life leaking onto personal devices and prevents me from accidentally publishing something personal (e.g. my shopping list for this weekend) to my public [digital garden](https://notes.jamesravey.me). Using FOAM in multi-root mode I can then transparently load all of these different sets of notes into the same workspace and connect relevant things together (for example, I could link to a public note I made about MySQL in a private work-related note I made to myself about debugging a client issue).
{{<figure src="images/git-sync.drawio.png" alt="diagram showing three way sync between my different markdown notes repositories hosted via different git services" caption="Separating my notes into different repositories allows me to ensure that work stays at work and that I don't publish something embarassing in my digital garden.">}}
### What I Tried First
The FOAM project maintainers [recommend](https://foambubble.github.io/foam/user/getting-started/recommended-extensions) using [GitDoc](https://github.com/lostintangent/gitdoc) as a turn-key solution for getting git auto-sync. GitDoc will monitor your project and automatically push your changes when you save files. However, I have so far only been able to get GitDoc to monitor one project at a time - whichever folder you open first inside VSCode. I have a feeling this might just be user error but I've spent a long time tweaking with my config and I couldn't get any further so I looked at another solution.
### My Solution
In the end I've sort of rolled my own sync solution using [git-sync](https://github.com/simonthum/git-sync) and the [save-and-run vscode plugin](https://github.com/wk-j/vscode-save-and-run).
Firstly `git-sync` is a bash script that automatically commits, pulls and pushes your depos (basically what GitDoc does in the background). I installed it to my local `bin` in my user directory:
```bash
mkdir ~/bin/git-sync
wget -O /bin/git-sync https://raw.githubusercontent.com/simonthum/git-sync/master/git-sync
chmod +x ~/bin/git-sync
```
Next we enable git sync for each of the projects we want to sync by setting a git config value. You specify the name of the branch you are interested in. For my notes at least, I don't do anything fancy with branches so I just use `main`:
```
cd ~/foam-workspace/public-notes
git config --bool branch.main.sync true
cd ~/foam-workspace/private-notes
git config --bool branch.main.sync true
```
By default `git-sync` will only commit and sync if it finds that an existing file has changed. If it detects the addition of new files that aren't already in git it will error. You can optionally run `git config --bool branch.main.syncNewFiles true` to override this behaviour if you wish but be aware that if you add a new file to the wrong repo it might be immediately synchronised (maybe don't turn this on if you have a build process that publishes your changes hooked up to the repo).
Now, we install the [save-and-run vscode plugin](https://github.com/wk-j/vscode-save-and-run) and configure it.
Assuming that you already have a VSCode workspace set up with the note repositories that you want to sync open, you can bring up the workspace settings.json (`CTRL` + `SHIFT` + `P` `>` `Preferences: Open Workspace Settings (JSON)`) and add something like the following to the `settings` section:
```json
"settings":{
...
"saveAndRun": {
"commands": [
{
"match": "\\.md$",
"cmd": "/home/user/bin/git-sync sync"
}
]
}
}
```
This will run `git-sync sync` inside the directory that you are editing files from whenever you save a `.md` file. It will be run in a terminal in vscode so you'll be able to see if something goes wrong and intervene if needed.
The final step is to enable the plugin by hitting `CTRL` + `SHIFT` + `P` `>` `Save and Run: Enable`