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
|
package git2go
import (
"bytes"
"context"
"encoding/gob"
"fmt"
"time"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/repository"
)
// 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
// AuthorName is the author name of submodule commit.
AuthorName string
// AuthorMail is the author mail of submodule commit.
AuthorMail string
// AuthorDate is the auithor date of submodule commit.
AuthorDate time.Time
// Message is the message to be used for the submodule commit.
Message string
// CommitSHA is where the submodule should point
CommitSHA string
// Submodule is the actual submodule string to commit to the tree
Submodule string
// Branch where to commit submodule update
Branch string
}
// SubmoduleResult contains results from a committing a submodule update
type SubmoduleResult struct {
// CommitID is the object ID of the generated submodule commit.
CommitID string
}
// Submodule attempts to commit the request submodule change
func (b *Executor) Submodule(ctx context.Context, repo repository.GitRepo, s SubmoduleCommand) (SubmoduleResult, error) {
if err := s.verify(); err != nil {
return SubmoduleResult{}, fmt.Errorf("submodule: %w", err)
}
input := &bytes.Buffer{}
const cmd = "submodule"
if err := gob.NewEncoder(input).Encode(s); err != nil {
return SubmoduleResult{}, fmt.Errorf("%s: %w", cmd, err)
}
// Ideally we would use `b.runWithGob` here to avoid the gob encoding
// boilerplate but it is not possible here because `runWithGob` adds error
// prefixes and the `LegacyErrPrefix*` errors must match exactly.
stdout, err := b.run(ctx, repo, input, cmd)
if err != nil {
return SubmoduleResult{}, err
}
var result Result
if err := gob.NewDecoder(stdout).Decode(&result); err != nil {
return SubmoduleResult{}, fmt.Errorf("%s: %w", cmd, err)
}
if result.Err != nil {
return SubmoduleResult{}, result.Err
}
return SubmoduleResult{
CommitID: result.CommitID,
}, 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
}
|