aboutsummaryrefslogtreecommitdiff
path: root/object/commit/clone.go
blob: 08987f26df27de3ff0cea333b7f5155b3a6b1c97 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package commit

import (
	"bytes"
	"slices"
)

// Clone returns a deep copy of the commit
// whose byte fields are independent of any memory the original may alias.
//
// Labels: Life-Independent.
func (commit *Commit) Clone() *Commit {
	clone := &Commit{
		Tree:      commit.Tree,
		Parents:   slices.Clone(commit.Parents),
		Author:    commit.Author.Clone(),
		Committer: commit.Committer.Clone(),
		Message:   bytes.Clone(commit.Message),
		ChangeID:  bytes.Clone(commit.ChangeID),
	}

	if commit.ExtraHeaders != nil {
		clone.ExtraHeaders = make([]ExtraHeader, len(commit.ExtraHeaders))
		for i, h := range commit.ExtraHeaders {
			clone.ExtraHeaders[i] = ExtraHeader{
				Key:   bytes.Clone(h.Key),
				Value: bytes.Clone(h.Value),
			}
		}
	}

	return clone
}