Sync Logseq between iOS and PC with git

I want to share with you how I sync logseq between iOS and PC with git.

TL;DR, using a-shell and some shortcuts to automatic pull and push.

Get Started

Download a-shell and logseq and open a-shell.

Configure git

[Documents]$ ssh-keygen -t ecdsa, keep enter until key pair are created.

[Documents]$ cat ~.ssh/id_ecdsa.pub, copy and add it to github.

[Documents]$ vim .gitconfig

[user]
    identityFile = ~/Documents/.ssh/id_ecdsa
    password = ""
    name = "your.name"
    email = "your.email"

Change identityFile if you choose a different type of key. Change password if your key pair has passphrase.

Configure logseq

[Documents]$ pickFolder, choose a folder with logseq’s icon.

Clone your repo, a-shell doesn’t have git, we are going to use lg2, like this.
[~Documents]$ lg2 clone path/to/your/logseq.git.

Open logseq and choose Folder, you should be able to see your journals.

Configure git-helper

Before we use shortcuts to automatic pull and push, we need some git-helper functions. I’m going to use python, and I will explain later why we need it.

Open a-shell and go to logseq.git, add helper.py to your repo.
[~logseq.git]$ vim helper.py

import os
import subprocess

DIRTY = "dirty"
CLEAN = "clean"
TAG = "a-shell"

def lg2_pull():
    print(f"[{TAG}] command: lg2 pull")
    os.system("lg2 pull")

def lg2_push():
    print(f"[{TAG}] command: lg2 add .")
    os.system("lg2 add .")
    print(f"[{TAG}] command: lg2 commit")
    os.system("lg2 commit -m 'autosaved by script'")
    print(f"[{TAG}] command: lg2 push")
    os.system("lg2 push")

def lg2_status():
    print(f"[{TAG}] command: lg2 status -s")
    output = subprocess.check_output("lg2 status -s")
    if not output:
        return CLEAN
    else:
        return DIRTY

def is_dirty():
    return lg2_status() == DIRTY

def pull():
    if not is_dirty():
        lg2_pull()

def push():
    if is_dirty():
        lg2_push()

Basically, we don’t want to pull if repo is dirty, and don’t want to push if repo is clean. And a-shell can not do this.

[[ -z $(git status -s) ]] && echo 'clean'
[[ -n $(git status -s) ]] && echo 'dirty'

Since we add our helper.py, our repo is dirty. You should be able to see the following output.

[~logseq.git]$ python -c "from helper import *;print(is_dirty())
[a-shell] command: lg2 status -s
True

Configure shortcuts

Now, we’re almost there, we don’t want to open a-shell and type commands everytime.

Open shortcuts.app and new shortcut.

Execute Command 
1. cd ~logseq.git
2. python -c "from helper import *;pull()" 

open logseq

Long pressed this shortcut and share to add to home screen. from now on, we use this shortcut instead of Logseq.app

New automation

When "Logseq is closed"
Do 
ExecuteCommand 
1. cd ~logseq.git
2. python -c "from helper import *; push()"
Then 
Go to Home Screen

Do some tests on your own, and turn off automation’ ask before running when everything is ok.

Done, and thanks for your time to read this.

More

Q: Why not use “working copy.app”?

A: Because “push changes” is not free.


Q: Why not use iSH.app?

A: iSH.app is a x86 emulator and Performance matters.
Please refer to this if you want to give a try.


Q: Why not use syncthing?

A: Because there’s no official syncthing app yet, and “Mobile sync.app” is not free. And I prefer to use terminal.


Q: Why not use iCloud?

A: Because iCloud is not that stable in China.

Feel free to ask me any questions.

7 Likes

This is awesome! Thank you so much for writing this down. Not only does it save me money, but I can also sync more than one journal with different Git repositories.

This also works by using the a-Shell mini app. Saves ~1GB of storage (only have 16GB).

1 Like

Thank you a lot! Can I please have your permission to translate your post to Chinese?

Thanks for your reply. ,feel free to repost it.
随意转载,我当初是觉得发论坛上看到的人会更多,没有额外写一篇中文的。

Thanks for your tutorial.

I am confused with “cd ~logseq.git” in your automation.

I don’t think this will bring the terminal to the note folder in logseq folder in a-shell. As far as I understand and tried, this can only be achieved by (as you said above) in terminal type pickFolder-> select the logseq folder. Am I missing something? You are able to automate this step with just executing cd ~logseq.git?

I figure it out. You did the bookmark the folder and didn’t include in the tutorial. See GitHub - holzschu/a-shell: A terminal for iOS, with multiple windows.

This all makes sense now. I will leave this comment as a reference for whoever follow this tutorial.