2016-11-03 16:16:01 -06:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 06:17:27 -06:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-03 16:16:01 -06: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 06:17:27 -06:00
|
|
|
import (
|
2019-10-11 18:13:27 -06:00
|
|
|
"bytes"
|
2019-05-11 09:29:17 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-04-19 06:17:27 -06:00
|
|
|
)
|
|
|
|
|
2016-12-22 02:30:52 -07:00
|
|
|
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
2019-07-23 12:50:39 -06:00
|
|
|
gogitTree, err := repo.gogitRepo.TreeObject(id)
|
2019-04-19 06:17:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:17:27 -06:00
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.gogitTree = gogitTree
|
|
|
|
return tree, nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
|
|
|
|
2016-12-22 02:30:52 -07:00
|
|
|
// GetTree find the tree object in the repository.
|
2016-11-03 16:16:01 -06:00
|
|
|
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
|
2018-11-28 00:00:25 -07:00
|
|
|
if len(idStr) != 40 {
|
2019-08-05 14:39:39 -06:00
|
|
|
res, err := NewCommand("rev-parse", "--verify", idStr).RunInDir(repo.Path)
|
2018-11-28 00:00:25 -07:00
|
|
|
if err != nil {
|
2019-03-27 03:33:00 -06:00
|
|
|
return nil, err
|
2018-11-28 00:00:25 -07:00
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
|
|
|
idStr = res[:len(res)-1]
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 16:16:01 -06:00
|
|
|
id, err := NewIDFromString(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 18:33:11 -06:00
|
|
|
resolvedID := id
|
2019-07-23 12:50:39 -06:00
|
|
|
commitObject, err := repo.gogitRepo.CommitObject(id)
|
2019-05-02 18:33:11 -06:00
|
|
|
if err == nil {
|
|
|
|
id = SHA1(commitObject.TreeHash)
|
2019-04-19 06:17:27 -06:00
|
|
|
}
|
2019-05-02 18:33:11 -06:00
|
|
|
treeObject, err := repo.getTree(id)
|
2019-04-19 06:17:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-02 18:33:11 -06:00
|
|
|
treeObject.ResolvedID = resolvedID
|
2019-04-19 06:17:27 -06:00
|
|
|
return treeObject, nil
|
2016-11-03 16:16:01 -06:00
|
|
|
}
|
2019-05-11 09:29:17 -06:00
|
|
|
|
|
|
|
// CommitTreeOpts represents the possible options to CommitTree
|
|
|
|
type CommitTreeOpts struct {
|
2019-10-16 07:42:42 -06:00
|
|
|
Parents []string
|
|
|
|
Message string
|
|
|
|
KeyID string
|
|
|
|
NoGPGSign bool
|
|
|
|
AlwaysSign bool
|
2019-05-11 09:29:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitTree creates a commit from a given tree id for the user with provided message
|
2020-09-19 10:44:55 -06:00
|
|
|
func (repo *Repository) CommitTree(author *Signature, committer *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
|
2020-09-05 10:42:58 -06:00
|
|
|
err := LoadGitVersion()
|
2019-10-11 18:13:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return SHA1{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:19:47 -06:00
|
|
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
2019-05-11 09:29:17 -06:00
|
|
|
|
|
|
|
// Because this may call hooks we should pass in the environment
|
|
|
|
env := append(os.Environ(),
|
2020-09-19 10:44:55 -06:00
|
|
|
"GIT_AUTHOR_NAME="+author.Name,
|
|
|
|
"GIT_AUTHOR_EMAIL="+author.Email,
|
2019-05-11 09:29:17 -06:00
|
|
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
2020-09-19 10:44:55 -06:00
|
|
|
"GIT_COMMITTER_NAME="+committer.Name,
|
|
|
|
"GIT_COMMITTER_EMAIL="+committer.Email,
|
2019-05-11 09:29:17 -06:00
|
|
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
|
|
|
)
|
|
|
|
cmd := NewCommand("commit-tree", tree.ID.String())
|
|
|
|
|
|
|
|
for _, parent := range opts.Parents {
|
|
|
|
cmd.AddArguments("-p", parent)
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:13:27 -06:00
|
|
|
messageBytes := new(bytes.Buffer)
|
|
|
|
_, _ = messageBytes.WriteString(opts.Message)
|
|
|
|
_, _ = messageBytes.WriteString("\n")
|
2019-05-11 09:29:17 -06:00
|
|
|
|
2020-09-05 10:42:58 -06:00
|
|
|
if CheckGitVersionConstraint(">= 1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
|
2019-05-11 09:29:17 -06:00
|
|
|
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
|
|
|
|
}
|
|
|
|
|
2020-09-05 10:42:58 -06:00
|
|
|
if CheckGitVersionConstraint(">= 2.0.0") == nil && opts.NoGPGSign {
|
2019-05-11 09:29:17 -06:00
|
|
|
cmd.AddArguments("--no-gpg-sign")
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:13:27 -06:00
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
|
2019-05-11 09:29:17 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2019-10-11 18:13:27 -06:00
|
|
|
return SHA1{}, concatenateError(err, stderr.String())
|
2019-05-11 09:29:17 -06:00
|
|
|
}
|
2019-10-11 18:13:27 -06:00
|
|
|
return NewIDFromString(strings.TrimSpace(stdout.String()))
|
2019-05-11 09:29:17 -06:00
|
|
|
}
|