Skip to content

Commit 48aed9e

Browse files
authored
Merge pull request #1 from vbvictor/docs
Docs
2 parents 485d9a3 + 137f3e6 commit 48aed9e

File tree

19 files changed

+292
-261
lines changed

19 files changed

+292
-261
lines changed

.github/workflows/go-main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/upload-artifact@v4
2626
with:
2727
name: code-coverage
28-
path: coverage.out
28+
path: coverage.out
2929

3030
Lint:
3131
name: lint

.github/workflows/go-pr.yaml

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,12 @@ jobs:
2121

2222
- name: Test
2323
run: make test-with-coverage
24-
25-
- name: Coverage
26-
id: coverage
27-
uses: vladopajic/go-test-coverage@v2
28-
with:
29-
config: ./.testcoverage.yml
30-
31-
# Coverage report for PR
32-
- name: Is report present
33-
uses: peter-evans/find-comment@v3
34-
id: fc
35-
with:
36-
issue-number: ${{ github.event.pull_request.number }}
37-
comment-author: 'github-actions[bot]'
38-
body-includes: 'go-test-coverage report:'
3924

40-
- name: Post coverage report
41-
uses: peter-evans/create-or-update-comment@v4
25+
- name: Archive code coverage results
26+
uses: actions/upload-artifact@v4
4227
with:
43-
token: ${{ secrets.GITHUB_TOKEN }}
44-
issue-number: ${{ github.event.pull_request.number }}
45-
comment-id: ${{ steps.fc.outputs.comment-id }}
46-
body: |
47-
go-test-coverage report:
48-
```
49-
${{ fromJSON(steps.coverage.outputs.report) }}
50-
```
51-
edit-mode: replace
52-
53-
- name: Check coverage
54-
if: steps.coverage.outcome == 'failure'
55-
shell: bash
56-
run: echo "coverage check failed" && exit 1
28+
name: code-coverage
29+
path: coverage.out
5730

5831
Lint:
5932
name: lint

.github/workflows/release.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: goreleaser
22

33
on:
44
push:
5-
# run only against tags
65
tags:
76
- "*"
87

@@ -28,4 +27,4 @@ jobs:
2827
version: "~> v2"
2928
args: release --clean
3029
env:
31-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.golangci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ linters:
55
- godox
66
- gci
77
- tenv
8-
- testpackage
9-
- exhaustruct
8+
- gomnd
9+
- testpackage
1010
- gochecknoglobals # store global configuration values
1111
- forbidigo # use printf for showing progress information and verbose output
1212
- gochecknoinits # init functions to setup cobra commands
1313
- nolintlint
14+
- exhaustruct
15+
- execinquery # deprecated
16+
- exportloopref # deprecated
1417
linters-settings:
1518
gocritic:
1619
enable-all: true

.goreleaser.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ before:
88

99
builds:
1010
- binary: grit
11-
main: ./
11+
main: ./grit
1212
env:
1313
- CGO_ENABLED=0
1414
goos:
@@ -60,7 +60,7 @@ nfpms:
6060
file_name_template: "{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
6161
homepage: https://github.com/vbvictor/grit
6262
maintainer: "Victor Baranov <https://github.com/vbvictor>"
63-
description: Churn complexity visualizer
63+
description: cli tool that helps developers understand their codebase maintainability index
6464
license: MIT
6565
section: golang
6666
formats:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ clean:
2323
rm -f coverage.out
2424

2525
lint:
26-
golangci-lint run
26+
golangci-lint run --fix
2727

2828
format:
2929
gofumpt -w .

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Go Refactoring Insight Tool
22

