diff --git a/internal/services/formatters/service.go b/internal/services/formatters/service.go index 5dc147ec5..089a8ac9e 100644 --- a/internal/services/formatters/service.go +++ b/internal/services/formatters/service.go @@ -143,10 +143,14 @@ func (s *Service) RemoveSrcFolderFromPath(path string) string { if path == "" || len(path) <= 4 || !strings.Contains(path[:4], "src") { return path } + + path = strings.Replace(filepath.ToSlash(path), "/src/", "", 1) + if runtime.GOOS == "windows" { - return strings.Replace(path, `src\`, "", 1) + return filepath.FromSlash(path) } - return strings.Replace(path, "src/", "", 1) + + return path } func (s *Service) GetCodeWithMaxCharacters(code string, column int) string { diff --git a/internal/services/formatters/service_test.go b/internal/services/formatters/service_test.go index 3fc7ce736..f37cd81dd 100644 --- a/internal/services/formatters/service_test.go +++ b/internal/services/formatters/service_test.go @@ -423,9 +423,14 @@ func TestService_GetCodeWithMaxCharacters(t *testing.T) { func TestRemoveSrcFolderFromPath(t *testing.T) { t.Run("should return path without src prefix", func(t *testing.T) { monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), &config.Config{}) - result := monitorController.RemoveSrcFolderFromPath(filepath.Join("src", "something")) + result := monitorController.RemoveSrcFolderFromPath(filepath.Join("/", "src", "something")) assert.Equal(t, filepath.Base("something"), result) }) + t.Run("should return path without src prefix", func(t *testing.T) { + monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), &config.Config{}) + result := monitorController.RemoveSrcFolderFromPath(filepath.Join("/", "src", "something", "test")) + assert.Equal(t, filepath.Join("something", "test"), result) + }) t.Run("should return path without diff when src is after 4 index position", func(t *testing.T) { monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), &config.Config{}) result := monitorController.RemoveSrcFolderFromPath(filepath.Join("something", "src")) diff --git a/internal/services/git/git.go b/internal/services/git/git.go index a0705817e..8f1152898 100644 --- a/internal/services/git/git.go +++ b/internal/services/git/git.go @@ -80,10 +80,12 @@ func (g *Git) executeCMD(line, filePath string) ([]byte, error) { stderr := bytes.NewBufferString("") // NOTE: Here we use ^ as json double quotes to work properly on all platforms. + // The --no-patch flag suppress diff output, avoiding parse errors. cmd := exec.Command( "git", "log", "-1", + "--no-patch", `--format={ ^^^^^author^^^^^: ^^^^^%an^^^^^, ^^^^^email^^^^^:^^^^^%ae^^^^^, @@ -110,13 +112,15 @@ func (g *Git) executeCMD(line, filePath string) ([]byte, error) { } func (g *Git) parseOutput(output []byte) (author commitauthor.CommitAuthor) { - output = g.getCleanOutput(output) + output = g.replaceCarets(output) + if err := json.Unmarshal(output, &author); err != nil { logger.LogErrorWithLevel( messages.MsgErrorGitCommitAuthorsParseOutput+string(output), err, ) return g.newCommitAuthorNotFound() } + return author } @@ -144,14 +148,8 @@ func (g *Git) parseLineStringToNumber(line string) string { return strconv.Itoa(num) } -func (g *Git) getCleanOutput(output []byte) []byte { - // Output from git log contains the diff changes - // so we need to extract only the json output data. - if idx := bytes.LastIndex(output, []byte("}")); idx >= 0 { - return bytes.ReplaceAll(output[:idx+1], []byte("^^^^^"), []byte(`"`)) - } - logger.LogWarn(fmt.Sprintf("Could not to clean git blame output: %s", output)) - return []byte("") +func (g *Git) replaceCarets(output []byte) []byte { + return bytes.ReplaceAll(output, []byte("^^^^^"), []byte(`"`)) } func (g *Git) existsGitFolderInPath() bool { diff --git a/internal/services/git/git_test.go b/internal/services/git/git_test.go index f1553e622..a74b54ff2 100644 --- a/internal/services/git/git_test.go +++ b/internal/services/git/git_test.go @@ -110,6 +110,14 @@ func TestGetCommitAuthor(t *testing.T) { t.Run("Should return a new service", func(t *testing.T) { assert.NotEmpty(t, New(&config.Config{})) }) + + t.Run("Should not return git diff in output", func(t *testing.T) { + bytes, err := service.executeCMD("1", "README.md") + + assert.NoError(t, err) + assert.NotEmpty(t, bytes) + assert.NotContains(t, string(bytes), "diff --git") + }) } func TestRepositoryIsShallow(t *testing.T) {