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