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

submodule.go « git2go « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c4e8fe07826f66aa89a0dcb24e95ad2bcc296169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package git2go

import (
	"context"
	"fmt"
	"io"
	"time"

	"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
)

// Error strings present in the legacy Ruby implementation
const (
	LegacyErrPrefixInvalidBranch        = "Invalid branch"
	LegacyErrPrefixInvalidSubmodulePath = "Invalid submodule path"
	LegacyErrPrefixFailedCommit         = "Failed to create commit"
)

// SubmoduleCommand instructs how to commit a submodule update to a repo
type SubmoduleCommand struct {
	// Repository is the path to commit the submodule change
	Repository string `json:"repository"`

	// AuthorName is the author name of submodule commit.
	AuthorName string `json:"author_name"`
	// AuthorMail is the author mail of submodule commit.
	AuthorMail string `json:"author_mail"`
	// AuthorDate is the auithor date of submodule commit.
	AuthorDate time.Time `json:"author_date"`
	// Message is the message to be used for the submodule commit.
	Message string `json:"message"`

	// CommitSHA is where the submodule should point
	CommitSHA string `json:"commit_sha"`
	// Submodule is the actual submodule string to commit to the tree
	Submodule string `json:"submodule"`
	// Branch where to commit submodule update
	Branch string `json:"branch"`
}

// SubmoduleResult contains results from a committing a submodule update
type SubmoduleResult struct {
	// CommitID is the object ID of the generated submodule commit.
	CommitID string `json:"commit_id"`
}

// SubmoduleCommandFromSerialized deserializes the submodule request from its JSON representation encoded with base64.
func SubmoduleCommandFromSerialized(serialized string) (SubmoduleCommand, error) {
	var request SubmoduleCommand
	if err := deserialize(serialized, &request); err != nil {
		return SubmoduleCommand{}, err
	}

	if err := request.verify(); err != nil {
		return SubmoduleCommand{}, fmt.Errorf("submodule: %w", err)
	}

	return request, nil
}

// SerializeTo serializes the submodule result and writes it into the writer.
func (s SubmoduleResult) SerializeTo(w io.Writer) error {
	return serializeTo(w, s)
}

// Run attempts to commit the request submodule change
func (s SubmoduleCommand) Run(ctx context.Context, cfg config.Cfg) (SubmoduleResult, error) {
	if err := s.verify(); err != nil {
		return SubmoduleResult{}, fmt.Errorf("submodule: %w", err)
	}

	serialized, err := serialize(s)
	if err != nil {
		return SubmoduleResult{}, err
	}

	stdout, err := run(ctx, binaryPathFromCfg(cfg), nil, "submodule", "-request", serialized)
	if err != nil {
		return SubmoduleResult{}, err
	}

	var response SubmoduleResult
	if err := deserialize(stdout.String(), &response); err != nil {
		return SubmoduleResult{}, err
	}

	return response, nil
}

func (s SubmoduleCommand) verify() (err error) {
	if s.Repository == "" {
		return InvalidArgumentError("missing repository")
	}
	if s.AuthorName == "" {
		return InvalidArgumentError("missing author name")
	}
	if s.AuthorMail == "" {
		return InvalidArgumentError("missing author mail")
	}
	if s.CommitSHA == "" {
		return InvalidArgumentError("missing commit SHA")
	}
	if s.Branch == "" {
		return InvalidArgumentError("missing branch name")
	}
	if s.Submodule == "" {
		return InvalidArgumentError("missing submodule")
	}
	return nil
}