github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/commit_convert_gogit.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 //go:build gogit 7 8 package git 9 10 import ( 11 "fmt" 12 "strings" 13 14 "github.com/go-git/go-git/v5/plumbing/object" 15 ) 16 17 func convertPGPSignature(c *object.Commit) *CommitGPGSignature { 18 if c.PGPSignature == "" { 19 return nil 20 } 21 22 var w strings.Builder 23 var err error 24 25 if _, err = fmt.Fprintf(&w, "tree %s\n", c.TreeHash.String()); err != nil { 26 return nil 27 } 28 29 for _, parent := range c.ParentHashes { 30 if _, err = fmt.Fprintf(&w, "parent %s\n", parent.String()); err != nil { 31 return nil 32 } 33 } 34 35 if _, err = fmt.Fprint(&w, "author "); err != nil { 36 return nil 37 } 38 39 if err = c.Author.Encode(&w); err != nil { 40 return nil 41 } 42 43 if _, err = fmt.Fprint(&w, "\ncommitter "); err != nil { 44 return nil 45 } 46 47 if err = c.Committer.Encode(&w); err != nil { 48 return nil 49 } 50 51 if _, err = fmt.Fprintf(&w, "\n\n%s", c.Message); err != nil { 52 return nil 53 } 54 55 return &CommitGPGSignature{ 56 Signature: c.PGPSignature, 57 Payload: w.String(), 58 } 59 } 60 61 func convertCommit(c *object.Commit) *Commit { 62 return &Commit{ 63 ID: c.Hash, 64 CommitMessage: c.Message, 65 Committer: &c.Committer, 66 Author: &c.Author, 67 Signature: convertPGPSignature(c), 68 Parents: c.ParentHashes, 69 } 70 }