@@ -2,6 +2,7 @@ package config
2
2
3
3
import (
4
4
"errors"
5
+ "strings"
5
6
6
7
"github.com/go-git/go-git/v5/plumbing"
7
8
format "github.com/go-git/go-git/v5/plumbing/format/config"
@@ -26,6 +27,12 @@ type Branch struct {
26
27
// "true" and "interactive". "false" is undocumented and
27
28
// typically represented by the non-existence of this field
28
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
29
36
30
37
raw * format.Subsection
31
38
}
@@ -75,16 +82,42 @@ func (b *Branch) marshal() *format.Subsection {
75
82
b .raw .SetOption (rebaseKey , b .Rebase )
76
83
}
77
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
+
78
92
return b .raw
79
93
}
80
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
+
81
106
func (b * Branch ) unmarshal (s * format.Subsection ) error {
82
107
b .raw = s
83
108
84
109
b .Name = b .raw .Name
85
110
b .Remote = b .raw .Options .Get (remoteSection )
86
111
b .Merge = plumbing .ReferenceName (b .raw .Options .Get (mergeKey ))
87
112
b .Rebase = b .raw .Options .Get (rebaseKey )
113
+ b .Description = unquoteDescription (b .raw .Options .Get (descriptionKey ))
88
114
89
115
return b .Validate ()
90
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
+ }
0 commit comments