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