From b939cf8471d8c1aac1960a833c7275e5b79ed013 Mon Sep 17 00:00:00 2001 From: merlin Date: Sun, 7 Nov 2021 20:39:51 +0300 Subject: config: add support for branch description --- config/branch.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'config/branch.go') diff --git a/config/branch.go b/config/branch.go index fe86cf5..a8fe0b5 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,20 @@ 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 } +func quoteDescription(desc string) string { + return strings.ReplaceAll(desc, "\n", `\n`) +} + func (b *Branch) unmarshal(s *format.Subsection) error { b.raw = s @@ -85,6 +103,11 @@ 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() } + +func unquoteDescription(desc string) string { + return strings.ReplaceAll(desc, `\n`, "\n") +} -- cgit From efc74e7730b7cfd72ae66602815e6acd67b2c01a Mon Sep 17 00:00:00 2001 From: merlin Date: Mon, 8 Nov 2021 00:33:17 +0300 Subject: config: describe reason for custom quote functions --- config/branch.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'config/branch.go') diff --git a/config/branch.go b/config/branch.go index a8fe0b5..652270a 100644 --- a/config/branch.go +++ b/config/branch.go @@ -92,6 +92,13 @@ func (b *Branch) marshal() *format.Subsection { 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`) } @@ -108,6 +115,9 @@ func (b *Branch) unmarshal(s *format.Subsection) error { 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") } -- cgit