aboutsummaryrefslogblamecommitdiffstats
path: root/file.go
blob: b0b01415f19903ce105f218c3c7047179da8b489 (plain) (tree)
1
2
3
4
5
6
7
8






                 
                                       


























                                                                        
package git

import (
	"bytes"
	"io"
	"strings"

	"gopkg.in/src-d/go-git.v3/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
}