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