From 97f1c689ed7a7b9e915ae0afb06e797831f68ca2 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 24 Sep 2020 08:30:07 +0200 Subject: git2go: Move generic functions into its own file --- internal/git2go/command.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ internal/git2go/merge.go | 40 -------------------------------------- 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 internal/git2go/command.go (limited to 'internal/git2go') diff --git a/internal/git2go/command.go b/internal/git2go/command.go new file mode 100644 index 000000000..df94a9c0a --- /dev/null +++ b/internal/git2go/command.go @@ -0,0 +1,48 @@ +package git2go + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "fmt" + "os/exec" + "path" + "strings" + + "gitlab.com/gitlab-org/gitaly/internal/command" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" +) + +func run(ctx context.Context, cfg config.Cfg, subcommand string, arg string) (string, error) { + binary := path.Join(cfg.BinDir, "gitaly-git2go") + + var stderr, stdout bytes.Buffer + cmd, err := command.New(ctx, exec.Command(binary, subcommand, "-request", arg), nil, &stdout, &stderr) + if err != nil { + return "", err + } + + if err := cmd.Wait(); err != nil { + if _, ok := err.(*exec.ExitError); ok { + return "", fmt.Errorf("%s", stderr.String()) + } + return "", err + } + + return stdout.String(), nil +} + +func serialize(v interface{}) (string, error) { + marshalled, err := json.Marshal(v) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(marshalled), nil +} + +func deserialize(serialized string, v interface{}) error { + base64Decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(serialized)) + jsonDecoder := json.NewDecoder(base64Decoder) + return jsonDecoder.Decode(v) +} diff --git a/internal/git2go/merge.go b/internal/git2go/merge.go index b901353af..39cf88b4a 100644 --- a/internal/git2go/merge.go +++ b/internal/git2go/merge.go @@ -1,18 +1,11 @@ package git2go import ( - "bytes" "context" - "encoding/base64" - "encoding/json" "errors" "fmt" - "os/exec" - "path" - "strings" "time" - "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" ) @@ -45,20 +38,6 @@ type MergeResult struct { CommitID string `json:"commit_id"` } -func serialize(v interface{}) (string, error) { - marshalled, err := json.Marshal(v) - if err != nil { - return "", err - } - return base64.StdEncoding.EncodeToString(marshalled), nil -} - -func deserialize(serialized string, v interface{}) error { - base64Decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(serialized)) - jsonDecoder := json.NewDecoder(base64Decoder) - return jsonDecoder.Decode(v) -} - // MergeCommandFromSerialized deserializes the merge request from its JSON representation encoded with base64. func MergeCommandFromSerialized(serialized string) (MergeCommand, error) { var request MergeCommand @@ -123,22 +102,3 @@ func (m MergeCommand) verify() error { } return nil } - -func run(ctx context.Context, cfg config.Cfg, subcommand string, arg string) (string, error) { - binary := path.Join(cfg.BinDir, "gitaly-git2go") - - var stderr, stdout bytes.Buffer - cmd, err := command.New(ctx, exec.Command(binary, subcommand, "-request", arg), nil, &stdout, &stderr) - if err != nil { - return "", err - } - - if err := cmd.Wait(); err != nil { - if _, ok := err.(*exec.ExitError); ok { - return "", fmt.Errorf("%s", stderr.String()) - } - return "", err - } - - return stdout.String(), nil -} -- cgit v1.2.3