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)

modules/git/last_commit_cache_nogogit.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"bufio"
1111
"context"
1212
"path"
13+
14+
"code.gitea.io/gitea/modules/log"
1315
)
1416

1517
// LastCommitCache represents a cache to store last commit
@@ -39,9 +41,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
3941
func (c *LastCommitCache) Get(ref, entryPath string, wr WriteCloserError, rd *bufio.Reader) (interface{}, error) {
4042
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
4143
if vs, ok := v.(string); ok {
42-
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
44+
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
4345
if commit, ok := c.commitCache[vs]; ok {
44-
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
46+
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
4547
return commit, nil
4648
}
4749
id, err := c.repo.ConvertToSHA1(vs)

modules/git/parse_nogogit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"io"
1414
"strconv"
1515
"strings"
16+
17+
"code.gitea.io/gitea/modules/log"
1618
)
1719

1820
// ParseTreeEntries parses the output of a `git ls-tree -l` command.
@@ -120,7 +122,7 @@ loop:
120122
case "40000":
121123
entry.entryMode = EntryModeTree
122124
default:
123-
log("Unknown mode: %v", string(mode))
125+
log.Debug("Unknown mode: %v", string(mode))
124126
return nil, fmt.Errorf("unknown mode: %v", string(mode))
125127
}
126128

modules/git/repo_base_nogogit.go

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

1719
// Repository represents a Git repository.
@@ -54,7 +56,7 @@ func OpenRepository(repoPath string) (*Repository, error) {
5456
// CatFileBatch obtains a CatFileBatch for this repository
5557
func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) {
5658
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
57-
log("Opening temporary cat file batch for: %s", repo.Path)
59+
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
5860
return CatFileBatch(repo.Path)
5961
}
6062
return repo.batchWriter, repo.batchReader, func() {}
@@ -63,7 +65,7 @@ func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func())
6365
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
6466
func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) {
6567
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
66-
log("Opening temporary cat file batch-check: %s", repo.Path)
68+
log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
6769
return CatFileBatchCheck(repo.Path)
6870
}
6971
return repo.checkWriter, repo.checkReader, func() {}

modules/git/repo_branch_nogogit.go

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

1719
// IsObjectExist returns true if given reference exists in the repository.
@@ -24,7 +26,7 @@ func (repo *Repository) IsObjectExist(name string) bool {
2426
defer cancel()
2527
_, err := wr.Write([]byte(name + "\n"))
2628
if err != nil {
27-
log("Error writing to CatFileBatchCheck %v", err)
29+
log.Debug("Error writing to CatFileBatchCheck %v", err)
2830
return false
2931
}
3032
sha, _, _, err := ReadBatchLine(rd)
@@ -41,7 +43,7 @@ func (repo *Repository) IsReferenceExist(name string) bool {
4143
defer cancel()
4244
_, err := wr.Write([]byte(name + "\n"))
4345
if err != nil {
44-
log("Error writing to CatFileBatchCheck %v", err)
46+
log.Debug("Error writing to CatFileBatchCheck %v", err)
4547
return false
4648
}
4749
_, _, _, err = ReadBatchLine(rd)

modules/git/repo_commit_nogogit.go

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

1719
// ResolveReference resolves a name to a reference
@@ -110,7 +112,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
110112

111113
return commit, nil
112114
default:
113-
log("Unknown typ: %s", typ)
115+
log.Debug("Unknown typ: %s", typ)
114116
_, err = rd.Discard(int(size) + 1)
115117
if err != nil {
116118
return nil, err

modules/git/repo_language_stats_nogogit.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math"
1414

1515
"code.gitea.io/gitea/modules/analyze"
16+
"code.gitea.io/gitea/modules/log"
1617

1718
"github.com/go-enry/go-enry/v2"
1819
)
@@ -34,19 +35,19 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
3435
}
3536
shaBytes, typ, size, err := ReadBatchLine(batchReader)
3637
if typ != "commit" {
37-
log("Unable to get commit for: %s. Err: %v", commitID, err)
38+
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
3839
return nil, ErrNotExist{commitID, ""}
3940
}
4041

4142
sha, err := NewIDFromString(string(shaBytes))
4243
if err != nil {
43-
log("Unable to get commit for: %s. Err: %v", commitID, err)
44+
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
4445
return nil, ErrNotExist{commitID, ""}
4546
}
4647

4748
commit, err := CommitFromReader(repo, sha, io.LimitReader(batchReader, size))
4849
if err != nil {
49-
log("Unable to get commit for: %s. Err: %v", commitID, err)
50+
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
5051
return nil, err
5152
}
5253
if _, err = batchReader.Discard(1); err != nil {
@@ -79,7 +80,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
7980
}
8081
_, _, size, err := ReadBatchLine(batchReader)
8182
if err != nil {
82-
log("Error reading blob: %s Err: %v", f.ID.String(), err)
83+
log.Debug("Error reading blob: %s Err: %v", f.ID.String(), err)
8384
return nil, err
8485
}
8586

0 commit comments

Comments
 (0)