aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/config/encoder.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2022-11-17 04:34:31 +0100
committerGitHub <noreply@github.com>2022-11-17 04:34:31 +0100
commitacd6c657ada037bcc8dcdb1f4e77898c90613ffb (patch)
treeb03f7a6bb30aa0ad0080b9314f7cce4bf4a382ec /plumbing/format/config/encoder.go
parent71f6540e90b9a8cd3d4e3455b7d1d53c60a39d16 (diff)
parent4efe4cbee9e0631d92ad91db23f1271058d03a46 (diff)
downloadgo-git-acd6c657ada037bcc8dcdb1f4e77898c90613ffb.tar.gz
Merge pull request #354 from dowy/issue/#309-clone-branch-with-hash-in-name
plumbing: config, Branch name with hash can be cloned. Fixes #309
Diffstat (limited to 'plumbing/format/config/encoder.go')
-rw-r--r--plumbing/format/config/encoder.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/plumbing/format/config/encoder.go b/plumbing/format/config/encoder.go
index 4eac896..de069ae 100644
--- a/plumbing/format/config/encoder.go
+++ b/plumbing/format/config/encoder.go
@@ -11,6 +11,10 @@ type Encoder struct {
w io.Writer
}
+var (
+ subsectionReplacer = strings.NewReplacer(`"`, `\"`, `\`, `\\`)
+ valueReplacer = strings.NewReplacer(`"`, `\"`, `\`, `\\`, "\n", `\n`, "\t", `\t`, "\b", `\b`)
+)
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
@@ -48,8 +52,7 @@ func (e *Encoder) encodeSection(s *Section) error {
}
func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error {
- //TODO: escape
- if err := e.printf("[%s \"%s\"]\n", sectionName, s.Name); err != nil {
+ if err := e.printf("[%s \"%s\"]\n", sectionName, subsectionReplacer.Replace(s.Name)); err != nil {
return err
}
@@ -58,12 +61,14 @@ func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error {
func (e *Encoder) encodeOptions(opts Options) error {
for _, o := range opts {
- pattern := "\t%s = %s\n"
- if strings.Contains(o.Value, "\\") {
- pattern = "\t%s = %q\n"
+ var value string
+ if strings.ContainsAny(o.Value, "#;\"\t\n\\") || strings.HasPrefix(o.Value, " ") || strings.HasSuffix(o.Value, " ") {
+ value = `"`+valueReplacer.Replace(o.Value)+`"`
+ } else {
+ value = o.Value
}
- if err := e.printf(pattern, o.Key, o.Value); err != nil {
+ if err := e.printf("\t%s = %s\n", o.Key, value); err != nil {
return err
}
}