Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeger-Jan van de Weg <git@zjvandeweg.nl>2019-11-06 23:02:09 +0300
committerZeger-Jan van de Weg <git@zjvandeweg.nl>2019-11-06 23:02:09 +0300
commitc339f6274584361df5cdb572cfcc8de1f06fd80e (patch)
tree370376d6a570ce6ba4ef9b31eafbe6da909a149d /internal/git
parent37f627ce9f45f1bdf60c6022d5e00eeaaaff6719 (diff)
parente54e89905113598be3e255abcaf474b0f7021122 (diff)
Merge branch 'jc-git-dsl-packrefs-refname' into 'master'
Use DSL on PackRefs, FindRefNames, FindAllBranchnames Closes #1974, #1973, and #1971 See merge request gitlab-org/gitaly!1564
Diffstat (limited to 'internal/git')
-rw-r--r--internal/git/safecmd.go23
1 files changed, 16 insertions, 7 deletions
diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go
index 43d7ed037..da1fa6197 100644
--- a/internal/git/safecmd.go
+++ b/internal/git/safecmd.go
@@ -28,6 +28,12 @@ func incrInvalidArg(subcmdName string) {
invalidationTotal.WithLabelValues(subcmdName).Inc()
}
+// Cmd is an interface for safe git commands
+type Cmd interface {
+ ValidateArgs() ([]string, error)
+ IsCmd()
+}
+
// SubCmd represents a specific git command
type SubCmd struct {
Name string // e.g. "log", or "cat-file", or "worktree"
@@ -38,6 +44,9 @@ type SubCmd struct {
var subCmdNameRegex = regexp.MustCompile(`^[[:alnum:]]+(-[[:alnum:]]+)*$`)
+// IsCmd allows SubCmd to satisfy the Cmd interface
+func (sc SubCmd) IsCmd() {}
+
// ValidateArgs checks all arguments in the sub command and validates them
func (sc SubCmd) ValidateArgs() ([]string, error) {
var safeArgs []string
@@ -185,7 +194,7 @@ func validatePositionalArg(arg string) error {
// SafeCmd creates a git.Command with the given args and Repository. It
// validates the arguments in the command before executing.
-func SafeCmd(ctx context.Context, repo repository.GitRepo, globals []Option, sc SubCmd) (*command.Command, error) {
+func SafeCmd(ctx context.Context, repo repository.GitRepo, globals []Option, sc Cmd) (*command.Command, error) {
args, err := combineArgs(globals, sc)
if err != nil {
return nil, err
@@ -196,7 +205,7 @@ func SafeCmd(ctx context.Context, repo repository.GitRepo, globals []Option, sc
// SafeBareCmd creates a git.Command with the given args, stdin/stdout/stderr,
// and env. It validates the arguments in the command before executing.
-func SafeBareCmd(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, env []string, globals []Option, sc SubCmd) (*command.Command, error) {
+func SafeBareCmd(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, env []string, globals []Option, sc Cmd) (*command.Command, error) {
args, err := combineArgs(globals, sc)
if err != nil {
return nil, err
@@ -228,15 +237,15 @@ func SafeCmdWithoutRepo(ctx context.Context, globals []Option, sc SubCmd) (*comm
return CommandWithoutRepo(ctx, args...)
}
-func combineArgs(globals []Option, sc SubCmd) (_ []string, err error) {
+func combineArgs(globals []Option, sc Cmd) (_ []string, err error) {
+ var args []string
+
defer func() {
- if err != nil && IsInvalidArgErr(err) {
- incrInvalidArg(sc.Name)
+ if err != nil && IsInvalidArgErr(err) && len(args) > 0 {
+ incrInvalidArg(args[0])
}
}()
- var args []string
-
for _, g := range globals {
gargs, err := g.ValidateArgs()
if err != nil {