Skip to content

Commit 5402abb

Browse files
author
fallion
committed
refactor!: replace debuglogger with apex
BREAKING CHANGE: this commit replaces the previous overly verbose logging solution with apex. All consumers will need to be able to use apex in order to control the log level. Overall it should however improve the usage of this package. Mitigation: Remove debugLogger from the init of this package. If you want to control the log level import the apex log package into your project and set it there.
1 parent 0ac908c commit 5402abb

17 files changed

+86
-59
lines changed

commit_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package git
22

33
import (
4-
"io/ioutil"
5-
"log"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
@@ -12,7 +10,7 @@ func TestCommit(t *testing.T) {
1210
repo := setupRepo()
1311
createTestHistory(repo)
1412

15-
testGit := &Git{repo: repo, DebugLogger: log.New(ioutil.Discard, "", 0)}
13+
testGit := &Git{repo: repo}
1614

1715
head, _ := testGit.CurrentCommit()
1816

commits_between_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package git
22

33
import (
4-
"io/ioutil"
5-
"log"
64
"testing"
75

86
"github.com/go-git/go-git/v5"
@@ -12,7 +10,7 @@ import (
1210

1311
func TestCommitsBetween(t *testing.T) {
1412
repo, _ := git.PlainOpen("./testdata/git_tags")
15-
testGit := &Git{repo: repo, DebugLogger: log.New(ioutil.Discard, "", 0)}
13+
testGit := &Git{repo: repo}
1614

1715
head, err := repo.Head()
1816

@@ -42,7 +40,7 @@ func TestNoToCommit(t *testing.T) {
4240

4341
head, _ := repo.Head()
4442

45-
testGit := &Git{repo: repo, DebugLogger: log.New(ioutil.Discard, "", 0)}
43+
testGit := &Git{repo: repo}
4644

4745
commits, err := testGit.CommitsBetween(head.Hash(), plumbing.Hash{})
4846

commits_on_branch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"errors"
55

6+
"github.com/apex/log"
67
"github.com/go-git/go-git/v5"
78
"github.com/go-git/go-git/v5/plumbing"
89
"github.com/go-git/go-git/v5/plumbing/object"
@@ -32,7 +33,7 @@ func (g *Git) CommitsOnBranch(
3233
})
3334

3435
if branchIterErr != nil {
35-
g.DebugLogger.Printf("Stopped getting commits on branch: %v", branchIterErr)
36+
log.Debugf("Stopped getting commits on branch: %v", branchIterErr)
3637
}
3738

3839
return branchCommits, nil

commits_on_branch_simple.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package git
22

33
import (
4+
"github.com/apex/log"
45
"github.com/go-git/go-git/v5"
56
"github.com/go-git/go-git/v5/plumbing"
67
"github.com/go-git/go-git/v5/plumbing/object"
78
)
89

10+
// SimpleCommit is a slimed down commit object of just Hash and Message
911
type SimpleCommit struct {
1012
Hash [20]byte
1113
Message string
@@ -35,7 +37,7 @@ func (g *Git) CommitsOnBranchSimple(
3537
})
3638

3739
if branchIterErr != nil {
38-
g.DebugLogger.Printf("Stopped getting commits on branch: %v", branchIterErr)
40+
log.Debugf("Stopped getting commits on branch: %v", branchIterErr)
3941
}
4042

4143
return branchCommits, nil

current_commit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"github.com/apex/log"
45
"github.com/go-git/go-git/v5/plumbing/object"
56
)
67

@@ -14,7 +15,7 @@ func (g *Git) CurrentCommit() (*object.Commit, error) {
1415

1516
currentCommitHash := head.Hash()
1617

17-
g.DebugLogger.Printf("current commitHash: %v \n", currentCommitHash)
18+
log.Debugf("current commitHash: %v \n", currentCommitHash)
1819

1920
commitObject, err := g.repo.CommitObject(currentCommitHash)
2021

current_commit_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package git
22

33
import (
4-
"io/ioutil"
5-
"log"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
@@ -12,7 +10,7 @@ func TestCurrentCommit(t *testing.T) {
1210
repo := setupRepo()
1311
createTestHistory(repo)
1412

15-
testGit := &Git{repo: repo, DebugLogger: log.New(ioutil.Discard, "", 0)}
13+
testGit := &Git{repo: repo}
1614

1715
currentCommit, err := testGit.CurrentCommit()
1816

current_tag.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package git
22

33
import (
44
"errors"
5+
6+
"github.com/apex/log"
57
)
68

79
var (
@@ -23,10 +25,10 @@ func (g *Git) CurrentTag() (*Tag, error) {
2325
return nil, err
2426
}
2527

26-
g.DebugLogger.Println("head hash: ", head.Hash())
28+
log.Debugf("head hash: %s", head.Hash())
2729

2830
for _, tag := range tags {
29-
g.DebugLogger.Printf("tag: %v, hash: %v", tag.Name, tag.Hash)
31+
log.Debugf("tag: %v, hash: %v", tag.Name, tag.Hash)
3032

3133
if tag.Hash == head.Hash() {
3234
return tag, nil

current_tag_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package git
22

33
import (
4-
"io/ioutil"
5-
"log"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
97
)
108

119
func TestCurrentTagHappy(t *testing.T) {
12-
testGit, err := OpenGit("./testdata/git_tags", nil)
10+
testGit, err := OpenGit("./testdata/git_tags")
1311

1412
assert.NoError(t, err)
1513

@@ -20,7 +18,7 @@ func TestCurrentTagHappy(t *testing.T) {
2018
}
2119

2220
func TestCurrentTagAnnotatedHappy(t *testing.T) {
23-
testGit, err := OpenGit("./testdata/annotated_git_tags_mix", nil)
21+
testGit, err := OpenGit("./testdata/annotated_git_tags_mix")
2422

2523
assert.NoError(t, err)
2624

@@ -34,7 +32,7 @@ func TestCurrentTagUnhappy(t *testing.T) {
3432
repo := setupRepo()
3533
createTestHistory(repo)
3634

37-
testGit := &Git{repo: repo, DebugLogger: log.New(ioutil.Discard, "", 0)}
35+
testGit := &Git{repo: repo}
3836

3937
_, err := testGit.CurrentTag()
4038

docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// Package history is a wrapper for Git actions. The main purpose is the unify some functionality in this package.
1+
// Package git is a wrapper for Git actions. The main purpose is the unify some functionality in this package.
22
package git

get_tags.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"sort"
55

6+
"github.com/apex/log"
67
"github.com/go-git/go-git/v5"
78
"github.com/go-git/go-git/v5/plumbing"
89
"github.com/go-git/go-git/v5/plumbing/object"
@@ -23,7 +24,7 @@ func (g *Git) getTags() ([]*Tag, error) {
2324
commitDate, err := g.commitDate(t.Hash())
2425

2526
if err != nil {
26-
g.DebugLogger.Printf("tag: %v ignored due to missing commit date: %v", t.Name(), err)
27+
log.Debugf("tag: %v ignored due to missing commit date: %v", t.Name(), err)
2728
return nil
2829
}
2930

@@ -50,9 +51,9 @@ func (g *Git) getTags() ([]*Tag, error) {
5051
// Tags are alphabetically ordered. We need to sort them by date.
5152
sortedTags := sortTags(g.repo, tags)
5253

53-
g.DebugLogger.Println("Sorted tag output: ")
54+
log.Debug("Sorted tag output: ")
5455
for _, taginstance := range sortedTags {
55-
g.DebugLogger.Printf("hash: %v time: %v", taginstance.Hash, taginstance.Date.UTC())
56+
log.Debugf("hash: %v time: %v", taginstance.Hash, taginstance.Date.UTC())
5657
}
5758

5859
return sortedTags, nil

0 commit comments

Comments
 (0)