Logseq 自动同步方案 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
Rebron1900
V2EX    分享创造

Logseq 自动同步方案

  •  
  •   Rebron1900 2022-07-13 22:24:46 +08:00 5656 次点击
    这是一个创建于 1196 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文: http://1900.live/logseq-auto-commit-sync/

    我目前正在使用的就是 Git 方案,该如何创建库之类的操作我就不再说了,社群和网上有很多资料,我这里单独说下我是怎么让 Git 实现多端(多个 window 间同步)同步,同时解决多端无脑同步的问题

    目前 Github 上有两三个专门针对 Logseq 写的自动 Commit 工具,分别是

    热心网友写的那个我之前一直出现各种莫名奇妙的问题导致无法提交,所以现在用的是第一个 Logseq 官方的自动提交脚本。

    但是这两个脚本都有一个问题 因为我平时有两个环境,一个公司一个家里。这就导致了我在公司写完笔记回去后在家里需要先进行一次手动的 pull 操作才能正常自动 Commit ,这让我觉得巨麻烦,难道就不能判断一下 push 操作的结果,如果提示不成功就先做下 pull 的操作同步最新的库再开始自动提交?

    所以我试着改动了一下官方脚本的代码,因为不懂 powershell 的脚本要怎么写,写了下面这段代码,但是一直无法执行。

    # 如果带了 push 参数则进行 push 操作 if ($PushToServer) { # 获取 gitpush 的执行结果 [string] $output = (& git push $Server $Branch) # 因为如果本地库和远端库不一致 git 会提示你需要执行 git pull 操作,所以这里判断一下执行结果里面有没有 git pull ,如果有则执行 git pull 操作 if($output.Contains("git pull")){ git pull } } 

    我再 Logseq 的官方 QQ 群里问了一下,不过大佬们似乎没时间,所以我再 V2EX 上发了个帖子求教,运气不好很快就得到了解决方法,moen大佬说:git 的输出是写到 stderr 的,所以得写成 & git push $Server $Branch 2>&1,我一改果然成功了。

    修改后的代码如下(文件名:Start-GitAutoCommit.ps1):

    # Usage: # git-auto ;; use current script dir as git dir, and Start-GitAutoCommitAnoPpush. # git-auto -d /path/to/your/note's/dir ;; set git dir # git-auto -p ;; Start-GitAutoCommitAndPush # git-auto -s origin -p ;; set remote server # git-auto -b main -p ;; set git branch # git-auto -i 30 -p ;; set interval seconds # git-auto -o -p;; execute once # parameters param ( [Alias('d')] [string] $Dir, [Alias('i')] [int] $Interval = 20, [Alias('p')] [switch] $PushToServer = $false, [Alias('o')] [switch] $Once= $false, [Alias('s')] [string] $Server, [Alias('b')] [string] $Branch ) # if -Dir/-d specified if ($Dir -ne "") { Set-Location $Dir } # if -Branch/-b specified if ($Branch -eq "") { $Branch = (& git rev-parse --abbrev-ref HEAD) } function Start-GitAutoCommitAndPush { [string] $status = (& git status) if (!$status.Contains("working tree clean")) { git add . git commit -m "auto commit" } if ($PushToServer) { [string] $output = (& git push $Server $Branch 2>&1) if($output.Contains("git pull")){ git pull } } } Get-Date if ($Once) { Start-GitAutoCommitAndPush } else { while ($true) { Start-GitAutoCommitAndPush Start-Sleep -Seconds $Interval } } 

    另外还写了一个作为调用入口的 bat 文件,因为这样我才能通过任务计划调用,你可能会问我为啥不直接用计划任务调用 ps1 文件?我只能告诉你我折腾过,但是失败了,我不会~。

    文件名:Auto-Commit.bat

     @echo off echo "DOCS PUSH BAT" echo "1. Move to working directory" ::移动到你的 logseq 库文件夹内 D: cd D:\developer\logseq echo "2. Start GitAutoCommit.ps1" ::执行 powershell 脚本,并设置远程分支和本地分支,并设置每 30 妙操作一次,并自动 push PowerShell.exe -file Start-GitAutoCommit.ps1 -s origin -b main -i 30 -p echo "Auto Commit Start" 

    通过计划任务调用这个 bat 脚本就能实现开机自动后台运行这个脚本。

    使用方法

    1. 进入你的 Logseq 库

    2. 创建一个名为Start-GitAutoCommit.ps1的文件,并把上面这个文件的代码复制粘粘贴进去

    3. 创建一个名为Auto-Commit.bat的文件,并把上面这个文件的代码复制粘贴进去,并修改::移动到你的 logseq 库文件夹内后面的D:cd D:\developer\logseq为你的 logseq 库的路径

    4. 运行Auto-Commit.bat即可

    8 条回复    2022-10-14 15:20:21 +08:00
    Rebron1900
        1
    Rebron1900  
    OP
       2022-07-13 22:26:10 +08:00
    @memo 感谢大佬指点
    Rebron1900
        2
    Rebron1900  
    OP
       2022-07-13 22:26:55 +08:00
    @moen 前面 @错人了。。。
    snoopyhai
        3
    snoopyhai  
       2022-07-14 08:23:21 +08:00
    现在 logseq 的 git 可以 push 了吗? 似乎以前只能 commit 不能 push
    Rebron1900
        4
    Rebron1900  
    OP
       2022-07-14 09:44:52 +08:00
    @snoopyhai logseq 自带的 git 我关掉了,不太好用。我是用的脚本自己控制 commit 以及 push
    wdssmq
        5
    wdssmq  
       2022-07-14 12:39:29 +08:00
    VSCode 内可以直接「同步」 Git ,就是自动拉取然后推送,,然而并不方便配合 Logseq 使用 - -

    所以现在姑且在用 Foam

    wdssmq/FoamTest: 基于 VSCode + Foam 的笔记;
    https://github.com/wdssmq/FoamTest
    Rebron1900
        6
    Rebron1900  
    OP
       2022-07-14 15:10:35 +08:00
    @wdssmq foam 我之前试用过,太 Geek 了,我用起来不是很习惯。
    wl2358
        7
    wl2358  
       2022-09-27 08:41:22 +08:00
    今天仔细看了下,你这只 pull 了没 push 。。
    Rebron1900
        8
    Rebron1900  
    OP
       2022-10-14 15:20:21 +08:00
    @wl2358 有 push 的, `[string] $output = (& git push $Server $Branch 2>&1) `
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5074 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 03:58 PVG 11:58 LAX 20:58 JFK 23:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86