Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gitea logging module for git module #16243

Merged
merged 9 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/git/batch_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"strings"

"code.gitea.io/gitea/modules/log"

"github.com/djherbis/buffer"
"github.com/djherbis/nio/v3"
)
Expand Down Expand Up @@ -99,7 +101,7 @@ func ReadBatchLine(rd *bufio.Reader) (sha []byte, typ string, size int64, err er
}
idx := strings.IndexByte(typ, ' ')
if idx < 0 {
log("missing space typ: %s", typ)
log.Debug("missing space typ: %s", typ)
err = ErrNotExist{ID: string(sha)}
return
}
Expand Down Expand Up @@ -230,7 +232,7 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
}
idx := bytes.IndexByte(readBytes, ' ')
if idx < 0 {
log("missing space in readBytes ParseTreeLine: %s", readBytes)
log.Debug("missing space in readBytes ParseTreeLine: %s", readBytes)

err = &ErrNotExist{}
return
Expand Down
6 changes: 4 additions & 2 deletions modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"io"
"io/ioutil"
"math"

"code.gitea.io/gitea/modules/log"
)

// Blob represents a Git object.
Expand Down Expand Up @@ -69,12 +71,12 @@ func (b *Blob) Size() int64 {
defer cancel()
_, err := wr.Write([]byte(b.ID.String() + "\n"))
if err != nil {
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0
}
_, _, b.size, err = ReadBatchLine(rd)
if err != nil {
log("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0
}

Expand Down
7 changes: 4 additions & 3 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"time"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
)

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

if len(dir) == 0 {
log(c.String())
log.Debug(c.String())
6543 marked this conversation as resolved.
Show resolved Hide resolved
} else {
log("%s: %v", dir, c)
log.Debug("%s: %v", dir, c)
}

ctx, cancel := context.WithTimeout(c.parentContext, timeout)
Expand Down Expand Up @@ -199,7 +200,7 @@ func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir st
}

if stdout.Len() > 0 {
log("stdout:\n%s", stdout.Bytes()[:1024])
log.Debug("stdout:\n%s", stdout.Bytes()[:1024])
}
return stdout.Bytes(), nil
}
Expand Down
4 changes: 3 additions & 1 deletion modules/git/commit_info_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"io"
"path"
"sort"

"code.gitea.io/gitea/modules/log"
)

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

Expand Down
3 changes: 2 additions & 1 deletion modules/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu
righHunk, _ = strconv.Atoi(rightRange[1])
}
} else {
log("Parse line number failed: %v", diffhunk)
log.Debug("Parse line number failed: %v", diffhunk)
rightLine = leftLine
righHunk = leftHunk
}
Expand Down
16 changes: 0 additions & 16 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
)

