aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-06-05 11:42:42 +0200
committerGitHub <noreply@github.com>2020-06-05 11:42:42 +0200
commit96a108e35075afd13e81df9ba2bf96c8c6125283 (patch)
treebad8ba6004f59497b00dd4edf098766a375f7beb
parent8019144b6534ff58ad234a355e5b143f1c99b45e (diff)
parent531a81eae684d8cae617d3d9a26923a66eb25d40 (diff)
downloadgo-git-96a108e35075afd13e81df9ba2bf96c8c6125283.tar.gz
Merge pull request #84 from go-git/tag-author
CreateTagOptions.Validate: load Tagger from config
-rw-r--r--config/config_test.go37
-rw-r--r--options.go36
-rw-r--r--options_test.go84
-rw-r--r--repository_test.go10
4 files changed, 155 insertions, 12 deletions
diff --git a/config/config_test.go b/config/config_test.go
index e68626b..5a88c19 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -1,6 +1,10 @@
package config
import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+
"github.com/go-git/go-git/v5/plumbing"
. "gopkg.in/check.v1"
)
@@ -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) {
diff --git a/options.go b/options.go
index 5367031..fd347d3 100644
--- a/options.go
+++ b/options.go
@@ -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
@@ -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 == "" {
@@ -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.
diff --git a/options_test.go b/options_test.go
index aa36dab..86d725a 100644
--- a/options_test.go
+++ b/options_test.go
@@ -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"
)
@@ -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", "")
+}
diff --git a/repository_test.go b/repository_test.go
index 37cd914..d1af7b6 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -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)
}