// Copyright 2013 The go-github AUTHORS. All rights reserved.//// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package githubimport ()// SignatureVerification represents GPG signature verification.typeSignatureVerificationstruct { Verified *bool`json:"verified,omitempty"` Reason *string`json:"reason,omitempty"` Signature *string`json:"signature,omitempty"` Payload *string`json:"payload,omitempty"`}// MessageSigner is used by GitService.CreateCommit to sign a commit.//// To create a MessageSigner that signs a commit with a [golang.org/x/crypto/openpgp.Entity],// or [github.com/ProtonMail/go-crypto/openpgp.Entity], use://// commit.Signer = github.MessageSignerFunc(func(w io.Writer, r io.Reader) error {// return openpgp.ArmoredDetachSign(w, openpgpEntity, r, nil)// })typeMessageSignerinterface {Sign(w io.Writer, r io.Reader) error}// MessageSignerFunc is a single function implementation of MessageSigner.typeMessageSignerFuncfunc(w io.Writer, r io.Reader) errorfunc ( MessageSignerFunc) ( io.Writer, io.Reader) error {return (, )}// Commit represents a GitHub commit.typeCommitstruct { SHA *string`json:"sha,omitempty"` Author *CommitAuthor`json:"author,omitempty"` Committer *CommitAuthor`json:"committer,omitempty"` Message *string`json:"message,omitempty"` Tree *Tree`json:"tree,omitempty"` Parents []*Commit`json:"parents,omitempty"` Stats *CommitStats`json:"stats,omitempty"` HTMLURL *string`json:"html_url,omitempty"` URL *string`json:"url,omitempty"` Verification *SignatureVerification`json:"verification,omitempty"` NodeID *string`json:"node_id,omitempty"`// CommentCount is the number of GitHub comments on the commit. This // is only populated for requests that fetch GitHub data like // Pulls.ListCommits, Repositories.ListCommits, etc. CommentCount *int`json:"comment_count,omitempty"`}func ( Commit) () string {returnStringify()}// CommitAuthor represents the author or committer of a commit. The commit// author may not correspond to a GitHub User.typeCommitAuthorstruct { Date *Timestamp`json:"date,omitempty"` Name *string`json:"name,omitempty"` Email *string`json:"email,omitempty"`// The following fields are only populated by Webhook events. Login *string`json:"username,omitempty"`// Renamed for go-github consistency.}func ( CommitAuthor) () string {returnStringify()}// GetCommit fetches the Commit object for a given SHA.//// GitHub API docs: https://docs.github.com/rest/git/commits#get-a-commit-object////meta:operation GET /repos/{owner}/{repo}/git/commits/{commit_sha}func ( *GitService) ( context.Context, string, string, string) (*Commit, *Response, error) { := fmt.Sprintf("repos/%v/%v/git/commits/%v", , , ) , := .client.NewRequest("GET", , nil)if != nil {returnnil, nil, } := new(Commit) , := .client.Do(, , )if != nil {returnnil, , }return , , nil}// createCommit represents the body of a CreateCommit request.type createCommit struct { Author *CommitAuthor`json:"author,omitempty"` Committer *CommitAuthor`json:"committer,omitempty"` Message *string`json:"message,omitempty"` Tree *string`json:"tree,omitempty"` Parents []string`json:"parents,omitempty"` Signature *string`json:"signature,omitempty"`}typeCreateCommitOptionsstruct {// CreateCommit will sign the commit with this signer. See MessageSigner doc for more details. // Ignored on commits where Verification.Signature is defined. Signer MessageSigner}// CreateCommit creates a new commit in a repository.// commit must not be nil.//// The commit.Committer is optional and will be filled with the commit.Author// data if omitted. If the commit.Author is omitted, it will be filled in with// the authenticated user’s information and the current date.//// GitHub API docs: https://docs.github.com/rest/git/commits#create-a-commit////meta:operation POST /repos/{owner}/{repo}/git/commitsfunc ( *GitService) ( context.Context, string, string, *Commit, *CreateCommitOptions) (*Commit, *Response, error) {if == nil {returnnil, nil, fmt.Errorf("commit must be provided") }if == nil { = &CreateCommitOptions{} } := fmt.Sprintf("repos/%v/%v/git/commits", , ) := make([]string, len(.Parents))for , := range .Parents { [] = *.SHA } := &createCommit{Author: .Author,Committer: .Committer,Message: .Message,Parents: , }if .Tree != nil { .Tree = .Tree.SHA }switch {case .Verification != nil: .Signature = .Verification.Signaturecase .Signer != nil: , := createSignature(.Signer, )if != nil {returnnil, nil, } .Signature = & } , := .client.NewRequest("POST", , )if != nil {returnnil, nil, } := new(Commit) , := .client.Do(, , )if != nil {returnnil, , }return , , nil}func createSignature( MessageSigner, *createCommit) (string, error) {if == nil {return"", errors.New("createSignature: invalid parameters") } , := createSignatureMessage()if != nil {return"", }varbytes.Buffer = .Sign(&, strings.NewReader())if != nil {return"", }return .String(), nil}func createSignatureMessage( *createCommit) (string, error) {if == nil || .Message == nil || *.Message == "" || .Author == nil {return"", errors.New("createSignatureMessage: invalid parameters") }var []stringif .Tree != nil { = append(, fmt.Sprintf("tree %s", *.Tree)) }for , := range .Parents { = append(, fmt.Sprintf("parent %s", )) } = append(, fmt.Sprintf("author %s <%s> %d %s", .Author.GetName(), .Author.GetEmail(), .Author.GetDate().Unix(), .Author.GetDate().Format("-0700"))) := .Committerif == nil { = .Author }// There needs to be a double newline after committer = append(, fmt.Sprintf("committer %s <%s> %d %s\n", .GetName(), .GetEmail(), .GetDate().Unix(), .GetDate().Format("-0700"))) = append(, *.Message)returnstrings.Join(, "\n"), nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.