2016-11-03 23:16:01 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 14:17:27 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 23:16:01 +01:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
2019-04-19 14:17:27 +02:00
|
|
|
import (
|
2019-10-12 02:13:27 +02:00
|
|
|
"bytes"
|
2019-05-11 17:29:17 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-04-19 14:17:27 +02:00
|
|
|
)
|
|
|
|
|
2019-05-11 17:29:17 +02:00
|
|
|
// CommitTreeOpts represents the possible options to CommitTree
|
|
|
|
type CommitTreeOpts struct {
|
2019-10-16 15:42:42 +02:00
|
|
|
Parents []string
|
|
|
|
Message string
|
|
|
|
KeyID string
|
|
|
|
NoGPGSign bool
|
|
|
|
AlwaysSign bool
|
2019-05-11 17:29:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitTree creates a commit from a given tree id for the user with provided message
|
2020-09-19 18:44:55 +02:00
|
|
|
func (repo *Repository) CommitTree(author *Signature, committer *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
|
2020-09-05 18:42:58 +02:00
|
|
|
err := LoadGitVersion()
|
2019-10-12 02:13:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return SHA1{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 21:19:47 +02:00
|
|
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
2019-05-11 17:29:17 +02:00
|
|
|
|
|
|
|
// Because this may call hooks we should pass in the environment
|
|
|
|
env := append(os.Environ(),
|
2020-09-19 18:44:55 +02:00
|
|
|
"GIT_AUTHOR_NAME="+author.Name,
|
|
|
|
"GIT_AUTHOR_EMAIL="+author.Email,
|
2019-05-11 17:29:17 +02:00
|
|
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
2020-09-19 18:44:55 +02:00
|
|
|
"GIT_COMMITTER_NAME="+committer.Name,
|
|
|
|
"GIT_COMMITTER_EMAIL="+committer.Email,
|
2019-05-11 17:29:17 +02:00
|
|
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
|
|
|
)
|
2021-11-30 21:06:32 +01:00
|
|
|
cmd := NewCommandContext(repo.Ctx, "commit-tree", tree.ID.String())
|
2019-05-11 17:29:17 +02:00
|
|
|
|
|
|
|
for _, parent := range opts.Parents {
|
|
|
|
cmd.AddArguments("-p", parent)
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
messageBytes := new(bytes.Buffer)
|
|
|
|
_, _ = messageBytes.WriteString(opts.Message)
|
|
|
|
_, _ = messageBytes.WriteString("\n")
|
2019-05-11 17:29:17 +02:00
|
|
|
|
2020-10-21 17:42:08 +02:00
|
|
|
if CheckGitVersionAtLeast("1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
|
2019-05-11 17:29:17 +02:00
|
|
|
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:42:08 +02:00
|
|
|
if CheckGitVersionAtLeast("2.0.0") == nil && opts.NoGPGSign {
|
2019-05-11 17:29:17 +02:00
|
|
|
cmd.AddArguments("--no-gpg-sign")
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
|
2019-05-11 17:29:17 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-17 15:00:47 +01:00
|
|
|
return SHA1{}, ConcatenateError(err, stderr.String())
|
2019-05-11 17:29:17 +02:00
|
|
|
}
|
2019-10-12 02:13:27 +02:00
|
|
|
return NewIDFromString(strings.TrimSpace(stdout.String()))
|
2019-05-11 17:29:17 +02:00
|
|
|
}
|