Skip to content

Commit 202ca79

Browse files
committed
Handle commits and tags without messages
Annotated tags don't necessarily have messages, and Git even tolerates commits without messages. So don't insist that the headers for such objects be followed by a blank line. Fixes #41.
1 parent 087b40a commit 202ca79

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
@@ -569,13 +569,24 @@ type ObjectHeaderIter struct {
569569
data string
570570
}
571571

572-
// Iterate over an object header. `data` should be the object's
573-
// contents, including the "\n\n" that separates the header from the
574-
// rest of the contents. `name` is used in error messages.
572+
// Iterate over a commit or tag object header. `data` should be the
573+
// object's contents, which is usually terminated by a blank line that
574+
// separates the header from the comment. However, annotated tags
575+
// don't always include comments, and Git even tolerates commits
576+
// without comments, so don't insist on a blank line. `name` is used
577+
// in error messages.
575578
func NewObjectHeaderIter(name string, data []byte) (ObjectHeaderIter, error) {
576579
headerEnd := bytes.Index(data, []byte("\n\n"))
577580
if headerEnd == -1 {
578-
return ObjectHeaderIter{}, fmt.Errorf("%s has no header separator", name)
581+
if len(data) == 0 {
582+
return ObjectHeaderIter{}, fmt.Errorf("%s has zero length", name)
583+
}
584+
585+
if data[len(data)-1] != '\n' {
586+
return ObjectHeaderIter{}, fmt.Errorf("%s has no terminating LF", name)
587+
}
588+
589+
return ObjectHeaderIter{name, string(data)}, nil
579590
}
580591
return ObjectHeaderIter{name, string(data[:headerEnd+1])}, nil
581592
}

0 commit comments

Comments
 (0)