Skip to content

Commit

Permalink
(#4) Read the commit log (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Feb 18, 2019
1 parent 73a78c5 commit 2738cc2
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .pdd
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
18 changes: 17 additions & 1 deletion cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
@@ -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",
)()
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
60 changes: 60 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
87 changes: 87 additions & 0 deletions internal/commits/commits.go
Original file line number Diff line number Diff line change
@@ -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,
)
}
91 changes: 91 additions & 0 deletions internal/commits/commits_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions internal/repo/repo.go
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

4 comments on commit 2738cc2

@0pdd
Copy link

@0pdd 0pdd commented on 2738cc2 Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 4-2f6700f4 discovered in cmd/go-gitlint/main.go and submitted as #9. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2738cc2 Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 4-dc893088 discovered in internal/commits/commits.go and submitted as #10. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2738cc2 Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 4-faa799fb discovered in internal/commits/commits.go and submitted as #11. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2738cc2 Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 4-03485093 discovered in internal/commits/commits.go and submitted as #12. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.