mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
- Go has a suite of small linters that helps with modernizing Go code by using newer functions and catching small mistakes, https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize. - Enable this linter in golangci-lint. - There's also [`go fix`](https://go.dev/blog/gofix), which is not yet released as a linter in golangci-lint: https://github.com/golangci/golangci-lint/pull/6385 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11936 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"forgejo.org/modules/util"
|
|
)
|
|
|
|
const (
|
|
beginpgp = "\n-----BEGIN PGP SIGNATURE-----\n"
|
|
endpgp = "\n-----END PGP SIGNATURE-----"
|
|
beginssh = "\n-----BEGIN SSH SIGNATURE-----\n"
|
|
endssh = "\n-----END SSH SIGNATURE-----"
|
|
)
|
|
|
|
// Tag represents a Git tag.
|
|
type Tag struct {
|
|
Name string
|
|
ID ObjectID
|
|
Object ObjectID // The id of this commit object
|
|
Type string
|
|
Tagger *Signature
|
|
Message string
|
|
Signature *ObjectSignature
|
|
}
|
|
|
|
// Commit return the commit of the tag reference
|
|
func (tag *Tag) Commit(gitRepo *Repository) (*Commit, error) {
|
|
return gitRepo.getCommit(tag.Object)
|
|
}
|
|
|
|
// Parse commit information from the (uncompressed) raw
|
|
// data from the commit object.
|
|
// \n\n separate headers from message
|
|
func parseTagData(objectFormat ObjectFormat, data []byte) (*Tag, error) {
|
|
tag := new(Tag)
|
|
tag.ID = objectFormat.EmptyObjectID()
|
|
tag.Object = objectFormat.EmptyObjectID()
|
|
tag.Tagger = &Signature{}
|
|
// we now have the contents of the commit object. Let's investigate...
|
|
nextline := 0
|
|
l:
|
|
for {
|
|
eol := bytes.IndexByte(data[nextline:], '\n')
|
|
switch {
|
|
case eol > 0:
|
|
line := data[nextline : nextline+eol]
|
|
before, after, _ := bytes.Cut(line, []byte{' '})
|
|
reftype := before
|
|
switch string(reftype) {
|
|
case "object":
|
|
id, err := NewIDFromString(string(after))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tag.Object = id
|
|
case "type":
|
|
// A commit can have one or more parents
|
|
tag.Type = string(after)
|
|
case "tagger":
|
|
tag.Tagger = parseSignatureFromCommitLine(util.UnsafeBytesToString(after))
|
|
}
|
|
nextline += eol + 1
|
|
case eol == 0:
|
|
tag.Message = string(data[nextline+1:])
|
|
break l
|
|
default:
|
|
break l
|
|
}
|
|
}
|
|
|
|
extractTagSignature := func(signatureBeginMark, signatureEndMark string) (bool, *ObjectSignature, string) {
|
|
idx := strings.LastIndex(tag.Message, signatureBeginMark)
|
|
if idx == -1 {
|
|
return false, nil, ""
|
|
}
|
|
|
|
endSigIdx := strings.Index(tag.Message[idx:], signatureEndMark)
|
|
if endSigIdx == -1 {
|
|
return false, nil, ""
|
|
}
|
|
|
|
return true, &ObjectSignature{
|
|
Signature: tag.Message[idx+1 : idx+endSigIdx+len(signatureEndMark)],
|
|
Payload: string(data[:bytes.LastIndex(data, []byte(signatureBeginMark))+1]),
|
|
}, tag.Message[:idx+1]
|
|
}
|
|
|
|
// Try to find an OpenPGP signature
|
|
found, sig, message := extractTagSignature(beginpgp, endpgp)
|
|
if !found {
|
|
// If not found, try an SSH one
|
|
found, sig, message = extractTagSignature(beginssh, endssh)
|
|
}
|
|
// If either is found, update the tag Signature and Message
|
|
if found {
|
|
tag.Signature = sig
|
|
tag.Message = message
|
|
}
|
|
|
|
return tag, nil
|
|
}
|