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-05-11 11:29:17 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-04-19 08:17:27 -04:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
)
|
|
|
|
|
2016-12-22 04:30:52 -05:00
|
|
|
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
2019-04-19 08:17:27 -04:00
|
|
|
gogitTree, err := repo.gogitRepo.TreeObject(plumbing.Hash(id))
|
|
|
|
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 {
|
|
|
|
res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path)
|
|
|
|
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-04-19 08:17:27 -04:00
|
|
|
commitObject, err := repo.gogitRepo.CommitObject(plumbing.Hash(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 {
|
|
|
|
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))
|
|
|
|
}
|