aboutsummaryrefslogtreecommitdiffstats
path: root/file.go
diff options
context:
space:
mode:
authorAlberto Cortés <alberto@sourced.tech>2015-11-27 12:39:34 +0100
committerAlberto Cortés <alberto@sourced.tech>2015-12-04 09:48:41 +0100
commit48bf5bdeb9092ee5004014c0bf7a21f0e2fbf6fc (patch)
treed96d8dfe4c8e4e18f19f2f50d67befc4d9a88d57 /file.go
parentd643cea1e8a6d618b2eddfdbed086c7bdf208658 (diff)
downloadgo-git-48bf5bdeb9092ee5004014c0bf7a21f0e2fbf6fc.tar.gz
fix PR#7 comments
Diffstat (limited to 'file.go')
-rw-r--r--file.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/file.go b/file.go
new file mode 100644
index 0000000..86baf7d
--- /dev/null
+++ b/file.go
@@ -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
+}