2016-11-03 22:16:01 +00:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 12:17:27 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
2019-04-19 12:17:27 +00:00
|
|
|
import (
|
2019-10-12 00:13:27 +00:00
|
|
|
"bytes"
|
2019-05-11 15:29:17 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-04-19 12:17:27 +00:00
|
|
|
)
|
|
|
|
|
2019-05-11 15:29:17 +00:00
|
|
|
// CommitTreeOpts represents the possible options to CommitTree
|
|
|
|
type CommitTreeOpts struct {
|
2019-10-16 13:42:42 +00:00
|
|
|
Parents []string
|
|
|
|
Message string
|
|
|
|
KeyID string
|
|
|
|
NoGPGSign bool
|
|
|
|
AlwaysSign bool
|
2019-05-11 15:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitTree creates a commit from a given tree id for the user with provided message
|
2021-12-20 04:41:31 +00:00
|
|
|
func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
|
2019-07-31 19:19:47 +00:00
|
|
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
2019-05-11 15:29:17 +00:00
|
|
|
|
|
|
|
// Because this may call hooks we should pass in the environment
|
|
|
|
env := append(os.Environ(),
|
2020-09-19 16:44:55 +00:00
|
|
|
"GIT_AUTHOR_NAME="+author.Name,
|
|
|
|
"GIT_AUTHOR_EMAIL="+author.Email,
|
2019-05-11 15:29:17 +00:00
|
|
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
2020-09-19 16:44:55 +00:00
|
|
|
"GIT_COMMITTER_NAME="+committer.Name,
|
|
|
|
"GIT_COMMITTER_EMAIL="+committer.Email,
|
2019-05-11 15:29:17 +00:00
|
|
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
|
|
|
)
|
2022-10-23 14:44:45 +00:00
|
|
|
cmd := NewCommand(repo.Ctx, "commit-tree").AddDynamicArguments(tree.ID.String())
|
2019-05-11 15:29:17 +00:00
|
|
|
|
|
|
|
for _, parent := range opts.Parents {
|
2022-10-23 14:44:45 +00:00
|
|
|
cmd.AddArguments("-p").AddDynamicArguments(parent)
|
2019-05-11 15:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 00:13:27 +00:00
|
|
|
messageBytes := new(bytes.Buffer)
|
|
|
|
_, _ = messageBytes.WriteString(opts.Message)
|
|
|
|
_, _ = messageBytes.WriteString("\n")
|
2019-05-11 15:29:17 +00:00
|
|
|
|
2022-06-16 15:47:44 +00:00
|
|
|
if opts.KeyID != "" || opts.AlwaysSign {
|
2022-10-23 14:44:45 +00:00
|
|
|
cmd.AddArguments(CmdArg(fmt.Sprintf("-S%s", opts.KeyID)))
|
2019-05-11 15:29:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 15:47:44 +00:00
|
|
|
if opts.NoGPGSign {
|
2019-05-11 15:29:17 +00:00
|
|
|
cmd.AddArguments("--no-gpg-sign")
|
|
|
|
}
|
|
|
|
|
2019-10-12 00:13:27 +00:00
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
2022-06-10 01:57:49 +00:00
|
|
|
err := cmd.Run(&RunOpts{
|
2022-04-01 02:55:30 +00:00
|
|
|
Env: env,
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdin: messageBytes,
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
2022-02-11 12:47:22 +00:00
|
|
|
})
|
2019-05-11 15:29:17 +00:00
|
|
|
if err != nil {
|
2020-12-17 14:00:47 +00:00
|
|
|
return SHA1{}, ConcatenateError(err, stderr.String())
|
2019-05-11 15:29:17 +00:00
|
|
|
}
|
2019-10-12 00:13:27 +00:00
|
|
|
return NewIDFromString(strings.TrimSpace(stdout.String()))
|
2019-05-11 15:29:17 +00:00
|
|
|
}
|