33
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Go Main](https://github.com/vbvictor/grit/actions/workflows/go-main.yaml/badge.svg)](https://github.com/vbvictor/grit/actions/workflows/go-main.yaml)
45

56
GRIT is a cli tool that helps developers understand their codebase
67
maintainability index through calculated key metrics: code churn, code complexity and test
@@ -15,9 +16,6 @@ refactoring and testing priorities.
1516
- [Getting Started](#getting-started)
1617
- [Usage](#usage)
1718
- [Help command](#help-command)
18-
- [Churn command](#churn-command)
19-
- [Complexity command](#complexity-command)
20-
- [Coverage command](#coverage-command)
2119
- [Roadmap](#roadmap)
2220
- [Contributing](#contributing)
2321
- [License](#license)
@@ -30,10 +28,11 @@ refactoring and testing priorities.
3028
- **Code Complexity**: Calculates cyclomatic complexity metric per file.
3129
- **Test Coverage**: Analyzes test coverage percentage per file.
3230

33-
All of these metrics are useful when making decisions about:
31+
All these metrics are useful when making decisions about:
3432

35-
- Best candidates for refactoring efforts
36-
- What most complex files to cover with unit tests first
33+
- Best file candidates for refactoring efforts
34+
- Best file candidates for unit tests coverage
35+
- Best file candidates for code complexity reduction
3736

3837
<!-- These metrics if measured regularly can address appearing maintainability issues in a large codebase. -->
3938

@@ -54,24 +53,31 @@ Or Download the latest binary release from [Github Releases](https://github.com/
5453
Run `grit -h` to check out all available commands and general help for `grit`:
5554

5655
```sh
57-
All-in-one tool for getting refactoring statistics.
56+
GRIT is an all-in-one cli tool that helps developers understand
57+
their codebase maintainability index through calculated metrics.
5858

5959
Usage:
6060
grit [command]
6161

6262
Available Commands:
6363
help Help about any command
64-
stat Get various statistics about code in the repository
64+
stat Calculate code metrics
6565

6666
Flags:
6767
-h, --help help for grit
68+
69+
Use "grit [command] --help" for more information about a command.
6870
```
6971

70-
### Churn command
72+
Each command has its own extensive description that can be accessed by running `grit [command] -h`.
73+
74+
<!-- ### Usage examples -->
75+
76+
<!-- #### Churn command -->
7177

72-
### Complexity command
78+
<!-- #### Complexity command -->
7379

74-
### Coverage command
80+
<!-- #### Coverage command -->
7581

7682
## Roadmap
7783

grit/cmd/flag/flags.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ var (
3333
ChurnFactor float64
3434
ComplexityFactor float64
3535
CoverageFactor float64
36-
RunCoverage string
37-
CoverageFile string
3836

3937
// Output format flags.
4038
JSON OutputType = "json"

grit/cmd/grit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
var gritCmd = &cobra.Command{
1212
Use: "grit",
1313
Long: `GRIT is an all-in-one cli tool that helps developers understand
14-
their codebase maintainability index through calculated metrics.`,
14+
their codebase maintainability index through calculated metrics.`,
1515
}
1616

1717
func Execute() {
@@ -36,5 +36,6 @@ func Execute() {
3636
func init() {
3737
gritCmd.Root().CompletionOptions.DisableDefaultCmd = true
3838

39+
// gritCmd.AddCommand(report.ReportCmd)
3940
gritCmd.AddCommand(stat.StatCmd)
4041
}

grit/cmd/report/report.go

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"errors"
55
"fmt"
66
"path/filepath"
7+
"time"
78

89
"github.com/spf13/cobra"
910
"github.com/vbvictor/grit/grit/cmd/flag"
10-
stat "github.com/vbvictor/grit/grit/cmd/stat/subcommands"
1111
"github.com/vbvictor/grit/pkg/complexity"
12+
"github.com/vbvictor/grit/pkg/coverage"
1213
"github.com/vbvictor/grit/pkg/git"
1314
"github.com/vbvictor/grit/pkg/report"
1415
)
@@ -22,54 +23,94 @@ func (e *factorError) Error() string {
2223
return fmt.Sprintf("%s factor is lower that 0: %f", e.factor, e.value)
2324
}
2425

26+
const (
27+
DefaultTop = 10
28+
)
29+
30+
var (
31+
excludeRegex string
32+
top int
33+
since string
34+
until string
35+
repoPath string
36+
)
37+
38+
var churnOpts = &git.ChurnOptions{
39+
SortBy: git.Changes,
40+
Top: 0,
41+
Extensions: nil,
42+
Since: time.Time{},
43+
Until: time.Time{},
44+
Path: "",
45+
ExcludePath: "",
46+
}
47+
48+
var complexityOpts = complexity.Options{
49+
Engine: complexity.Gocyclo,
50+
ExcludePath: "",
51+
Top: 0, //nolint:mnd // default value
52+
}
53+
54+
var coverageOpts = coverage.Options{
55+
SortBy: coverage.Worst,
56+
Top: 0, //nolint:mnd // default value
57+
ExcludePath: "",
58+
RunCoverage: flag.Auto,
59+
CoverageFilename: "coverage.out",
60+
}
61+
62+
var reportOpts = report.Options{
63+
Top: DefaultTop,
64+
ExcludePath: "",
65+
ChurnFactor: 1.0,
66+
ComplexityFactor: 1.0,
67+
CoverageFactor: 1.0,
68+
}
69+
2570
var ReportCmd = &cobra.Command{
2671
Use: "report [flags] <repository>",
2772
Short: "Compare code complexity and churn metrics",
2873
Args: cobra.ExactArgs(1),
2974
SilenceErrors: true,
3075
PreRunE: func(_ *cobra.Command, _ []string) error {
31-
return validateFactors(&report.ReportOpts)
76+
return validateFactors(&reportOpts)
3277
},
3378
RunE: func(_ *cobra.Command, args []string) error {
34-
repoPath, err := filepath.Abs(args[0])
79+
var err error
80+
repoPath, err = filepath.Abs(args[0])
3581
if err != nil {
3682
return errors.Join(&flag.AbsRepoPathError{Path: args[0]}, err)
3783
}
3884

39-
flag.LogIfVerbose("Processing directory: %s", repoPath)
85+
flag.LogIfVerbose("Processing directory: %s\n", repoPath)
4086

41-
churnOpts, err := stat.ChurnOptsFromFlags()
42-
if err != nil {
43-
return fmt.Errorf("failed to create churn options: %w", err)
87+
flag.LogIfVerbose("Analyzing churn data...\n")
88+
if err := git.PopulateOpts(churnOpts, []string{"go"}, since, until, repoPath); err != nil {
89+
return fmt.Errorf("failed to create options: %w", err)
4490
}
45-
churnOpts.Path = repoPath
4691

47-
flag.LogIfVerbose("Analyzing churn data...")
48-
churnStats, err := git.ReadGitChurn(repoPath, churnOpts)
92+
churns, err := git.ReadGitChurn(repoPath, churnOpts)
4993
if err != nil {
50-
return fmt.Errorf("error getting churn stats: %w", err)
94+
return fmt.Errorf("error getting churn metrics: %w", err)
5195
}
52-
flag.LogIfVerbose("Got %d files", len(churnStats))
96+
flag.LogIfVerbose("Got %d churn files\n", len(churns))
5397

54-
// Get complexity data
55-
complexityOpts, err := stat.ComplexityOptsFromFlags()
98+
flag.LogIfVerbose("Analyzing complexity data...\n")
99+
complexityStats, err := complexity.RunComplexity(repoPath, complexityOpts)
56100
if err != nil {
57-
return fmt.Errorf("failed to create complexity options: %w", err)
101+
return fmt.Errorf("error running complexity analysis: %w", err)
58102
}
59103

60-
flag.LogIfVerbose("Analyzing complexity data...")
61-
complexityStats, err := complexity.RunComplexity(repoPath, complexityOpts)
104+
flag.LogIfVerbose("Got %d complexity files\n", len(complexityStats))
105+
106+
flag.LogIfVerbose("Analyzing coverage data...\n")
107+
covData, err := coverage.GetCoverageData(repoPath, coverageOpts)
62108
if err != nil {
63-
return fmt.Errorf("error running complexity analysis: %w", err)
109+
return fmt.Errorf("failed to get coverage data: %w", err)
64110
}
65-
flag.LogIfVerbose("Got %d files", len(complexityStats))
111+
flag.LogIfVerbose("Got %d coverage files\n", len(covData))
66112

67113
/*
68-
// Get coverage data
69-
coverageStats, err := stat.GetCoverageData(repoPath, flag.CoverageFile, stat.CoverageOptsFromFlags())
70-
if err != nil {
71-
return fmt.Errorf("error reading coverage: %w", err)
72-
}
73114
74115
75116
// Combine data into FileScores
@@ -104,27 +145,27 @@ func init() {
104145
flags := ReportCmd.PersistentFlags()
105146

106147
// Common flags
107-
flags.StringVar(&flag.ExcludePath, flag.LongExclude, "", "Exclude files matching regex pattern")
108-
flags.IntVarP(&flag.Top, flag.LongTop, flag.ShortTop, flag.DefaultTop, "Number of top files to display")
148+
flags.StringVar(&excludeRegex, flag.LongExclude, "", "Exclude files matching regex pattern")
149+
flags.IntVarP(&top, flag.LongTop, flag.ShortTop, flag.DefaultTop, "Number of top files to display")
109150
flags.BoolVarP(&flag.Verbose, flag.LongVerbose, flag.ShortVerbose, false, "Show detailed progress")
110151

111152
// Churn flags
112-
flags.StringVar(&flag.SortBy, flag.LongSort, "commits", "Sort by: changes, additions, deletions, commits")
113-
flags.StringVarP(&flag.Since, flag.LongSince, flag.ShortSince, "", "Start date for analysis (YYYY-MM-DD)")
114-
flags.StringVarP(&flag.Until, flag.LongUntil, flag.ShortUntil, "", "End date for analysis (YYYY-MM-DD)")
153+
flags.StringVarP(&since, flag.LongSince, flag.ShortSince, "", "Start date for analysis (YYYY-MM-DD)")
154+
flags.StringVarP(&until, flag.LongUntil, flag.ShortUntil, "", "End date for analysis (YYYY-MM-DD)")
115155

116156
// Complexity flags
117-
flags.StringVarP(&flag.Engine, flag.LongEngine, flag.ShortEngine, complexity.Gocyclo, "Complexity calculation engine")
157+
flags.StringVarP(&complexityOpts.Engine, flag.LongEngine, flag.ShortEngine, complexity.Gocyclo,
158+
"Complexity calculation engine")
118159

119160
// Coverage flags
120-
flags.StringVarP(&flag.RunCoverage, flag.LongRunCoverage, flag.ShortRunCoverage, flag.Auto, "Specify tests run format")
121-
flags.StringVarP(&flag.CoverageFile, flag.LongFileCoverage, flag.ShortFileCoverage, "coverage.out",
161+
flags.StringVarP(&coverageOpts.RunCoverage, flag.LongRunCoverage, flag.ShortRunCoverage, flag.Auto, "tests run format")
162+
flags.StringVarP(&coverageOpts.CoverageFilename, flag.LongFileCoverage, flag.ShortFileCoverage, "coverage.out",
122163
"Coverage file name")
123164

124165
// Report specific flags
125-
flags.Float64Var(&report.ReportOpts.ChurnFactor, "churn-factor", 1.0, "Churn factor")
126-
flags.Float64Var(&report.ReportOpts.ComplexityFactor, "comp-factor", 1.0, "Complexity factor")
127-
flags.Float64Var(&report.ReportOpts.CoverageFactor, "cov-factor", 1.0, "Coverage factor")
166+
flags.Float64Var(&reportOpts.ChurnFactor, "churn-factor", 1.0, "Churn factor")
167+
flags.Float64Var(&reportOpts.ComplexityFactor, "comp-factor", 1.0, "Complexity factor")
168+
flags.Float64Var(&reportOpts.CoverageFactor, "cov-factor", 1.0, "Coverage factor")
128169
}
129170

130171
func validateFactors(opts *report.Options) error {

0 commit comments

Comments
 (0)