aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-13 16:01:07 +0000
committerGravatar Runxi Yu2026-06-13 16:01:07 +0000
commit7aa2f5df73712deb3c51e4e3d7ad884b64b80b8f (patch)
tree2eadf181cd9b232bfba6a3bb7c56ba105f47e773 /object
parentobject/commit: Don't clone on parse (diff)
object/commit: Clone
Diffstat (limited to 'object')
-rw-r--r--object/commit/clone.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/object/commit/clone.go b/object/commit/clone.go
new file mode 100644
index 00000000..08987f26
--- /dev/null
+++ b/object/commit/clone.go
@@ -0,0 +1,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
+}