rewriteHeadAsSingleCommit rewrites the current branch tip to a single-parentless commit and leaves history squashed.
(repo *git.Repository, branch plumbing.ReferenceName, commitHash plumbing.Hash, message string, signature *object.Signature)
| 883 | |
| 884 | // rewriteHeadAsSingleCommit rewrites the current branch tip to a single-parentless commit and leaves history squashed. |
| 885 | func (s *GitTokenStore) rewriteHeadAsSingleCommit(repo *git.Repository, branch plumbing.ReferenceName, commitHash plumbing.Hash, message string, signature *object.Signature) error { |
| 886 | commitObj, err := repo.CommitObject(commitHash) |
| 887 | if err != nil { |
| 888 | return fmt.Errorf("git token store: inspect head commit: %w", err) |
| 889 | } |
| 890 | squashed := &object.Commit{ |
| 891 | Author: *signature, |
| 892 | Committer: *signature, |
| 893 | Message: message, |
| 894 | TreeHash: commitObj.TreeHash, |
| 895 | ParentHashes: nil, |
| 896 | Encoding: commitObj.Encoding, |
| 897 | ExtraHeaders: commitObj.ExtraHeaders, |
| 898 | } |
| 899 | mem := &plumbing.MemoryObject{} |
| 900 | mem.SetType(plumbing.CommitObject) |
| 901 | if err := squashed.Encode(mem); err != nil { |
| 902 | return fmt.Errorf("git token store: encode squashed commit: %w", err) |
| 903 | } |
| 904 | newHash, err := repo.Storer.SetEncodedObject(mem) |
| 905 | if err != nil { |
| 906 | return fmt.Errorf("git token store: write squashed commit: %w", err) |
| 907 | } |
| 908 | if err := repo.Storer.SetReference(plumbing.NewHashReference(branch, newHash)); err != nil { |
| 909 | return fmt.Errorf("git token store: update branch reference: %w", err) |
| 910 | } |
| 911 | return nil |
| 912 | } |
| 913 | |
| 914 | func (s *GitTokenStore) maybeRunGC(repoDir string) { |
| 915 | now := time.Now() |