MCPcopy Create free account
hub / github.com/alibaba/open-code-review / prepareCodeTools

Function prepareCodeTools

internal/behaviortask/code.go:39–129  ·  view source on GitHub ↗
(ctx context.Context, input Input)

Source from the content-addressed store, hash-verified

37}
38
39func prepareCodeTools(ctx context.Context, input Input) (*codeTools, error) {
40 resolution := input.Resolution
41 if (input.Mode != "range" && input.Mode != "commit") || !revisionPattern.MatchString(resolution.ResolvedHead) ||
42 (!resolution.BaseIsEmpty && !revisionPattern.MatchString(resolution.ResolvedBase)) ||
43 (resolution.BaseIsEmpty && (input.Mode != "commit" || resolution.ResolvedBase != "")) {
44 return nil, fmt.Errorf("behavior requires a resolved commit or range comparison")
45 }
46 gitRunner, err := input.GitRunner.LocalForRepository(ctx, input.RepoDir)
47 if err != nil {
48 return nil, err
49 }
50 if input.Mode == "commit" {
51 actual, err := diff.NewCommitProvider(input.RepoDir, resolution.ResolvedHead, gitRunner).ResolveCommitComparison(ctx)
52 if err != nil {
53 return nil, err
54 }
55 if actual.ResolvedBase != resolution.ResolvedBase || actual.BaseIsEmpty != resolution.BaseIsEmpty {
56 return nil, fmt.Errorf("commit comparison does not match the resolved parent or empty tree")
57 }
58 }
59 comparison := Comparison{Repository: input.RepoDir, Mode: input.Mode, Old: resolution.ResolvedBase,
60 Head: resolution.ResolvedHead, BaseIsEmpty: resolution.BaseIsEmpty}
61 comparison.ID = contentHash(fmt.Sprintf("%s\x00%s\x00%s\x00%s\x00%t", comparison.Repository, comparison.Mode, comparison.Old, comparison.Head, comparison.BaseIsEmpty))
62 current := tool.FileReader{RepoDir: input.RepoDir, Mode: tool.ModeCommit, Ref: comparison.Head, Runner: gitRunner.WithAttributeSource(comparison.Head)}
63 previous := tool.FileReader{RepoDir: input.RepoDir, Mode: tool.ModeEmpty, Runner: gitRunner}
64 if !comparison.BaseIsEmpty {
65 previous.Mode = tool.ModeCommit
66 previous.Ref = comparison.Old
67 previous.Runner = gitRunner.WithAttributeSource(comparison.Old)
68 }
69 access, err := tool.NewComparisonCodeAccess(current, previous)
70 if err != nil {
71 return nil, err
72 }
73 result := &codeTools{runner: gitRunner, registry: tool.NewRegistry(), comparison: comparison,
74 allowed: map[string]map[string]bool{}, diffs: map[string]model.Diff{}, scope: []Change{}}
75 for side, reader := range map[string]tool.FileReader{"old": previous, "new": current} {
76 result.allowed[side] = map[string]bool{}
77 if reader.Mode == tool.ModeEmpty {
78 continue
79 }
80 if _, err := gitRunner.Output(ctx, input.RepoDir, "cat-file", "-e", reader.Ref+"^{commit}"); err != nil {
81 return nil, fmt.Errorf("%s revision is not an available commit: %w", side, err)
82 }
83 data, err := gitRunner.Output(ctx, input.RepoDir, "ls-tree", "-r", "-z", "--full-tree", "--end-of-options", reader.Ref)
84 if err != nil {
85 return nil, fmt.Errorf("list %s revision: %w", side, err)
86 }
87 for entry := range strings.SplitSeq(string(data), "\x00") {
88 if entry == "" {
89 continue
90 }
91 metadata, filePath, ok := strings.Cut(entry, "\t")
92 if !ok {
93 return nil, fmt.Errorf("invalid tree entry")
94 }
95 fields := strings.Fields(metadata)
96 if len(fields) != 3 {

Calls 15

NewCommitProviderFunction · 0.92
NewComparisonCodeAccessFunction · 0.92
NewRegistryFunction · 0.92
ProviderDirPrefixFunction · 0.92
NewFileReadDiffFunction · 0.92
NewDiffMapFunction · 0.92
contentHashFunction · 0.85
safePathFunction · 0.85
LocalForRepositoryMethod · 0.80
WithAttributeSourceMethod · 0.80
OutputMethod · 0.80