Skip to content

Commit 7a04a47

Browse files
committed
need to tolerate \r\n in message
1 parent 6793e8d commit 7a04a47

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

modules/git/commit_reader.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,56 +27,70 @@ func CommitFromReader(gitRepo *Repository, sha plumbing.Hash, reader io.Reader)
2727
pgpsig := false
2828

2929
scanner := bufio.NewScanner(reader)
30+
// Split by '\n' but include the '\n'
31+
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
32+
if atEOF && len(data) == 0 {
33+
return 0, nil, nil
34+
}
35+
if i := bytes.IndexByte(data, '\n'); i >= 0 {
36+
// We have a full newline-terminated line.
37+
return i + 1, data[0 : i+1], nil
38+
}
39+
// If we're at EOF, we have a final, non-terminated line. Return it.
40+
if atEOF {
41+
return len(data), data, nil
42+
}
43+
// Request more data.
44+
return 0, nil, nil
45+
})
46+
3047
for scanner.Scan() {
3148
line := scanner.Bytes()
3249
if pgpsig {
3350
if len(line) > 0 && line[0] == ' ' {
34-
line = bytes.TrimLeft(line, " ")
35-
_, _ = signatureSB.Write(line)
36-
_ = signatureSB.WriteByte('\n')
51+
_, _ = signatureSB.Write(line[1:])
3752
continue
3853
} else {
3954
pgpsig = false
4055
}
4156
}
4257

4358
if !message {
59+
// This is probably not correct but is copied from go-gits interpretation...
4460
trimmed := bytes.TrimSpace(line)
4561
if len(trimmed) == 0 {
4662
message = true
47-
_, _ = payloadSB.WriteString("\n")
63+
_, _ = payloadSB.Write(line)
4864
continue
4965
}
5066

5167
split := bytes.SplitN(trimmed, []byte{' '}, 2)
68+
var data []byte
69+
if len(split) > 1 {
70+
data = split[1]
71+
}
5272

5373
switch string(split[0]) {
5474
case "tree":
55-
commit.Tree = *NewTree(gitRepo, plumbing.NewHash(string(split[1])))
75+
commit.Tree = *NewTree(gitRepo, plumbing.NewHash(string(data)))
5676
_, _ = payloadSB.Write(line)
57-
_ = payloadSB.WriteByte('\n')
5877
case "parent":
59-
commit.Parents = append(commit.Parents, plumbing.NewHash(string(split[1])))
78+
commit.Parents = append(commit.Parents, plumbing.NewHash(string(data)))
6079
_, _ = payloadSB.Write(line)
61-
_ = payloadSB.WriteByte('\n')
6280
case "author":
6381
commit.Author = &Signature{}
64-
commit.Author.Decode(split[1])
82+
commit.Author.Decode(data)
6583
_, _ = payloadSB.Write(line)
66-
_ = payloadSB.WriteByte('\n')
6784
case "committer":
6885
commit.Committer = &Signature{}
69-
commit.Committer.Decode(split[1])
86+
commit.Committer.Decode(data)
7087
_, _ = payloadSB.Write(line)
71-
_ = payloadSB.WriteByte('\n')
7288
case "gpgsig":
73-
_, _ = signatureSB.Write(split[1])
74-
_ = signatureSB.WriteByte('\n')
89+
_, _ = signatureSB.Write(data)
7590
pgpsig = true
7691
}
7792
} else {
7893
_, _ = messageSB.Write(line)
79-
_ = messageSB.WriteByte('\n')
8094
}
8195
}
8296
commit.CommitMessage = messageSB.String()

0 commit comments

Comments
 (0)