Skip to content

Commit

Permalink
🎨 改进数据同步以避免旧的本地数据覆盖云端数据 siyuan-note/siyuan#7403
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 20, 2023
1 parent 995bd41 commit 3ef79d7
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ func (repo *Repo) sync0(context map[string]interface{},
cloudUpserts, cloudRemoves = repo.DiffUpsertRemove(cloudLatestFiles, latestFiles)
}

var cloudUpsertIgnore, localUpsertIgnore *entity.File
// 避免旧的本地数据覆盖云端数据 https://github.com/siyuan-note/siyuan/issues/7403
localUpserts = repo.filterLocalUpserts(localUpserts, cloudUpserts)

// 记录本地 syncignore 变更
var localUpsertIgnore *entity.File
for _, upsert := range localUpserts {
if "/.siyuan/syncignore" == upsert.Path {
localUpsertIgnore = upsert
Expand All @@ -230,6 +234,7 @@ func (repo *Repo) sync0(context map[string]interface{},
// 计算冲突的 upsert 和无冲突能够合并的 upsert
// 冲突的文件以本地 upsert 和 remove 为准
var tmpMergeConflicts []*entity.File
var cloudUpsertIgnore *entity.File
for _, cloudUpsert := range cloudUpserts {
if "/.siyuan/syncignore" == cloudUpsert.Path {
cloudUpsertIgnore = cloudUpsert
Expand Down Expand Up @@ -289,15 +294,15 @@ func (repo *Repo) sync0(context map[string]interface{},
}

ignoreMatcher := ignore.CompileIgnoreLines(ignoreLines...)
var tmp []*entity.File
var mergeResultRemovesTmp []*entity.File
for _, remove := range mergeResult.Removes {
if !ignoreMatcher.MatchesPath(remove.Path) {
tmp = append(tmp, remove)
mergeResultRemovesTmp = append(mergeResultRemovesTmp, remove)
continue
}
// logging.LogInfof("sync merge ignore remove [%s]", remove.Path)
}
mergeResult.Removes = tmp
mergeResult.Removes = mergeResultRemovesTmp

// 冲突文件复制到数据历史文件夹
if 0 < len(tmpMergeConflicts) {
Expand Down Expand Up @@ -415,6 +420,30 @@ func (repo *Repo) sync0(context map[string]interface{},
return
}

// filterLocalUpserts 避免旧的本地数据覆盖云端数据 https://github.com/siyuan-note/siyuan/issues/7403
func (repo *Repo) filterLocalUpserts(localUpserts, cloudUpserts []*entity.File) (ret []*entity.File) {
cloudUpsertsMap := map[string]*entity.File{}
for _, cloudUpsert := range cloudUpserts {
cloudUpsertsMap[cloudUpsert.Path] = cloudUpsert
}

var toRemoveLocalUpsertPaths []string
for _, localUpsert := range localUpserts {
if cloudUpsert := cloudUpsertsMap[localUpsert.Path]; nil != cloudUpsert {
if localUpsert.Updated < cloudUpsert.Updated-1000*60*7 { // 本地早于云端 7 分钟
toRemoveLocalUpsertPaths = append(toRemoveLocalUpsertPaths, localUpsert.Path) // 使用云端数据覆盖本地数据
}
}
}

for _, localUpsert := range localUpserts {
if !gulu.Str.Contains(localUpsert.Path, toRemoveLocalUpsertPaths) {
ret = append(ret, localUpsert)
}
}
return
}

func (repo *Repo) getSyncCloudFiles(context map[string]interface{}) (fetchedFiles []*entity.File, err error) {
latest, err := repo.Latest()
if nil != err {
Expand Down

0 comments on commit 3ef79d7

Please sign in to comment.