Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/17261

Daniel677063aa44d5
diff --git a/kernel/model/history.go b/kernel/model/history.go
index bada96e934..eba3c5fde1 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -235,7 +235,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
FlushTxQueue()
- box, err := getRollbackBox(boxID)
+ box, needResetTree, err := getRollbackBox(boxID)
if err != nil {
logging.LogErrorf("get rollback box [%s] failed: %s", boxID, err)
return
@@ -246,12 +246,8 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
var destPath, parentHPath string
rootID := util.GetTreeID(historyPath)
workingDoc := treenode.GetBlockTree(rootID)
- if nil != workingDoc && "d" == workingDoc.Type {
- workingDocPath := filepath.Join(util.DataDir, boxID, workingDoc.Path)
- if err = filelock.Remove(workingDocPath); err != nil {
- return
- }
- logging.LogInfof("removed working doc file [%s]", workingDocPath)
+ if needResetTree {
+ workingDoc = nil
}
destPath, parentHPath, err = getRollbackDockPath(boxID, historyPath, workingDoc)
@@ -287,7 +283,18 @@ 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")
+ if needResetTree {
+ resetTree(tree, "", true)
+ }
+
// 重置重复的块 ID https://github.com/siyuan-note/siyuan/issues/14358
+ if nil != workingDoc && "d" == workingDoc.Type {
+ workingDocPath := filepath.Join(util.DataDir, boxID, workingDoc.Path)
+ if err = filelock.Remove(workingDocPath); err != nil {
+ return
+ }
+ logging.LogInfof("removed working doc file [%s]", workingDocPath)
+ }
if nil != workingDoc {
treenode.RemoveBlockTreesByRootID(rootID)
}
@@ -1033,7 +1040,7 @@ func subscribeSQLHistoryEvents() {
})
}
-func getRollbackBox(boxID string) (ret *Box, err error) {
+func getRollbackBox(boxID string) (ret *Box, created bool, err error) {
ret = Conf.Box(boxID)
if nil == ret {
boxName := "Rollback"
@@ -1049,6 +1056,7 @@ func getRollbackBox(boxID string) (ret *Box, err error) {
return
}
ret = Conf.Box(id)
+ created = true
}
}
if nil == ret {
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index 1706ae151b..81f6d3ad99 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -223,20 +223,24 @@ func RollbackRepoSnapshotFile(fileID string) (err error) {
}
if strings.HasSuffix(file.Path, ".sy") {
- var destPath, parentHPath string
- rootID := util.GetTreeID(file.Path)
- workingDoc := treenode.GetBlockTree(rootID)
boxID := strings.TrimPrefix(file.Path, "/")
boxID = strings.Split(boxID, "/")[0]
var box *Box
- box, err = getRollbackBox(boxID)
+ var needResetTree bool
+ box, needResetTree, err = getRollbackBox(boxID)
if err != nil {
logging.LogErrorf("get rollback box [%s] failed: %s", boxID, err)
return
}
boxID = box.ID
+ var destPath, parentHPath string
+ rootID := util.GetTreeID(file.Path)
+ workingDoc := treenode.GetBlockTree(rootID)
+ if needResetTree {
+ workingDoc = nil
+ }
destPath, parentHPath, err = getRollbackDockPath(boxID, file.Path, workingDoc)
if err != nil {
return
@@ -253,6 +257,10 @@ func RollbackRepoSnapshotFile(fileID string) (err error) {
tree.Box = boxID
tree.Path = filepath.ToSlash(strings.TrimPrefix(destPath, util.DataDir+string(os.PathSeparator)+boxID))
tree.HPath = parentHPath + "/" + tree.Root.IALAttr("title")
+ if needResetTree {
+ resetTree(tree, "", true)
+ }
+
if nil != workingDoc && "d" == workingDoc.Type {
workingDocPath := filepath.Join(util.DataDir, boxID, workingDoc.Path)
if err = filelock.Remove(workingDocPath); err != nil {
@@ -271,10 +279,8 @@ func RollbackRepoSnapshotFile(fileID string) (err error) {
ReloadFiletree()
ReloadProtyle(rootID)
- if box := Conf.GetBox(boxID); nil != box {
- msg := fmt.Sprintf(Conf.Language(286), path.Join(box.Name, tree.HPath))
- util.PushMsg(msg, 7000)
- }
+ msg := fmt.Sprintf(Conf.Language(286), path.Join(box.Name, tree.HPath))
+ util.PushMsg(msg, 7000)
} else {
to := filepath.Join(util.DataDir, file.Path)
if err = filelock.CopyNewtimes(from, to); nil != err {