Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add branch description support #409

Merged
merged 3 commits into from Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions config/branch.go
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -75,16 +82,42 @@ 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

b.Name = b.raw.Name
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")
}
1 change: 1 addition & 0 deletions config/config.go
Expand Up @@ -247,6 +247,7 @@ const (
rebaseKey = "rebase"
nameKey = "name"
emailKey = "email"
descriptionKey = "description"
defaultBranchKey = "defaultBranch"

// DefaultPackWindow holds the number of previous objects used to
Expand Down
11 changes: 8 additions & 3 deletions config/config_test.go
Expand Up @@ -50,6 +50,7 @@ func (s *ConfigSuite) TestUnmarshal(c *C) {
[branch "master"]
remote = origin
merge = refs/heads/master
description = "Add support for branch description.\\n\\nEdit branch description: git branch --edit-description\\n"
[init]
defaultBranch = main
[url "ssh://git@github.com/"]
Expand Down Expand Up @@ -86,6 +87,7 @@ func (s *ConfigSuite) TestUnmarshal(c *C) {
c.Assert(cfg.Submodules["qux"].Branch, Equals, "bar")
c.Assert(cfg.Branches["master"].Remote, Equals, "origin")
c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master"))
c.Assert(cfg.Branches["master"].Description, Equals, "Add support for branch description.\n\nEdit branch description: git branch --edit-description\n")
c.Assert(cfg.Init.DefaultBranch, Equals, "main")
}

Expand All @@ -111,6 +113,7 @@ func (s *ConfigSuite) TestMarshal(c *C) {
[branch "master"]
remote = origin
merge = refs/heads/master
description = "Add support for branch description.\\n\\nEdit branch description: git branch --edit-description\\n"
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
[init]
Expand Down Expand Up @@ -149,9 +152,10 @@ func (s *ConfigSuite) TestMarshal(c *C) {
}

cfg.Branches["master"] = &Branch{
Name: "master",
Remote: "origin",
Merge: "refs/heads/master",
Name: "master",
Remote: "origin",
Merge: "refs/heads/master",
Description: "Add support for branch description.\n\nEdit branch description: git branch --edit-description\n",
}

cfg.URLs["ssh://git@github.com/"] = &URL{
Expand Down Expand Up @@ -364,4 +368,5 @@ func (s *ConfigSuite) TestRemoveUrlOptions(c *C) {
if strings.Contains(string(buf), "url") {
c.Fatal("conifg should not contain any url sections")
}
c.Assert(err, IsNil)
}