| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/go-git/go-billy/v5/memfs" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/object" |
| 11 |
"github.com/go-git/go-git/v5/storage/memory" |
| 12 |
) |
| 13 |
|
| 14 |
type b64signer struct{} |
| 15 |
|
| 16 |
// This is not secure, and is only used as an example for testing purposes. |
| 17 |
// Please don't do this. |
| 18 |
func (b64signer) Sign(message io.Reader) ([]byte, error) { |
| 19 |
b, err := io.ReadAll(message) |
| 20 |
if err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
out := make([]byte, base64.StdEncoding.EncodedLen(len(b))) |
| 24 |
base64.StdEncoding.Encode(out, b) |
| 25 |
return out, nil |
| 26 |
} |
| 27 |
|
| 28 |
func ExampleSigner() { |
| 29 |
repo, err := Init(memory.NewStorage(), memfs.New()) |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
w, err := repo.Worktree() |
| 34 |
if err != nil { |
| 35 |
panic(err) |
| 36 |
} |
| 37 |
commit, err := w.Commit("example commit", &CommitOptions{ |
| 38 |
Author: &object.Signature{ |
| 39 |
Name: "John Doe", |
| 40 |
Email: "[email protected]", |
| 41 |
When: time.UnixMicro(1234567890).UTC(), |
| 42 |
}, |
| 43 |
Signer: b64signer{}, |
| 44 |
AllowEmptyCommits: true, |
| 45 |
}) |
| 46 |
if err != nil { |
| 47 |
panic(err) |
| 48 |
} |
| 49 |
|
| 50 |
obj, err := repo.CommitObject(commit) |
| 51 |
if err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
fmt.Println(obj.PGPSignature) |
| 55 |
// Output: dHJlZSA0YjgyNWRjNjQyY2I2ZWI5YTA2MGU1NGJmOGQ2OTI4OGZiZWU0OTA0CmF1dGhvciBKb2huIERvZSA8am9obkBleGFtcGxlLmNvbT4gMTIzNCArMDAwMApjb21taXR0ZXIgSm9obiBEb2UgPGpvaG5AZXhhbXBsZS5jb20+IDEyMzQgKzAwMDAKCmV4YW1wbGUgY29tbWl0 |
| 56 |
} |