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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// +build static,system_libgit2
package main
import (
"context"
"flag"
"fmt"
"io"
"time"
git "github.com/libgit2/git2go/v30"
"gitlab.com/gitlab-org/gitaly/internal/git2go"
)
type submoduleSubcommand struct {
request string
}
func (cmd *submoduleSubcommand) Flags() *flag.FlagSet {
flags := flag.NewFlagSet("submodule", flag.ExitOnError)
flags.StringVar(&cmd.request, "request", "", "git2go.SubmoduleCommand")
return flags
}
func (cmd *submoduleSubcommand) Run(_ context.Context, _ io.Reader, w io.Writer) error {
request, err := git2go.SubmoduleCommandFromSerialized(cmd.request)
if err != nil {
return fmt.Errorf("deserializing submodule command request: %w", err)
}
if request.AuthorDate.IsZero() {
request.AuthorDate = time.Now()
}
smCommitOID, err := git.NewOid(request.CommitSHA)
if err != nil {
return fmt.Errorf("converting %s to OID: %w", request.CommitSHA, err)
}
repo, err := git.OpenRepository(request.Repository)
if err != nil {
return fmt.Errorf("open repository: %w", err)
}
fullBranchRefName := "refs/heads/" + request.Branch
o, err := repo.RevparseSingle(fullBranchRefName)
if err != nil {
return fmt.Errorf("%s: %w", git2go.LegacyErrPrefixInvalidBranch, err) //nolint
}
startCommit, err := o.AsCommit()
if err != nil {
return fmt.Errorf("peeling %s as a commit: %w", o.Id(), err)
}
rootTree, err := startCommit.Tree()
if err != nil {
return fmt.Errorf("root tree from starting commit: %w", err)
}
index, err := git.NewIndex()
if err != nil {
return fmt.Errorf("creating new index: %w", err)
}
if err := index.ReadTree(rootTree); err != nil {
return fmt.Errorf("reading root tree into index: %w", err)
}
smEntry, err := index.EntryByPath(request.Submodule, 0)
if err != nil {
return fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixInvalidSubmodulePath, err,
) //nolint
}
if smEntry.Id.Cmp(smCommitOID) == 0 {
//nolint
return fmt.Errorf(
"The submodule %s is already at %s",
request.Submodule, request.CommitSHA,
)
}
if smEntry.Mode != git.FilemodeCommit {
return fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixInvalidSubmodulePath, err,
) //nolint
}
newEntry := *smEntry // copy by value
newEntry.Id = smCommitOID // assign new commit SHA
if err := index.Add(&newEntry); err != nil {
return fmt.Errorf("add new submodule entry to index: %w", err)
}
newRootTreeOID, err := index.WriteTreeTo(repo)
if err != nil {
return fmt.Errorf("write index to repo: %w", err)
}
newTree, err := repo.LookupTree(newRootTreeOID)
if err != nil {
return fmt.Errorf("looking up new submodule entry root tree: %w", err)
}
committer := git.Signature(
git2go.NewSignature(
request.AuthorName,
request.AuthorMail,
request.AuthorDate,
),
)
newCommitOID, err := repo.CreateCommit(
"", // caller should update branch with hooks
&committer,
&committer,
request.Message,
newTree,
startCommit,
)
if err != nil {
// nolint
return fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixFailedCommit, err,
)
}
return git2go.SubmoduleResult{
CommitID: newCommitOID.String(),
}.SerializeTo(w)
}
|