Skip to content

Commit

Permalink
CreateTagOptions.Validate: load Tagger from config
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Jun 5, 2020
1 parent 8019144 commit 531a81e
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 12 deletions.
37 changes: 36 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/go-git/go-git/v5/plumbing"
. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -172,8 +176,39 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) {

func (s *ConfigSuite) TestLoadConfig(c *C) {
cfg, err := LoadConfig(GlobalScope)
c.Assert(err, IsNil)
c.Assert(cfg.User.Email, Not(Equals), "")
c.Assert(err, IsNil)

}

func (s *ConfigSuite) TestLoadConfigXDG(c *C) {
cfg := NewConfig()
cfg.User.Name = "foo"
cfg.User.Email = "foo@foo.com"

tmp, err := ioutil.TempDir("", "test-commit-options")
c.Assert(err, IsNil)
defer os.RemoveAll(tmp)

err = os.Mkdir(filepath.Join(tmp, "git"), 0777)
c.Assert(err, IsNil)

os.Setenv("XDG_CONFIG_HOME", tmp)
defer func() {
os.Setenv("XDG_CONFIG_HOME", "")
}()

content, err := cfg.Marshal()
c.Assert(err, IsNil)

cfgFile := filepath.Join(tmp, "git/config")
err = ioutil.WriteFile(cfgFile, content, 0777)
c.Assert(err, IsNil)

cfg, err = LoadConfig(GlobalScope)
c.Assert(err, IsNil)

c.Assert(cfg.User.Email, Equals, "foo@foo.com")
}

func (s *ConfigSuite) TestValidateConfig(c *C) {
Expand Down
36 changes: 34 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ var (

// CreateTagOptions describes how a tag object should be created.
type CreateTagOptions struct {
// Tagger defines the signature of the tag creator.
// Tagger defines the signature of the tag creator. If Tagger is empty the
// Name and Email is read from the config, and time.Now it's used as When.
Tagger *object.Signature
// Message defines the annotation of the tag. It is canonicalized during
// validation into the format expected by git - no leading whitespace and
Expand All @@ -478,7 +479,9 @@ type CreateTagOptions struct {
// Validate validates the fields and sets the default values.
func (o *CreateTagOptions) Validate(r *Repository, hash plumbing.Hash) error {
if o.Tagger == nil {
return ErrMissingTagger
if err := o.loadConfigTagger(r); err != nil {
return err
}
}

if o.Message == "" {
Expand All @@ -491,6 +494,35 @@ func (o *CreateTagOptions) Validate(r *Repository, hash plumbing.Hash) error {
return nil
}

func (o *CreateTagOptions) loadConfigTagger(r *Repository) error {
cfg, err := r.ConfigScoped(config.SystemScope)
if err != nil {
return err
}

if o.Tagger == nil && cfg.Author.Email != "" && cfg.Author.Name != "" {
o.Tagger = &object.Signature{
Name: cfg.Author.Name,
Email: cfg.Author.Email,
When: time.Now(),
}
}

if o.Tagger == nil && cfg.User.Email != "" && cfg.User.Name != "" {
o.Tagger = &object.Signature{
Name: cfg.User.Name,
Email: cfg.User.Email,
When: time.Now(),
}
}

if o.Tagger == nil {
return ErrMissingTagger
}

return nil
}

// ListOptions describes how a remote list should be performed.
type ListOptions struct {
// Auth credentials, if required, to use with the remote repository.
Expand Down
84 changes: 84 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package git

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
. "gopkg.in/check.v1"
)
Expand All @@ -27,3 +33,81 @@ func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) {

c.Assert(o.Committer, Equals, o.Author)
}

func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) {
cfg := config.NewConfig()
cfg.User.Name = "foo"
cfg.User.Email = "foo@foo.com"

s.writeGlobalConfig(c, cfg)
defer s.clearGlobalConfig(c)

o := CommitOptions{}
err := o.Validate(s.Repository)
c.Assert(err, IsNil)

c.Assert(o.Author.Name, Equals, "foo")
c.Assert(o.Author.Email, Equals, "foo@foo.com")
c.Assert(o.Committer.Name, Equals, "foo")
c.Assert(o.Committer.Email, Equals, "foo@foo.com")
}

func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) {
cfg := config.NewConfig()
cfg.User.Name = "foo"
cfg.User.Email = "foo@foo.com"
cfg.Committer.Name = "bar"
cfg.Committer.Email = "bar@bar.com"

s.writeGlobalConfig(c, cfg)
defer s.clearGlobalConfig(c)

o := CommitOptions{}
err := o.Validate(s.Repository)
c.Assert(err, IsNil)

c.Assert(o.Author.Name, Equals, "foo")
c.Assert(o.Author.Email, Equals, "foo@foo.com")
c.Assert(o.Committer.Name, Equals, "bar")
c.Assert(o.Committer.Email, Equals, "bar@bar.com")
}

func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) {
cfg := config.NewConfig()
cfg.User.Name = "foo"
cfg.User.Email = "foo@foo.com"

s.writeGlobalConfig(c, cfg)
defer s.clearGlobalConfig(c)

o := CreateTagOptions{
Message: "foo",
}

err := o.Validate(s.Repository, plumbing.ZeroHash)
c.Assert(err, IsNil)

c.Assert(o.Tagger.Name, Equals, "foo")
c.Assert(o.Tagger.Email, Equals, "foo@foo.com")
}

func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) {
tmp, err := ioutil.TempDir("", "test-options")
c.Assert(err, IsNil)

err = os.Mkdir(filepath.Join(tmp, "git"), 0777)
c.Assert(err, IsNil)

os.Setenv("XDG_CONFIG_HOME", tmp)

content, err := cfg.Marshal()
c.Assert(err, IsNil)

cfgFile := filepath.Join(tmp, "git/config")
err = ioutil.WriteFile(cfgFile, content, 0777)
c.Assert(err, IsNil)
}

func (s *OptionsSuite) clearGlobalConfig(c *C) {
os.Setenv("XDG_CONFIG_HOME", "")
}
10 changes: 1 addition & 9 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2103,15 +2103,7 @@ func (s *RepositorySuite) TestCreateTagAnnotatedBadOpts(c *C) {

expectedHash := h.Hash()

ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{
Message: "foo bar baz qux",
})
c.Assert(ref, IsNil)
c.Assert(err, Equals, ErrMissingTagger)

ref, err = r.CreateTag("foobar", expectedHash, &CreateTagOptions{
Tagger: defaultSignature(),
})
ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{})
c.Assert(ref, IsNil)
c.Assert(err, Equals, ErrMissingMessage)
}
Expand Down

0 comments on commit 531a81e

Please sign in to comment.