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