ai_xiaopei
6 days ago 1196f409d86bc61e7596eb274840244a62ce84ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package index
 
import (
    "os/exec"
    "strings"
)
 
// GetGitCommit 获取知识库当前 git commit hash
func GetGitCommit(vaultPath string) (string, error) {
    cmd := exec.Command("git", "-C", vaultPath, "rev-parse", "HEAD")
    out, err := cmd.Output()
    if err != nil {
        return "", err // 不是 git 仓库
    }
    return strings.TrimSpace(string(out)), nil
}
 
// NeedsRebuild 检查是否需要重建索引
func NeedsRebuild(store *Store, vaultPath string) (bool, string, error) {
    currentCommit, err := GetGitCommit(vaultPath)
    if err != nil {
        // 不是 git 仓库,总是需要重建
        return true, "", nil
    }
 
    storedCommit, err := store.GetMeta("git_commit")
    if err != nil {
        return true, currentCommit, nil
    }
 
    if storedCommit == "" {
        // 没有记录,需要重建
        return true, currentCommit, nil
    }
 
    if storedCommit != currentCommit {
        // commit 变了,需要重建
        return true, currentCommit, nil
    }
 
    // 检查是否有节点
    count, _ := store.NodeCount()
    if count == 0 {
        return true, currentCommit, nil
    }
 
    return false, currentCommit, nil
}