Important to keep the index sorted because git diffs

This commit is contained in:
comp500 2019-04-26 18:45:59 +01:00
parent 0d7ff15db7
commit 2e4ddb667f
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5

View File

@ -77,8 +77,8 @@ func (in *Index) resortIndex() {
}) })
} }
// UpdateFile calculates the hash for a given path and updates it in the index // updateFile calculates the hash for a given path and updates it in the index
func (in *Index) UpdateFile(path string) error { func (in *Index) updateFile(path string) error {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return err return err
@ -176,7 +176,7 @@ func (in *Index) Refresh() error {
return nil return nil
} }
return in.UpdateFile(path) return in.updateFile(path)
}) })
if err != nil { if err != nil {
return err return err
@ -186,7 +186,7 @@ func (in *Index) Refresh() error {
i := 0 i := 0
for _, file := range in.Files { for _, file := range in.Files {
if file.fileExistsTemp { if file.fileExistsTemp {
// Keep file if it exists (already checked in UpdateFile) // Keep file if it exists (already checked in updateFile)
in.Files[i] = file in.Files[i] = file
i++ i++
} }
@ -197,6 +197,16 @@ func (in *Index) Refresh() error {
return nil return nil
} }
// RefreshFile calculates the hash for a given path and updates it in the index (also sorts the index)
func (in *Index) RefreshFile(path string) error {
err := in.updateFile(path)
if err != nil {
return err
}
in.resortIndex()
return nil
}
// Write saves the index file // Write saves the index file
func (in Index) Write() error { func (in Index) Write() error {
f, err := os.Create(in.indexFile) f, err := os.Create(in.indexFile)