Skip to content

Commit

Permalink
⚡ QD-9824 Add support HEAD~1 syntax for go-git. Copy changes.json to …
Browse files Browse the repository at this point in the history
…log folder.
  • Loading branch information
avafanasiev committed Aug 31, 2024
1 parent 1f0b2b6 commit 0cab50b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ func writeChangesFile(options *QodanaOptions, start string, end string) (string,
return "", fmt.Errorf("failed to write scope file: %w", err)
}

err = platform.CopyFile(file.Name(), filepath.Join(options.LogDirPath(), "changes.json"))
if err != nil {
return "", err
}

return file.Name(), nil
}

Expand Down
19 changes: 17 additions & 2 deletions platform/git_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"path/filepath"
"regexp"
"sort"
"strings"
)

Expand Down Expand Up @@ -74,12 +75,22 @@ func getChangedFilesBetweenCommits(repo *git.Repository, cwd, hash1, hash2 strin
if err != nil {
return ChangedFiles{}, fmt.Errorf("failed to get absolute path of root folder %s: %v", cwd, err)
}
commit1, err := repo.CommitObject(plumbing.NewHash(hash1))
revision1, err := repo.ResolveRevision(plumbing.Revision(hash1))
if err != nil {
return ChangedFiles{}, fmt.Errorf("failed to resolve revision %s: %v", hash1, err)
}

revision2, err := repo.ResolveRevision(plumbing.Revision(hash2))
if err != nil {
return ChangedFiles{}, fmt.Errorf("failed to resolve revision %s: %v", hash2, err)
}

commit1, err := repo.CommitObject(*revision1)
if err != nil {
return ChangedFiles{}, fmt.Errorf("failed to find commit %s: %v", hash1, err)
}

commit2, err := repo.CommitObject(plumbing.NewHash(hash2))
commit2, err := repo.CommitObject(*revision2)
if err != nil {
return ChangedFiles{}, fmt.Errorf("failed to find commit %s: %v", hash2, err)
}
Expand Down Expand Up @@ -148,6 +159,10 @@ func getChangedFilesBetweenCommits(repo *git.Repository, cwd, hash1, hash2 strin
files = append(files, file)
}

sort.Slice(files, func(i, j int) bool {
return files[i].Path < files[j].Path
})

return ChangedFiles{Files: files}, nil
}

Expand Down
10 changes: 4 additions & 6 deletions platform/git_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func TestChangesCalculation(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.action, func(t *testing.T) {
repo, hashBefore, hashAfter := createRepo(t, tc)
repo := createRepo(t, tc)
defer func(path string) {
_ = os.RemoveAll(path)
}(repo)

commits, err := GitChangedFiles(repo, hashBefore, hashAfter)
commits, err := GitChangedFiles(repo, "HEAD~1", "HEAD")
for _, file := range commits.Files {
file.Path = filepath.Base(file.Path)
}
Expand All @@ -167,7 +167,7 @@ func TestChangesCalculation(t *testing.T) {
}
}

func createRepo(t *testing.T, tc TestConfig) (string, string, string) {
func createRepo(t *testing.T, tc TestConfig) string {
// Step 1: Create a new directory for the repository
repoDir, err := os.MkdirTemp("", "testrepo")
assert.NoError(t, err)
Expand Down Expand Up @@ -200,8 +200,6 @@ func createRepo(t *testing.T, tc TestConfig) (string, string, string) {
err = cmd.Run()
assert.NoError(t, err)

initHash := getCurrentHash(t, repoDir)

// Step 4: Perform the action specified
switch tc.action {
case "modify":
Expand Down Expand Up @@ -234,7 +232,7 @@ func createRepo(t *testing.T, tc TestConfig) (string, string, string) {
err = cmd.Run()
assert.NoError(t, err)

return repoDir, initHash, getCurrentHash(t, repoDir)
return repoDir
}

func getCurrentHash(t *testing.T, repoDir string) string {

Check warning on line 238 in platform/git_changes_test.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused function

Unused function `getCurrentHash`
Expand Down

0 comments on commit 0cab50b

Please sign in to comment.