本站源代码
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
1.2KB

  1. package filesystem
  2. import (
  3. "bufio"
  4. "fmt"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
  7. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  8. )
  9. // ShallowStorage where the shallow commits are stored, an internal to
  10. // manipulate the shallow file
  11. type ShallowStorage struct {
  12. dir *dotgit.DotGit
  13. }
  14. // SetShallow save the shallows in the shallow file in the .git folder as one
  15. // commit per line represented by 40-byte hexadecimal object terminated by a
  16. // newline.
  17. func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
  18. f, err := s.dir.ShallowWriter()
  19. if err != nil {
  20. return err
  21. }
  22. defer ioutil.CheckClose(f, &err)
  23. for _, h := range commits {
  24. if _, err := fmt.Fprintf(f, "%s\n", h); err != nil {
  25. return err
  26. }
  27. }
  28. return err
  29. }
  30. // Shallow return the shallow commits reading from shallo file from .git
  31. func (s *ShallowStorage) Shallow() ([]plumbing.Hash, error) {
  32. f, err := s.dir.Shallow()
  33. if f == nil || err != nil {
  34. return nil, err
  35. }
  36. defer ioutil.CheckClose(f, &err)
  37. var hash []plumbing.Hash
  38. scn := bufio.NewScanner(f)
  39. for scn.Scan() {
  40. hash = append(hash, plumbing.NewHash(scn.Text()))
  41. }
  42. return hash, scn.Err()
  43. }
上海开阖软件有限公司 沪ICP备12045867号-1