aboutsummaryrefslogtreecommitdiffstats
path: root/config/branch.go
diff options
context:
space:
mode:
authorpaul.t <paul.t@gembaadvantage.com>2021-12-15 06:32:38 +0000
committerpaul.t <paul.t@gembaadvantage.com>2021-12-15 06:32:38 +0000
commit8d923a6fb77d7916da2df33e5a65e038cceef4e1 (patch)
tree5a823fa6e7ffd5e38df105cadcab5a0d6b7475e9 /config/branch.go
parentb0f5eb894deb6d6a1051d697f0809082abfad395 (diff)
parent53a714bdc90026135e2f2ada1c4d6c925b2733cd (diff)
downloadgo-git-8d923a6fb77d7916da2df33e5a65e038cceef4e1.tar.gz
Merge branch 'master' into codecommit-ref-delta
Diffstat (limited to 'config/branch.go')
-rw-r--r--config/branch.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/config/branch.go b/config/branch.go
index fe86cf5..652270a 100644
--- a/config/branch.go
+++ b/config/branch.go
@@ -2,6 +2,7 @@ package config
import (
"errors"
+ "strings"
"github.com/go-git/go-git/v5/plumbing"
format "github.com/go-git/go-git/v5/plumbing/format/config"
@@ -26,6 +27,12 @@ type Branch struct {
// "true" and "interactive". "false" is undocumented and
// typically represented by the non-existence of this field
Rebase string
+ // Description explains what the branch is for.
+ // Multi-line explanations may be used.
+ //
+ // Original git command to edit:
+ // git branch --edit-description
+ Description string
raw *format.Subsection
}
@@ -75,9 +82,27 @@ func (b *Branch) marshal() *format.Subsection {
b.raw.SetOption(rebaseKey, b.Rebase)
}
+ if b.Description == "" {
+ b.raw.RemoveOption(descriptionKey)
+ } else {
+ desc := quoteDescription(b.Description)
+ b.raw.SetOption(descriptionKey, desc)
+ }
+
return b.raw
}
+// hack to trigger conditional quoting in the
+// plumbing/format/config/Encoder.encodeOptions
+//
+// Current Encoder implementation uses Go %q format if value contains a backslash character,
+// which is not consistent with reference git implementation.
+// git just replaces newline characters with \n, while Encoder prints them directly.
+// Until value quoting fix, we should escape description value by replacing newline characters with \n.
+func quoteDescription(desc string) string {
+ return strings.ReplaceAll(desc, "\n", `\n`)
+}
+
func (b *Branch) unmarshal(s *format.Subsection) error {
b.raw = s
@@ -85,6 +110,14 @@ func (b *Branch) unmarshal(s *format.Subsection) error {
b.Remote = b.raw.Options.Get(remoteSection)
b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey))
b.Rebase = b.raw.Options.Get(rebaseKey)
+ b.Description = unquoteDescription(b.raw.Options.Get(descriptionKey))
return b.Validate()
}
+
+// hack to enable conditional quoting in the
+// plumbing/format/config/Encoder.encodeOptions
+// goto quoteDescription for details.
+func unquoteDescription(desc string) string {
+ return strings.ReplaceAll(desc, `\n`, "\n")
+}