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
145
146
147
148
149
150
151
152
153
154
155
156
|
package localrepo
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/text"
)
// ErrMissingTree indicates a missing tree when attemping to write a commit
var ErrMissingTree = errors.New("missing tree")
// ErrMissingCommitterName indicates an attempt to write a commit without a
// comitter name
var ErrMissingCommitterName = errors.New("missing committer name")
// ErrMissingAuthorName indicates an attempt to write a commit without a
// comitter name
var ErrMissingAuthorName = errors.New("missing author name")
// WriteCommitConfig contains fields for writing a commit
type WriteCommitConfig struct {
Reference string
Parents []git.ObjectID
AuthorDate time.Time
AuthorName string
AuthorEmail string
CommitterName string
CommitterEmail string
CommitterDate time.Time
Message string
TreeEntries []TreeEntry
TreeID git.ObjectID
AlternateObjectDir string
}
func validateWriteCommitConfig(cfg WriteCommitConfig) error {
if cfg.TreeID == "" {
return ErrMissingTree
}
if cfg.AuthorName == "" {
return ErrMissingAuthorName
}
if cfg.CommitterName == "" {
return ErrMissingCommitterName
}
return nil
}
// WriteCommit writes a new commit into the target repository.
func (repo *Repo) WriteCommit(ctx context.Context, cfg WriteCommitConfig) (git.ObjectID, error) {
if err := validateWriteCommitConfig(cfg); err != nil {
return "", err
}
if cfg.AuthorDate.IsZero() {
cfg.AuthorDate = time.Now()
}
if cfg.CommitterDate.IsZero() {
cfg.CommitterDate = time.Now()
}
// Use 'commit-tree' instead of 'commit' because we are in a bare
// repository. What we do here is the same as "commit -m message
// --allow-empty".
commitArgs := []string{string(cfg.TreeID)}
repoPath, err := repo.Path()
if err != nil {
return "", fmt.Errorf("getting repo path: %w", err)
}
var env []string
if cfg.AlternateObjectDir != "" {
if !filepath.IsAbs(cfg.AlternateObjectDir) {
return "", errors.New("alternate object directory must be an absolute path")
}
if err := os.MkdirAll(cfg.AlternateObjectDir, perm.SharedDir); err != nil {
return "", err
}
env = append(env,
fmt.Sprintf("GIT_OBJECT_DIRECTORY=%s", cfg.AlternateObjectDir),
fmt.Sprintf("GIT_ALTERNATE_OBJECT_DIRECTORIES=%s", filepath.Join(repoPath, "objects")),
)
}
env = append(env,
fmt.Sprintf("GIT_AUTHOR_DATE=%s", cfg.AuthorDate.String()),
fmt.Sprintf("GIT_AUTHOR_NAME=%s", cfg.AuthorName),
fmt.Sprintf("GIT_AUTHOR_EMAIL=%s", cfg.AuthorEmail),
fmt.Sprintf("GIT_COMMITTER_DATE=%s", cfg.CommitterDate.String()),
fmt.Sprintf("GIT_COMMITTER_NAME=%s", cfg.CommitterName),
fmt.Sprintf("GIT_COMMITTER_EMAIL=%s", cfg.CommitterEmail),
)
var flags []git.Option
for _, parent := range cfg.Parents {
flags = append(flags, git.ValueFlag{Name: "-p", Value: parent.String()})
}
flags = append(flags, git.ValueFlag{Name: "-F", Value: "-"})
var stdout, stderr bytes.Buffer
if err := repo.ExecAndWait(ctx,
git.Command{
Name: "commit-tree",
Flags: flags,
Args: commitArgs,
},
git.WithStdout(&stdout),
git.WithStderr(&stderr),
git.WithStdin(strings.NewReader(cfg.Message)),
git.WithEnv(env...),
); err != nil {
return "", fmt.Errorf("commit-tree: %w: %s %s", err, stderr.String(), stdout.String())
}
objectHash, err := repo.ObjectHash(ctx)
if err != nil {
return "", fmt.Errorf("detecting object hash: %w", err)
}
oid, err := objectHash.FromHex(text.ChompBytes(stdout.Bytes()))
if err != nil {
return "", fmt.Errorf("hex to object hash: %w", err)
}
if cfg.Reference != "" {
if err := repo.UpdateRef(
ctx,
git.ReferenceName(cfg.Reference),
oid,
"",
); err != nil {
return "", fmt.Errorf("updating ref: %w", err)
}
}
return oid, nil
}
|