2018-11-28 05:52:20 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-06-19 10:14:15 +08:00
|
|
|
errBranchEmptyName = errors.New("branch config: empty name")
|
|
|
|
errBranchInvalidMerge = errors.New("branch config: invalid merge")
|
|
|
|
errBranchInvalidRebase = errors.New("branch config: rebase must be one of 'true' or 'interactive'")
|
2018-11-28 05:52:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Branch contains information on the
|
|
|
|
// local branches and which remote to track
|
|
|
|
type Branch struct {
|
|
|
|
// Name of branch
|
|
|
|
Name string
|
|
|
|
// Remote name of remote to track
|
|
|
|
Remote string
|
|
|
|
// Merge is the local refspec for the branch
|
|
|
|
Merge plumbing.ReferenceName
|
2019-06-19 10:14:15 +08:00
|
|
|
// Rebase instead of merge when pulling. Valid values are
|
|
|
|
// "true" and "interactive". "false" is undocumented and
|
|
|
|
// typically represented by the non-existence of this field
|
|
|
|
Rebase string
|
2018-11-28 05:52:20 +08:00
|
|
|
|
|
|
|
raw *format.Subsection
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates fields of branch
|
|
|
|
func (b *Branch) Validate() error {
|
|
|
|
if b.Name == "" {
|
|
|
|
return errBranchEmptyName
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Merge != "" && !b.Merge.IsBranch() {
|
|
|
|
return errBranchInvalidMerge
|
|
|
|
}
|
|
|
|
|
2019-06-19 10:14:15 +08:00
|
|
|
if b.Rebase != "" &&
|
|
|
|
b.Rebase != "true" &&
|
|
|
|
b.Rebase != "interactive" &&
|
|
|
|
b.Rebase != "false" {
|
|
|
|
return errBranchInvalidRebase
|
|
|
|
}
|
|
|
|
|
2018-11-28 05:52:20 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Branch) marshal() *format.Subsection {
|
|
|
|
if b.raw == nil {
|
|
|
|
b.raw = &format.Subsection{}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.raw.Name = b.Name
|
|
|
|
|
|
|
|
if b.Remote == "" {
|
|
|
|
b.raw.RemoveOption(remoteSection)
|
|
|
|
} else {
|
|
|
|
b.raw.SetOption(remoteSection, b.Remote)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Merge == "" {
|
|
|
|
b.raw.RemoveOption(mergeKey)
|
|
|
|
} else {
|
|
|
|
b.raw.SetOption(mergeKey, string(b.Merge))
|
|
|
|
}
|
|
|
|
|
2019-06-19 10:14:15 +08:00
|
|
|
if b.Rebase == "" {
|
|
|
|
b.raw.RemoveOption(rebaseKey)
|
|
|
|
} else {
|
|
|
|
b.raw.SetOption(rebaseKey, string(b.Rebase))
|
|
|
|
}
|
|
|
|
|
2018-11-28 05:52:20 +08:00
|
|
|
return b.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Branch) unmarshal(s *format.Subsection) error {
|
|
|
|
b.raw = s
|
|
|
|
|
|
|
|
b.Name = b.raw.Name
|
|
|
|
b.Remote = b.raw.Options.Get(remoteSection)
|
|
|
|
b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
|
2019-06-19 10:14:15 +08:00
|
|
|
b.Rebase = b.raw.Options.Get(rebaseKey)
|
2018-11-28 05:52:20 +08:00
|
|
|
|
|
|
|
return b.Validate()
|
|
|
|
}
|