diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-05-24 09:41:19 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2020-05-24 09:41:19 +0200 |
commit | 16207827862af21b6e822df9aafd0007ba19a36f (patch) | |
tree | b6f0dac17077fd9c0825af6cf02b861d40db5df5 | |
parent | c80979499a2177f3e172b6d89dabaa0bf64e81d1 (diff) | |
download | go-git-16207827862af21b6e822df9aafd0007ba19a36f.tar.gz |
config: Config.[User|Author|Commit] marshal/unmarshal
-rw-r--r-- | config/config.go | 71 | ||||
-rw-r--r-- | config/config_test.go | 40 |
2 files changed, 103 insertions, 8 deletions
diff --git a/config/config.go b/config/config.go index bec35b0..e17ec5d 100644 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,27 @@ type Config struct { CommentChar string } + User struct { + // Name is the personal name of the author and the commiter of a commit. + Name string + // Email is the email of the author and the commiter of a commit. + Email string + } + + Author struct { + // Name is the personal name of the author of a commit. + Name string + // Email is the email of the author of a commit. + Email string + } + + Committer struct { + // Name is the personal name of the commiter of a commit. + Name string + // Email is the email of the the commiter of a commit. + Email string + } + Pack struct { // Window controls the size of the sliding window for delta // compression. The default is 10. A value of 0 turns off @@ -113,6 +134,9 @@ const ( branchSection = "branch" coreSection = "core" packSection = "pack" + userSection = "user" + authorSection = "author" + committerSection = "committer" fetchKey = "fetch" urlKey = "url" bareKey = "bare" @@ -121,6 +145,8 @@ const ( windowKey = "window" mergeKey = "merge" rebaseKey = "rebase" + nameKey = "name" + emailKey = "email" // DefaultPackWindow holds the number of previous objects used to // generate deltas. The value 10 is the same used by git command. @@ -138,6 +164,7 @@ func (c *Config) Unmarshal(b []byte) error { } c.unmarshalCore() + c.unmarshalUser() if err := c.unmarshalPack(); err != nil { return err } @@ -160,6 +187,20 @@ func (c *Config) unmarshalCore() { c.Core.CommentChar = s.Options.Get(commentCharKey) } +func (c *Config) unmarshalUser() { + s := c.Raw.Section(userSection) + c.User.Name = s.Options.Get(nameKey) + c.User.Email = s.Options.Get(emailKey) + + s = c.Raw.Section(authorSection) + c.Author.Name = s.Options.Get(nameKey) + c.Author.Email = s.Options.Get(emailKey) + + s = c.Raw.Section(committerSection) + c.Committer.Name = s.Options.Get(nameKey) + c.Committer.Email = s.Options.Get(emailKey) +} + func (c *Config) unmarshalPack() error { s := c.Raw.Section(packSection) window := s.Options.Get(windowKey) @@ -220,6 +261,7 @@ func (c *Config) unmarshalBranches() error { // Marshal returns Config encoded as a git-config file. func (c *Config) Marshal() ([]byte, error) { c.marshalCore() + c.marshalUser() c.marshalPack() c.marshalRemotes() c.marshalSubmodules() @@ -242,6 +284,35 @@ func (c *Config) marshalCore() { } } +func (c *Config) marshalUser() { + s := c.Raw.Section(userSection) + if c.User.Name != "" { + s.SetOption(nameKey, c.User.Name) + } + + if c.User.Email != "" { + s.SetOption(emailKey, c.User.Email) + } + + s = c.Raw.Section(authorSection) + if c.Author.Name != "" { + s.SetOption(nameKey, c.Author.Name) + } + + if c.Author.Email != "" { + s.SetOption(emailKey, c.Author.Email) + } + + s = c.Raw.Section(committerSection) + if c.Committer.Name != "" { + s.SetOption(nameKey, c.Committer.Name) + } + + if c.Committer.Email != "" { + s.SetOption(emailKey, c.Committer.Email) + } +} + func (c *Config) marshalPack() { s := c.Raw.Section(packSection) if c.Pack.Window != DefaultPackWindow { diff --git a/config/config_test.go b/config/config_test.go index e5e3be5..157be95 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,8 +1,8 @@ package config import ( - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + . "gopkg.in/check.v1" ) type ConfigSuite struct{} @@ -11,14 +11,23 @@ var _ = Suite(&ConfigSuite{}) func (s *ConfigSuite) TestUnmarshal(c *C) { input := []byte(`[core] - bare = true + bare = true worktree = foo commentchar = bar +[user] + name = John Doe + email = john@example.com +[author] + name = Jane Roe + email = jane@example.com +[committer] + name = Richard Roe + email = richard@example.com [pack] window = 20 [remote "origin"] - url = git@github.com:mcuadros/go-git.git - fetch = +refs/heads/*:refs/remotes/origin/* + url = git@github.com:mcuadros/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* [remote "alt"] url = git@github.com:mcuadros/go-git.git url = git@github.com:src-d/go-git.git @@ -27,12 +36,12 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { [remote "win-local"] url = X:\\Git\\ [submodule "qux"] - path = qux - url = https://github.com/foo/qux.git + path = qux + url = https://github.com/foo/qux.git branch = bar [branch "master"] - remote = origin - merge = refs/heads/master + remote = origin + merge = refs/heads/master `) cfg := NewConfig() @@ -42,6 +51,12 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { c.Assert(cfg.Core.IsBare, Equals, true) c.Assert(cfg.Core.Worktree, Equals, "foo") c.Assert(cfg.Core.CommentChar, Equals, "bar") + c.Assert(cfg.User.Name, Equals, "John Doe") + c.Assert(cfg.User.Email, Equals, "john@example.com") + c.Assert(cfg.Author.Name, Equals, "Jane Roe") + c.Assert(cfg.Author.Email, Equals, "jane@example.com") + c.Assert(cfg.Committer.Name, Equals, "Richard Roe") + c.Assert(cfg.Committer.Email, Equals, "richard@example.com") c.Assert(cfg.Pack.Window, Equals, uint(20)) c.Assert(cfg.Remotes, HasLen, 3) c.Assert(cfg.Remotes["origin"].Name, Equals, "origin") @@ -124,6 +139,15 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { bare = true worktree = foo custom = ignored +[user] + name = John Doe + email = john@example.com +[author] + name = Jane Roe + email = jane@example.com +[committer] + name = Richard Roe + email = richard@example.co [pack] window = 20 [remote "origin"] |