Skip to content

Commit

Permalink
print changed lines
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre committed Nov 9, 2018
1 parent 1883010 commit 74f629c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (cmd Common) Tidy() {
ErrFatalf(err, "unable to change directory: %v\n", err)

for repeat := 2; repeat > 0; repeat-- {
gomod := exec.Command("go", "mod", "tidy", "-v")
gomod := exec.Command("go", "mod", "tidy")
gomod.Env = append(os.Environ(), "GO111MODULE=on")
gomod.Stdout, gomod.Stderr = os.Stderr, os.Stderr
err = gomod.Run()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module storj.io/gospace

require github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
29 changes: 26 additions & 3 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/kylelemons/godebug/diff"
)

type IsTidy struct {
Expand All @@ -24,16 +29,34 @@ func (cmd *IsTidy) Parse(args []string) error {
func (cmd *IsTidy) Exec() {
repodir := cmd.RepoDir()

before, err := HashFiles(filepath.Join(repodir, "go.mod"))
before, err := ioutil.ReadFile(filepath.Join(repodir, "go.mod"))
ErrFatal(err)

cmd.Tidy()

after, err := HashFiles(filepath.Join(repodir, "go.mod"))
after, err := ioutil.ReadFile(filepath.Join(repodir, "go.mod"))
ErrFatal(err)

if before != after {
if string(before) != string(after) {
fmt.Fprintln(os.Stderr, "go.mod is not tidy")
fmt.Fprintln(os.Stderr, difflines(string(before), string(after)))
os.Exit(1)
}
}

func difflines(a, b string) string {
alines, blines := strings.Split(a, "\n"), strings.Split(b, "\n")

chunks := diff.DiffChunks(alines, blines)

buf := new(bytes.Buffer)
for _, c := range chunks {
for _, line := range c.Added {
fmt.Fprintf(buf, "+%s\n", line)
}
for _, line := range c.Deleted {
fmt.Fprintf(buf, "-%s\n", line)
}
}
return strings.TrimRight(buf.String(), "\n")
}

0 comments on commit 74f629c

Please sign in to comment.