fork of go-git with some jj specific features
1package git
2
3import (
4 "os"
5
6 "github.com/go-git/go-billy/v5/util"
7 "github.com/go-git/go-git/v5/config"
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/object"
10 . "gopkg.in/check.v1"
11)
12
13type OptionsSuite struct {
14 BaseSuite
15}
16
17var _ = Suite(&OptionsSuite{})
18
19func (s *OptionsSuite) TestCommitOptionsParentsFromHEAD(c *C) {
20 o := CommitOptions{Author: &object.Signature{}}
21 err := o.Validate(s.Repository)
22 c.Assert(err, IsNil)
23 c.Assert(o.Parents, HasLen, 1)
24}
25
26func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) {
27 sig := &object.Signature{}
28
29 o := CommitOptions{Author: sig}
30 err := o.Validate(s.Repository)
31 c.Assert(err, IsNil)
32
33 c.Assert(o.Committer, Equals, o.Author)
34}
35
36func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) {
37 cfg := config.NewConfig()
38 cfg.User.Name = "foo"
39 cfg.User.Email = "foo@foo.com"
40
41 clean := s.writeGlobalConfig(c, cfg)
42 defer clean()
43
44 o := CommitOptions{}
45 err := o.Validate(s.Repository)
46 c.Assert(err, IsNil)
47
48 c.Assert(o.Author.Name, Equals, "foo")
49 c.Assert(o.Author.Email, Equals, "foo@foo.com")
50 c.Assert(o.Committer.Name, Equals, "foo")
51 c.Assert(o.Committer.Email, Equals, "foo@foo.com")
52}
53
54func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) {
55 cfg := config.NewConfig()
56 cfg.User.Name = "foo"
57 cfg.User.Email = "foo@foo.com"
58 cfg.Committer.Name = "bar"
59 cfg.Committer.Email = "bar@bar.com"
60
61 clean := s.writeGlobalConfig(c, cfg)
62 defer clean()
63
64 o := CommitOptions{}
65 err := o.Validate(s.Repository)
66 c.Assert(err, IsNil)
67
68 c.Assert(o.Author.Name, Equals, "foo")
69 c.Assert(o.Author.Email, Equals, "foo@foo.com")
70 c.Assert(o.Committer.Name, Equals, "bar")
71 c.Assert(o.Committer.Email, Equals, "bar@bar.com")
72}
73
74func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) {
75 cfg := config.NewConfig()
76 cfg.User.Name = "foo"
77 cfg.User.Email = "foo@foo.com"
78
79 clean := s.writeGlobalConfig(c, cfg)
80 defer clean()
81
82 o := CreateTagOptions{
83 Message: "foo",
84 }
85
86 err := o.Validate(s.Repository, plumbing.ZeroHash)
87 c.Assert(err, IsNil)
88
89 c.Assert(o.Tagger.Name, Equals, "foo")
90 c.Assert(o.Tagger.Email, Equals, "foo@foo.com")
91}
92
93func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) func() {
94 fs, clean := s.TemporalFilesystem()
95
96 tmp, err := util.TempDir(fs, "", "test-options")
97 c.Assert(err, IsNil)
98
99 err = fs.MkdirAll(fs.Join(tmp, "git"), 0777)
100 c.Assert(err, IsNil)
101
102 os.Setenv("XDG_CONFIG_HOME", fs.Join(fs.Root(), tmp))
103
104 content, err := cfg.Marshal()
105 c.Assert(err, IsNil)
106
107 cfgFile := fs.Join(tmp, "git/config")
108 err = util.WriteFile(fs, cfgFile, content, 0777)
109 c.Assert(err, IsNil)
110
111 return func() {
112 clean()
113 os.Setenv("XDG_CONFIG_HOME", "")
114
115 }
116}