本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
912B

  1. package filesystem
  2. import (
  3. "bufio"
  4. "os"
  5. "gopkg.in/src-d/go-git.v4/plumbing/format/index"
  6. "gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
  7. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  8. )
  9. type IndexStorage struct {
  10. dir *dotgit.DotGit
  11. }
  12. func (s *IndexStorage) SetIndex(idx *index.Index) (err error) {
  13. f, err := s.dir.IndexWriter()
  14. if err != nil {
  15. return err
  16. }
  17. defer ioutil.CheckClose(f, &err)
  18. bw := bufio.NewWriter(f)
  19. defer func() {
  20. if e := bw.Flush(); err == nil && e != nil {
  21. err = e
  22. }
  23. }()
  24. e := index.NewEncoder(bw)
  25. err = e.Encode(idx)
  26. return err
  27. }
  28. func (s *IndexStorage) Index() (i *index.Index, err error) {
  29. idx := &index.Index{
  30. Version: 2,
  31. }
  32. f, err := s.dir.Index()
  33. if err != nil {
  34. if os.IsNotExist(err) {
  35. return idx, nil
  36. }
  37. return nil, err
  38. }
  39. defer ioutil.CheckClose(f, &err)
  40. d := index.NewDecoder(bufio.NewReader(f))
  41. err = d.Decode(idx)
  42. return idx, err
  43. }
上海开阖软件有限公司 沪ICP备12045867号-1