Skip to content

Commit 3ef23d5

Browse files
6543zeripath
andauthored
Use gitea logging module for git module (#16243)
remove log() func from gogs times and switch to proper logging Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent 44b8b07 commit 3ef23d5

19 files changed

+60
-58
lines changed

modules/git/batch_reader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
"code.gitea.io/gitea/modules/log"
16+
1517
"github.com/djherbis/buffer"
1618
"github.com/djherbis/nio/v3"
1719
)
@@ -99,7 +101,7 @@ func ReadBatchLine(rd *bufio.Reader) (sha []byte, typ string, size int64, err er
99101
}
100102
idx := strings.IndexByte(typ, ' ')
101103
if idx < 0 {
102-
log("missing space typ: %s", typ)
104+
log.Debug("missing space typ: %s", typ)
103105
err = ErrNotExist{ID: string(sha)}
104106
return
105107
}
@@ -230,7 +232,7 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
230232
}
231233
idx := bytes.IndexByte(readBytes, ' ')
232234
if idx < 0 {
233-
log("missing space in readBytes ParseTreeLine: %s", readBytes)
235+
log.Debug("missing space in readBytes ParseTreeLine: %s", readBytes)
234236

235237
err = &ErrNotExist{}
236238
return

modules/git/blob_nogogit.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"io"
1313
"io/ioutil"
1414
"math"
15+
16+
"code.gitea.io/gitea/modules/log"
1517
)
1618

1719
// Blob represents a Git object.
@@ -69,12 +71,12 @@ func (b *Blob) Size() int64 {
6971
defer cancel()
7072
_, err := wr.Write([]byte(b.ID.String() + "\n"))
7173
if err != nil {
72-
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
74+
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
7375
return 0
7476
}
7577
_, _, b.size, err = ReadBatchLine(rd)
7678
if err != nil {
77-
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
79+
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
7880
return 0
7981
}
8082

modules/git/command.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616
"time"
1717

18+
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/process"
1920
)
2021

@@ -114,9 +115,9 @@ func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.
114115
}
115116

116117
if len(dir) == 0 {
117-
log(c.String())
118+
log.Debug("%s", c)
118119
} else {
119-
log("%s: %v", dir, c)
120+
log.Debug("%s: %v", dir, c)
120121
}
121122

122123
ctx, cancel := context.WithTimeout(c.parentContext, timeout)
@@ -197,9 +198,8 @@ func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir st
197198
if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil {
198199
return nil, ConcatenateError(err, stderr.String())
199200
}
200-
201-
if stdout.Len() > 0 {
202-
log("stdout:\n%s", stdout.Bytes()[:1024])
201+
if stdout.Len() > 0 && log.IsTrace() {
202+
log.Trace("Stdout:\n %s", stdout.Bytes()[:1024])
203203
}
204204
return stdout.Bytes(), nil
205205
}

modules/git/commit_info_nogogit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"io"
1313
"path"
1414
"sort"
15+
16+
"code.gitea.io/gitea/modules/log"
1517
)
1618

1719
// GetCommitsInfo gets information of all commits that are corresponding to these entries
@@ -78,7 +80,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
7880
commitsInfo[i].SubModuleFile = subModuleFile
7981
}
8082
} else {
81-
log("missing commit for %s", entry.Name())
83+
log.Debug("missing commit for %s", entry.Name())
8284
}
8385
}
8486

modules/git/diff.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717

18+
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/process"
1920
)
2021

@@ -113,7 +114,7 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
113114
righHunk, _ = strconv.Atoi(rightRange[1])
114115
}
115116
} else {
116-
log("Parse line number failed: %v", diffhunk)
117+
log.Debug("Parse line number failed: %v", diffhunk)
117118
rightLine = leftLine
118119
righHunk = leftHunk
119120
}

modules/git/git.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ import (
1919
)
2020

2121
var (
22-
// Debug enables verbose logging on everything.
23-
// This should be false in case Gogs starts in SSH mode.
24-
Debug = false
2522
// Prefix the log prefix
2623
Prefix = "[git-module] "
2724
// GitVersionRequired is the minimum Git version required
@@ -41,19 +38,6 @@ var (
4138
goVersionLessThan115 = true
4239
)
4340

44-
func log(format string, args ...interface{}) {
45-
if !Debug {
46-
return
47-
}
48-
49-
fmt.Print(Prefix)
50-
if len(args) == 0 {
51-
fmt.Println(format)
52-
} else {
53-
fmt.Printf(format+"\n", args...)
54-
}
55-
}
56-
5741
// LocalVersion returns current Git version from shell.
5842
func LocalVersion() (*version.Version, error) {
5943
if err := LoadGitVersion(); err != nil {

modules/git/git_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"fmt"
1010
"os"
1111
"testing"
12+
13+
"code.gitea.io/gitea/modules/log"
1214
)
1315

1416
func fatalTestError(fmtStr string, args ...interface{}) {
@@ -17,6 +19,8 @@ func fatalTestError(fmtStr string, args ...interface{}) {
1719
}
1820

1921
func TestMain(m *testing.M) {
22+
_ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`)
23+
2024
if err := Init(context.Background()); err != nil {
2125
fatalTestError("Init failed: %v", err)
2226
}

modules/git/hook.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2021 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -12,6 +13,7 @@ import (
1213
"path/filepath"
1314
"strings"
1415

16+
"code.gitea.io/gitea/modules/log"
1517
"code.gitea.io/gitea/modules/util"
1618
)
1719

@@ -126,11 +128,11 @@ const (
126128

127129
// SetUpdateHook writes given content to update hook of the repository.
128130
func SetUpdateHook(repoPath, content string) (err error) {
129-
log("Setting update hook: %s", repoPath)
131+
log.Debug("Setting update hook: %s", repoPath)
130132
hookPath := path.Join(repoPath, HookPathUpdate)
131133
isExist, err := util.IsExist(hookPath)
132134
if err != nil {
133-
log("Unable to check if %s exists. Error: %v", hookPath, err)
135+
log.Debug("Unable to check if %s exists. Error: %v", hookPath, err)
134136
return err
135137
}
136138
if isExist {

modules/git/last_commit_cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package git
77
import (
88
"crypto/sha256"
99
"fmt"
10+
11+
"code.gitea.io/gitea/modules/log"
1012
)
1113

1214
// Cache represents a caching interface
@@ -24,6 +26,6 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
2426

2527
// Put put the last commit id with commit and entry path
2628
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
27-
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
29+
log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
2830
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
2931
}

modules/git/last_commit_cache_gogit.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"context"
1111
"path"
1212

13+
"code.gitea.io/gitea/modules/log"
14+
1315
"github.com/go-git/go-git/v5/plumbing/object"
1416
cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
1517
)
@@ -41,9 +43,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
4143
func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
4244
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
4345
if vs, ok := v.(string); ok {
44-
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
46+
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
4547
if commit, ok := c.commitCache[vs]; ok {
46-
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
48+
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
4749
return commit, nil
4850
}
4951
id, err := c.repo.ConvertToSHA1(vs)

0 commit comments

Comments
 (0)