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

91 lines
1.9KB

  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. 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. }
上海开阖软件有限公司 沪ICP备12045867号-1