common: fix parsing commit messages

This commit is contained in:
2025-05-07 12:31:11 +02:00
parent b4b0d075be
commit 801fff6e22
2 changed files with 68 additions and 8 deletions

View File

@@ -478,21 +478,29 @@ func parseGitMsg(data <-chan byte) (GitMsg, error) {
}, nil
}
func parseGitCommitHdr(data <-chan byte) ([2]string, error) {
func parseGitCommitHdr(oldHdr [2]string, data <-chan byte) ([2]string, int, error) {
hdr := make([]byte, 0, 60)
val := make([]byte, 0, 1000)
c := <-data
size := 1
if c != '\n' { // end of header marker
for ; c != ' '; c = <-data {
hdr = append(hdr, c)
size++
}
if size == 1 { // continuation header here
hdr = []byte(oldHdr[0])
val = append([]byte(oldHdr[1]), '\n')
}
for c := <-data; c != '\n'; c = <-data {
val = append(val, c)
size++
}
size++
}
return [2]string{string(hdr), string(val)}, nil
return [2]string{string(hdr), string(val)}, size, nil
}
func parseGitCommitMsg(data <-chan byte, l int) (string, error) {
@@ -502,7 +510,6 @@ func parseGitCommitMsg(data <-chan byte, l int) (string, error) {
msg = append(msg, c)
l--
}
// l--
if l != 0 {
return "", fmt.Errorf("Unexpected data in the git commit msg: l=%d", l)
@@ -522,12 +529,14 @@ func parseGitCommit(data <-chan byte) (GitCommit, error) {
var c GitCommit
l := hdr.size
for {
hdr, err := parseGitCommitHdr(data)
var hdr [2]string
hdr, size, err := parseGitCommitHdr(hdr, data)
if err != nil {
return GitCommit{}, nil
}
l -= size
if len(hdr[0])+len(hdr[1]) == 0 { // hdr end marker
if size == 1 {
break
}
@@ -535,10 +544,7 @@ func parseGitCommit(data <-chan byte) (GitCommit, error) {
case "tree":
c.Tree = hdr[1]
}
l -= len(hdr[0]) + len(hdr[1]) + 2
}
l--
c.Msg, err = parseGitCommitMsg(data, l)
return c, err