diff options
author | Alberto Cortés <alberto@sourced.tech> | 2015-11-27 12:39:34 +0100 |
---|---|---|
committer | Alberto Cortés <alberto@sourced.tech> | 2015-12-04 09:48:41 +0100 |
commit | 48bf5bdeb9092ee5004014c0bf7a21f0e2fbf6fc (patch) | |
tree | d96d8dfe4c8e4e18f19f2f50d67befc4d9a88d57 /file.go | |
parent | d643cea1e8a6d618b2eddfdbed086c7bdf208658 (diff) | |
download | go-git-48bf5bdeb9092ee5004014c0bf7a21f0e2fbf6fc.tar.gz |
fix PR#7 comments
Diffstat (limited to 'file.go')
-rw-r--r-- | file.go | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +package git + +import ( + "bytes" + "io" + "strings" + + "gopkg.in/src-d/go-git.v2/core" +) + +// File represents git file objects. +type File struct { + Name string + io.Reader + Hash core.Hash +} + +// Contents returns the contents of a file as a string. +func (f *File) Contents() string { + buf := new(bytes.Buffer) + buf.ReadFrom(f) + return buf.String() +} + +// Lines returns a slice of lines from the contents of a file, stripping +// all end of line characters. If the last line is empty (does not end +// in an end of line), it is also stripped. +func (f *File) Lines() []string { + splits := strings.Split(f.Contents(), "\n") + // remove the last line if it is empty + if splits[len(splits)-1] == "" { + return splits[:len(splits)-1] + } + return splits +} |