From 0de833dbf42814fb3f3923e0f7ad8f809c8c325d Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 16 May 2026 08:55:54 +0000 Subject: object/signature: Append --- object/signature/append.go | 32 ++++++++++++++++++++++++++++++++ object/signature/bytes.go | 33 --------------------------------- 2 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 object/signature/append.go delete mode 100644 object/signature/bytes.go diff --git a/object/signature/append.go b/object/signature/append.go new file mode 100644 index 00000000..3db52752 --- /dev/null +++ b/object/signature/append.go @@ -0,0 +1,32 @@ +package signature + +import ( + "fmt" + "strconv" + "slices" +) + +// Bytes renders the signature in canonical Git format. +func (signature Signature) AppendTo(dst []byte) ([]byte) { + slices.Grow(dst, len(signature.Name) + len(signature.Email) + 32) + dst = append(dst, signature.Name...) + dst = append(dst, ' ', '<') + dst = append(dst, signature.Email...) + dst = append(dst, '>', ' ') + strconv.AppendInt(dst, signature.WhenUnix, 10) + dst = append(dst, ' ') + + offset := signature.OffsetMinutes + + sign := '+' + if offset < 0 { + sign = '-' + offset = -offset + } + + hh := offset / 60 + mm := offset % 60 + dst = fmt.Appendf(dst, "%c%02d%02d", sign, hh, mm) + + return dst +} diff --git a/object/signature/bytes.go b/object/signature/bytes.go deleted file mode 100644 index 0afff5e0..00000000 --- a/object/signature/bytes.go +++ /dev/null @@ -1,33 +0,0 @@ -package signature - -import ( - "fmt" - "strconv" - "strings" -) - -// Bytes renders the signature in canonical Git format. -func (signature Signature) Bytes() ([]byte, error) { - var b strings.Builder - b.Grow(len(signature.Name) + len(signature.Email) + 32) - b.Write(signature.Name) - b.WriteString(" <") - b.Write(signature.Email) - b.WriteString("> ") - b.WriteString(strconv.FormatInt(signature.WhenUnix, 10)) - b.WriteByte(' ') - - offset := signature.OffsetMinutes - - sign := '+' - if offset < 0 { - sign = '-' - offset = -offset - } - - hh := offset / 60 - mm := offset % 60 - fmt.Fprintf(&b, "%c%02d%02d", sign, hh, mm) - - return []byte(b.String()), nil -} -- cgit v1.3.1-10-gc9f91