diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-04-11 10:19:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-11 10:19:49 +0200 |
commit | 0db54e829f81a28f71c22d54c03daba5ec144c8d (patch) | |
tree | ef2271a5bea7a7c001700a1a2981d59370af978a /config/branch.go | |
parent | 409919132cfe037421fbd631338492d36e8ba89b (diff) | |
parent | 02335b10dee417d0338bf6ea070feeead18e636b (diff) | |
download | go-git-0db54e829f81a28f71c22d54c03daba5ec144c8d.tar.gz |
Merge pull request #803 from TheHipbot/branch-tracking-on-clonev4.3.0
config: adds branches to config for tracking branches against remotes…
Diffstat (limited to 'config/branch.go')
-rw-r--r-- | config/branch.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/config/branch.go b/config/branch.go new file mode 100644 index 0000000..e18073c --- /dev/null +++ b/config/branch.go @@ -0,0 +1,71 @@ +package config + +import ( + "errors" + + "gopkg.in/src-d/go-git.v4/plumbing" + format "gopkg.in/src-d/go-git.v4/plumbing/format/config" +) + +var ( + errBranchEmptyName = errors.New("branch config: empty name") + errBranchInvalidMerge = errors.New("branch config: invalid merge") +) + +// 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 + + 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 + } + + 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)) + } + + 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)) + + return b.Validate() +} |