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 137 138 139 140 141 142 143 144
|
//go:build static && system_libgit2
package main
import (
"context"
"encoding/gob"
"flag"
"fmt"
"time"
git "github.com/libgit2/git2go/v34"
"gitlab.com/gitlab-org/gitaly/v16/cmd/gitaly-git2go/git2goutil"
"gitlab.com/gitlab-org/gitaly/v16/internal/git2go"
)
type submoduleSubcommand struct{}
func (cmd *submoduleSubcommand) Flags() *flag.FlagSet {
return flag.NewFlagSet("submodule", flag.ExitOnError)
}
func (cmd *submoduleSubcommand) Run(_ context.Context, decoder *gob.Decoder, encoder *gob.Encoder) error {
var request git2go.SubmoduleCommand
if err := decoder.Decode(&request); err != nil {
return fmt.Errorf("deserializing submodule command request: %w", err)
}
res, err := cmd.run(request)
if err != nil {
return err
}
return encoder.Encode(res)
}
func (cmd *submoduleSubcommand) run(request git2go.SubmoduleCommand) (*git2go.SubmoduleResult, error) {
if request.AuthorDate.IsZero() {
request.AuthorDate = time.Now()
}
smCommitOID, err := git.NewOid(request.CommitSHA)
if err != nil {
return nil, fmt.Errorf("converting %s to OID: %w", request.CommitSHA, err)
}
repo, err := git2goutil.OpenRepository(request.Repository)
if err != nil {
return nil, fmt.Errorf("open repository: %w", err)
}
fullBranchRefName := "refs/heads/" + request.Branch
o, err := repo.RevparseSingle(fullBranchRefName)
if err != nil {
return nil, fmt.Errorf("%s: %w", git2go.LegacyErrPrefixInvalidBranch, err)
}
startCommit, err := o.AsCommit()
if err != nil {
return nil, fmt.Errorf("peeling %s as a commit: %w", o.Id(), err)
}
rootTree, err := startCommit.Tree()
if err != nil {
return nil, fmt.Errorf("root tree from starting commit: %w", err)
}
index, err := git.NewIndex()
if err != nil {
return nil, fmt.Errorf("creating new index: %w", err)
}
if err := index.ReadTree(rootTree); err != nil {
return nil, fmt.Errorf("reading root tree into index: %w", err)
}
smEntry, err := index.EntryByPath(request.Submodule, 0)
if err != nil {
return nil, fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixInvalidSubmodulePath, err,
)
}
if smEntry.Id.Cmp(smCommitOID) == 0 {
//nolint
return nil, fmt.Errorf(
"The submodule %s is already at %s",
request.Submodule, request.CommitSHA,
)
}
if smEntry.Mode != git.FilemodeCommit {
return nil, fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixInvalidSubmodulePath, err,
)
}
newEntry := *smEntry // copy by value
newEntry.Id = smCommitOID // assign new commit SHA
if err := index.Add(&newEntry); err != nil {
return nil, fmt.Errorf("add new submodule entry to index: %w", err)
}
newRootTreeOID, err := index.WriteTreeTo(repo)
if err != nil {
return nil, fmt.Errorf("write index to repo: %w", err)
}
newTree, err := repo.LookupTree(newRootTreeOID)
if err != nil {
return nil, fmt.Errorf("looking up new submodule entry root tree: %w", err)
}
committer := git.Signature(
git2go.NewSignature(
request.AuthorName,
request.AuthorMail,
request.AuthorDate,
),
)
newCommitOID, err := git2goutil.NewCommitSubmitter(repo, request.SigningKey).
Commit(
&committer,
&committer,
git.MessageEncodingUTF8,
request.Message,
newTree,
startCommit,
)
if err != nil {
return nil, fmt.Errorf(
"%s: %w",
git2go.LegacyErrPrefixFailedCommit, err,
)
}
return &git2go.SubmoduleResult{
CommitID: newCommitOID.String(),
}, nil
}
|