mirror of
https://github.com/go-gitea/gitea
synced 2024-12-26 18:44:27 +00:00
a9b4c8171f
* update gopkg.in/src-d/go-git.v4 v4.13.0 * mod tidy * vendor
55 lines
912 B
Go
55 lines
912 B
Go
package filesystem
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
|
|
"gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
|
|
"gopkg.in/src-d/go-git.v4/utils/ioutil"
|
|
)
|
|
|
|
type IndexStorage struct {
|
|
dir *dotgit.DotGit
|
|
}
|
|
|
|
func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
|
|
f, err := s.dir.IndexWriter()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer ioutil.CheckClose(f, &err)
|
|
bw := bufio.NewWriter(f)
|
|
defer func() {
|
|
if e := bw.Flush(); err == nil && e != nil {
|
|
err = e
|
|
}
|
|
}()
|
|
|
|
e := index.NewEncoder(bw)
|
|
err = e.Encode(idx)
|
|
return err
|
|
}
|
|
|
|
func (s *IndexStorage) Index() (i *index.Index, err error) {
|
|
idx := &index.Index{
|
|
Version: 2,
|
|
}
|
|
|
|
f, err := s.dir.Index()
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return idx, nil
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
defer ioutil.CheckClose(f, &err)
|
|
|
|
d := index.NewDecoder(bufio.NewReader(f))
|
|
err = d.Decode(idx)
|
|
return idx, err
|
|
}
|