Debian Codegraph @

config/branch.go

raw
1 package config
2
3 import (
4 "errors"
5 "strings"
6
7 "github.com/go-git/go-git/v5/plumbing"
8 format "github.com/go-git/go-git/v5/plumbing/format/config"
9 )
10
11 var (
12 errBranchEmptyName = errors.New("branch config: empty name")
13 errBranchInvalidMerge = errors.New("branch config: invalid merge")
14 errBranchInvalidRebase = errors.New("branch config: rebase must be one of 'true' or 'interactive'")
15 )
16
17 // Branch contains information on the
18 // local branches and which remote to track
19 type Branch struct {
20 // Name of branch
21 Name string
22 // Remote name of remote to track
23 Remote string
24 // Merge is the local refspec for the branch
25 Merge plumbing.ReferenceName
26 // Rebase instead of merge when pulling. Valid values are
27 // "true" and "interactive". "false" is undocumented and
28 // typically represented by the non-existence of this field
29 Rebase string
30 // Description explains what the branch is for.
31 // Multi-line explanations may be used.
32 //
33 // Original git command to edit:
34 // git branch --edit-description
35 Description string
36
37 raw *format.Subsection
38 }
39
40 // Validate validates fields of branch
41 func (b *Branch) Validate() error {
42 if b.Name == "" {
43 return errBranchEmptyName
44 }
45
46 if b.Merge != "" && !b.Merge.IsBranch() {
47 return errBranchInvalidMerge
48 }
49
50 if b.Rebase != "" &&
51 b.Rebase != "true" &&
52 b.Rebase != "interactive" &&
53 b.Rebase != "false" {
54 return errBranchInvalidRebase
55 }
56
57 return plumbing.NewBranchReferenceName(b.Name).Validate()
58 }
59
60 func (b *Branch) marshal() *format.Subsection {
61 if b.raw == nil {
62 b.raw = &format.Subsection{}
63 }
64
65 b.raw.Name = b.Name
66
67 if b.Remote == "" {
68 b.raw.RemoveOption(remoteSection)
69 } else {
70 b.raw.SetOption(remoteSection, b.Remote)
71 }
72
73 if b.Merge == "" {
74 b.raw.RemoveOption(mergeKey)
75 } else {
76 b.raw.SetOption(mergeKey, string(b.Merge))
77 }
78
79 if b.Rebase == "" {
80 b.raw.RemoveOption(rebaseKey)
81 } else {
82 b.raw.SetOption(rebaseKey, b.Rebase)
83 }
84
85 if b.Description == "" {
86 b.raw.RemoveOption(descriptionKey)
87 } else {
88 desc := quoteDescription(b.Description)
89 b.raw.SetOption(descriptionKey, desc)
90 }
91
92 return b.raw
93 }
94
95 // hack to trigger conditional quoting in the
96 // plumbing/format/config/Encoder.encodeOptions
97 //
98 // Current Encoder implementation uses Go %q format if value contains a backslash character,
99 // which is not consistent with reference git implementation.
100 // git just replaces newline characters with \n, while Encoder prints them directly.
101 // Until value quoting fix, we should escape description value by replacing newline characters with \n.
102 func quoteDescription(desc string) string {
103 return strings.ReplaceAll(desc, "\n", `\n`)
104 }
105
106 func (b *Branch) unmarshal(s *format.Subsection) error {
107 b.raw = s
108
109 b.Name = b.raw.Name
110 b.Remote = b.raw.Options.Get(remoteSection)
111 b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
112 b.Rebase = b.raw.Options.Get(rebaseKey)
113 b.Description = unquoteDescription(b.raw.Options.Get(descriptionKey))
114
115 return b.Validate()
116 }
117
118 // hack to enable conditional quoting in the
119 // plumbing/format/config/Encoder.encodeOptions
120 // goto quoteDescription for details.
121 func unquoteDescription(desc string) string {
122 return strings.ReplaceAll(desc, `\n`, "\n")
123 }