From 2738cc2d2e296e737a9afcb1f9ca7910dd39f657 Mon Sep 17 00:00:00 2001 From: George Aristy Date: Mon, 18 Feb 2019 17:33:16 -0500 Subject: [PATCH] (#4) Read the commit log (#7) --- .golangci.yml | 2 +- .pdd | 6 +++ Makefile | 2 +- README.md | 2 +- cmd/go-gitlint/main.go | 18 ++++++- go.mod | 6 +++ go.sum | 60 +++++++++++++++++++++ internal/commits/commits.go | 87 ++++++++++++++++++++++++++++++ internal/commits/commits_test.go | 91 ++++++++++++++++++++++++++++++++ internal/repo/repo.go | 20 +++++++ internal/repo/repo_test.go | 71 +++++++++++++++++++++++++ 11 files changed, 361 insertions(+), 4 deletions(-) create mode 100644 internal/commits/commits.go create mode 100644 internal/commits/commits_test.go create mode 100644 internal/repo/repo.go create mode 100644 internal/repo/repo_test.go diff --git a/.golangci.yml b/.golangci.yml index 76ca235..c125da5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -103,7 +103,7 @@ linters-settings: #local-prefixes: github.com/org/project gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 7 + min-complexity: 5 maligned: # print struct with more effective memory layout or not, false by default suggest-new: true diff --git a/.pdd b/.pdd index c264c25..28aad3a 100644 --- a/.pdd +++ b/.pdd @@ -1,5 +1,11 @@ --source=. --verbose +--exclude dist/**/* +--exclude bin/**/* +--exclude gitlint +--exclude coverage.txt +--exclude .gitignore +--exclude go.sum --rule min-words:20 --rule min-estimate:0 --rule max-estimate:0 diff --git a/Makefile b/Makefile index 5bb31ca..60ee0f0 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ test: go test -count=1 -race -cover -coverprofile=coverage.txt -covermode=atomic ./... lint: - ./bin/golangci-lint run -v + ./bin/golangci-lint run pdd: pdd --file=puzzles.xml diff --git a/README.md b/README.md index bba3571..0faa356 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ As an architect of other (not necessarily golang) projects hosted on GitHub I ne ## Contributing Just open a PR, make sure `make checks` works. -## Dependencies +## Build dependencies To run `make checks` you will need: * Go `1.11.x` diff --git a/cmd/go-gitlint/main.go b/cmd/go-gitlint/main.go index bbc2b43..63bd24c 100644 --- a/cmd/go-gitlint/main.go +++ b/cmd/go-gitlint/main.go @@ -1,5 +1,21 @@ package main +import ( + "os" + + "github.com/llorllale/go-gitlint/internal/commits" + "github.com/llorllale/go-gitlint/internal/repo" +) + +// @todo #4 The path should be passed in as a command line +// flag instead of being hard coded. All other configuration +// options should be passed in through CLI as well. func main() { - panic("not yet implemented") + commits.Printed( + commits.In( + repo.Filesystem("."), + ), + os.Stdout, + "\n", + )() } diff --git a/go.mod b/go.mod index 04748f1..22d4e59 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,7 @@ module github.com/llorllale/go-gitlint + +require ( + github.com/google/uuid v1.1.0 + github.com/stretchr/testify v1.2.2 + gopkg.in/src-d/go-git.v4 v4.10.0 +) diff --git a/go.sum b/go.sum index e69de29..5ca8839 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,60 @@ +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo= +github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw= +github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s= +github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8= +github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA= +github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +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/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4= +github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro= +github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas= +golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo= +gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk= +gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 h1:XWW/s5W18RaJpmo1l0IYGqXKuJITWRFuA45iOf1dKJs= +gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g= +gopkg.in/src-d/go-git.v4 v4.10.0 h1:NWjTJTQnk8UpIGlssuefyDZ6JruEjo5s88vm88uASbw= +gopkg.in/src-d/go-git.v4 v4.10.0/go.mod h1:Vtut8izDyrM8BUVQnzJ+YvmNcem2J89EmfZYCkLokZk= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= diff --git a/internal/commits/commits.go b/internal/commits/commits.go new file mode 100644 index 0000000..653b5b0 --- /dev/null +++ b/internal/commits/commits.go @@ -0,0 +1,87 @@ +package commits + +import ( + "fmt" + "io" + + "github.com/llorllale/go-gitlint/internal/repo" + git "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing/object" +) + +// Commits returns commits. +// @todo #4 Figure out how to disable the golint check that +// forces us to write redundant comments of the form +// 'comment on exported type Commits should be of the form +// "Commits ..." (with optional leading article)' and rewrite +// all comments. +type Commits func() []*Commit + +// Commit holds data for a single git commit. +type Commit struct { + Hash string + Subject string + Body string +} + +// In returns the commits in the repo. +// @todo #4 These err checks are extremely annoying. Figure out +// how to handle them elegantly and reduce the cyclo complexity +// of this function (currently at 4). +func In(repository repo.Repo) Commits { + return func() []*Commit { + r := repository() + ref, err := r.Head() + if err != nil { + panic(err) + } + iter, err := r.Log(&git.LogOptions{From: ref.Hash()}) + if err != nil { + panic(err) + } + commits := make([]*Commit, 0) + _ = iter.ForEach(func(c *object.Commit) error { //nolint[errcheck] + commits = append( + commits, + &Commit{ + Hash: c.Hash.String(), + // @todo #4 Figure out how to split the commit's message into a subject + // and a body. I believe the separator is the first instance of the + // CRLFCRLF sequence. + Subject: "don't know", + Body: c.Message, + }, + ) + return nil + }) + return commits + } +} + +// Printed prints the commits to the file. +func Printed(commits Commits, writer io.Writer, sep string) Commits { + return func() []*Commit { + input := commits() + for _, c := range input { + _, err := writer.Write( + []byte(fmt.Sprintf("%s%s", &pretty{c}, sep)), + ) + if err != nil { + panic(err) + } + } + return input + } +} + +// a Stringer for pretty-printing the commit. +type pretty struct { + *Commit +} + +func (p *pretty) String() string { + return fmt.Sprintf( + "hash: %s subject=%s body=%s", + p.Commit.Hash, p.Commit.Subject, p.Commit.Body, + ) +} diff --git a/internal/commits/commits_test.go b/internal/commits/commits_test.go new file mode 100644 index 0000000..34afe18 --- /dev/null +++ b/internal/commits/commits_test.go @@ -0,0 +1,91 @@ +package commits + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "github.com/llorllale/go-gitlint/internal/repo" + "github.com/stretchr/testify/assert" + git "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing/object" +) + +func TestIn(t *testing.T) { + msgs := []string{"commit1", "commit2", "commit3"} + r, cleanup := tmpRepo(msgs...) + defer cleanup() + commits := In(r)() + assert.Len(t, commits, len(msgs), + "commits.In() did not return the correct number of commits") + for i, msg := range msgs { + assert.Equal(t, msg, commits[len(commits)-i-1].Body, + "commits.In() returned commits with incorrect body messages") + } +} + +func TestPrinted(t *testing.T) { + commit := &Commit{ + Hash: "abc123", + Subject: "subject", + Body: "body", + } + const sep = " " + buffer := &bytes.Buffer{} + _ = Printed( + func() []*Commit { return []*Commit{commit} }, + buffer, + sep, + )() + assert.Equal(t, (&pretty{commit}).String()+sep, string(buffer.Bytes()), + "commits.Printed() did not pretty-print the commit correctly") +} + +// A git repo initialized and with one commit per each of the messages provided. +// This repo is created in a temporary directory; use the cleanup function +// to delete it afterwards. +func tmpRepo(msgs ...string) (r repo.Repo, cleanup func()) { + folder, err := ioutil.TempDir( + "", + strings.Replace(uuid.New().String(), "-", "", -1), //nolint[gocritic] + ) + panicIf(err) + cleanup = func() { + panicIf(os.RemoveAll(folder)) + } + r = func() *git.Repository { + r, err := git.PlainInit(folder, false) + panicIf(err) + wt, err := r.Worktree() + panicIf(err) + for i, msg := range msgs { + file := fmt.Sprintf("msg%d.txt", i) + panicIf(ioutil.WriteFile(filepath.Join(folder, file), []byte(msg), 0644)) + _, err = wt.Add(file) + panicIf(err) + _, err = wt.Commit(msg, &git.CommitOptions{ + Author: &object.Signature{ + Name: "John Doe", + Email: "john@doe.org", + When: time.Now(), + }, + }) + panicIf(err) + } + return r + } + return r, cleanup +} + +// panics if err is not nil. +func panicIf(err error) { + if err != nil { + panic(err) + } +} diff --git a/internal/repo/repo.go b/internal/repo/repo.go new file mode 100644 index 0000000..805db43 --- /dev/null +++ b/internal/repo/repo.go @@ -0,0 +1,20 @@ +package repo + +import ( + git "gopkg.in/src-d/go-git.v4" +) + +// Repo is an initialized git repository. +type Repo func() *git.Repository + +// Filesystem is a pre-existing git repository on the filesystem +// with directory as root. +func Filesystem(directory string) Repo { + return func() *git.Repository { + repo, err := git.PlainOpen(directory) + if err != nil { + panic(err) + } + return repo + } +} diff --git a/internal/repo/repo_test.go b/internal/repo/repo_test.go new file mode 100644 index 0000000..9787caa --- /dev/null +++ b/internal/repo/repo_test.go @@ -0,0 +1,71 @@ +package repo + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + git "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing/object" +) + +func TestFilesystem(t *testing.T) { + msgs := []string{"commit1", "commit2", "commit3"} + r, path, cleanup := tmpGitRepo(msgs...) + defer cleanup() + test := Filesystem(path)() + head, err := test.Head() + require.NoError(t, err) + iter, err := r.Log(&git.LogOptions{From: head.Hash()}) + require.NoError(t, err) + _ = iter.ForEach(func(c *object.Commit) error { //nolint[errcheck] + assert.Contains(t, msgs, c.Message, + "repo.Filesystem() did not return all commits") + return nil + }) +} + +func tmpGitRepo(msgs ...string) (r *git.Repository, folder string, cleanup func()) { + var err error + folder, err = ioutil.TempDir( + "", + strings.Replace(uuid.New().String(), "-", "", -1), //nolint[gocritic] + ) + panicIf(err) + cleanup = func() { + panicIf(os.RemoveAll(folder)) + } + r, err = git.PlainInit(folder, false) + panicIf(err) + wt, err := r.Worktree() + panicIf(err) + for i, msg := range msgs { + file := fmt.Sprintf("msg%d.txt", i) + panicIf(ioutil.WriteFile(filepath.Join(folder, file), []byte(msg), 0644)) + _, err = wt.Add(file) + panicIf(err) + _, err = wt.Commit(msg, &git.CommitOptions{ + Author: &object.Signature{ + Name: "John Doe", + Email: "john@doe.org", + When: time.Now(), + }, + }) + panicIf(err) + } + return r, folder, cleanup +} + +// panics if err is not nil. +func panicIf(err error) { + if err != nil { + panic(err) + } +}