feat: 增加 git 仓库校验

This commit is contained in:
kite 2026-05-06 11:25:29 +08:00
parent 22d93edd9a
commit 39d02d1a04

View file

@ -27,6 +27,10 @@ func runReview(args []string) error {
return nil
}
if err := requireGitRepo(opts.repoDir); err != nil {
return err
}
tpl, err := template.LoadDefault()
if err != nil {
return fmt.Errorf("load default template: %w", err)
@ -155,6 +159,19 @@ func resolveRepoDir(input string) (string, error) {
return absPath, nil
}
// requireGitRepo validates that the given directory is part of a git repository.
func requireGitRepo(dir string) error {
repoDir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("resolve path: %w", err)
}
out, err := runGitCmd(repoDir, "rev-parse", "--git-dir")
if err != nil || len(out) == 0 {
return fmt.Errorf("%s is not a git repository, code review requires a valid git repository", repoDir)
}
return nil
}
func buildToolRegistry(collector *tool.CommentCollector, fr *tool.FileReader, diffMap map[string]string) tool.Registry {
reg := tool.NewRegistry()
reg.Register(tool.NewFileRead(fr))