Skip to content

Commit

Permalink
Provide arguments to NewGitServer and NewGitProtocol as Opts structs
Browse files Browse the repository at this point in the history
There are now way too many arguments for these functions, so let's do a
small API-breaking change to make it easier to extend. #major
  • Loading branch information
lhchavez committed Dec 1, 2021
1 parent 261b874 commit ae2b260
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 289 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ the `git_repositories/` directory:
package main

import (
"github.com/inconshreveable/log15"
"github.com/omegaup/githttp"
       "net/http"
"net/http"

"github.com/omegaup/githttp"
)

func main() {
panic(http.Server{
Addr: ":80",
               Handler: githttp.GitServer("git_repositories", true, nil, nil, nil, log15.New()),
}.ListenAndServe())
panic(http.Server{
Addr: ":80",
Handler: githttp.NewGitServer(githttp.GitServerOpts{
RootPath: "git_repositories",
EnableBrowse: true,
}),
}.ListenAndServe())
}
```
112 changes: 32 additions & 80 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ import (

func TestHandleRefs(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -60,20 +55,16 @@ func TestHandleRefs(t *testing.T) {

func TestHandleRefsWithReferenceDiscoveryCallback(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
func(
protocol := NewGitProtocol(GitProtocolOpts{
ReferenceDiscoveryCallback: func(
ctx context.Context,
repository *git.Repository,
referenceName string,
) bool {
return referenceName == "refs/heads/public"
},
nil,
nil,
false,
log,
)
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand All @@ -100,14 +91,9 @@ func TestHandleRefsWithReferenceDiscoveryCallback(t *testing.T) {

func TestHandleRestrictedRefs(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -142,14 +128,9 @@ func TestHandleRestrictedRefs(t *testing.T) {

func TestHandleArchiveCommit(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -184,14 +165,9 @@ func TestHandleArchiveCommit(t *testing.T) {

func TestHandleLog(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -254,14 +230,9 @@ func TestHandleLog(t *testing.T) {

func TestHandleLogCommit(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -308,14 +279,9 @@ func TestHandleLogCommit(t *testing.T) {

func TestHandleShowCommit(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -359,14 +325,9 @@ func TestHandleShowCommit(t *testing.T) {

func TestHandleShowTree(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -414,14 +375,9 @@ func TestHandleShowTree(t *testing.T) {

func TestHandleShowBlob(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
nil,
nil,
nil,
false,
log,
)
protocol := NewGitProtocol(GitProtocolOpts{
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down Expand Up @@ -461,20 +417,16 @@ func TestHandleShowBlob(t *testing.T) {

func TestHandleNotFound(t *testing.T) {
log := base.StderrLog(false)
protocol := NewGitProtocol(
nil,
func(
protocol := NewGitProtocol(GitProtocolOpts{
ReferenceDiscoveryCallback: func(
ctx context.Context,
repository *git.Repository,
referenceName string,
) bool {
return referenceName == "refs/heads/public"
},
nil,
nil,
false,
log,
)
Log: log,
})

repository, err := git.OpenRepository("testdata/repo.git")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/omegaup/githttp
module github.com/omegaup/githttp/v2

go 1.17

Expand Down
55 changes: 30 additions & 25 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,38 +125,43 @@ type GitProtocol struct {
log log15.Logger
}

// GitProtocolOpts contains all the possible options to initialize the git Server.
type GitProtocolOpts struct {
doNotCompare

AuthCallback AuthorizationCallback
ReferenceDiscoveryCallback ReferenceDiscoveryCallback
UpdateCallback UpdateCallback
PreprocessCallback PreprocessCallback
AllowNonFastForward bool
Log log15.Logger
}

// NewGitProtocol returns a new instance of GitProtocol.
func NewGitProtocol(
authCallback AuthorizationCallback,
referenceDiscoveryCallback ReferenceDiscoveryCallback,
updateCallback UpdateCallback,
preprocessCallback PreprocessCallback,
allowNonFastForward bool,
log log15.Logger,
) *GitProtocol {
if authCallback == nil {
authCallback = noopAuthorizationCallback
func NewGitProtocol(opts GitProtocolOpts) *GitProtocol {
if opts.AuthCallback == nil {
opts.AuthCallback = noopAuthorizationCallback
}

if referenceDiscoveryCallback == nil {
referenceDiscoveryCallback = noopReferenceDiscoveryCallback
if opts.ReferenceDiscoveryCallback == nil {
opts.ReferenceDiscoveryCallback = noopReferenceDiscoveryCallback
}

if updateCallback == nil {
updateCallback = noopUpdateCallback
if opts.UpdateCallback == nil {
opts.UpdateCallback = noopUpdateCallback
}

if preprocessCallback == nil {
preprocessCallback = noopPreprocessCallback
if opts.PreprocessCallback == nil {
opts.PreprocessCallback = noopPreprocessCallback
}
if opts.Log == nil {
opts.Log = log15.New()
}

return &GitProtocol{
AuthCallback: authCallback,
ReferenceDiscoveryCallback: referenceDiscoveryCallback,
UpdateCallback: updateCallback,
PreprocessCallback: preprocessCallback,
AllowNonFastForward: allowNonFastForward,
log: log,
AuthCallback: opts.AuthCallback,
ReferenceDiscoveryCallback: opts.ReferenceDiscoveryCallback,
UpdateCallback: opts.UpdateCallback,
PreprocessCallback: opts.PreprocessCallback,
AllowNonFastForward: opts.AllowNonFastForward,
log: opts.Log,
}
}

Expand Down
Loading

0 comments on commit ae2b260

Please sign in to comment.