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-10-12 05:43:27 +05:30
|
|
|
|
|
|
|
"github.com/mcuadros/go-version"
|
2019-04-19 17:47:27 +05:30
|
|
|
)
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
2019-07-24 00:20:39 +05:30
|
|
|
gogitTree, err := repo.gogitRepo.TreeObject(id)
|
2019-04-19 17:47:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2019-04-19 17:47:27 +05:30
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.gogitTree = gogitTree
|
|
|
|
return tree, nil
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// GetTree find the tree object in the repository.
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
|
2018-11-28 12:30:25 +05:30
|
|
|
if len(idStr) != 40 {
|
2019-08-06 02:09:39 +05:30
|
|
|
res, err := NewCommand("rev-parse", "--verify", idStr).RunInDir(repo.Path)
|
2018-11-28 12:30:25 +05:30
|
|
|
if err != nil {
|
2019-03-27 15:03:00 +05:30
|
|
|
return nil, err
|
2018-11-28 12:30:25 +05:30
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
|
|
|
idStr = res[:len(res)-1]
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 03:46:01 +05:30
|
|
|
id, err := NewIDFromString(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 06:03:11 +05:30
|
|
|
resolvedID := id
|
2019-07-24 00:20:39 +05:30
|
|
|
commitObject, err := repo.gogitRepo.CommitObject(id)
|
2019-05-03 06:03:11 +05:30
|
|
|
if err == nil {
|
|
|
|
id = SHA1(commitObject.TreeHash)
|
2019-04-19 17:47:27 +05:30
|
|
|
}
|
2019-05-03 06:03:11 +05:30
|
|
|
treeObject, err := repo.getTree(id)
|
2019-04-19 17:47:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 06:03:11 +05:30
|
|
|
treeObject.ResolvedID = resolvedID
|
2019-04-19 17:47:27 +05:30
|
|
|
return treeObject, nil
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2019-05-11 20:59:17 +05:30
|
|
|
|
|
|
|
// CommitTreeOpts represents the possible options to CommitTree
|
|
|
|
type CommitTreeOpts struct {
|
|
|
|
Parents []string
|
|
|
|
Message string
|
|
|
|
KeyID string
|
|
|
|
NoGPGSign bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12 05:43:27 +05:30
|
|
|
binVersion, err := BinVersion()
|
|
|
|
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(),
|
|
|
|
"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-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
|
|
|
|
|
|
|
if opts.KeyID != "" {
|
|
|
|
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
|
|
|
|
}
|
|
|
|
|
2019-10-12 05:43:27 +05:30
|
|
|
if version.Compare(binVersion, "2.0.0", ">=") && 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)
|
|
|
|
err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
|
2019-05-11 20:59:17 +05:30
|
|
|
|
|
|
|
if err != nil {
|
2019-10-12 05:43:27 +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
|
|
|
}
|