Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retrieve a list of diffs for all files that have outstanding changes #161

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 257 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package git

import (
"bufio"
"strconv"
"strings"

"github.com/purpleclay/chomp"
"github.com/purpleclay/gitz/scan"
)

const (
// git diff header delimiter > @@ ... @@
hdrDelim = "@@"
// prefix for lines added
addPrefix = "+"
// prefix for lines removed
remPrefix = "-"
)

// DiffOption provides a way for setting specific options during a diff
// operation. Each supported option can customize the way the diff is
// executed against the current repository (working directory)
type DiffOption func(*diffOptions)

type diffOptions struct {
DiffPaths []string
}

// WithDiffPaths allows the diff to be targeted to specific files and
// folers within the current repository (working directory). Paths to
// files and folders are relative to the root of the repository. All
// leading and trailing whitepsace will be trimmed from the file paths,
// allowing empty paths to be ignored
func WithDiffPaths(paths ...string) DiffOption {
return func(opts *diffOptions) {
opts.DiffPaths = trim(paths...)
}
}

// FileDiff represents a snapshot containing all of the changes to
// a file within a repository (working directory)
type FileDiff struct {
// Path of the file within the repository (working directory)
Path string

// DiffChunk contains all of the identified changes within
// the file
Chunks []DiffChunk
}

// DiffChunk represents a snapshot of a single change (chunk) to
// a file within a repository (working directory)
type DiffChunk struct {
// Added optionally contains details of the text that has
// been added to a file as part of the current change
Added DiffChange

// Removed optionally contains details of the text that has
// been removed from a file as part of the current change
Removed DiffChange
}

// DiffChange captures details about an individual chunk
// within a git diff. It contains both the changed text and
// its exact position (and line count) within the file
type DiffChange struct {
// LineNo is the position within the file where the
// change starts
LineNo int

// Count is the number of lines that has changed
Count int

// Change contains the text that has changed
Change string
}

// Diff captures the changes made to files within the current repository (working
// directory). Options can be provided to customize how the current diff is
// determined. By default, all diffs (or changes) to files within the repository
// will be retrieved. The diff is generated using the following git options:
//
// git diff -U0 --no-color
func (c *Client) Diff(opts ...DiffOption) ([]FileDiff, error) {
options := &diffOptions{}
for _, opt := range opts {
opt(options)
}

var buf strings.Builder
buf.WriteString("git diff -U0 --no-color")

if len(options.DiffPaths) > 0 {
buf.WriteString(" -- ")
buf.WriteString(strings.Join(options.DiffPaths, " "))
}

out, err := c.exec(buf.String())
if err != nil {
return nil, err
}
return parseDiffs(out)
}

func parseDiffs(log string) ([]FileDiff, error) {
var diffs []FileDiff

scanner := bufio.NewScanner(strings.NewReader(log))
scanner.Split(scan.DiffLines())

for scanner.Scan() {
diff, err := parseDiff(scanner.Text())
if err != nil {
return nil, err
}

diffs = append(diffs, diff)
}

return diffs, nil
}

func parseDiff(diff string) (FileDiff, error) {
rem, path, err := diffPath()(diff)
if err != nil {
return FileDiff{}, err
}

rem, _, err = chomp.Until(hdrDelim)(rem)
if err != nil {
return FileDiff{}, err
}

chunks, err := diffChunks(rem)
if err != nil {
return FileDiff{}, err
}

return FileDiff{
Path: path,
Chunks: chunks,
}, nil
}

func diffPath() chomp.Combinator[string] {
return func(s string) (string, string, error) {
var rem string
var err error

if rem, _, err = chomp.Tag("diff --git ")(s); err != nil {
return rem, "", err
}

var path string
if rem, path, err = chomp.Until(" ")(rem); err != nil {
return rem, "", err
}
path = path[strings.Index(path, "/")+1:]

rem, _, err = chomp.Eol()(rem)
return rem, path, err
}
}

func diffChunks(in string) ([]DiffChunk, error) {
_, chunks, err := chomp.Map(chomp.Many(diffChunk()),
func(in []string) []DiffChunk {
var diffChunks []DiffChunk

for i := 0; i+5 < len(in); i += 6 {
chunk := DiffChunk{
Removed: DiffChange{
LineNo: mustInt(in[i]),
Count: mustInt(in[i+1]),
Change: in[i+4],
},
Added: DiffChange{
LineNo: mustInt(in[i+2]),
Count: mustInt(in[i+3]),
Change: in[i+5],
},
}

if chunk.Added.Count == 0 {
chunk.Added.Count = 1
}

if chunk.Removed.Count == 0 {
chunk.Removed.Count = 1
}

diffChunks = append(diffChunks, chunk)
}

return diffChunks
},
)(in)

return chunks, err
}

