2016-11-04 06:16:01 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 20:17:27 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-11-04 06:16:01 +08: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 20:17:27 +08:00
|
|
|
import (
|
2019-05-11 23:29:17 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-04-19 20:17:27 +08:00
|
|
|
)
|
|
|
|
|
2016-12-22 17:30:52 +08:00
|
|
|
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
2019-07-24 02:50:39 +08:00
|
|
|
gogitTree, err := repo.gogitRepo.TreeObject(id)
|
2019-04-19 20:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2019-04-19 20:17:27 +08:00
|
|
|
tree := NewTree(repo, id)
|
|
|
|
tree.gogitTree = gogitTree
|
|
|
|
return tree, nil
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 17:30:52 +08:00
|
|
|
// GetTree find the tree object in the repository.
|
2016-11-04 06:16:01 +08:00
|
|
|
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
|
2018-11-28 15:00:25 +08:00
|
|
|
if len(idStr) != 40 {
|
|
|
|
res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
2019-03-27 17:33:00 +08:00
|
|
|
return nil, err
|
2018-11-28 15:00:25 +08:00
|
|
|
}
|
|
|
|
if len(res) > 0 {
|
|
|
|
idStr = res[:len(res)-1]
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 06:16:01 +08:00
|
|
|
id, err := NewIDFromString(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 08:33:11 +08:00
|
|
|
resolvedID := id
|
2019-07-24 02:50:39 +08:00
|
|
|
commitObject, err := repo.gogitRepo.CommitObject(id)
|
2019-05-03 08:33:11 +08:00
|
|
|
if err == nil {
|
|
|
|
id = SHA1(commitObject.TreeHash)
|
2019-04-19 20:17:27 +08:00
|
|
|
}
|
2019-05-03 08:33:11 +08:00
|
|
|
treeObject, err := repo.getTree(id)
|
2019-04-19 20:17:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 08:33:11 +08:00
|
|
|
treeObject.ResolvedID = resolvedID
|
2019-04-19 20:17:27 +08:00
|
|
|
return treeObject, nil
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
2019-05-11 23:29:17 +08:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
commitTimeStr := time.Now().Format(time.UnixDate)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddArguments("-m", opts.Message)
|
|
|
|
|
|
|
|
if opts.KeyID != "" {
|
|
|
|
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.NoGPGSign {
|
|
|
|
cmd.AddArguments("--no-gpg-sign")
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cmd.RunInDirWithEnv(repo.Path, env)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return SHA1{}, err
|
|
|
|
}
|
|
|
|
return NewIDFromString(strings.TrimSpace(res))
|
|
|
|
}
|