2020-01-15 03:32:57 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-15 03:32:57 -05:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CommitFromReader will generate a Commit from a provided reader
|
2020-12-17 09:00:47 -05:00
|
|
|
// We need this to interpret commits from cat-file or cat-file --batch
|
|
|
|
//
|
|
|
|
// If used as part of a cat-file --batch stream you need to limit the reader to the correct size
|
2023-12-13 16:02:00 -05:00
|
|
|
func CommitFromReader(gitRepo *Repository, objectID ObjectID, reader io.Reader) (*Commit, error) {
|
2020-01-15 03:32:57 -05:00
|
|
|
commit := &Commit{
|
2023-12-13 16:02:00 -05:00
|
|
|
ID: objectID,
|
2021-05-12 01:11:42 -04:00
|
|
|
Author: &Signature{},
|
|
|
|
Committer: &Signature{},
|
2020-01-15 03:32:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
payloadSB := new(strings.Builder)
|
|
|
|
signatureSB := new(strings.Builder)
|
|
|
|
messageSB := new(strings.Builder)
|
|
|
|
message := false
|
|
|
|
pgpsig := false
|
|
|
|
|
2020-12-17 09:00:47 -05:00
|
|
|
bufReader, ok := reader.(*bufio.Reader)
|
|
|
|
if !ok {
|
|
|
|
bufReader = bufio.NewReader(reader)
|
|
|
|
}
|
2020-01-15 03:32:57 -05:00
|
|
|
|
2020-12-17 09:00:47 -05:00
|
|
|
readLoop:
|
|
|
|
for {
|
|
|
|
line, err := bufReader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2020-12-17 15:42:33 -05:00
|
|
|
if message {
|
|
|
|
_, _ = messageSB.Write(line)
|
|
|
|
}
|
|
|
|
_, _ = payloadSB.Write(line)
|
2020-12-17 09:00:47 -05:00
|
|
|
break readLoop
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 03:32:57 -05:00
|
|
|
if pgpsig {
|
|
|
|
if len(line) > 0 && line[0] == ' ' {
|
|
|
|
_, _ = signatureSB.Write(line[1:])
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
pgpsig = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !message {
|
|
|
|
// This is probably not correct but is copied from go-gits interpretation...
|
|
|
|
trimmed := bytes.TrimSpace(line)
|
|
|
|
if len(trimmed) == 0 {
|
|
|
|
message = true
|
|
|
|
_, _ = payloadSB.Write(line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
split := bytes.SplitN(trimmed, []byte{' '}, 2)
|
|
|
|
var data []byte
|
|
|
|
if len(split) > 1 {
|
|
|
|
data = split[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch string(split[0]) {
|
|
|
|
case "tree":
|
2023-12-19 02:20:47 -05:00
|
|
|
commit.Tree = *NewTree(gitRepo, MustIDFromString(string(data)))
|
2020-01-15 03:32:57 -05:00
|
|
|
_, _ = payloadSB.Write(line)
|
|
|
|
case "parent":
|
2023-12-19 02:20:47 -05:00
|
|
|
commit.Parents = append(commit.Parents, MustIDFromString(string(data)))
|
2020-01-15 03:32:57 -05:00
|
|
|
_, _ = payloadSB.Write(line)
|
|
|
|
case "author":
|
|
|
|
commit.Author = &Signature{}
|
|
|
|
commit.Author.Decode(data)
|
|
|
|
_, _ = payloadSB.Write(line)
|
|
|
|
case "committer":
|
|
|
|
commit.Committer = &Signature{}
|
|
|
|
commit.Committer.Decode(data)
|
|
|
|
_, _ = payloadSB.Write(line)
|
2024-03-29 19:03:11 -04:00
|
|
|
case "encoding":
|
|
|
|
_, _ = payloadSB.Write(line)
|
2020-01-15 03:32:57 -05:00
|
|
|
case "gpgsig":
|
2024-01-19 11:05:02 -05:00
|
|
|
fallthrough
|
|
|
|
case "gpgsig-sha256": // FIXME: no intertop, so only 1 exists at present.
|
2020-01-15 03:32:57 -05:00
|
|
|
_, _ = signatureSB.Write(data)
|
|
|
|
_ = signatureSB.WriteByte('\n')
|
|
|
|
pgpsig = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, _ = messageSB.Write(line)
|
2020-12-17 15:42:33 -05:00
|
|
|
_, _ = payloadSB.Write(line)
|
2020-01-15 03:32:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
commit.CommitMessage = messageSB.String()
|
2024-02-29 03:44:35 -05:00
|
|
|
commit.Signature = &ObjectSignature{
|
2020-01-15 03:32:57 -05:00
|
|
|
Signature: signatureSB.String(),
|
|
|
|
Payload: payloadSB.String(),
|
|
|
|
}
|
|
|
|
if len(commit.Signature.Signature) == 0 {
|
|
|
|
commit.Signature = nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 09:00:47 -05:00
|
|
|
return commit, nil
|
2020-01-15 03:32:57 -05:00
|
|
|
}
|