Skip to content

Commit 96a108e

Browse files
authored
Merge pull request go-git#84 from go-git/tag-author
CreateTagOptions.Validate: load Tagger from config
2 parents 8019144 + 531a81e commit 96a108e

File tree

4 files changed

+155
-12
lines changed

4 files changed

+155
-12
lines changed

config/config_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package config
22

33
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
48
"github.com/go-git/go-git/v5/plumbing"
59
. "gopkg.in/check.v1"
610
)
@@ -172,8 +176,39 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) {
172176

173177
func (s *ConfigSuite) TestLoadConfig(c *C) {
174178
cfg, err := LoadConfig(GlobalScope)
175-
c.Assert(err, IsNil)
176179
c.Assert(cfg.User.Email, Not(Equals), "")
180+
c.Assert(err, IsNil)
181+
182+
}
183+
184+
func (s *ConfigSuite) TestLoadConfigXDG(c *C) {
185+
cfg := NewConfig()
186+
cfg.User.Name = "foo"
187+
cfg.User.Email = "[email protected]"
188+
189+
tmp, err := ioutil.TempDir("", "test-commit-options")
190+
c.Assert(err, IsNil)
191+
defer os.RemoveAll(tmp)
192+
193+
err = os.Mkdir(filepath.Join(tmp, "git"), 0777)
194+
c.Assert(err, IsNil)
195+
196+
os.Setenv("XDG_CONFIG_HOME", tmp)
197+
defer func() {
198+
os.Setenv("XDG_CONFIG_HOME", "")
199+
}()
200+
201+
content, err := cfg.Marshal()
202+
c.Assert(err, IsNil)
203+
204+
cfgFile := filepath.Join(tmp, "git/config")
205+
err = ioutil.WriteFile(cfgFile, content, 0777)
206+
c.Assert(err, IsNil)
207+
208+
cfg, err = LoadConfig(GlobalScope)
209+
c.Assert(err, IsNil)
210+
211+
c.Assert(cfg.User.Email, Equals, "[email protected]")
177212
}
178213

179214
func (s *ConfigSuite) TestValidateConfig(c *C) {

options.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ var (
464464

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

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

497+
func (o *CreateTagOptions) loadConfigTagger(r *Repository) error {
498+
cfg, err := r.ConfigScoped(config.SystemScope)
499+
if err != nil {
500+
return err
501+
}
502+
503+
if o.Tagger == nil && cfg.Author.Email != "" && cfg.Author.Name != "" {
504+
o.Tagger = &object.Signature{
505+
Name: cfg.Author.Name,
506+
Email: cfg.Author.Email,
507+
When: time.Now(),
508+
}
509+
}
510+
511+
if o.Tagger == nil && cfg.User.Email != "" && cfg.User.Name != "" {
512+
o.Tagger = &object.Signature{
513+
Name: cfg.User.Name,
514+
Email: cfg.User.Email,
515+
When: time.Now(),
516+
}
517+
}
518+
519+
if o.Tagger == nil {
520+
return ErrMissingTagger
521+
}
522+
523+
return nil
524+
}
525+
494526
// ListOptions describes how a remote list should be performed.
495527
type ListOptions struct {
496528
// Auth credentials, if required, to use with the remote repository.

options_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package git
22

33
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/go-git/go-git/v5/config"
9+
"github.com/go-git/go-git/v5/plumbing"
410
"github.com/go-git/go-git/v5/plumbing/object"
511
. "gopkg.in/check.v1"
612
)
@@ -27,3 +33,81 @@ func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) {
2733

2834
c.Assert(o.Committer, Equals, o.Author)
2935
}
36+
37+
func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) {
38+
cfg := config.NewConfig()
39+
cfg.User.Name = "foo"
40+
cfg.User.Email = "[email protected]"
41+
42+
s.writeGlobalConfig(c, cfg)
43+
defer s.clearGlobalConfig(c)
44+
45+
o := CommitOptions{}
46+
err := o.Validate(s.Repository)
47+
c.Assert(err, IsNil)
48+
49+
c.Assert(o.Author.Name, Equals, "foo")
50+
c.Assert(o.Author.Email, Equals, "[email protected]")
51+
c.Assert(o.Committer.Name, Equals, "foo")
52+
c.Assert(o.Committer.Email, Equals, "[email protected]")
53+
}
54+
55+
func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) {
56+
cfg := config.NewConfig()
57+
cfg.User.Name = "foo"
58+
cfg.User.Email = "[email protected]"
59+
cfg.Committer.Name = "bar"
60+
cfg.Committer.Email = "[email protected]"
61+
62+
s.writeGlobalConfig(c, cfg)
63+
defer s.clearGlobalConfig(c)
64+
65+
o := CommitOptions{}
66+
err := o.Validate(s.Repository)
67+
c.Assert(err, IsNil)
68+
69+
c.Assert(o.Author.Name, Equals, "foo")
70+
c.Assert(o.Author.Email, Equals, "[email protected]")
71+
c.Assert(o.Committer.Name, Equals, "bar")
72+
c.Assert(o.Committer.Email, Equals, "[email protected]")
73+
}
74+
75+
func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) {
76+
cfg := config.NewConfig()
77+
cfg.User.Name = "foo"
78+
cfg.User.Email = "[email protected]"
79+
80+
s.writeGlobalConfig(c, cfg)
81+
defer s.clearGlobalConfig(c)
82+
83+
o := CreateTagOptions{
84+
Message: "foo",
85+
}
86+
87+
err := o.Validate(s.Repository, plumbing.ZeroHash)
88+
c.Assert(err, IsNil)
89+
90+
c.Assert(o.Tagger.Name, Equals, "foo")
91+
c.Assert(o.Tagger.Email, Equals, "[email protected]")
92+
}
93+
94+
func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) {
95+
tmp, err := ioutil.TempDir("", "test-options")
96+
c.Assert(err, IsNil)
97+
98+
err = os.Mkdir(filepath.Join(tmp, "git"), 0777)
99+
c.Assert(err, IsNil)
100+
101+
os.Setenv("XDG_CONFIG_HOME", tmp)
102+
103+
content, err := cfg.Marshal()
104+
c.Assert(err, IsNil)
105+
106+
cfgFile := filepath.Join(tmp, "git/config")
107+
err = ioutil.WriteFile(cfgFile, content, 0777)
108+
c.Assert(err, IsNil)
109+
}
110+
111+
func (s *OptionsSuite) clearGlobalConfig(c *C) {
112+
os.Setenv("XDG_CONFIG_HOME", "")
113+
}

repository_test.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -2103,15 +2103,7 @@ func (s *RepositorySuite) TestCreateTagAnnotatedBadOpts(c *C) {
21032103

21042104
expectedHash := h.Hash()
21052105

2106-
ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{
2107-
Message: "foo bar baz qux",
2108-
})
2109-
c.Assert(ref, IsNil)
2110-
c.Assert(err, Equals, ErrMissingTagger)
2111-
2112-
ref, err = r.CreateTag("foobar", expectedHash, &CreateTagOptions{
2113-
Tagger: defaultSignature(),
2114-
})
2106+
ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{})
21152107
c.Assert(ref, IsNil)
21162108
c.Assert(err, Equals, ErrMissingMessage)
21172109
}

0 commit comments

Comments
 (0)