Skip to content

Commit

Permalink
Accept "git diff" as input
Browse files Browse the repository at this point in the history
In addition to supporting file list input, this commit adds support for
raw "git diff" output. In this case, only the changed files will appear,
and only the lines marked as "added" in the diff will be shown. For
every such line, its coverage status will be reported of course
(red/green/gray).
  • Loading branch information
orlangure committed Jun 25, 2022
1 parent 07456cc commit 5103a12
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/muesli/termenv v0.9.0
github.com/sebdah/goldie/v2 v2.5.3
github.com/stretchr/testify v1.7.0
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d
golang.org/x/tools v0.1.8
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d h1:xQcF7b7cZLWZG/+7A4G7un1qmEDYHIvId9qxRS1mZMs=
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d/go.mod h1:BzSc3WEF8R+lCaP5iGFRxd5kIXy4JKOZAwNe1w0cdc0=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -88,5 +91,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
68 changes: 53 additions & 15 deletions internal/codeview/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var (
Align(lipgloss.Right).
BorderForeground(lipgloss.Color(lineNumberColor)).
Border(lipgloss.NormalBorder(), false, true, false, false)

blankBlockSeparatorStyle = lipgloss.NewStyle().
MarginTop(1).MarginBottom(1).
Foreground(lipgloss.Color(lineNumberColor))
)

// New creates a new codeview model which is rendered into the provided width
Expand All @@ -60,13 +64,14 @@ func New(width, height int) Model {

// Model is the codeview model. Use New to create a new instance.
type Model struct {
viewport viewport.Model
help help.Model
width int
height int
title string
lines []string
showHelp bool
viewport viewport.Model
help help.Model
width int
height int
title string
lines []string
filteredLines []int
showHelp bool
}

// Update is used to update the internal model state based on the external
Expand Down Expand Up @@ -115,6 +120,14 @@ func (m *Model) SetContent(lines []string) {
m.viewport.SetYOffset(0)
}

// SetFilteredLines sets the lines that should be displayed, while all other
// lines are hidden. If not set, everything is displayed.
func (m *Model) SetFilteredLines(filteredLines []int) {
m.filteredLines = filteredLines
m.redrawLines()
m.viewport.SetYOffset(0)
}

func (m *Model) redrawLines() {
content := m.formatLines(m.lines)
m.viewport.SetContent(content)
Expand Down Expand Up @@ -201,22 +214,47 @@ func (m *Model) recalculateSize() {
}

func (m *Model) formatLines(lines []string) string {
if len(lines) == 0 {
return ""
}

lineNumberStyle := lineNumberStylePlaceholder.Copy().Width(len(fmt.Sprintf("%d", len(lines))) + 1)
lineNumberPlaceholder := lineNumberStyle.Render(fmt.Sprintf("%d", 1))
availableWidth := m.width - lipgloss.Width(lineNumberPlaceholder) - lipgloss.Width(ellipsis)
filterApplied := len(m.filteredLines) > 0

var buf strings.Builder
var (
buf strings.Builder
printSingleLine = func(line string, number int) {
line = m.replaceTabsWithSpaces(line)
lineNumber := lineNumberStyle.Render(fmt.Sprintf("%d", number))

for i, line := range lines {
line = m.replaceTabsWithSpaces(line)
lineNumber := lineNumberStyle.Render(fmt.Sprintf("%d", i+1))
if lipgloss.Width(line) > availableWidth {
line = truncate.StringWithTail(line, uint(availableWidth), ellipsis)
}

if lipgloss.Width(line) > availableWidth {
line = truncate.StringWithTail(line, uint(availableWidth), ellipsis)
buf.WriteString(lipgloss.JoinHorizontal(lipgloss.Left, lineNumber, line))
buf.WriteString(newLine)
}
)

if filterApplied {
lastPrintedLine := 0
separator := blankBlockSeparatorStyle.Render(strings.Repeat("─", max(0, m.width)))

for _, thisLine := range m.filteredLines {
if thisLine-lastPrintedLine > 1 {
buf.WriteString(separator)
buf.WriteString(newLine)
}

buf.WriteString(lipgloss.JoinHorizontal(lipgloss.Left, lineNumber, line))
buf.WriteString(newLine)
printSingleLine(lines[thisLine-1], thisLine)
lastPrintedLine = thisLine
}
} else {
for i, line := range lines {
printSingleLine(line, i+1)
}
}

return buf.String()
Expand Down
2 changes: 2 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Model struct {
sortByCoverage bool
detectedPackageName string
requestedFiles map[string]bool
filteredLinesByFile map[string][]int

activeView viewName
helpState helpState
Expand Down Expand Up @@ -257,6 +258,7 @@ func (m *Model) onKeyPressed(key string) (tea.Model, tea.Cmd) {
item, ok := m.list.SelectedItem().(*coverProfile)
if ok {
m.code.SetTitle(item.profile.FileName)
m.code.SetFilteredLines(m.filteredLinesByFile[item.profile.FileName])

adjustedFileName := path.Join(m.codeRoot, item.profile.FileName)

Expand Down
22 changes: 22 additions & 0 deletions internal/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ func WithRequestedFiles(files []string) Option {
}
}
}

// WithFilteredLines sets a list of lines to display for every file. Other
// lines will not appear.
func WithFilteredLines(files map[string][]int) Option {
return func(m *Model) {
m.filteredLinesByFile = make(map[string][]int, len(files))
uniqueLines := map[int]interface{}{}

for file, lines := range files {
linesWithContext := make([]int, 0, len(lines))

for _, line := range lines {
if _, ok := uniqueLines[line]; !ok {
linesWithContext = append(linesWithContext, line)
uniqueLines[line] = nil
}
}

m.filteredLinesByFile[file] = linesWithContext
}
}
}
31 changes: 25 additions & 6 deletions internal/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/orlangure/gocovsh/internal/model"
"github.com/waigani/diffparser"
)

const (
Expand Down Expand Up @@ -72,12 +73,14 @@ type Program struct {
profileFilename string
sortByCoverage bool

flagSet *flag.FlagSet
args []string
input fs.File
output io.Writer
logFile string
flagSet *flag.FlagSet
args []string
input fs.File
output io.Writer
logFile string

requestedFiles []string
diffLines map[string][]int
}

// Run parses the command line arguments and runs the program.
Expand Down Expand Up @@ -111,6 +114,7 @@ func (p *Program) Run() error {
model.WithProfileFilename(p.profileFilename),
model.WithRequestedFiles(p.requestedFiles),
model.WithCoverageSorting(p.sortByCoverage),
model.WithFilteredLines(p.diffLines),
)

if p.logFile != "" {
Expand Down Expand Up @@ -140,7 +144,22 @@ func (p *Program) parseInput() error {
return fmt.Errorf("failed to read stdin: %w", err)
}

p.requestedFiles = p.splitLines(string(bs))
if diff, err := diffparser.Parse(string(bs)); err != nil {
// fall back to file list mode - this is not an error
p.requestedFiles = p.splitLines(string(bs))
} else {
p.diffLines = diff.Changed()

for file := range p.diffLines {
if !strings.HasSuffix(file, ".go") {
delete(p.diffLines, file)
}
}

for _, file := range diff.Files {
p.requestedFiles = append(p.requestedFiles, file.NewName)
}
}
}

return nil
Expand Down

0 comments on commit 5103a12

Please sign in to comment.