diff --git a/kernel/model/history.go b/kernel/model/history.go index 5ece95aee..381292afc 100644 --- a/kernel/model/history.go +++ b/kernel/model/history.go @@ -237,8 +237,8 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { srcPath := historyPath var destPath, parentHPath string - id := util.GetTreeID(historyPath) - workingDoc := treenode.GetBlockTree(id) + rootID := util.GetTreeID(historyPath) + workingDoc := treenode.GetBlockTree(rootID) if nil != workingDoc { if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil { return @@ -250,10 +250,6 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { return } - if err = filelock.CopyNewtimes(srcPath, destPath); err != nil { - return - } - var avIDs []string tree, _ := loadTree(srcPath, util.NewLute()) if nil != tree { @@ -283,14 +279,53 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { tree.Path = filepath.ToSlash(strings.TrimPrefix(destPath, util.DataDir+string(os.PathSeparator)+boxID)) tree.HPath = parentHPath + "/" + tree.Root.IALAttr("title") + // 重置重复的块 ID https://github.com/siyuan-note/siyuan/issues/14358 + if nil != workingDoc { + treenode.RemoveBlockTreesByRootID(rootID) + } + nodes := map[string]*ast.Node{} + ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { + if !entering || !n.IsBlock() { + return ast.WalkContinue + } + + nodes[n.ID] = n + return ast.WalkContinue + }) + var ids []string + for nodeID, _ := range nodes { + ids = append(ids, nodeID) + } + idMap := treenode.ExistBlockTrees(ids) + var duplicatedIDs []string + for nodeID, exist := range idMap { + if exist { + duplicatedIDs = append(duplicatedIDs, nodeID) + } + } + for _, nodeID := range duplicatedIDs { + node := nodes[nodeID] + node.ID = ast.NewNodeID() + node.SetIALAttr("id", node.ID) + created := util.TimeFromID(node.ID) + updated := node.IALAttr("updated") + if "" == updated { + updated = created + } + if updated < created { + updated = created + } + node.SetIALAttr("updated", updated) + } + // 仅重新索引该文档,不进行全量索引 // Reindex only the current document after rolling back the document https://github.com/siyuan-note/siyuan/issues/12320 - treenode.RemoveBlockTree(id) - treenode.IndexBlockTree(tree) - sql.RemoveTreeQueue(id) - sql.IndexTreeQueue(tree) + sql.RemoveTreeQueue(rootID) + if writeErr := indexWriteTreeIndexQueue(tree); nil != writeErr { + return + } util.PushReloadFiletree() - util.PushReloadProtyle(id) + util.PushReloadProtyle(rootID) util.PushMsg(Conf.Language(102), 3000) IncSync() @@ -303,12 +338,12 @@ func RollbackDocHistory(boxID, historyPath string) (err error) { go func() { sql.FlushQueue() - tree, _ = LoadTreeByBlockID(id) + tree, _ = LoadTreeByBlockID(rootID) if nil == tree { return } - refreshProtyle(id) + refreshProtyle(rootID) // 刷新页签名 refText := getNodeRefText(tree.Root)