2016-11-04 06:16:01 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-11-04 06:16:01 +08:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
2017-01-06 09:51:15 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sort"
|
2019-06-08 22:31:11 +08:00
|
|
|
"strings"
|
2024-02-09 11:02:53 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-01-06 09:51:15 +08:00
|
|
|
)
|
2016-11-04 06:16:01 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
const (
|
|
|
|
beginpgp = "\n-----BEGIN PGP SIGNATURE-----\n"
|
|
|
|
endpgp = "\n-----END PGP SIGNATURE-----"
|
|
|
|
)
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2016-11-04 06:16:01 +08:00
|
|
|
// Tag represents a Git tag.
|
|
|
|
type Tag struct {
|
2020-12-17 22:00:47 +08:00
|
|
|
Name string
|
2023-12-14 05:02:00 +08:00
|
|
|
ID ObjectID
|
|
|
|
Object ObjectID // The id of this commit object
|
2020-12-17 22:00:47 +08:00
|
|
|
Type string
|
|
|
|
Tagger *Signature
|
|
|
|
Message string
|
|
|
|
Signature *CommitGPGSignature
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 17:30:52 +08:00
|
|
|
// Commit return the commit of the tag reference
|
2022-01-13 04:37:46 +08:00
|
|
|
func (tag *Tag) Commit(gitRepo *Repository) (*Commit, error) {
|
|
|
|
return gitRepo.getCommit(tag.Object)
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse commit information from the (uncompressed) raw
|
|
|
|
// data from the commit object.
|
|
|
|
// \n\n separate headers from message
|
2023-12-14 05:02:00 +08:00
|
|
|
func parseTagData(objectFormat ObjectFormat, data []byte) (*Tag, error) {
|
2016-11-04 06:16:01 +08:00
|
|
|
tag := new(Tag)
|
2023-12-17 19:56:08 +08:00
|
|
|
tag.ID = objectFormat.EmptyObjectID()
|
|
|
|
tag.Object = objectFormat.EmptyObjectID()
|
2021-05-12 13:11:42 +08:00
|
|
|
tag.Tagger = &Signature{}
|
2016-11-04 06:16:01 +08:00
|
|
|
// 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]
|
|
|
|
spacepos := bytes.IndexByte(line, ' ')
|
|
|
|
reftype := line[:spacepos]
|
|
|
|
switch string(reftype) {
|
|
|
|
case "object":
|
2023-12-19 15:20:47 +08:00
|
|
|
id, err := NewIDFromString(string(line[spacepos+1:]))
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tag.Object = id
|
|
|
|
case "type":
|
|
|
|
// A commit can have one or more parents
|
|
|
|
tag.Type = string(line[spacepos+1:])
|
|
|
|
case "tagger":
|
2024-02-09 11:02:53 +08:00
|
|
|
tag.Tagger = parseSignatureFromCommitLine(util.UnsafeBytesToString(line[spacepos+1:]))
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
nextline += eol + 1
|
|
|
|
case eol == 0:
|
2020-12-23 04:44:25 +08:00
|
|
|
tag.Message = string(data[nextline+1:])
|
2016-11-04 06:16:01 +08:00
|
|
|
break l
|
|
|
|
default:
|
|
|
|
break l
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 22:00:47 +08:00
|
|
|
idx := strings.LastIndex(tag.Message, beginpgp)
|
|
|
|
if idx > 0 {
|
|
|
|
endSigIdx := strings.Index(tag.Message[idx:], endpgp)
|
|
|
|
if endSigIdx > 0 {
|
|
|
|
tag.Signature = &CommitGPGSignature{
|
|
|
|
Signature: tag.Message[idx+1 : idx+endSigIdx+len(endpgp)],
|
|
|
|
Payload: string(data[:bytes.LastIndex(data, []byte(beginpgp))+1]),
|
|
|
|
}
|
|
|
|
tag.Message = tag.Message[:idx+1]
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 06:16:01 +08:00
|
|
|
return tag, nil
|
|
|
|
}
|
2017-01-06 09:51:15 +08:00
|
|
|
|
|
|
|
type tagSorter []*Tag
|
|
|
|
|
|
|
|
func (ts tagSorter) Len() int {
|
|
|
|
return len([]*Tag(ts))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts tagSorter) Less(i, j int) bool {
|
|
|
|
return []*Tag(ts)[i].Tagger.When.After([]*Tag(ts)[j].Tagger.When)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts tagSorter) Swap(i, j int) {
|
|
|
|
[]*Tag(ts)[i], []*Tag(ts)[j] = []*Tag(ts)[j], []*Tag(ts)[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortTagsByTime
|
|
|
|
func sortTagsByTime(tags []*Tag) {
|
|
|
|
sorter := tagSorter(tags)
|
|
|
|
sort.Sort(sorter)
|
|
|
|
}
|