Skip to content

Commit 81349c9

Browse files
authored
Merge pull request #46 from github/headers-without-messages
Handle commits and tags without messages
2 parents e15a0f5 + 202ca79 commit 81349c9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

git/git.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,24 @@ type ObjectHeaderIter struct {
576576
data string
577577
}
578578

579-
// Iterate over an object header. `data` should be the object's
580-
// contents, including the "\n\n" that separates the header from the
581-
// rest of the contents. `name` is used in error messages.
579+
// Iterate over a commit or tag object header. `data` should be the
580+
// object's contents, which is usually terminated by a blank line that
581+
// separates the header from the comment. However, annotated tags
582+
// don't always include comments, and Git even tolerates commits
583+
// without comments, so don't insist on a blank line. `name` is used
584+
// in error messages.
582585
func NewObjectHeaderIter(name string, data []byte) (ObjectHeaderIter, error) {
583586
headerEnd := bytes.Index(data, []byte("\n\n"))
584587
if headerEnd == -1 {
585-
return ObjectHeaderIter{}, fmt.Errorf("%s has no header separator", name)
588+
if len(data) == 0 {
589+
return ObjectHeaderIter{}, fmt.Errorf("%s has zero length", name)
590+
}
591+
592+
if data[len(data)-1] != '\n' {
593+
return ObjectHeaderIter{}, fmt.Errorf("%s has no terminating LF", name)
594+
}
595+
596+
return ObjectHeaderIter{name, string(data)}, nil
586597
}
587598
return ObjectHeaderIter{name, string(data[:headerEnd+1])}, nil
588599
}

0 commit comments

Comments
 (0)