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.

branch.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package config
  2. import (
  3. "errors"
  4. "github.com/go-git/go-git/v5/plumbing"
  5. format "github.com/go-git/go-git/v5/plumbing/format/config"
  6. )
  7. var (
  8. errBranchEmptyName = errors.New("branch config: empty name")
  9. errBranchInvalidMerge = errors.New("branch config: invalid merge")
  10. errBranchInvalidRebase = errors.New("branch config: rebase must be one of 'true' or 'interactive'")
  11. )
  12. // Branch contains information on the
  13. // local branches and which remote to track
  14. type Branch struct {
  15. // Name of branch
  16. Name string
  17. // Remote name of remote to track
  18. Remote string
  19. // Merge is the local refspec for the branch
  20. Merge plumbing.ReferenceName
  21. // Rebase instead of merge when pulling. Valid values are
  22. // "true" and "interactive". "false" is undocumented and
  23. // typically represented by the non-existence of this field
  24. Rebase string
  25. raw *format.Subsection
  26. }
  27. // Validate validates fields of branch
  28. func (b *Branch) Validate() error {
  29. if b.Name == "" {
  30. return errBranchEmptyName
  31. }
  32. if b.Merge != "" && !b.Merge.IsBranch() {
  33. return errBranchInvalidMerge
  34. }
  35. if b.Rebase != "" &&
  36. b.Rebase != "true" &&
  37. b.Rebase != "interactive" &&
  38. b.Rebase != "false" {
  39. return errBranchInvalidRebase
  40. }
  41. return nil
  42. }
  43. func (b *Branch) marshal() *format.Subsection {
  44. if b.raw == nil {
  45. b.raw = &format.Subsection{}
  46. }
  47. b.raw.Name = b.Name
  48. if b.Remote == "" {
  49. b.raw.RemoveOption(remoteSection)
  50. } else {
  51. b.raw.SetOption(remoteSection, b.Remote)
  52. }
  53. if b.Merge == "" {
  54. b.raw.RemoveOption(mergeKey)
  55. } else {
  56. b.raw.SetOption(mergeKey, string(b.Merge))
  57. }
  58. if b.Rebase == "" {
  59. b.raw.RemoveOption(rebaseKey)
  60. } else {
  61. b.raw.SetOption(rebaseKey, b.Rebase)
  62. }
  63. return b.raw
  64. }
  65. func (b *Branch) unmarshal(s *format.Subsection) error {
  66. b.raw = s
  67. b.Name = b.raw.Name
  68. b.Remote = b.raw.Options.Get(remoteSection)
  69. b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
  70. b.Rebase = b.raw.Options.Get(rebaseKey)
  71. return b.Validate()
  72. }