diff --git a/browser.go b/browser.go index 653d3d3..95a858b 100644 --- a/browser.go +++ b/browser.go @@ -13,8 +13,8 @@ import ( "strings" "time" - base "github.com/omegaup/go-base/v2" - tracing "github.com/omegaup/go-base/v2/tracing" + base "github.com/omegaup/go-base/v3" + tracing "github.com/omegaup/go-base/v3/tracing" git "github.com/libgit2/git2go/v33" "github.com/pkg/errors" @@ -751,9 +751,19 @@ func handleBrowse( lockfile := NewLockfile(repository.Path()) if ok, err := lockfile.TryRLock(); !ok { - protocol.log.Info("Waiting for the lockfile", "err", err) + protocol.log.Info( + "Waiting for the lockfile", + map[string]interface{}{ + "err": err, + }, + ) if err := lockfile.RLock(); err != nil { - protocol.log.Crit("Failed to acquire the lockfile", "err", err) + protocol.log.Error( + "Failed to acquire the lockfile", + map[string]interface{}{ + "err": err, + }, + ) return err } } diff --git a/browser_test.go b/browser_test.go index 5ade5db..c121251 100644 --- a/browser_test.go +++ b/browser_test.go @@ -8,13 +8,14 @@ import ( "reflect" "testing" - base "github.com/omegaup/go-base/v2" + log15 "github.com/omegaup/go-base/logging/log15" + base "github.com/omegaup/go-base/v3" git "github.com/libgit2/git2go/v33" ) func TestHandleRefs(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -54,7 +55,7 @@ func TestHandleRefs(t *testing.T) { } func TestHandleRefsWithReferenceDiscoveryCallback(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ ReferenceDiscoveryCallback: func( ctx context.Context, @@ -90,7 +91,7 @@ func TestHandleRefsWithReferenceDiscoveryCallback(t *testing.T) { } func TestHandleRestrictedRefs(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -127,7 +128,7 @@ func TestHandleRestrictedRefs(t *testing.T) { } func TestHandleArchiveCommit(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -164,7 +165,7 @@ func TestHandleArchiveCommit(t *testing.T) { } func TestHandleLog(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -229,7 +230,7 @@ func TestHandleLog(t *testing.T) { } func TestHandleLogCommit(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -278,7 +279,7 @@ func TestHandleLogCommit(t *testing.T) { } func TestHandleShowCommit(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -324,7 +325,7 @@ func TestHandleShowCommit(t *testing.T) { } func TestHandleShowTree(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -374,7 +375,7 @@ func TestHandleShowTree(t *testing.T) { } func TestHandleShowBlob(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ Log: log, }) @@ -416,7 +417,7 @@ func TestHandleShowBlob(t *testing.T) { } func TestHandleNotFound(t *testing.T) { - log := base.StderrLog(false) + log, _ := log15.New("info", false) protocol := NewGitProtocol(GitProtocolOpts{ ReferenceDiscoveryCallback: func( ctx context.Context, diff --git a/commits.go b/commits.go index 5650bb5..91b0a7e 100644 --- a/commits.go +++ b/commits.go @@ -12,9 +12,9 @@ import ( "sort" "strings" - "github.com/omegaup/go-base/v2/tracing" + "github.com/omegaup/go-base/v3/logging" + "github.com/omegaup/go-base/v3/tracing" - "github.com/inconshreveable/log15" git "github.com/libgit2/git2go/v33" "github.com/pkg/errors" ) @@ -198,7 +198,7 @@ func SplitTree( originalRepository *git.Repository, paths []string, repository *git.Repository, - log log15.Logger, + log logging.Logger, ) (*git.Tree, error) { treebuilder, err := repository.TreeBuilder() if err != nil { @@ -262,7 +262,14 @@ func SplitTree( } defer tree.Free() - log.Debug("Creating subtree", "name", name, "contents", subpaths, "id", tree.Id().String()) + log.Debug( + "Creating subtree", + map[string]interface{}{ + "name": name, + "contents": subpaths, + "id": tree.Id().String(), + }, + ) if err = treebuilder.Insert(name, tree.Id(), originalEntry.Filemode); err != nil { return errors.Wrapf(err, "failed to insert %s into treebuilder", name) } @@ -315,7 +322,7 @@ func SplitCommit( repository *git.Repository, author, committer *git.Signature, commitMessageTag string, - log log15.Logger, + log logging.Logger, ) ([]SplitCommitResult, error) { originalTree, err := originalCommit.Tree() if err != nil { @@ -335,10 +342,23 @@ func SplitCommit( return ErrObjectLimitExceeded } path := path.Join(parent, entry.Name) - log.Debug("Considering", "path", path, "entry", *entry) + log.Debug( + "Considering", + map[string]interface{}{ + "path": path, + "entry": *entry, + }, + ) for i, description := range descriptions { if description.ContainsPath(path) { - log.Debug("Found a match for a path", "path", path, "entry", *entry, "description", description) + log.Debug( + "Found a match for a path", + map[string]interface{}{ + "path": path, + "entry": *entry, + "description": description, + }, + ) treePaths[i] = append(treePaths[i], path) break } @@ -444,7 +464,7 @@ func SpliceCommit( reference *git.Reference, commitMessageTag string, newPackPath string, - log log15.Logger, + log logging.Logger, ) ([]*GitCommand, error) { newRepository, err := openRepository(context.TODO(), repository.Path()) if err != nil { @@ -682,7 +702,7 @@ func SpliceCommit( func BuildTree( repository *git.Repository, files map[string]io.Reader, - log log15.Logger, + log logging.Logger, ) (*git.Tree, error) { treebuilder, err := repository.TreeBuilder() if err != nil { @@ -703,7 +723,14 @@ func BuildTree( if err != nil { return nil, errors.Wrapf(err, "failed to create blob for %s", name) } - log.Debug("Creating blob", "path", name, "len", len(contents), "id", oid) + log.Debug( + "Creating blob", + map[string]interface{}{ + "path": name, + "len": len(contents), + "id": oid, + }, + ) if err = treebuilder.Insert(name, oid, 0100644); err != nil { return nil, errors.Wrapf(err, "failed to insert %s into treebuilder", name) } @@ -741,7 +768,12 @@ func BuildTree( if err != nil { return nil, errors.Wrap(err, "failed to create tree") } - log.Debug("Creating tree", "id", mergedTreeID) + log.Debug( + "Creating tree", + map[string]interface{}{ + "id": mergedTreeID, + }, + ) return repository.LookupTree(mergedTreeID) } diff --git a/commits_test.go b/commits_test.go index f87be2b..6356836 100644 --- a/commits_test.go +++ b/commits_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - base "github.com/omegaup/go-base/v2" + "github.com/omegaup/go-base/logging/log15" git "github.com/libgit2/git2go/v33" ) @@ -29,7 +29,7 @@ func TestSplitTrees(t *testing.T) { } defer repository.Free() - log := base.StderrLog(false) + log, _ := log15.New("info", false) originalTree, err := BuildTree( repository, @@ -95,7 +95,13 @@ func TestSplitTrees(t *testing.T) { newPaths := make([]string, 0) err = splitTree.Walk(func(parent string, entry *git.TreeEntry) error { path := path.Join(parent, entry.Name) - log.Debug("Considering", "path", path, "entry", *entry) + log.Debug( + "Considering", + map[string]interface{}{ + "path": path, + "entry": *entry, + }, + ) if entry.Type != git.ObjectBlob { return nil } @@ -127,7 +133,7 @@ func TestMergeTrees(t *testing.T) { } defer repo.Free() - log := base.StderrLog(false) + log, _ := log15.New("info", false) type testEntry struct { trees []map[string]io.Reader @@ -244,7 +250,7 @@ func TestSpliceCommit(t *testing.T) { } defer repository.Free() - log := base.StderrLog(false) + log, _ := log15.New("info", false) originalTree, err := BuildTree( repository, @@ -338,5 +344,10 @@ func TestSpliceCommit(t *testing.T) { t.Fatalf("Error splicing commit: %v", err) } - log.Debug("Commands changed", "newCommands", newCommands) + log.Debug( + "Commands changed", + map[string]interface{}{ + "newCommands": newCommands, + }, + ) } diff --git a/go.mod b/go.mod index c94c8a3..c006d53 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,18 @@ module github.com/omegaup/githttp/v2 go 1.17 require ( - github.com/go-stack/stack v1.8.0 // indirect - github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac + github.com/go-stack/stack v1.8.1 // indirect github.com/libgit2/git2go/v33 v33.0.4 - github.com/mattn/go-colorable v0.1.6 // indirect - github.com/mattn/go-isatty v0.0.12 // indirect - github.com/omegaup/go-base/v2 v2.3.0 + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/omegaup/go-base/logging/log15 v0.0.0-20211204182432-a117421dbd30 + github.com/omegaup/go-base/v3 v3.1.0 github.com/pkg/errors v0.9.1 golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c // indirect - golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect + golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect +) + +require ( + github.com/go-logfmt/logfmt v0.5.1 // indirect + github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac // indirect ) diff --git a/go.sum b/go.sum index 0e4ad41..7115dbb 100644 --- a/go.sum +++ b/go.sum @@ -1,27 +1,22 @@ -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac h1:n1DqxAo4oWPMvH1+v+DLYlMCecgumhhgnxAPdqDIFHI= github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o= github.com/libgit2/git2go/v33 v33.0.4 h1:37xovFBzibhDEdQRLbfWwx3a44JhOIY06UICn2teenc= github.com/libgit2/git2go/v33 v33.0.4/go.mod h1:KdpqkU+6+++4oHna/MIOgx4GCQ92IPCdpVRMRI80J+4= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/omegaup/go-base/v2 v2.0.0 h1:A4TGNKDbbAiF55EfxPh5r8knS/zDpFFJpFCYerqByzE= -github.com/omegaup/go-base/v2 v2.0.0/go.mod h1:k5PYTZcULZt7Dbx67WI0Wk3WHJSMmyadEG1aWx6YbVg= -github.com/omegaup/go-base/v2 v2.1.0 h1:pehTzNMArOQrVBOa2oLhZDIhWSU80phIHrBi+gL46LY= -github.com/omegaup/go-base/v2 v2.1.0/go.mod h1:DotC1e60GE6S+iqe9RyusanekwK/eyU/YL3so86X3SA= -github.com/omegaup/go-base/v2 v2.2.0 h1:bb4PrDOlJ+v17QHblBN3QGNTWoRhtgOW+k7x9q2L9Ik= -github.com/omegaup/go-base/v2 v2.2.0/go.mod h1:DotC1e60GE6S+iqe9RyusanekwK/eyU/YL3so86X3SA= -github.com/omegaup/go-base/v2 v2.3.0 h1:u5juOm0O96ZeuJu3xYSwvyGtstIkCEDT/F016gzSfow= -github.com/omegaup/go-base/v2 v2.3.0/go.mod h1:DotC1e60GE6S+iqe9RyusanekwK/eyU/YL3so86X3SA= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/omegaup/go-base/logging/log15 v0.0.0-20211204182432-a117421dbd30 h1:NzSSVKqF1u8vDe4ooKP/fxWR8rr4QtzYJYADK+X/UUU= +github.com/omegaup/go-base/logging/log15 v0.0.0-20211204182432-a117421dbd30/go.mod h1:bZLSqNMf9PDRAYKax8zOS0a6si1t0BUMHkh3SRZ9dEU= +github.com/omegaup/go-base/v3 v3.0.0/go.mod h1:mJFH+bckd4aAXcpXDBFN6Et5p3sxYsHCxK8pUDp0YlM= +github.com/omegaup/go-base/v3 v3.1.0 h1:8WT7h5gTSF0pJpxuajxtpmU2Cq09VDIfziooiSKn7Go= +github.com/omegaup/go-base/v3 v3.1.0/go.mod h1:mJFH+bckd4aAXcpXDBFN6Et5p3sxYsHCxK8pUDp0YlM= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -30,14 +25,11 @@ golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c h1:9HhBz5L/UjnK9XLtiZhYAd golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191206220618-eeba5f6aabab/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/protocol.go b/protocol.go index 58e03f6..f1e0eed 100644 --- a/protocol.go +++ b/protocol.go @@ -12,10 +12,10 @@ import ( "strconv" "strings" - base "github.com/omegaup/go-base/v2" - tracing "github.com/omegaup/go-base/v2/tracing" + base "github.com/omegaup/go-base/v3" + "github.com/omegaup/go-base/v3/logging" + "github.com/omegaup/go-base/v3/tracing" - "github.com/inconshreveable/log15" git "github.com/libgit2/git2go/v33" "github.com/pkg/errors" ) @@ -123,7 +123,7 @@ type GitProtocol struct { UpdateCallback UpdateCallback PreprocessCallback PreprocessCallback AllowNonFastForward bool - log log15.Logger + log logging.Logger } // GitProtocolOpts contains all the possible options to initialize the git Server. @@ -135,7 +135,7 @@ type GitProtocolOpts struct { UpdateCallback UpdateCallback PreprocessCallback PreprocessCallback AllowNonFastForward bool - Log log15.Logger + Log logging.Logger } // NewGitProtocol returns a new instance of GitProtocol. @@ -152,9 +152,6 @@ func NewGitProtocol(opts GitProtocolOpts) *GitProtocol { if opts.PreprocessCallback == nil { opts.PreprocessCallback = noopPreprocessCallback } - if opts.Log == nil { - opts.Log = log15.New() - } return &GitProtocol{ AuthCallback: opts.AuthCallback, @@ -219,10 +216,20 @@ func (p *GitProtocol) PushPackfile( if !ValidateFastForward(repository, commit, command.Reference) && !p.AllowNonFastForward { command.err = ErrNonFastForward } else if level == AuthorizationAllowedRestricted && isRestrictedRef(command.ReferenceName) { - p.log.Info("restricted ref", "ref", command.ReferenceName) + p.log.Info( + "restricted ref", + map[string]interface{}{ + "ref": command.ReferenceName, + }, + ) command.err = ErrRestrictedRef } else if !p.ReferenceDiscoveryCallback(ctx, repository, command.ReferenceName) { - p.log.Info("user does not have access", "ref", command.ReferenceName) + p.log.Info( + "user does not have access", + map[string]interface{}{ + "ref": command.ReferenceName, + }, + ) command.err = ErrRestrictedRef } else { parentCommit := commit.Parent(0) @@ -262,7 +269,12 @@ func (p *GitProtocol) PushPackfile( acquireLockSegment := txn.StartSegment("acquire lock") if ok, err := lockfile.TryLock(); !ok { - p.log.Info("Waiting for the lockfile", "err", err) + p.log.Info( + "Waiting for the lockfile", + map[string]interface{}{ + "err": err, + }, + ) err = lockfile.Lock() acquireLockSegment.End() if err != nil { @@ -321,7 +333,12 @@ func (p *GitProtocol) PushPackfile( } updatedRefs = append(updatedRefs, updatedRef) ref.Free() - p.log.Info("Ref successfully updated", "command", command) + p.log.Info( + "Ref successfully updated", + map[string]interface{}{ + "command": command, + }, + ) } return updatedRefs, nil, nil @@ -466,7 +483,7 @@ func handleInfoRefs( sendCapabilities bool, level AuthorizationLevel, protocol *GitProtocol, - log log15.Logger, + log logging.Logger, w io.Writer, ) error { repository, err := openRepository(ctx, repositoryPath) @@ -480,7 +497,12 @@ func handleInfoRefs( lockfile := NewLockfile(repository.Path()) if ok, err := lockfile.TryRLock(); !ok { - log.Info("Waiting for the lockfile", "err", err) + log.Info( + "Waiting for the lockfile", + map[string]interface{}{ + "err": err, + }, + ) if err := lockfile.RLock(); err != nil { return errors.Wrap( err, @@ -533,7 +555,12 @@ func handleInfoRefs( ref, err := it.Next() if err != nil { if !git.IsErrorCode(err, git.ErrorCodeIterOver) { - log.Error("Error getting reference", "err", err) + log.Error( + "Error getting reference", + map[string]interface{}{ + "err": err, + }, + ) } break } @@ -578,7 +605,7 @@ func handlePrePull( repositoryPath string, level AuthorizationLevel, protocol *GitProtocol, - log log15.Logger, + log logging.Logger, w io.Writer, ) error { return handleInfoRefs( @@ -603,7 +630,7 @@ func handlePull( ctx context.Context, repositoryPath string, level AuthorizationLevel, - log log15.Logger, + log logging.Logger, r io.Reader, w io.Writer, ) error { @@ -618,7 +645,12 @@ func handlePull( lockfile := NewLockfile(repository.Path()) if ok, err := lockfile.TryRLock(); !ok { - log.Info("Waiting for the lockfile", "err", err) + log.Info( + "Waiting for the lockfile", + map[string]interface{}{ + "err": err, + }, + ) if err := lockfile.RLock(); err != nil { return errors.Wrap( err, @@ -658,7 +690,12 @@ func handlePull( ), ) } - log.Debug("pktline", "data", strings.Trim(string(line), "\n")) + log.Debug( + "pktline", + map[string]interface{}{ + "data": strings.Trim(string(line), "\n"), + }, + ) tokens := strings.FieldsFunc( strings.Trim(string(line), "\n"), func(r rune) bool { @@ -680,7 +717,12 @@ func handlePull( ) } } - log.Debug("client capabilities", "list", tokens[2:]) + log.Debug( + "client capabilities", + map[string]interface{}{ + "list": tokens[2:], + }, + ) } if tokens[0] == "want" { if len(tokens) < 2 { @@ -698,7 +740,12 @@ func handlePull( } commit, err := repository.LookupCommit(oid) if err != nil { - log.Debug("Unknown commit requested", "oid", tokens[1]) + log.Debug( + "Unknown commit requested", + map[string]interface{}{ + "oid": tokens[1], + }, + ) pw := NewPktLineWriter(w) pw.WritePktLine([]byte(fmt.Sprintf("ERR upload-pack: not our ref %s", oid.String()))) return nil @@ -728,7 +775,12 @@ func handlePull( ) } } else { - log.Debug("unknown command", "command", tokens[0]) + log.Debug( + "unknown command", + map[string]interface{}{ + "command": tokens[0], + }, + ) } } @@ -769,7 +821,12 @@ func handlePull( ), ) } - log.Debug("pktline", "data", strings.Trim(string(line), "\n")) + log.Debug( + "pktline", + map[string]interface{}{ + "data": strings.Trim(string(line), "\n"), + }, + ) tokens := strings.FieldsFunc( strings.Trim(string(line), "\n"), func(r rune) bool { @@ -807,10 +864,17 @@ func handlePull( } } - log.Debug("Negotiation", "want", wantMap, "have", haveSet, "common", commonSet) + log.Debug( + "Negotiation", + map[string]interface{}{ + "want": wantMap, + "have": haveSet, + "common": commonSet, + }, + ) if !done { - log.Debug("missing 'done' pkt-line") + log.Debug("missing 'done' pkt-line", nil) return nil } @@ -822,13 +886,23 @@ func handlePull( } depth-- if _, ok := shallowSet[current.Id().String()]; ok { - log.Debug("Skipping commit", "commit", current.Id().String()) + log.Debug( + "Skipping commit", + map[string]interface{}{ + "commit": current.Id().String(), + }, + ) continue } if _, ok := commonSet[current.Id().String()]; ok { break } - log.Debug("Adding commit", "commit", current.Id().String()) + log.Debug( + "Adding commit", + map[string]interface{}{ + "commit": current.Id().String(), + }, + ) if err := pb.InsertCommit(current.Id()); err != nil { return errors.Wrap( err, @@ -842,7 +916,12 @@ func handlePull( pw.WritePktLine([]byte("NAK\n")) } if err := pb.Write(w); err != nil { - log.Error("Error writing pack", "err", err) + log.Error( + "Error writing pack", + map[string]interface{}{ + "err": err, + }, + ) } return nil @@ -857,7 +936,7 @@ func handlePrePush( repositoryPath string, level AuthorizationLevel, protocol *GitProtocol, - log log15.Logger, + log logging.Logger, w io.Writer, ) error { return handleInfoRefs( @@ -882,7 +961,7 @@ func handlePush( repositoryPath string, level AuthorizationLevel, protocol *GitProtocol, - log log15.Logger, + log logging.Logger, r io.Reader, w io.Writer, ) error { @@ -897,7 +976,12 @@ func handlePush( lockfile := NewLockfile(repository.Path()) if ok, err := lockfile.TryRLock(); !ok { - log.Info("Waiting for the lockfile", "err", err) + log.Info( + "Waiting for the lockfile", + map[string]interface{}{ + "err": err, + }, + ) if err := lockfile.RLock(); err != nil { return errors.Wrap( err, @@ -937,7 +1021,12 @@ func handlePush( ) } if len(tokens) > 3 { - log.Debug("client capabilities", "list", tokens[3:]) + log.Debug( + "client capabilities", + map[string]interface{}{ + "list": tokens[3:], + }, + ) for _, token := range tokens[3:] { if token == "report-status" { reportStatus = true @@ -968,7 +1057,12 @@ func handlePush( } } - log.Debug("Commands", "commands", commands) + log.Debug( + "Commands", + map[string]interface{}{ + "commands": commands, + }, + ) _, err, unpackErr := protocol.PushPackfile( ctx, diff --git a/protocol_test.go b/protocol_test.go index a894c4f..6612e04 100644 --- a/protocol_test.go +++ b/protocol_test.go @@ -13,7 +13,7 @@ import ( "testing" "time" - base "github.com/omegaup/go-base/v2" + "github.com/omegaup/go-base/logging/log15" git "github.com/libgit2/git2go/v33" ) @@ -59,7 +59,7 @@ func TestDiscoverReferences(t *testing.T) { func TestHandlePrePullRestricted(t *testing.T) { var buf bytes.Buffer - log := base.StderrLog(false) + log, _ := log15.New("info", false) err := handlePrePull( context.Background(), "testdata/repo.git", @@ -92,7 +92,7 @@ func TestHandlePrePullRestricted(t *testing.T) { func TestHandlePrePull(t *testing.T) { var buf bytes.Buffer - log := base.StderrLog(false) + log, _ := log15.New("info", false) err := handlePrePull( context.Background(), "testdata/repo.git", @@ -126,7 +126,7 @@ func TestHandlePrePull(t *testing.T) { func TestHandlePrePush(t *testing.T) { var buf bytes.Buffer - log := base.StderrLog(false) + log, _ := log15.New("info", false) err := handlePrePush( context.Background(), "testdata/repo.git", @@ -159,7 +159,7 @@ func TestHandlePrePush(t *testing.T) { func TestHandleEmptyPrePull(t *testing.T) { var buf bytes.Buffer - log := base.StderrLog(false) + log, _ := log15.New("info", false) err := handlePrePull( context.Background(), "testdata/empty.git", @@ -189,7 +189,7 @@ func TestHandleEmptyPrePull(t *testing.T) { func TestHandleEmptyPrePush(t *testing.T) { var buf bytes.Buffer - log := base.StderrLog(false) + log, _ := log15.New("info", false) err := handlePrePush( context.Background(), "testdata/empty.git", @@ -236,7 +236,7 @@ func TestHandlePullUnknownRef(t *testing.T) { pw.WritePktLine([]byte("done")) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -277,7 +277,7 @@ func TestHandleClone(t *testing.T) { pw.WritePktLine([]byte("done")) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -353,7 +353,7 @@ func TestHandlePull(t *testing.T) { pw.WritePktLine([]byte("done")) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -424,7 +424,7 @@ func TestHandleCloneShallowNegotiation(t *testing.T) { pw.Flush() } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -466,7 +466,7 @@ func TestHandleCloneShallowClone(t *testing.T) { pw.WritePktLine([]byte("done")) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -543,7 +543,7 @@ func TestHandleCloneShallowUnshallow(t *testing.T) { pw.WritePktLine([]byte("done")) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePull( context.Background(), "testdata/repo.git", @@ -633,7 +633,7 @@ func TestHandlePushUnborn(t *testing.T) { t.Fatalf("Failed to copy the packfile: %v", err) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -726,7 +726,7 @@ func TestHandlePushPreprocess(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -745,7 +745,12 @@ func TestHandlePushPreprocess(t *testing.T) { originalCommit, err := originalRepository.LookupCommit(originalCommands[0].New) if err != nil { - log.Error("Error looking up commit", "err", err) + log.Error( + "Error looking up commit", + map[string]interface{}{ + "err": err, + }, + ) return originalPackPath, originalCommands, err } defer originalCommit.Free() @@ -787,11 +792,22 @@ func TestHandlePushPreprocess(t *testing.T) { log, ) if err != nil { - log.Error("Error splicing commit", "err", err) + log.Error( + "Error splicing commit", + map[string]interface{}{ + "err": err, + }, + ) return originalPackPath, originalCommands, err } - log.Debug("Commands changed", "old commands", originalCommands, "newCommands", newCommands) + log.Debug( + "Commands changed", + map[string]interface{}{ + "old commands": originalCommands, + "newCommands": newCommands, + }, + ) return newPackPath, newCommands, nil }, @@ -882,7 +898,7 @@ func TestHandlePushCallback(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -952,7 +968,7 @@ func TestHandlePushUnknownCommit(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -1013,7 +1029,7 @@ func TestHandlePushRestrictedRef(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -1074,7 +1090,7 @@ func TestHandlePushMerge(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -1135,7 +1151,7 @@ func TestHandlePushMultipleCommits(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, @@ -1196,7 +1212,7 @@ func TestHandleNonFastForward(t *testing.T) { } } - log := base.StderrLog(false) + log, _ := log15.New("info", false) err = handlePush( context.Background(), dir, diff --git a/server.go b/server.go index ac26428..f9367c1 100644 --- a/server.go +++ b/server.go @@ -10,10 +10,10 @@ import ( "path" "strings" - base "github.com/omegaup/go-base/v2" - tracing "github.com/omegaup/go-base/v2/tracing" + base "github.com/omegaup/go-base/v3" + "github.com/omegaup/go-base/v3/logging" + "github.com/omegaup/go-base/v3/tracing" - "github.com/inconshreveable/log15" git "github.com/libgit2/git2go/v33" ) @@ -244,7 +244,7 @@ type gitHTTPHandler struct { contextCallback ContextCallback protocol *GitProtocol tracing tracing.Provider - log log15.Logger + log logging.Logger } func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -254,9 +254,11 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if len(splitPath) < 2 { h.log.Error( "Request", - "Method", r.Method, - "path", r.URL.Path[1:], - "error", "not found", + map[string]interface{}{ + "Method": r.Method, + "path": r.URL.Path[1:], + "error": "not found", + }, ) w.WriteHeader(http.StatusNotFound) return @@ -266,9 +268,11 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(repositoryName, ".") { h.log.Error( "Request", - "Method", r.Method, - "path", r.URL.Path[1:], - "error", "repository path starts with .", + map[string]interface{}{ + "Method": r.Method, + "path": r.URL.Path[1:], + "error": "repository path starts with .", + }, ) w.WriteHeader(http.StatusNotFound) return @@ -285,10 +289,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if _, err := os.Stat(repositoryPath); os.IsNotExist(err) { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) w.WriteHeader(http.StatusNotFound) return @@ -302,10 +308,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if level == AuthorizationDenied { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "authorization denied", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "authorization denied", + }, ) return } @@ -315,10 +323,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := handlePrePull(ctx, repositoryPath, level, h.protocol, h.log, w); err != nil { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) WriteHeader(w, err, true) return @@ -329,10 +339,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if level == AuthorizationDenied { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "authorization denied", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "authorization denied", + }, ) return } @@ -342,10 +354,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := handlePull(ctx, repositoryPath, level, h.log, r.Body, w); err != nil { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) WriteHeader(w, err, true) return @@ -357,20 +371,24 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if level == AuthorizationDenied { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "authorization denied", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "authorization denied", + }, ) return } if level == AuthorizationAllowedReadOnly { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "insufficient permissions to modify repository", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "insufficient permissions to modify repository", + }, ) WriteHeader(w, ErrForbidden, true) return @@ -381,10 +399,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := handlePrePush(ctx, repositoryPath, level, h.protocol, h.log, w); err != nil { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) WriteHeader(w, err, true) return @@ -395,20 +415,24 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if level == AuthorizationDenied { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "authorization denied", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "authorization denied", + }, ) return } if level == AuthorizationAllowedReadOnly { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "insufficient permissions to modify repository", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "insufficient permissions to modify repository", + }, ) WriteHeader(w, ErrForbidden, true) return @@ -427,10 +451,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ); err != nil { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) WriteHeader(w, err, true) return @@ -440,10 +466,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if level == AuthorizationDenied { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "authorization denied", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "authorization denied", + }, ) return } @@ -452,10 +480,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(cleanedPath, ".") { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "path starts with .", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "path starts with .", + }, ) w.WriteHeader(http.StatusNotFound) return @@ -476,10 +506,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ); err != nil { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", err, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": err, + }, ) WriteHeader(w, err, true) return @@ -487,10 +519,12 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } else { h.log.Error( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, - "error", "not found", + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + "error": "not found", + }, ) w.WriteHeader(http.StatusNotFound) return @@ -498,9 +532,11 @@ func (h *gitHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.log.Info( "Request", - "Method", r.Method, - "URL", relativeURL, - "path", repositoryPath, + map[string]interface{}{ + "Method": r.Method, + "URL": relativeURL, + "path": repositoryPath, + }, ) } @@ -513,7 +549,7 @@ type GitServerOpts struct { EnableBrowse bool Protocol *GitProtocol ContextCallback ContextCallback - Log log15.Logger + Log logging.Logger Tracing tracing.Provider } @@ -529,9 +565,6 @@ func NewGitServer(opts GitServerOpts) http.Handler { if opts.ContextCallback == nil { opts.ContextCallback = noopContextCallback } - if opts.Log == nil { - opts.Log = log15.New() - } return &gitHTTPHandler{ rootPath: opts.RootPath, diff --git a/server_test.go b/server_test.go index 788ac21..dca908d 100644 --- a/server_test.go +++ b/server_test.go @@ -12,7 +12,7 @@ import ( "strings" "testing" - base "github.com/omegaup/go-base/v2" + "github.com/omegaup/go-base/logging/log15" git "github.com/libgit2/git2go/v33" ) @@ -48,7 +48,7 @@ func TestServerClone(t *testing.T) { } defer os.RemoveAll(dir) - log := base.StderrLog(false) + log, _ := log15.New("info", false) handler := NewGitServer(GitServerOpts{ RootPath: "testdata", RepositorySuffix: ".git", @@ -92,7 +92,7 @@ func TestServerCloneShallow(t *testing.T) { } defer os.RemoveAll(dir) - log := base.StderrLog(false) + log, _ := log15.New("info", false) handler := NewGitServer(GitServerOpts{ RootPath: "testdata", RepositorySuffix: ".git", @@ -147,9 +147,14 @@ func TestServerPush(t *testing.T) { if err != nil { t.Fatalf("Failed to create directory: %v", err) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) if os.Getenv("PRESERVE") != "" { - log.Info("Preserving test directory", "path", dir) + log.Info( + "Preserving test directory", + map[string]interface{}{ + "path": dir, + }, + ) } else { defer os.RemoveAll(dir) } @@ -242,9 +247,14 @@ func TestGitbomb(t *testing.T) { if err != nil { t.Fatalf("Failed to create directory: %v", err) } - log := base.StderrLog(false) + log, _ := log15.New("info", false) if os.Getenv("PRESERVE") != "" { - log.Info("Preserving test directory", "path", dir) + log.Info( + "Preserving test directory", + map[string]interface{}{ + "path": dir, + }, + ) } else { defer os.RemoveAll(dir) }