fix for branch create in empty repo, minor config changes for err shadow

This commit is contained in:
Enver Bisevac 2023-03-21 11:53:23 +01:00
parent 75ec201c2b
commit e38b0ebc65
5 changed files with 14 additions and 6 deletions

View File

@ -297,6 +297,8 @@ issues:
max-same-issues: 50 max-same-issues: 50
exclude-rules: exclude-rules:
- text: 'shadow: declaration of "err" shadows declaration at'
linters: [ govet ]
- source: "^//\\s*go:generate\\s" - source: "^//\\s*go:generate\\s"
linters: [ lll ] linters: [ lll ]
- source: "(noinspection|TODO)" - source: "(noinspection|TODO)"

2
.vscode/launch.json vendored
View File

@ -18,7 +18,7 @@
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}", "program": "cmd/gitness",
"args": ["server", "../../.local.env"] "args": ["server", "../../.local.env"]
} }
] ]

View File

@ -7,7 +7,6 @@ package gitrpc
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"github.com/harness/gitness/gitrpc/rpc" "github.com/harness/gitness/gitrpc/rpc"
@ -31,19 +30,19 @@ func (params *BlameParams) Validate() error {
} }
if params.GitRef == "" { if params.GitRef == "" {
return fmt.Errorf("git ref needs to be provided: %w", ErrInvalidArgument) return Errorf(StatusInvalidArgument, "git ref needs to be provided")
} }
if params.Path == "" { if params.Path == "" {
return fmt.Errorf("file path needs to be provided: %w", ErrInvalidArgument) return Errorf(StatusInvalidArgument, "file path needs to be provided")
} }
if params.LineFrom < 0 || params.LineTo < 0 { if params.LineFrom < 0 || params.LineTo < 0 {
return fmt.Errorf("line from and line to can't be negative: %w", ErrInvalidArgument) return Errorf(StatusInvalidArgument, "line from and line to can't be negative")
} }
if params.LineTo > 0 && params.LineFrom > params.LineTo { if params.LineTo > 0 && params.LineFrom > params.LineTo {
return fmt.Errorf("line from can't be after line after: %w", ErrInvalidArgument) return Errorf(StatusInvalidArgument, "line from can't be after line after")
} }
return nil return nil

View File

@ -36,6 +36,10 @@ func (s ReferenceService) CreateBranch(ctx context.Context,
return nil, processGitErrorf(err, "failed to open repo") return nil, processGitErrorf(err, "failed to open repo")
} }
if ok, err := repo.IsEmpty(); ok {
return nil, ErrInvalidArgumentf("branch cannot be created on empty repository", err)
}
sharedRepo, err := NewSharedRepo(s.tmpDir, base.GetRepoUid(), repo) sharedRepo, err := NewSharedRepo(s.tmpDir, base.GetRepoUid(), repo)
if err != nil { if err != nil {
return nil, processGitErrorf(err, "failed to create new shared repo") return nil, processGitErrorf(err, "failed to create new shared repo")

View File

@ -64,6 +64,9 @@ func Errorf(code codes.Code, format string, args ...any) (err error) {
newargs := make([]any, 0, len(args)) newargs := make([]any, 0, len(args))
for _, arg := range args { for _, arg := range args {
if arg == nil {
continue
}
switch t := arg.(type) { switch t := arg.(type) {
case error: case error:
err = t err = t