var (
// Debug enables verbose logging on everything.
// This should be false in case Gogs starts in SSH mode.
Debug = false
// Prefix the log prefix
Prefix = "[git-module] "
// GitVersionRequired is the minimum Git version required
Expand All @@ -41,19 +38,6 @@ var (
goVersionLessThan115 = true
)

func log(format string, args ...interface{}) {
if !Debug {
return
}

fmt.Print(Prefix)
if len(args) == 0 {
fmt.Println(format)
} else {
fmt.Printf(format+"\n", args...)
}
}

// LocalVersion returns current Git version from shell.
func LocalVersion() (*version.Version, error) {
if err := LoadGitVersion(); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions modules/git/hook.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand All @@ -12,6 +13,7 @@ import (
"path/filepath"
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

Expand Down Expand Up @@ -126,11 +128,11 @@ const (

// SetUpdateHook writes given content to update hook of the repository.
func SetUpdateHook(repoPath, content string) (err error) {
log("Setting update hook: %s", repoPath)
log.Debug("Setting update hook: %s", repoPath)
hookPath := path.Join(repoPath, HookPathUpdate)
isExist, err := util.IsExist(hookPath)
if err != nil {
log("Unable to check if %s exists. Error: %v", hookPath, err)
log.Debug("Unable to check if %s exists. Error: %v", hookPath, err)
return err
}
if isExist {
Expand Down
4 changes: 3 additions & 1 deletion modules/git/last_commit_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package git
import (
"crypto/sha256"
"fmt"

"code.gitea.io/gitea/modules/log"
)

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

// Put put the last commit id with commit and entry path
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
log("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
}
6 changes: 4 additions & 2 deletions modules/git/last_commit_cache_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"context"
"path"

"code.gitea.io/gitea/modules/log"

"github.com/go-git/go-git/v5/plumbing/object"
cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
)
Expand Down Expand Up @@ -41,9 +43,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
if vs, ok := v.(string); ok {
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
if commit, ok := c.commitCache[vs]; ok {
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
return commit, nil
}
id, err := c.repo.ConvertToSHA1(vs)
Expand Down
6 changes: 4 additions & 2 deletions modules/git/last_commit_cache_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"bufio"
"context"
"path"

"code.gitea.io/gitea/modules/log"
)

// LastCommitCache represents a cache to store last commit
Expand Down Expand Up @@ -39,9 +41,9 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
func (c *LastCommitCache) Get(ref, entryPath string, wr WriteCloserError, rd *bufio.Reader) (interface{}, error) {
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
if vs, ok := v.(string); ok {
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
if commit, ok := c.commitCache[vs]; ok {
log("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs)
return commit, nil
}
id, err := c.repo.ConvertToSHA1(vs)
Expand Down
4 changes: 3 additions & 1 deletion modules/git/parse_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"io"
"strconv"
"strings"

"code.gitea.io/gitea/modules/log"
)

// ParseTreeEntries parses the output of a `git ls-tree -l` command.
Expand Down Expand Up @@ -120,7 +122,7 @@ loop:
case "40000":
entry.entryMode = EntryModeTree
default:
log("Unknown mode: %v", string(mode))
log.Debug("Unknown mode: %v", string(mode))
return nil, fmt.Errorf("unknown mode: %v", string(mode))
}

Expand Down
6 changes: 4 additions & 2 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"context"
"errors"
"path/filepath"

"code.gitea.io/gitea/modules/log"
)

// Repository represents a Git repository.
Expand Down Expand Up @@ -54,7 +56,7 @@ func OpenRepository(repoPath string) (*Repository, error) {
// CatFileBatch obtains a CatFileBatch for this repository
func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) {
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
log("Opening temporary cat file batch for: %s", repo.Path)
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
return CatFileBatch(repo.Path)
}
return repo.batchWriter, repo.batchReader, func() {}
Expand All @@ -63,7 +65,7 @@ func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func())
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) {
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
log("Opening temporary cat file batch-check: %s", repo.Path)
log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
return CatFileBatchCheck(repo.Path)
}
return repo.checkWriter, repo.checkReader, func() {}
Expand Down
6 changes: 4 additions & 2 deletions modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"bytes"
"io"
"strings"

"code.gitea.io/gitea/modules/log"
)

// IsObjectExist returns true if given reference exists in the repository.
Expand All @@ -24,7 +26,7 @@ func (repo *Repository) IsObjectExist(name string) bool {
defer cancel()
_, err := wr.Write([]byte(name + "\n"))
if err != nil {
log("Error writing to CatFileBatchCheck %v", err)
log.Debug("Error writing to CatFileBatchCheck %v", err)
return false
}
sha, _, _, err := ReadBatchLine(rd)
Expand All @@ -41,7 +43,7 @@ func (repo *Repository) IsReferenceExist(name string) bool {
defer cancel()
_, err := wr.Write([]byte(name + "\n"))
if err != nil {
log("Error writing to CatFileBatchCheck %v", err)
log.Debug("Error writing to CatFileBatchCheck %v", err)
return false
}
_, _, _, err = ReadBatchLine(rd)
Expand Down
4 changes: 3 additions & 1 deletion modules/git/repo_commit_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"io"
"io/ioutil"
"strings"

"code.gitea.io/gitea/modules/log"
)

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

return commit, nil
default:
log("Unknown typ: %s", typ)
log.Debug("Unknown typ: %s", typ)
_, err = rd.Discard(int(size) + 1)
if err != nil {
return nil, err
Expand Down
9 changes: 5 additions & 4 deletions modules/git/repo_language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"math"

"code.gitea.io/gitea/modules/analyze"
"code.gitea.io/gitea/modules/log"

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

sha, err := NewIDFromString(string(shaBytes))
if err != nil {
log("Unable to get commit for: %s. Err: %v", commitID, err)
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, ErrNotExist{commitID, ""}
}

commit, err := CommitFromReader(repo, sha, io.LimitReader(batchReader, size))
if err != nil {
log("Unable to get commit for: %s. Err: %v", commitID, err)
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)
return nil, err
}
if _, err = batchReader.Discard(1); err != nil {
Expand Down Expand Up @@ -79,7 +80,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
}
_, _, size, err := ReadBatchLine(batchReader)
if err != nil {
log("Error reading blob: %s Err: %v", f.ID.String(), err)
log.Debug("Error reading blob: %s Err: %v", f.ID.String(), err)
return nil, err
}

Expand Down
4 changes: 3 additions & 1 deletion modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package git
import (
"fmt"
"strings"

"code.gitea.io/gitea/modules/log"
)

// TagPrefix tags prefix path on the repository
Expand All @@ -33,7 +35,7 @@ func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error
func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
t, ok := repo.tagCache.Get(tagID.String())
if ok {
log("Hit cache: %s", tagID)
log.Debug("Hit cache: %s", tagID)
tagClone := *t.(*Tag)
tagClone.Name = name // This is necessary because lightweight tags may have same id
return &tagClone, nil
Expand Down
Loading