-
Notifications
You must be signed in to change notification settings - Fork 7
Support commit-specific code review (支持指定commit审查) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
67e919e
4ce9f67
f5a1979
146a325
3cdf5d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| .vscode | ||
| code-review.md | ||
| code-review.md | ||
| stellar |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ import ( | |||||
| "github.com/cloudwego/eino/compose" | ||||||
| "github.com/cloudwego/eino/schema" | ||||||
| "github.com/go-git/go-git/v5" | ||||||
| "github.com/go-git/go-git/v5/plumbing" | ||||||
| "github.com/go-git/go-git/v5/plumbing/object" | ||||||
| "github.com/sergi/go-diff/diffmatchpatch" | ||||||
| ) | ||||||
|
|
@@ -29,16 +30,18 @@ type ReviewEngine struct { | |||||
| ctx context.Context | ||||||
|
|
||||||
| reviewPath string | ||||||
| commitID string | ||||||
| chatModel *openai.ChatModel | ||||||
|
|
||||||
| // 文件锁 | ||||||
| mutex sync.Mutex | ||||||
| } | ||||||
|
|
||||||
| func NewReviewEngine(ctx context.Context, path string) *ReviewEngine { | ||||||
| func NewReviewEngine(ctx context.Context, path string, commitID string) *ReviewEngine { | ||||||
| return &ReviewEngine{ | ||||||
| ctx: ctx, | ||||||
| reviewPath: path, | ||||||
| commitID: commitID, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -96,6 +99,83 @@ func (e *ReviewEngine) gitDiff() ([]gitDiff, error) { | |||||
| return nil, fmt.Errorf("failed to open repo: workPath = %s, err= %v,", workPath, err) | ||||||
| } | ||||||
|
|
||||||
| // 如果指定了 commitID,则审查指定 commit 的变更 | ||||||
| if e.commitID != "" { | ||||||
| return e.getCommitDiff(repo) | ||||||
| } | ||||||
|
|
||||||
| // 否则审查工作区变更(原有逻辑) | ||||||
| return e.getWorkingTreeDiff(repo, workPath) | ||||||
| } | ||||||
|
|
||||||
| func (e *ReviewEngine) getCommitDiff(repo *git.Repository) ([]gitDiff, error) { | ||||||
| // 解析指定的 commit | ||||||
| commitHash, err := repo.ResolveRevision(plumbing.Revision(e.commitID)) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to resolve commit %s: %v", e.commitID, err) | ||||||
| } | ||||||
|
|
||||||
| commit, err := repo.CommitObject(*commitHash) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to get commit object %s: %v", e.commitID, err) | ||||||
| } | ||||||
|
|
||||||
| // 获取 commit 的 tree | ||||||
| commitTree, err := commit.Tree() | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to get commit tree: %v", err) | ||||||
| } | ||||||
|
|
||||||
| var parentTree *object.Tree | ||||||
| // 获取父 commit 的 tree(如果存在) | ||||||
| if commit.NumParents() > 0 { | ||||||
| parentCommit, err := commit.Parent(0) | ||||||
| if err != nil { | ||||||
| // 如果无法获取父 commit,可能是初始 commit,我们就与空 tree 比较 | ||||||
| fmt.Printf("Warning: Could not get parent commit (this might be an initial commit): %v\n", err) | ||||||
|
||||||
| fmt.Printf("Warning: Could not get parent commit (this might be an initial commit): %v\n", err) | |
| log.Printf("WARNING: Could not get parent commit (this might be an initial commit): %v", err) |
Copilot
AI
Aug 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file filtering logic is duplicated between getCommitDiff() and getWorkingTreeDiff(). Consider extracting this logic into a separate helper function to avoid code duplication.
| if fileName == "go.sum" || fileName == "go.mod" || strings.Contains(fileName, "README") { | |
| if shouldSkipFile(fileName) { |
Copilot
AI
Aug 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using a proper logger instead of fmt.Printf for error messages. This would provide better control over log levels and output formatting.
| fmt.Printf("failed to get patch for file: path= %s, err= %v \n", fileName, err) | |
| log.Printf("failed to get patch for file: path= %s, err= %v \n", fileName, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message format is inconsistent. Consider using standard Go error formatting: 'failed to load config file: %v' instead of 'load config file failed: err= %v'.