Skip to content

Commit

Permalink
🚸 Compute absolute cwd before running git. Improve tests. Clean up un…
Browse files Browse the repository at this point in the history
…used code.
  • Loading branch information
hybloid authored and avafanasiev committed Sep 3, 2024
1 parent 9e034c0 commit d0fdfb0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 84 deletions.
12 changes: 3 additions & 9 deletions platform/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package platform
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"strings"
)

Expand All @@ -35,10 +34,8 @@ func gitRun(cwd string, command []string, logdir string) (string, string, error)
stdout, stderr, _, err := RunCmdRedirectOutput(cwd, args...)
logger.Printf("Executing command: %v", args)
logger.Println(stdout)
_, _ = fmt.Fprintf(os.Stdout, "%s\n", stdout)
if stderr != "" {
logger.Error(stderr + "\n")
_, _ = fmt.Fprintf(os.Stderr, "%s\n", stderr)
err = fmt.Errorf("error while executing command %v: %v", args, stderr)
}
if err != nil {
Expand Down Expand Up @@ -97,8 +94,7 @@ func GitRemoteUrl(cwd string, logdir string) (string, error) {
if err != nil {
return "", err
}
output := strings.Split(strings.TrimSpace(stdout), "\n")
return output[0], nil
return strings.TrimSpace(stdout), nil
}

// GitBranch returns the current branch of the git repository.
Expand All @@ -107,15 +103,13 @@ func GitBranch(cwd string, logdir string) (string, error) {
if err != nil {
return "", err
}
output := strings.Split(strings.TrimSpace(stdout), "\n")
return output[0], nil
return strings.TrimSpace(stdout), nil
}

func GitCurrentRevision(cwd string, logdir string) (string, error) {
stdout, _, err := gitRun(cwd, []string{"rev-parse", "HEAD"}, logdir)
if err != nil {
return "", err
}
output := strings.Split(strings.TrimSpace(stdout), "\n")
return output[0], nil
return strings.TrimSpace(stdout), nil
}
27 changes: 8 additions & 19 deletions platform/git_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,17 @@ type ChangedFiles struct {
Files []*ChangedFile `json:"files"`
}

func GitDiffNameOnly(cwd string, diffStart string, diffEnd string, logdir string) ([]string, error) {
stdout, _, err := gitRun(cwd, []string{"diff", "--name-only", "--relative", diffStart, diffEnd}, logdir)
func computeAbsCwd(cwd string) (string, error) {
cwd, err := filepath.EvalSymlinks(cwd)
if err != nil {
return []string{""}, err
}
relPaths := strings.Split(strings.TrimSpace(stdout), "\n")
absPaths := make([]string, 0)
for _, relPath := range relPaths {
if relPath == "" {
continue
}
filePath := filepath.Join(cwd, relPath)
absFilePath, err := filepath.Abs(filePath)
if err != nil {
log.Fatalf("Failed to resolve absolute path of %s: %s", filePath, err)
}
absPaths = append(absPaths, absFilePath)
return "", err
}
return absPaths, nil
cwdAbs, err := filepath.Abs(cwd)
return cwdAbs, err
}

func GitChangedFiles(cwd string, diffStart string, diffEnd string, logdir string) (ChangedFiles, error) {
cwd, err := filepath.EvalSymlinks(cwd)
absCwd, err := computeAbsCwd(cwd)
if err != nil {
return ChangedFiles{}, err
}
Expand All @@ -83,11 +71,12 @@ func GitChangedFiles(cwd string, diffStart string, diffEnd string, logdir string
if err != nil {
return ChangedFiles{}, err
}
return parseDiff(stdout, repoRoot, cwd)
return parseDiff(stdout, repoRoot, absCwd)
}

// parseDiff parses the git diff output and extracts changes
func parseDiff(diff string, repoRoot string, cwd string) (ChangedFiles, error) {
log.Debugf("Parsing diff of length: %d, repo root: %s, cwd: %s", len(diff), repoRoot, cwd)
var changes []HunkChange
scanner := bufio.NewScanner(strings.NewReader(diff))

Expand Down
18 changes: 0 additions & 18 deletions platform/git_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,13 @@ import (
"fmt"
log "github.com/sirupsen/logrus"
"os/exec"
"path/filepath"
"strings"
)

func GitBranchLegacy(cwd string) string {
return gitOutput(cwd, []string{"rev-parse", "--abbrev-ref", "HEAD"})[0]
}

func GitDiffNameOnlyLegacy(cwd string, diffStart string, diffEnd string) []string {
relPaths := gitOutput(cwd, []string{"diff", "--name-only", "--relative", diffStart, diffEnd})
absPaths := make([]string, 0)
for _, relPath := range relPaths {
if relPath == "" {
continue
}
filePath := filepath.Join(cwd, relPath)
absFilePath, err := filepath.Abs(filePath)
if err != nil {
log.Fatalf("Failed to resolve absolute path of %s: %s", filePath, err)
}
absPaths = append(absPaths, absFilePath)
}
return absPaths
}

func GitCurrentRevisionLegacy(cwd string) string {
return gitOutput(cwd, []string{"rev-parse", "HEAD"})[0]
}
Expand Down
86 changes: 48 additions & 38 deletions platform/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,47 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"testing"
)

func TestGitDiffNamesOnly(t *testing.T) {
const (
REV = "aa1fe0eac28bbc363036b39ab937b081f06f407a"
BRANCH = "my-branch"
REPO = "https://github.com/JetBrains/code-analytics-examples"
)

func TestGitFunctionalityChange(t *testing.T) {
temp, _ := os.MkdirTemp("", "")
projectPath := createNativeProject(t, "casamples")
defer deferredCleanup(projectPath)

strings := [2]string{"", "duplicates"}
oldRev := "12bf1bf4dd267b972e05a586cd7dfd3ad9a5b4a0"
newRev := "d6f64adb1d594ea4bb6e40d027e1e863726c4323"

// Iterate over the array using a for loop.
for _, str := range strings {
path := filepath.Join(projectPath, str)
diff, err := GitDiffNameOnly(path, oldRev, newRev, temp)
if err != nil {
t.Fatalf("GitDiffNameOnly() error = %v", err)
}
diffLegacy := GitDiffNameOnlyLegacy(path, oldRev, newRev)
sort.Strings(diff)
sort.Strings(diffLegacy)
equal := reflect.DeepEqual(diff, diffLegacy)
if !equal {
t.Fatalf("Old and new diffs are not equal: old: %v new: %v", diffLegacy, diff)
}
branch, _ := GitBranch(path, temp)
branchLegacy := GitBranchLegacy(path)
if branch != branchLegacy {
t.Fatalf("Old and new branch are not equal: old: %v new: %v", branchLegacy, branch)
}
revision, _ := GitCurrentRevision(path, temp)
revisionLegacy := GitCurrentRevisionLegacy(path)
if revision != revisionLegacy {
t.Fatalf("Old and new revision are not equal: old: %v new: %v", revisionLegacy, revision)
}
remoteUrl, _ := GitRemoteUrl(path, temp)
remoteUrlLegacy := GitRemoteUrlLegacy(path)
if remoteUrl != remoteUrlLegacy {
t.Fatalf("Old and new url are not equal: old: %v new: %v", remoteUrlLegacy, remoteUrl)
}
branch, _ := GitBranch(projectPath, temp)
branchLegacy := GitBranchLegacy(projectPath)
if branch != branchLegacy {
t.Fatalf("Old and new branch are not equal: old: %v new: %v", branchLegacy, branch)
}
if branch != BRANCH {
t.Fatalf("New and expected branch are not equal: new: %v expected: %v", branch, BRANCH)
}
revision, _ := GitCurrentRevision(projectPath, temp)
revisionLegacy := GitCurrentRevisionLegacy(projectPath)
if revision != revisionLegacy {
t.Fatalf("Old and new revision are not equal: old: %v new: %v", revisionLegacy, revision)
}
if revision != REV {
t.Fatalf("New and expected revision are not equal: new: %v expected: %v", revision, REV)
}
remoteUrl, _ := GitRemoteUrl(projectPath, temp)
remoteUrlLegacy := GitRemoteUrlLegacy(projectPath)
if remoteUrl != remoteUrlLegacy {
t.Fatalf("Old and new url are not equal: old: %v new: %v", remoteUrlLegacy, remoteUrl)
}
if remoteUrl != REPO {
t.Fatalf("New and expected repo urls are not equal: new: %v expected: %v", remoteUrl, REPO)
}
rootPath, _ := GitRoot(projectPath, temp)
if rootPath != projectPath {
t.Fatalf("Computed git root path are not equal: new: %v expected: %v", rootPath, projectPath)
}
}

Expand All @@ -80,14 +78,14 @@ func createNativeProject(t *testing.T, name string) string {
t.Fatal(err)
}
location := filepath.Join(home, ".qodana_scan_", name)
err = gitClone("https://github.com/JetBrains/code-analytics-examples", location)
err = gitClone("https://github.com/JetBrains/code-analytics-examples", location, REV, BRANCH)
if err != nil {
t.Fatal(err)
}
return location
}

func gitClone(repoURL, directory string) error {
func gitClone(repoURL, directory string, revision string, branch string) error {
if _, err := os.Stat(directory); !os.IsNotExist(err) {
err = os.RemoveAll(directory)
if err != nil {
Expand All @@ -99,5 +97,17 @@ func gitClone(repoURL, directory string) error {
if err != nil {
return err
}
cmd = exec.Command("git", "checkout", revision)
cmd.Dir = directory
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command("git", "checkout", "-b", branch)
cmd.Dir = directory
err = cmd.Run()
if err != nil {
return err
}
return nil
}

0 comments on commit d0fdfb0

Please sign in to comment.