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.

71 lines
1.3 KiB

  1. package config
  2. import (
  3. "errors"
  4. "gopkg.in/src-d/go-git.v4/plumbing"
  5. format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
  6. )
  7. var (
  8. errBranchEmptyName = errors.New("branch config: empty name")
  9. errBranchInvalidMerge = errors.New("branch config: invalid merge")
  10. )
  11. // Branch contains information on the
  12. // local branches and which remote to track
  13. type Branch struct {
  14. // Name of branch
  15. Name string
  16. // Remote name of remote to track
  17. Remote string
  18. // Merge is the local refspec for the branch
  19. Merge plumbing.ReferenceName
  20. raw *format.Subsection
  21. }
  22. // Validate validates fields of branch
  23. func (b *Branch) Validate() error {
  24. if b.Name == "" {
  25. return errBranchEmptyName
  26. }
  27. if b.Merge != "" && !b.Merge.IsBranch() {
  28. return errBranchInvalidMerge
  29. }
  30. return nil
  31. }
  32. func (b *Branch) marshal() *format.Subsection {
  33. if b.raw == nil {
  34. b.raw = &format.Subsection{}
  35. }
  36. b.raw.Name = b.Name
  37. if b.Remote == "" {
  38. b.raw.RemoveOption(remoteSection)
  39. } else {
  40. b.raw.SetOption(remoteSection, b.Remote)
  41. }
  42. if b.Merge == "" {
  43. b.raw.RemoveOption(mergeKey)
  44. } else {
  45. b.raw.SetOption(mergeKey, string(b.Merge))
  46. }
  47. return b.raw
  48. }
  49. func (b *Branch) unmarshal(s *format.Subsection) error {
  50. b.raw = s
  51. b.Name = b.raw.Name
  52. b.Remote = b.raw.Options.Get(remoteSection)
  53. b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
  54. return b.Validate()
  55. }