Skip to content

Conversation

Copy link

Copilot AI commented Jul 12, 2025

Implements the ability to review changes from specific commits using the existing --commit-id flag, which was previously defined but not functional.

Changes Made

Core Implementation:

  • Added commitID field to ReviewEngine struct to store the target commit
  • Modified NewReviewEngine() constructor to accept commitID parameter
  • Updated CLI command to pass --commit-id flag value to the review engine
  • Split gitDiff() method into specialized functions:
    • getCommitDiff(): Compares specified commit with its parent using object.DiffTree()
    • getWorkingTreeDiff(): Preserves original working directory comparison logic

Enhanced Features:

  • Initial commit support: Gracefully handles commits with no parent (e.g., initial commits)
  • Short hash support: Works with both full and partial commit hashes
  • Enhanced reporting: Review reports now include commit ID information to distinguish between commit and working directory reviews
  • Robust error handling: Proper error messages for invalid commit IDs

Usage Examples

# Review working directory changes (original functionality preserved)
stellar review

# Review specific commit changes (new functionality)
stellar review --commit-id f5a1979

# Works with short hashes
stellar review --commit-id f5a19

# Handles initial commits
stellar review --commit-id 943465c

Testing

The implementation has been tested with:

  • ✅ Recent commits with code changes
  • ✅ Initial commits (commits with no parent)
  • ✅ Invalid commit hashes (shows proper error messages)
  • ✅ Short and full commit hashes
  • ✅ Backward compatibility with existing working directory review

Technical Details

The implementation uses go-git's object.DiffTree() function to efficiently compare the specified commit with its parent, generating proper diff content that can be analyzed by the LLM. When no parent exists (initial commit), it compares against an empty tree to show all files as additions.

Fixes #1.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 4 commits July 12, 2025 10:30
Co-authored-by: zzy2210 <44086018+zzy2210@users.noreply.github.com>
Co-authored-by: zzy2210 <44086018+zzy2210@users.noreply.github.com>
Co-authored-by: zzy2210 <44086018+zzy2210@users.noreply.github.com>
Co-authored-by: zzy2210 <44086018+zzy2210@users.noreply.github.com>
Copilot AI changed the title [WIP] 支持指定commit审查 Support commit-specific code review (支持指定commit审查) Jul 12, 2025
Copilot AI requested a review from zzy2210 July 12, 2025 10:40
@zzy2210 zzy2210 marked this pull request as ready for review August 3, 2025 09:00
Copilot AI review requested due to automatic review settings August 3, 2025 09:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the previously defined but non-functional --commit-id flag to enable code review of specific commit changes rather than just working directory changes.

  • Added commit-specific diff functionality using go-git's object.DiffTree() method
  • Enhanced the review engine constructor to accept and store a commit ID parameter
  • Modified CLI command handling to pass the commit ID flag to the review engine
  • Added improved error messaging and reporting features

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
internal/reviewer/reviewer.go Implements core commit diff functionality with new methods and enhanced reporting
cmd/stellarspec.go Updates CLI to pass commit ID parameter and improves error messaging

if fileName == "" {
fileName = change.From.Name
}
if fileName == "go.sum" || fileName == "go.mod" || strings.Contains(fileName, "README") {
Copy link

Copilot AI Aug 3, 2025

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.

Suggested change
if fileName == "go.sum" || fileName == "go.mod" || strings.Contains(fileName, "README") {
if shouldSkipFile(fileName) {

Copilot uses AI. Check for mistakes.
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)
Copy link

Copilot AI Aug 3, 2025

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 warning messages. This would provide better control over log levels and output formatting.

Suggested change
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 uses AI. Check for mistakes.
// 生成 patch
patch, err := change.Patch()
if err != nil {
fmt.Printf("failed to get patch for file: path= %s, err= %v \n", fileName, err)
Copy link

Copilot AI Aug 3, 2025

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
baseConf, err := config.LoadFile(configPath)
if err != nil {
fmt.Println("load config file failed: err= %v", err)
fmt.Printf("load config file failed: err= %v\n", err)
Copy link

Copilot AI Aug 3, 2025

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'.

Suggested change
fmt.Printf("load config file failed: err= %v\n", err)
fmt.Printf("failed to load config file: %v\n", err)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

支持指定commit审查

2 participants