code.gitea.io/gitea@v1.19.3/modules/git/repo_tree.go (about) 1 // Copyright 2015 The Gogs Authors. All rights reserved. 2 // Copyright 2019 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 package git 6 7 import ( 8 "bytes" 9 "os" 10 "strings" 11 "time" 12 ) 13 14 // CommitTreeOpts represents the possible options to CommitTree 15 type CommitTreeOpts struct { 16 Parents []string 17 Message string 18 KeyID string 19 NoGPGSign bool 20 AlwaysSign bool 21 } 22 23 // CommitTree creates a commit from a given tree id for the user with provided message 24 func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) { 25 commitTimeStr := time.Now().Format(time.RFC3339) 26 27 // Because this may call hooks we should pass in the environment 28 env := append(os.Environ(), 29 "GIT_AUTHOR_NAME="+author.Name, 30 "GIT_AUTHOR_EMAIL="+author.Email, 31 "GIT_AUTHOR_DATE="+commitTimeStr, 32 "GIT_COMMITTER_NAME="+committer.Name, 33 "GIT_COMMITTER_EMAIL="+committer.Email, 34 "GIT_COMMITTER_DATE="+commitTimeStr, 35 ) 36 cmd := NewCommand(repo.Ctx, "commit-tree").AddDynamicArguments(tree.ID.String()) 37 38 for _, parent := range opts.Parents { 39 cmd.AddArguments("-p").AddDynamicArguments(parent) 40 } 41 42 messageBytes := new(bytes.Buffer) 43 _, _ = messageBytes.WriteString(opts.Message) 44 _, _ = messageBytes.WriteString("\n") 45 46 if opts.KeyID != "" || opts.AlwaysSign { 47 cmd.AddOptionFormat("-S%s", opts.KeyID) 48 } 49 50 if opts.NoGPGSign { 51 cmd.AddArguments("--no-gpg-sign") 52 } 53 54 stdout := new(bytes.Buffer) 55 stderr := new(bytes.Buffer) 56 err := cmd.Run(&RunOpts{ 57 Env: env, 58 Dir: repo.Path, 59 Stdin: messageBytes, 60 Stdout: stdout, 61 Stderr: stderr, 62 }) 63 if err != nil { 64 return SHA1{}, ConcatenateError(err, stderr.String()) 65 } 66 return NewIDFromString(strings.TrimSpace(stdout.String())) 67 }