func mustInt(in string) int {
out, _ := strconv.Atoi(in)
return out
}

func diffChunk() chomp.Combinator[[]string] {
return func(s string) (string, []string, error) {
var rem string
var err error

var changes []string
rem, changes, err = chomp.Delimited(
chomp.Tag(hdrDelim+" "),
chomp.SepPair(diffChunkHeaderChange(remPrefix), chomp.Tag(" "), diffChunkHeaderChange(addPrefix)),
chomp.Eol(),
)(s)
if err != nil {
return rem, nil, err
}

var removed string
rem, removed, err = chomp.Map(
chomp.ManyN(chomp.Prefixed(chomp.Eol(), chomp.Tag(remPrefix)), 0),
func(in []string) string { return strings.Join(in, "\n") },
)(rem)
if err != nil {
return rem, nil, err
}

var added string
rem, added, err = chomp.Map(
chomp.ManyN(chomp.Prefixed(chomp.Eol(), chomp.Tag(addPrefix)), 0),
func(in []string) string { return strings.Join(in, "\n") },
)(rem)
if err != nil {
return rem, nil, err
}

return rem, append(changes, removed, added), nil
}
}

func diffChunkHeaderChange(prefix string) chomp.Combinator[[]string] {
return func(s string) (string, []string, error) {
rem, _, err := chomp.Tag(prefix)(s)
if err != nil {
return rem, nil, err
}

return chomp.All(
chomp.While(chomp.IsDigit),
chomp.Opt(chomp.Prefixed(chomp.While(chomp.IsDigit), chomp.Tag(","))),
)(rem)
}
}
83 changes: 83 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package git_test

import (
"testing"

git "github.com/purpleclay/gitz"
"github.com/purpleclay/gitz/gittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDiff(t *testing.T) {
gittest.InitRepository(t,
gittest.WithCommittedFiles("main.go"),
gittest.WithFileContent("main.go", `package main

import "fmt"

func print() {
fmt.Println("Hello, World!")
}

func main() {
print()
}`))

overwriteFile(t, "main.go", `package main

import (
"fmt"
"os"
)

func main() {
fmt.Printf("Hello, %s\n" + os.Args[1])
}`)

client, _ := git.NewClient()
diffs, err := client.Diff()
require.NoError(t, err)

require.Len(t, diffs, 1)
assert.Equal(t, "main.go", diffs[0].Path)

require.Len(t, diffs[0].Chunks, 2)
assert.Equal(t, 3, diffs[0].Chunks[0].Added.LineNo)
assert.Equal(t, 4, diffs[0].Chunks[0].Added.Count)
assert.Equal(t, `import (
"fmt"
"os"
)`, diffs[0].Chunks[0].Added.Change)

assert.Equal(t, 3, diffs[0].Chunks[0].Removed.LineNo)
assert.Equal(t, 5, diffs[0].Chunks[0].Removed.Count)
assert.Equal(t, `import "fmt"

func print() {
fmt.Println("Hello, World!")
}`, diffs[0].Chunks[0].Removed.Change)

assert.Equal(t, 9, diffs[0].Chunks[1].Added.LineNo)
assert.Equal(t, 1, diffs[0].Chunks[1].Added.Count)
assert.Equal(t, ` fmt.Printf("Hello, %s\n" + os.Args[1])`, diffs[0].Chunks[1].Added.Change)

assert.Equal(t, 10, diffs[0].Chunks[1].Removed.LineNo)
assert.Equal(t, 1, diffs[0].Chunks[1].Removed.Count)
assert.Equal(t, ` print()`, diffs[0].Chunks[1].Removed.Change)
}

func TestDiffWithDiffPaths(t *testing.T) {
gittest.InitRepository(t,
gittest.WithCommittedFiles("file1.txt", "file2.txt"),
gittest.WithFileContent("file1.txt", "Hello, World!", "file2.txt", "Goodbye, World!"))

overwriteFile(t, "file1.txt", "Goodbye, World!")
overwriteFile(t, "file2.txt", "Hello, World!")

client, _ := git.NewClient()
diffs, err := client.Diff(git.WithDiffPaths("file1.txt"))
require.NoError(t, err)

assert.Len(t, diffs, 1)
}
22 changes: 0 additions & 22 deletions gittest/log.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
/*
Copyright (c) 2023 Purple Clay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package gittest

import (
Expand Down
22 changes: 0 additions & 22 deletions gittest/log_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
/*
Copyright (c) 2023 Purple Clay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package gittest_test

import (
Expand Down
Loading
Loading