Files
2026-06-22 08:55:57 +08:00

35 lines
952 B
Go

package git
import (
"bytes"
"context"
)
// ListTrackedFiles 返回 dir 仓库中被 git 跟踪的文件(git ls-files -z),
// 路径相对 worktree 根,NUL 分隔以正确处理含空格/特殊字符的文件名。
// 仅跟踪文件 —— 忽略 .gitignore / 未跟踪 / vendored,避免索引器遍历海量无关文件。
func (r *DefaultRunner) ListTrackedFiles(ctx context.Context, dir string) ([]string, error) {
ctx, cancel := withCtx(ctx, r.cfg.OpsTimeout)
defer cancel()
out, _, err := r.exec(ctx, dir, nil, "ls-files", "-z")
if err != nil {
return nil, err
}
return parseNULList(out), nil
}
// parseNULList 解析 NUL 分隔的文件列表(git ls-files -z)。空输入返回 nil。
func parseNULList(buf []byte) []string {
if len(buf) == 0 {
return nil
}
var out []string
for _, rec := range bytes.Split(buf, []byte{0}) {
if len(rec) == 0 {
continue
}
out = append(out, string(rec))
}
return out
}