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

67 line
1.6KB

  1. package git
  2. import (
  3. "errors"
  4. "time"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/storer"
  7. )
  8. type PruneHandler func(unreferencedObjectHash plumbing.Hash) error
  9. type PruneOptions struct {
  10. // OnlyObjectsOlderThan if set to non-zero value
  11. // selects only objects older than the time provided.
  12. OnlyObjectsOlderThan time.Time
  13. // Handler is called on matching objects
  14. Handler PruneHandler
  15. }
  16. var ErrLooseObjectsNotSupported = errors.New("Loose objects not supported")
  17. // DeleteObject deletes an object from a repository.
  18. // The type conveniently matches PruneHandler.
  19. func (r *Repository) DeleteObject(hash plumbing.Hash) error {
  20. los, ok := r.Storer.(storer.LooseObjectStorer)
  21. if !ok {
  22. return ErrLooseObjectsNotSupported
  23. }
  24. return los.DeleteLooseObject(hash)
  25. }
  26. func (r *Repository) Prune(opt PruneOptions) error {
  27. los, ok := r.Storer.(storer.LooseObjectStorer)
  28. if !ok {
  29. return ErrLooseObjectsNotSupported
  30. }
  31. pw := newObjectWalker(r.Storer)
  32. err := pw.walkAllRefs()
  33. if err != nil {
  34. return err
  35. }
  36. // Now walk all (loose) objects in storage.
  37. return los.ForEachObjectHash(func(hash plumbing.Hash) error {
  38. // Get out if we have seen this object.
  39. if pw.isSeen(hash) {
  40. return nil
  41. }
  42. // Otherwise it is a candidate for pruning.
  43. // Check out for too new objects next.
  44. if !opt.OnlyObjectsOlderThan.IsZero() {
  45. // Errors here are non-fatal. The object may be e.g. packed.
  46. // Or concurrently deleted. Skip such objects.
  47. t, err := los.LooseObjectTime(hash)
  48. if err != nil {
  49. return nil
  50. }
  51. // Skip too new objects.
  52. if !t.Before(opt.OnlyObjectsOlderThan) {
  53. return nil
  54. }
  55. }
  56. return opt.Handler(hash)
  57. })
  58. }
上海开阖软件有限公司 沪ICP备12045867号-1