diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2021-05-02 23:40:08 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2021-05-02 23:40:08 +0200 |
commit | 01df7536992af375a54bbedf45369a475953e372 (patch) | |
tree | 8435050f4388f15b3a85d135a3f7d11b1ff61540 | |
parent | 67af9d7223b0cc643025d958c592291f7475ac75 (diff) | |
download | go-git-01df7536992af375a54bbedf45369a475953e372.tar.gz |
*: use go-billy instead of os calls
-rw-r--r-- | common_test.go | 31 | ||||
-rw-r--r-- | config/config.go | 3 | ||||
-rw-r--r-- | config/config_test.go | 20 | ||||
-rw-r--r-- | go.mod | 11 | ||||
-rw-r--r-- | go.sum | 64 | ||||
-rw-r--r-- | options_test.go | 37 | ||||
-rw-r--r-- | plumbing/protocol/packp/advrefs_test.go | 67 | ||||
-rw-r--r-- | plumbing/protocol/packp/ulreq_encode_test.go | 5 | ||||
-rw-r--r-- | plumbing/protocol/packp/ulreq_test.go | 79 | ||||
-rw-r--r-- | plumbing/transport/ssh/auth_method.go | 2 | ||||
-rw-r--r-- | plumbing/transport/ssh/auth_method_test.go | 26 | ||||
-rw-r--r-- | plumbing/transport/ssh/upload_pack_test.go | 5 | ||||
-rw-r--r-- | remote_test.go | 88 | ||||
-rw-r--r-- | repository.go | 55 | ||||
-rw-r--r-- | repository_test.go | 263 | ||||
-rw-r--r-- | storage/filesystem/config_test.go | 7 | ||||
-rw-r--r-- | storage/filesystem/dotgit/dotgit_test.go | 117 | ||||
-rw-r--r-- | storage/filesystem/dotgit/repository_filesystem_test.go | 15 | ||||
-rw-r--r-- | storage/filesystem/dotgit/writers_test.go | 37 | ||||
-rw-r--r-- | storage/filesystem/object_test.go | 8 | ||||
-rw-r--r-- | storage/filesystem/storage_test.go | 23 | ||||
-rw-r--r-- | submodule_test.go | 5 | ||||
-rw-r--r-- | worktree_commit_test.go | 21 | ||||
-rw-r--r-- | worktree_js.go | 26 | ||||
-rw-r--r-- | worktree_test.go | 74 |
25 files changed, 521 insertions, 568 deletions
diff --git a/common_test.go b/common_test.go index 8154359..5f5bc4c 100644 --- a/common_test.go +++ b/common_test.go @@ -1,6 +1,7 @@ package git import ( + "os" "testing" "github.com/go-git/go-git/v5/plumbing" @@ -12,6 +13,7 @@ import ( "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" + "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/util" fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" @@ -131,6 +133,35 @@ func (s *BaseSuite) GetLocalRepositoryURL(f *fixtures.Fixture) string { return f.DotGit().Root() } +func (s *BaseSuite) TemporalDir() (path string, clean func()) { + fs := osfs.New(os.TempDir()) + path, err := util.TempDir(fs, "", "") + if err != nil { + panic(err) + } + + return fs.Join(fs.Root(), path), func() { + util.RemoveAll(fs, path) + } +} + +func (s *BaseSuite) TemporalFilesystem() (fs billy.Filesystem, clean func()) { + fs = osfs.New(os.TempDir()) + path, err := util.TempDir(fs, "", "") + if err != nil { + panic(err) + } + + fs, err = fs.Chroot(path) + if err != nil { + panic(err) + } + + return fs, func() { + util.RemoveAll(fs, path) + } +} + type SuiteCommon struct{} var _ = Suite(&SuiteCommon{}) diff --git a/config/config.go b/config/config.go index 1f737b5..1aee25a 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ import ( "sort" "strconv" + "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5/internal/url" format "github.com/go-git/go-git/v5/plumbing/format/config" "github.com/mitchellh/go-homedir" @@ -158,7 +159,7 @@ func LoadConfig(scope Scope) (*Config, error) { } for _, file := range files { - f, err := os.Open(file) + f, err := osfs.Default.Open(file) if err != nil { if os.IsNotExist(err) { continue diff --git a/config/config_test.go b/config/config_test.go index b4a9ac3..6f0242d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,11 +1,12 @@ package config import ( - "io/ioutil" "os" "path/filepath" "strings" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" "github.com/go-git/go-git/v5/plumbing" . "gopkg.in/check.v1" ) @@ -154,7 +155,7 @@ func (s *ConfigSuite) TestMarshal(c *C) { } cfg.URLs["ssh://git@github.com/"] = &URL{ - Name: "ssh://git@github.com/", + Name: "ssh://git@github.com/", InsteadOf: "https://github.com/", } @@ -204,23 +205,16 @@ func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { c.Assert(string(output), DeepEquals, string(input)) } -func (s *ConfigSuite) TestLoadConfig(c *C) { - cfg, err := LoadConfig(GlobalScope) - 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") + tmp, err := util.TempDir(osfs.Default, "", "test-commit-options") c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + defer util.RemoveAll(osfs.Default, tmp) - err = os.Mkdir(filepath.Join(tmp, "git"), 0777) + err = osfs.Default.MkdirAll(filepath.Join(tmp, "git"), 0777) c.Assert(err, IsNil) os.Setenv("XDG_CONFIG_HOME", tmp) @@ -232,7 +226,7 @@ func (s *ConfigSuite) TestLoadConfigXDG(c *C) { c.Assert(err, IsNil) cfgFile := filepath.Join(tmp, "git/config") - err = ioutil.WriteFile(cfgFile, content, 0777) + err = util.WriteFile(osfs.Default, cfgFile, content, 0777) c.Assert(err, IsNil) cfg, err = LoadConfig(GlobalScope) @@ -2,15 +2,16 @@ module github.com/go-git/go-git/v5 require ( github.com/Microsoft/go-winio v0.4.16 // indirect - github.com/ProtonMail/go-crypto v0.0.0-20210329181949-3900d675f39b + github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 + github.com/acomagu/bufpipe v1.0.3 github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 github.com/emirpasic/gods v1.12.0 github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect github.com/gliderlabs/ssh v0.2.2 github.com/go-git/gcfg v1.5.0 - github.com/go-git/go-billy/v5 v5.1.0 - github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12 + github.com/go-git/go-billy/v5 v5.3.0 + github.com/go-git/go-git-fixtures/v4 v4.2.0 github.com/google/go-cmp v0.3.0 github.com/imdario/mergo v0.3.12 github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 @@ -19,9 +20,9 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/sergi/go-diff v1.1.0 github.com/xanzy/ssh-agent v0.3.0 - golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b golang.org/x/net v0.0.0-20210326060303-6b1517762897 - golang.org/x/sys v0.0.0-20210415045647-66c3f260301c // indirect + golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 golang.org/x/text v0.3.3 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c gopkg.in/warnings.v0 v0.1.2 // indirect @@ -1,10 +1,10 @@ github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk= github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= -github.com/ProtonMail/go-crypto v0.0.0-20210329181949-3900d675f39b h1:E0jcApeWTn0zEUOANmwLg2k3IfTIyX4ffz2l95AEIBk= -github.com/ProtonMail/go-crypto v0.0.0-20210329181949-3900d675f39b/go.mod h1:HTM9X7e9oLwn7RiqLG0UVwVRJenLs3wN+tQ0NPAfwMQ= -github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= -github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ= +github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= +github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= +github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -21,46 +21,36 @@ github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= -github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM= -github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= -github.com/go-git/go-billy/v5 v5.1.0 h1:4pl5BV4o7ZG/lterP4S6WzJ6xr49Ba5ET9ygheTYahk= -github.com/go-git/go-billy/v5 v5.1.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= -github.com/go-git/go-git-fixtures/v4 v4.0.1 h1:q+IFMfLx200Q3scvt2hN79JsEzy4AmBTp/pqnefH+Bc= -github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= -github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12 h1:PbKy9zOy4aAKrJ5pibIRpVO2BXnK1Tlcg+caKI7Ox5M= -github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= +github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-billy/v5 v5.3.0 h1:KZL1OFdS+afiIjN4hr/zpj5cEtC0OJhbmTA18PsBb8c= +github.com/go-git/go-billy/v5 v5.3.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git-fixtures/v4 v4.2.0 h1:6fkid8QaA0iWWZoRIMR8lY+nTS/T+ajMKXjPV3DNqyQ= +github.com/go-git/go-git-fixtures/v4 v4.2.0/go.mod h1:qiUdjIecVv4CVoAxqy1kQBCZXfi/MhoyOiItV+0ppBk= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= -github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= -github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= -github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= +github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -72,55 +62,37 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= -github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= -golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs= golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4= -golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E= +golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/options_test.go b/options_test.go index 86d725a..171222c 100644 --- a/options_test.go +++ b/options_test.go @@ -1,10 +1,9 @@ package git import ( - "io/ioutil" "os" - "path/filepath" + "github.com/go-git/go-billy/v5/util" "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" @@ -39,8 +38,8 @@ func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) { cfg.User.Name = "foo" cfg.User.Email = "foo@foo.com" - s.writeGlobalConfig(c, cfg) - defer s.clearGlobalConfig(c) + clean := s.writeGlobalConfig(c, cfg) + defer clean() o := CommitOptions{} err := o.Validate(s.Repository) @@ -59,8 +58,8 @@ func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) { cfg.Committer.Name = "bar" cfg.Committer.Email = "bar@bar.com" - s.writeGlobalConfig(c, cfg) - defer s.clearGlobalConfig(c) + clean := s.writeGlobalConfig(c, cfg) + defer clean() o := CommitOptions{} err := o.Validate(s.Repository) @@ -77,8 +76,8 @@ func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) { cfg.User.Name = "foo" cfg.User.Email = "foo@foo.com" - s.writeGlobalConfig(c, cfg) - defer s.clearGlobalConfig(c) + clean := s.writeGlobalConfig(c, cfg) + defer clean() o := CreateTagOptions{ Message: "foo", @@ -91,23 +90,27 @@ func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) { c.Assert(o.Tagger.Email, Equals, "foo@foo.com") } -func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) { - tmp, err := ioutil.TempDir("", "test-options") +func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) func() { + fs, clean := s.TemporalFilesystem() + + tmp, err := util.TempDir(fs, "", "test-options") c.Assert(err, IsNil) - err = os.Mkdir(filepath.Join(tmp, "git"), 0777) + err = fs.MkdirAll(fs.Join(tmp, "git"), 0777) c.Assert(err, IsNil) - os.Setenv("XDG_CONFIG_HOME", tmp) + os.Setenv("XDG_CONFIG_HOME", fs.Join(fs.Root(), tmp)) content, err := cfg.Marshal() c.Assert(err, IsNil) - cfgFile := filepath.Join(tmp, "git/config") - err = ioutil.WriteFile(cfgFile, content, 0777) + cfgFile := fs.Join(tmp, "git/config") + err = util.WriteFile(fs, cfgFile, content, 0777) c.Assert(err, IsNil) -} -func (s *OptionsSuite) clearGlobalConfig(c *C) { - os.Setenv("XDG_CONFIG_HOME", "") + return func() { + clean() + os.Setenv("XDG_CONFIG_HOME", "") + + } } diff --git a/plumbing/protocol/packp/advrefs_test.go b/plumbing/protocol/packp/advrefs_test.go index d163e1f..1b8db98 100644 --- a/plumbing/protocol/packp/advrefs_test.go +++ b/plumbing/protocol/packp/advrefs_test.go @@ -2,9 +2,7 @@ package packp import ( "bytes" - "fmt" "io" - "strings" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/pktline" @@ -380,68 +378,3 @@ func (s *AdvRefsDecodeEncodeSuite) TestAllSmartBug(c *C) { s.test(c, input, expected, false) } - -func ExampleAdvRefs_Decode() { - // Here is a raw advertised-ref message. - raw := "" + - "0065a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n" + - "003fa6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n" + - "00441111111111111111111111111111111111111111 refs/tags/v2.6.11-tree\n" + - "00475555555555555555555555555555555555555555 refs/tags/v2.6.11-tree^{}\n" + - "0035shallow 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c\n" + - "0000" - - // Use the raw message as our input. - input := strings.NewReader(raw) - - // Decode the input into a newly allocated AdvRefs value. - ar := NewAdvRefs() - _ = ar.Decode(input) // error check ignored for brevity - - // Do something interesting with the AdvRefs, e.g. print its contents. - fmt.Println("head =", ar.Head) - fmt.Println("capabilities =", ar.Capabilities.String()) - fmt.Println("...") - fmt.Println("shallows =", ar.Shallows) - // Output: head = a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 - // capabilities = multi_ack ofs-delta symref=HEAD:/refs/heads/master - // ... - // shallows = [5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c] -} - -func ExampleAdvRefs_Encode() { - // Create an AdvRefs with the contents you want... - ar := NewAdvRefs() - - // ...add a hash for the HEAD... - head := plumbing.NewHash("1111111111111111111111111111111111111111") - ar.Head = &head - - // ...add some server capabilities... - ar.Capabilities.Add(capability.MultiACK) - ar.Capabilities.Add(capability.OFSDelta) - ar.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master") - - // ...add a couple of references... - ar.References["refs/heads/master"] = plumbing.NewHash("2222222222222222222222222222222222222222") - ar.References["refs/tags/v1"] = plumbing.NewHash("3333333333333333333333333333333333333333") - - // ...including a peeled ref... - ar.Peeled["refs/tags/v1"] = plumbing.NewHash("4444444444444444444444444444444444444444") - - // ...and finally add a shallow - ar.Shallows = append(ar.Shallows, plumbing.NewHash("5555555555555555555555555555555555555555")) - - // Encode the packpContents to a bytes.Buffer. - // You can encode into stdout too, but you will not be able - // see the '\x00' after "HEAD". - var buf bytes.Buffer - _ = ar.Encode(&buf) // error checks ignored for brevity - - // Print the contents of the buffer as a quoted string. - // Printing is as a non-quoted string will be prettier but you - // will miss the '\x00' after "HEAD". - fmt.Printf("%q", buf.String()) - // Output: - // "00651111111111111111111111111111111111111111 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n003f2222222222222222222222222222222222222222 refs/heads/master\n003a3333333333333333333333333333333333333333 refs/tags/v1\n003d4444444444444444444444444444444444444444 refs/tags/v1^{}\n0035shallow 5555555555555555555555555555555555555555\n0000" -} diff --git a/plumbing/protocol/packp/ulreq_encode_test.go b/plumbing/protocol/packp/ulreq_encode_test.go index a16e321..ba6df1a 100644 --- a/plumbing/protocol/packp/ulreq_encode_test.go +++ b/plumbing/protocol/packp/ulreq_encode_test.go @@ -2,6 +2,7 @@ package packp import ( "bytes" + "runtime" "time" "github.com/go-git/go-git/v5/plumbing" @@ -236,6 +237,10 @@ func (s *UlReqEncodeSuite) TestDepthSinceUTC(c *C) { } func (s *UlReqEncodeSuite) TestDepthSinceNonUTC(c *C) { + if runtime.GOOS == "js" { + c.Skip("time.LoadLocation not supported in wasm") + } + ur := NewUploadRequest() ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) berlin, err := time.LoadLocation("Europe/Berlin") diff --git a/plumbing/protocol/packp/ulreq_test.go b/plumbing/protocol/packp/ulreq_test.go index a0bb401..0b3b616 100644 --- a/plumbing/protocol/packp/ulreq_test.go +++ b/plumbing/protocol/packp/ulreq_test.go @@ -1,13 +1,9 @@ package packp import ( - "fmt" - "os" - "strings" "time" "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-git/v5/plumbing/format/pktline" "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" . "gopkg.in/check.v1" @@ -111,78 +107,3 @@ func (s *UlReqSuite) TestValidateConflictMultiACK(c *C) { err := r.Validate() c.Assert(err, NotNil) } - -func ExampleUploadRequest_Encode() { - // Create an empty UlReq with the contents you want... - ur := NewUploadRequest() - - // Add a couple of wants - ur.Wants = append(ur.Wants, plumbing.NewHash("3333333333333333333333333333333333333333")) - ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) - ur.Wants = append(ur.Wants, plumbing.NewHash("2222222222222222222222222222222222222222")) - - // And some capabilities you will like the server to use - ur.Capabilities.Add(capability.OFSDelta) - ur.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master") - - // Add a couple of shallows - ur.Shallows = append(ur.Shallows, plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) - ur.Shallows = append(ur.Shallows, plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - - // And retrict the answer of the server to commits newer than "2015-01-02 03:04:05 UTC" - since := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC) - ur.Depth = DepthSince(since) - - // Create a new Encode for the stdout... - e := newUlReqEncoder(os.Stdout) - // ...and encode the upload-request to it. - _ = e.Encode(ur) // ignoring errors for brevity - // Output: - // 005bwant 1111111111111111111111111111111111111111 ofs-delta symref=HEAD:/refs/heads/master - // 0032want 2222222222222222222222222222222222222222 - // 0032want 3333333333333333333333333333333333333333 - // 0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - // 0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb - // 001cdeepen-since 1420167845 - // 0000 -} - -func ExampleUploadRequest_Decode() { - // Here is a raw advertised-ref message. - raw := "" + - "005bwant 1111111111111111111111111111111111111111 ofs-delta symref=HEAD:/refs/heads/master\n" + - "0032want 2222222222222222222222222222222222222222\n" + - "0032want 3333333333333333333333333333333333333333\n" + - "0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + - "0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" + - "001cdeepen-since 1420167845\n" + // 2015-01-02 03:04:05 +0000 UTC - pktline.FlushString - - // Use the raw message as our input. - input := strings.NewReader(raw) - - // Create the Decoder reading from our input. - d := newUlReqDecoder(input) - - // Decode the input into a newly allocated UlReq value. - ur := NewUploadRequest() - _ = d.Decode(ur) // error check ignored for brevity - - // Do something interesting with the UlReq, e.g. print its contents. - fmt.Println("capabilities =", ur.Capabilities.String()) - fmt.Println("wants =", ur.Wants) - fmt.Println("shallows =", ur.Shallows) - switch depth := ur.Depth.(type) { - case DepthCommits: - fmt.Println("depth =", int(depth)) - case DepthSince: - fmt.Println("depth =", time.Time(depth)) - case DepthReference: - fmt.Println("depth =", string(depth)) - } - // Output: - // capabilities = ofs-delta symref=HEAD:/refs/heads/master - // wants = [1111111111111111111111111111111111111111 2222222222222222222222222222222222222222 3333333333333333333333333333333333333333] - // shallows = [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] - // depth = 2015-01-02 03:04:05 +0000 UTC -} diff --git a/plumbing/transport/ssh/auth_method.go b/plumbing/transport/ssh/auth_method.go index 568ec86..3514669 100644 --- a/plumbing/transport/ssh/auth_method.go +++ b/plumbing/transport/ssh/auth_method.go @@ -11,7 +11,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" "github.com/mitchellh/go-homedir" - "github.com/xanzy/ssh-agent" + sshagent "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" ) diff --git a/plumbing/transport/ssh/auth_method_test.go b/plumbing/transport/ssh/auth_method_test.go index ade344f..b275018 100644 --- a/plumbing/transport/ssh/auth_method_test.go +++ b/plumbing/transport/ssh/auth_method_test.go @@ -3,10 +3,12 @@ package ssh import ( "bufio" "fmt" - "io/ioutil" "os" + "runtime" "strings" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/testdata" @@ -108,6 +110,10 @@ func (s *SuiteCommon) TestPublicKeysCallbackString(c *C) { c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysCallbackName)) } func (s *SuiteCommon) TestNewSSHAgentAuth(c *C) { + if runtime.GOOS == "js" { + c.Skip("tcp connections are not available in wasm") + } + if os.Getenv("SSH_AUTH_SOCK") == "" { c.Skip("SSH_AUTH_SOCK or SSH_TEST_PRIVATE_KEY are required") } @@ -153,12 +159,16 @@ func (*SuiteCommon) TestNewPublicKeysWithEncryptedEd25519PEM(c *C) { } func (*SuiteCommon) TestNewPublicKeysFromFile(c *C) { - f, err := ioutil.TempFile("", "ssh-test") + if runtime.GOOS == "js" { + c.Skip("not available in wasm") + } + + f, err := util.TempFile(osfs.Default, "", "ssh-test") c.Assert(err, IsNil) _, err = f.Write(testdata.PEMBytes["rsa"]) c.Assert(err, IsNil) c.Assert(f.Close(), IsNil) - defer os.RemoveAll(f.Name()) + defer osfs.Default.Remove(f.Name()) auth, err := NewPublicKeysFromFile("foo", f.Name(), "") c.Assert(err, IsNil) @@ -172,9 +182,13 @@ func (*SuiteCommon) TestNewPublicKeysWithInvalidPEM(c *C) { } func (*SuiteCommon) TestNewKnownHostsCallback(c *C) { + if runtime.GOOS == "js" { + c.Skip("not available in wasm") + } + var mock = mockKnownHosts{} - f, err := ioutil.TempFile("", "known-hosts") + f, err := util.TempFile(osfs.Default, "", "known-hosts") c.Assert(err, IsNil) _, err = f.Write(mock.knownHosts()) @@ -183,9 +197,9 @@ func (*SuiteCommon) TestNewKnownHostsCallback(c *C) { err = f.Close() c.Assert(err, IsNil) - defer os.RemoveAll(f.Name()) + defer util.RemoveAll(osfs.Default, f.Name()) - f, err = os.Open(f.Name()) + f, err = osfs.Default.Open(f.Name()) c.Assert(err, IsNil) defer f.Close() diff --git a/plumbing/transport/ssh/upload_pack_test.go b/plumbing/transport/ssh/upload_pack_test.go index 1bcb82b..e65e04a 100644 --- a/plumbing/transport/ssh/upload_pack_test.go +++ b/plumbing/transport/ssh/upload_pack_test.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "sync" @@ -32,6 +33,10 @@ type UploadPackSuite struct { var _ = Suite(&UploadPackSuite{}) func (s *UploadPackSuite) SetUpSuite(c *C) { + if runtime.GOOS == "js" { + c.Skip("tcp connections are not available in wasm") + } + l, err := net.Listen("tcp", "localhost:0") c.Assert(err, IsNil) diff --git a/remote_test.go b/remote_test.go index b39f5df..2cd2a6e 100644 --- a/remote_test.go +++ b/remote_test.go @@ -5,8 +5,6 @@ import ( "context" "errors" "io" - "io/ioutil" - "os" "runtime" "time" @@ -20,7 +18,6 @@ import ( "github.com/go-git/go-git/v5/storage/filesystem" "github.com/go-git/go-git/v5/storage/memory" - "github.com/go-git/go-billy/v5/osfs" fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" ) @@ -284,21 +281,17 @@ func (m *mockPackfileWriter) PackfileWriter() (io.WriteCloser, error) { } func (s *RemoteSuite) TestFetchWithPackfileWriter(c *C) { - dir, err := ioutil.TempDir("", "fetch") - c.Assert(err, IsNil) - - defer os.RemoveAll(dir) // clean up - - fss := filesystem.NewStorage(osfs.New(dir), cache.NewObjectLRUDefault()) - c.Assert(err, IsNil) + fs, clean := s.TemporalFilesystem() + defer clean() + fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) mock := &mockPackfileWriter{Storer: fss} url := s.GetBasicLocalRepositoryURL() r := NewRemote(mock, &config.RemoteConfig{Name: "foo", URLs: []string{url}}) refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*") - err = r.Fetch(&FetchOptions{ + err := r.Fetch(&FetchOptions{ RefSpecs: []config.RefSpec{refspec}, }) @@ -421,12 +414,10 @@ func (s *RemoteSuite) TestFetchFastForwardMem(c *C) { } func (s *RemoteSuite) TestFetchFastForwardFS(c *C) { - dir, err := ioutil.TempDir("", "fetch") - c.Assert(err, IsNil) + fs, clean := s.TemporalFilesystem() + defer clean() - defer os.RemoveAll(dir) // clean up - - fss := filesystem.NewStorage(osfs.New(dir), cache.NewObjectLRUDefault()) + fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) // This exercises `storage.filesystem.Storage.CheckAndSetReference()`. s.testFetchFastForward(c, fss) @@ -445,7 +436,9 @@ func (s *RemoteSuite) TestString(c *C) { } func (s *RemoteSuite) TestPushToEmptyRepository(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -482,7 +475,9 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) { } func (s *RemoteSuite) TestPushContext(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -511,7 +506,9 @@ func (s *RemoteSuite) TestPushContext(c *C) { } func (s *RemoteSuite) TestPushContextCanceled(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -540,7 +537,9 @@ func (s *RemoteSuite) TestPushContextCanceled(c *C) { } func (s *RemoteSuite) TestPushTags(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -585,7 +584,10 @@ func (s *RemoteSuite) TestPushDeleteReference(c *C) { fs := fixtures.Basic().One().DotGit() sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + url, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) @@ -609,7 +611,10 @@ func (s *RemoteSuite) TestForcePushDeleteReference(c *C) { fs := fixtures.Basic().One().DotGit() sto := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + url, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) @@ -634,7 +639,10 @@ func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) { fs := fixtures.Basic().One().DotGit() server := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + url, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) @@ -714,13 +722,19 @@ func (s *RemoteSuite) TestPushForceWithOption(c *C) { func (s *RemoteSuite) TestPushPrune(c *C) { fs := fixtures.Basic().One().DotGit() - url := c.MkDir() + + url, clean := s.TemporalDir() + defer clean() + server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, true, &CloneOptions{ URL: url, }) c.Assert(err, IsNil) @@ -771,13 +785,19 @@ func (s *RemoteSuite) TestPushPrune(c *C) { func (s *RemoteSuite) TestPushNewReference(c *C) { fs := fixtures.Basic().One().DotGit() - url := c.MkDir() + + url, clean := s.TemporalDir() + defer clean() + server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, true, &CloneOptions{ URL: url, }) c.Assert(err, IsNil) @@ -804,13 +824,19 @@ func (s *RemoteSuite) TestPushNewReference(c *C) { func (s *RemoteSuite) TestPushNewReferenceAndDeleteInBatch(c *C) { fs := fixtures.Basic().One().DotGit() - url := c.MkDir() + + url, clean := s.TemporalDir() + defer clean() + server, err := PlainClone(url, true, &CloneOptions{ URL: fs.Root(), }) c.Assert(err, IsNil) - r, err := PlainClone(c.MkDir(), true, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, true, &CloneOptions{ URL: url, }) c.Assert(err, IsNil) @@ -1007,7 +1033,9 @@ func (s *RemoteSuite) TestUpdateShallows(c *C) { } func (s *RemoteSuite) TestUseRefDeltas(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + _, err := PlainInit(url, true) c.Assert(err, IsNil) diff --git a/repository.go b/repository.go index 601244b..d3fbf97 100644 --- a/repository.go +++ b/repository.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "errors" "fmt" - "io" stdioutil "io/ioutil" "os" "path" @@ -14,9 +13,10 @@ import ( "strings" "time" - "github.com/go-git/go-git/v5/storage/filesystem/dotgit" - "github.com/ProtonMail/go-crypto/openpgp" + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/internal/revision" "github.com/go-git/go-git/v5/plumbing" @@ -26,11 +26,9 @@ import ( "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/storage" "github.com/go-git/go-git/v5/storage/filesystem" + "github.com/go-git/go-git/v5/storage/filesystem/dotgit" "github.com/go-git/go-git/v5/utils/ioutil" "github.com/imdario/mergo" - - "github.com/go-git/go-billy/v5" - "github.com/go-git/go-billy/v5/osfs" ) // GitDirName this is a special folder where all the git stuff is. @@ -275,17 +273,18 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, return nil, nil, err } - pathinfo, err := os.Stat(path) - if !os.IsNotExist(err) { - if !pathinfo.IsDir() && detect { - path = filepath.Dir(path) - } - } - var fs billy.Filesystem var fi os.FileInfo for { fs = osfs.New(path) + + pathinfo, err := fs.Stat("/") + if !os.IsNotExist(err) { + if !pathinfo.IsDir() && detect { + fs = osfs.New(filepath.Dir(path)) + } + } + fi, err = fs.Stat(GitDirName) if err == nil { // no error; stop @@ -429,7 +428,7 @@ func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository { } func checkIfCleanupIsNeeded(path string) (cleanup bool, cleanParent bool, err error) { - fi, err := os.Stat(path) + fi, err := osfs.Default.Stat(path) if err != nil { if os.IsNotExist(err) { return true, true, nil @@ -442,44 +441,30 @@ func checkIfCleanupIsNeeded(path string) (cleanup bool, cleanParent bool, err er return false, false, fmt.Errorf("path is not a directory: %s", path) } - f, err := os.Open(path) + files, err := osfs.Default.ReadDir(path) if err != nil { return false, false, err } - defer ioutil.CheckClose(f, &err) - - _, err = f.Readdirnames(1) - if err == io.EOF { + if len(files) == 0 { return true, false, nil } - if err != nil { - return false, false, err - } - return false, false, nil } func cleanUpDir(path string, all bool) error { if all { - return os.RemoveAll(path) + return util.RemoveAll(osfs.Default, path) } - f, err := os.Open(path) + files, err := osfs.Default.ReadDir(path) if err != nil { return err } - defer ioutil.CheckClose(f, &err) - - names, err := f.Readdirnames(-1) - if err != nil { - return err - } - - for _, name := range names { - if err := os.RemoveAll(filepath.Join(path, name)); err != nil { + for _, fi := range files { + if err := util.RemoveAll(osfs.Default, osfs.Default.Join(path, fi.Name())); err != nil { return err } } @@ -890,11 +875,13 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error { Name: branchName, Merge: branchRef, } + if o.RemoteName == "" { b.Remote = "origin" } else { b.Remote = o.RemoteName } + if err := r.CreateBranch(b); err != nil { return err } diff --git a/repository_test.go b/repository_test.go index fc98fbc..2bc5c90 100644 --- a/repository_test.go +++ b/repository_test.go @@ -31,6 +31,7 @@ import ( "github.com/go-git/go-git/v5/storage/filesystem" "github.com/go-git/go-git/v5/storage/memory" + "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/util" @@ -54,9 +55,8 @@ func (s *RepositorySuite) TestInit(c *C) { } func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { - dir, err := ioutil.TempDir("", "init-non-standard") - c.Assert(err, IsNil) - c.Assert(os.RemoveAll(dir), IsNil) + dir, clean := s.TemporalDir() + defer clean() fs := osfs.New(dir) dot, _ := fs.Chroot("storage") @@ -80,9 +80,8 @@ func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) { } func (s *RepositorySuite) TestInitStandardDotGit(c *C) { - dir, err := ioutil.TempDir("", "init-standard") - c.Assert(err, IsNil) - c.Assert(os.RemoveAll(dir), IsNil) + dir, clean := s.TemporalDir() + defer clean() fs := osfs.New(dir) dot, _ := fs.Chroot(".git") @@ -398,9 +397,8 @@ func (s *RepositorySuite) TestDeleteBranch(c *C) { } func (s *RepositorySuite) TestPlainInit(c *C) { - dir, err := ioutil.TempDir("", "plain-init") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -412,9 +410,8 @@ func (s *RepositorySuite) TestPlainInit(c *C) { } func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { - dir, err := ioutil.TempDir("", "plain-init") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -426,9 +423,8 @@ func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { } func (s *RepositorySuite) TestPlainOpen(c *C) { - dir, err := ioutil.TempDir("", "plain-open") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -440,9 +436,8 @@ func (s *RepositorySuite) TestPlainOpen(c *C) { } func (s *RepositorySuite) TestPlainOpenBare(c *C) { - dir, err := ioutil.TempDir("", "plain-open") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, true) c.Assert(err, IsNil) @@ -454,9 +449,8 @@ func (s *RepositorySuite) TestPlainOpenBare(c *C) { } func (s *RepositorySuite) TestPlainOpenNotBare(c *C) { - dir, err := ioutil.TempDir("", "plain-open") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -468,22 +462,27 @@ func (s *RepositorySuite) TestPlainOpenNotBare(c *C) { } func (s *RepositorySuite) testPlainOpenGitFile(c *C, f func(string, string) string) { - dir, err := ioutil.TempDir("", "plain-open") + fs, clean := s.TemporalFilesystem() + defer clean() + + dir, err := util.TempDir(fs, "", "plain-open") c.Assert(err, IsNil) - defer os.RemoveAll(dir) - r, err := PlainInit(dir, true) + r, err := PlainInit(fs.Join(fs.Root(), dir), true) c.Assert(err, IsNil) c.Assert(r, NotNil) - altDir, err := ioutil.TempDir("", "plain-open") + altDir, err := util.TempDir(fs, "", "plain-open") c.Assert(err, IsNil) - defer os.RemoveAll(altDir) - err = ioutil.WriteFile(filepath.Join(altDir, ".git"), []byte(f(dir, altDir)), 0644) + err = util.WriteFile(fs, fs.Join(altDir, ".git"), + []byte(f(fs.Join(fs.Root(), dir), fs.Join(fs.Root(), altDir))), + 0644, + ) + c.Assert(err, IsNil) - r, err = PlainOpen(altDir) + r, err = PlainOpen(fs.Join(fs.Root(), altDir)) c.Assert(err, IsNil) c.Assert(r, NotNil) } @@ -517,17 +516,23 @@ func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileNoEOL(c *C) { } func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileTrailingGarbage(c *C) { - dir, err := ioutil.TempDir("", "plain-open") + fs, clean := s.TemporalFilesystem() + defer clean() + + dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) - defer os.RemoveAll(dir) r, err := PlainInit(dir, true) c.Assert(err, IsNil) c.Assert(r, NotNil) - altDir, err := ioutil.TempDir("", "plain-open") + altDir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) - err = ioutil.WriteFile(filepath.Join(altDir, ".git"), []byte(fmt.Sprintf("gitdir: %s\nTRAILING", altDir)), 0644) + + err = util.WriteFile(fs, fs.Join(altDir, ".git"), + []byte(fmt.Sprintf("gitdir: %s\nTRAILING", fs.Join(fs.Root(), altDir))), + 0644, + ) c.Assert(err, IsNil) r, err = PlainOpen(altDir) @@ -536,20 +541,26 @@ func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileTrailingGarbage(c * } func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileBadPrefix(c *C) { - dir, err := ioutil.TempDir("", "plain-open") + fs, clean := s.TemporalFilesystem() + defer clean() + + dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) - defer os.RemoveAll(dir) - r, err := PlainInit(dir, true) + r, err := PlainInit(fs.Join(fs.Root(), dir), true) c.Assert(err, IsNil) c.Assert(r, NotNil) - altDir, err := ioutil.TempDir("", "plain-open") + altDir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) - err = ioutil.WriteFile(filepath.Join(altDir, ".git"), []byte(fmt.Sprintf("xgitdir: %s\n", dir)), 0644) + + err = util.WriteFile(fs, fs.Join(altDir, ".git"), []byte( + fmt.Sprintf("xgitdir: %s\n", fs.Join(fs.Root(), dir)), + ), 0644) + c.Assert(err, IsNil) - r, err = PlainOpen(altDir) + r, err = PlainOpen(fs.Join(fs.Root(), altDir)) c.Assert(err, ErrorMatches, ".*gitdir.*") c.Assert(r, IsNil) } @@ -561,42 +572,43 @@ func (s *RepositorySuite) TestPlainOpenNotExists(c *C) { } func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) { - dir, err := ioutil.TempDir("", "plain-open") + fs, clean := s.TemporalFilesystem() + defer clean() + + dir, err := util.TempDir(fs, "", "") c.Assert(err, IsNil) - defer os.RemoveAll(dir) subdir := filepath.Join(dir, "a", "b") - err = os.MkdirAll(subdir, 0755) + err = fs.MkdirAll(subdir, 0755) c.Assert(err, IsNil) - file := filepath.Join(subdir, "file.txt") - f, err := os.Create(file) + file := fs.Join(subdir, "file.txt") + f, err := fs.Create(file) c.Assert(err, IsNil) f.Close() - r, err := PlainInit(dir, false) + r, err := PlainInit(fs.Join(fs.Root(), dir), false) c.Assert(err, IsNil) c.Assert(r, NotNil) opt := &PlainOpenOptions{DetectDotGit: true} - r, err = PlainOpenWithOptions(subdir, opt) + r, err = PlainOpenWithOptions(fs.Join(fs.Root(), subdir), opt) c.Assert(err, IsNil) c.Assert(r, NotNil) - r, err = PlainOpenWithOptions(file, opt) + r, err = PlainOpenWithOptions(fs.Join(fs.Root(), file), opt) c.Assert(err, IsNil) c.Assert(r, NotNil) optnodetect := &PlainOpenOptions{DetectDotGit: false} - r, err = PlainOpenWithOptions(file, optnodetect) + r, err = PlainOpenWithOptions(fs.Join(fs.Root(), file), optnodetect) c.Assert(err, NotNil) c.Assert(r, IsNil) } func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) { - dir, err := ioutil.TempDir("", "plain-open") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() opt := &PlainOpenOptions{DetectDotGit: true} r, err := PlainOpenWithOptions(dir, opt) @@ -605,7 +617,10 @@ func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) { } func (s *RepositorySuite) TestPlainClone(c *C) { - r, err := PlainClone(c.MkDir(), false, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) @@ -621,7 +636,10 @@ func (s *RepositorySuite) TestPlainClone(c *C) { } func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) { - r, err := PlainClone(c.MkDir(), false, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), RemoteName: "test", }) @@ -634,12 +652,14 @@ func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) { } func (s *RepositorySuite) TestPlainCloneOverExistingGitDirectory(c *C) { - tmpDir := c.MkDir() - r, err := PlainInit(tmpDir, false) + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainInit(dir, false) c.Assert(r, NotNil) c.Assert(err, IsNil) - r, err = PlainClone(tmpDir, false, &CloneOptions{ + r, err = PlainClone(dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) c.Assert(r, IsNil) @@ -650,7 +670,10 @@ func (s *RepositorySuite) TestPlainCloneContextCancel(c *C) { ctx, cancel := context.WithCancel(context.Background()) cancel() - r, err := PlainCloneContext(ctx, c.MkDir(), false, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainCloneContext(ctx, dir, false, &CloneOptions{ URL: s.GetBasicLocalRepositoryURL(), }) @@ -662,19 +685,22 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithExistentDir(c *C) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - tmpDir := c.MkDir() - repoDir := tmpDir + fs, clean := s.TemporalFilesystem() + defer clean() - r, err := PlainCloneContext(ctx, repoDir, false, &CloneOptions{ + dir, err := util.TempDir(fs, "", "") + c.Assert(err, IsNil) + + r, err := PlainCloneContext(ctx, dir, false, &CloneOptions{ URL: "incorrectOnPurpose", }) c.Assert(r, NotNil) c.Assert(err, Equals, transport.ErrRepositoryNotFound) - _, err = os.Stat(repoDir) + _, err = fs.Stat(dir) c.Assert(os.IsNotExist(err), Equals, false) - names, err := ioutil.ReadDir(repoDir) + names, err := fs.ReadDir(dir) c.Assert(err, IsNil) c.Assert(names, HasLen, 0) } @@ -683,7 +709,12 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNonExistentDir(c * ctx, cancel := context.WithCancel(context.Background()) defer cancel() - tmpDir := c.MkDir() + fs, clean := s.TemporalFilesystem() + defer clean() + + tmpDir, err := util.TempDir(fs, "", "") + c.Assert(err, IsNil) + repoDir := filepath.Join(tmpDir, "repoDir") r, err := PlainCloneContext(ctx, repoDir, false, &CloneOptions{ @@ -692,7 +723,7 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNonExistentDir(c * c.Assert(r, NotNil) c.Assert(err, Equals, transport.ErrRepositoryNotFound) - _, err = os.Stat(repoDir) + _, err = fs.Stat(repoDir) c.Assert(os.IsNotExist(err), Equals, true) } @@ -700,19 +731,25 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotDir(c *C) { ctx, cancel := context.WithCancel(context.Background()) cancel() - tmpDir := c.MkDir() - repoDir := filepath.Join(tmpDir, "repoDir") - f, err := os.Create(repoDir) + fs, clean := s.TemporalFilesystem() + defer clean() + + tmpDir, err := util.TempDir(fs, "", "") + c.Assert(err, IsNil) + + repoDir := fs.Join(tmpDir, "repoDir") + + f, err := fs.Create(repoDir) c.Assert(err, IsNil) c.Assert(f.Close(), IsNil) - r, err := PlainCloneContext(ctx, repoDir, false, &CloneOptions{ + r, err := PlainCloneContext(ctx, fs.Join(fs.Root(), repoDir), false, &CloneOptions{ URL: "incorrectOnPurpose", }) c.Assert(r, IsNil) c.Assert(err, ErrorMatches, ".*not a directory.*") - fi, err := os.Stat(repoDir) + fi, err := fs.Stat(repoDir) c.Assert(err, IsNil) c.Assert(fi.IsDir(), Equals, false) } @@ -721,22 +758,27 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - tmpDir := c.MkDir() - repoDirPath := filepath.Join(tmpDir, "repoDir") - err := os.Mkdir(repoDirPath, 0777) + fs, clean := s.TemporalFilesystem() + defer clean() + + tmpDir, err := util.TempDir(fs, "", "") + c.Assert(err, IsNil) + + repoDir := filepath.Join(tmpDir, "repoDir") + err = fs.MkdirAll(repoDir, 0777) c.Assert(err, IsNil) - dummyFile := filepath.Join(repoDirPath, "dummyFile") - err = ioutil.WriteFile(dummyFile, []byte("dummyContent"), 0644) + dummyFile := filepath.Join(repoDir, "dummyFile") + err = util.WriteFile(fs, dummyFile, []byte("dummyContent"), 0644) c.Assert(err, IsNil) - r, err := PlainCloneContext(ctx, repoDirPath, false, &CloneOptions{ + r, err := PlainCloneContext(ctx, fs.Join(fs.Root(), repoDir), false, &CloneOptions{ URL: "incorrectOnPurpose", }) c.Assert(r, NotNil) c.Assert(err, Equals, transport.ErrRepositoryNotFound) - _, err = os.Stat(dummyFile) + _, err = fs.Stat(dummyFile) c.Assert(err, IsNil) } @@ -745,12 +787,14 @@ func (s *RepositorySuite) TestPlainCloneContextNonExistingOverExistingGitDirecto ctx, cancel := context.WithCancel(context.Background()) defer cancel() - tmpDir := c.MkDir() - r, err := PlainInit(tmpDir, false) + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainInit(dir, false) c.Assert(r, NotNil) c.Assert(err, IsNil) - r, err = PlainCloneContext(ctx, tmpDir, false, &CloneOptions{ + r, err = PlainCloneContext(ctx, dir, false, &CloneOptions{ URL: "incorrectOnPurpose", }) c.Assert(r, IsNil) @@ -762,9 +806,8 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) { c.Skip("skipping test in short mode.") } - dir, err := ioutil.TempDir("", "plain-clone-submodule") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() path := fixtures.ByTag("submodule").One().Worktree().Root() r, err := PlainClone(dir, false, &CloneOptions{ @@ -782,9 +825,8 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) { } func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) { - dir, err := ioutil.TempDir("", "plain-clone-no-checkout") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() path := fixtures.ByTag("submodule").One().Worktree().Root() r, err := PlainClone(dir, false, &CloneOptions{ @@ -1134,7 +1176,9 @@ func (s *RepositorySuite) TestCloneDetachedHEADAnnotatedTag(c *C) { } func (s *RepositorySuite) TestPush(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + server, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -1161,7 +1205,9 @@ func (s *RepositorySuite) TestPush(c *C) { } func (s *RepositorySuite) TestPushContext(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + _, err := PlainInit(url, true) c.Assert(err, IsNil) @@ -1183,22 +1229,29 @@ func (s *RepositorySuite) TestPushContext(c *C) { // installPreReceiveHook installs a pre-receive hook in the .git // directory at path which prints message m before exiting // successfully. -func installPreReceiveHook(c *C, path, m string) { - hooks := filepath.Join(path, "hooks") - err := os.MkdirAll(hooks, 0777) +func installPreReceiveHook(c *C, fs billy.Filesystem, path, m string) { + hooks := fs.Join(path, "hooks") + err := fs.MkdirAll(hooks, 0777) c.Assert(err, IsNil) - err = ioutil.WriteFile(filepath.Join(hooks, "pre-receive"), preReceiveHook(m), 0777) + err = util.WriteFile(fs, fs.Join(hooks, "pre-receive"), preReceiveHook(m), 0777) c.Assert(err, IsNil) } func (s *RepositorySuite) TestPushWithProgress(c *C) { - url := c.MkDir() + fs, clean := s.TemporalFilesystem() + defer clean() + + path, err := util.TempDir(fs, "", "") + c.Assert(err, IsNil) + + url := fs.Join(fs.Root(), path) + server, err := PlainInit(url, true) c.Assert(err, IsNil) m := "Receiving..." - installPreReceiveHook(c, url, m) + installPreReceiveHook(c, fs, path, m) _, err = s.Repository.CreateRemote(&config.RemoteConfig{ Name: "bar", @@ -1222,7 +1275,9 @@ func (s *RepositorySuite) TestPushWithProgress(c *C) { } func (s *RepositorySuite) TestPushDepth(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + server, err := PlainClone(url, true, &CloneOptions{ URL: fixtures.Basic().One().DotGit().Root(), }) @@ -2312,15 +2367,13 @@ func (s *RepositorySuite) TestDeleteTagAnnotated(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - dir, err := ioutil.TempDir("", "go-git-test-deletetag-annotated") - c.Assert(err, IsNil) - - defer os.RemoveAll(dir) // clean up + fs, clean := s.TemporalFilesystem() + defer clean() - fss := filesystem.NewStorage(osfs.New(dir), cache.NewObjectLRUDefault()) + fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) r, _ := Init(fss, nil) - err = r.clone(context.Background(), &CloneOptions{URL: url}) + err := r.clone(context.Background(), &CloneOptions{URL: url}) c.Assert(err, IsNil) ref, err := r.Tag("annotated-tag") @@ -2347,7 +2400,7 @@ func (s *RepositorySuite) TestDeleteTagAnnotated(c *C) { err = r.RepackObjects(&RepackConfig{}) c.Assert(err, IsNil) - r, err = PlainOpen(dir) + r, err = PlainOpen(fs.Root()) c.Assert(r, NotNil) c.Assert(err, IsNil) @@ -2362,15 +2415,13 @@ func (s *RepositorySuite) TestDeleteTagAnnotatedUnpacked(c *C) { fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(), ) - dir, err := ioutil.TempDir("", "go-git-test-deletetag-annotated-unpacked") - c.Assert(err, IsNil) - - defer os.RemoveAll(dir) // clean up + fs, clean := s.TemporalFilesystem() + defer clean() - fss := filesystem.NewStorage(osfs.New(dir), cache.NewObjectLRUDefault()) + fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) r, _ := Init(fss, nil) - err = r.clone(context.Background(), &CloneOptions{URL: url}) + err := r.clone(context.Background(), &CloneOptions{URL: url}) c.Assert(err, IsNil) // Create a tag for the deletion test. This ensures that the ultimate loose diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go index c092d14..ce6a959 100644 --- a/storage/filesystem/config_test.go +++ b/storage/filesystem/config_test.go @@ -1,14 +1,13 @@ package filesystem import ( - "io/ioutil" "os" "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" + fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" - - fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" ) @@ -22,7 +21,7 @@ type ConfigSuite struct { var _ = Suite(&ConfigSuite{}) func (s *ConfigSuite) SetUpTest(c *C) { - tmp, err := ioutil.TempDir("", "go-git-filestystem-config") + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") c.Assert(err, IsNil) s.dir = dotgit.New(osfs.New(tmp)) diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index 237605f..4c2ae94 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/go-git/go-billy/v5" - "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" fixtures "github.com/go-git/go-git-fixtures/v4" + "github.com/go-git/go-git/v5/plumbing" . "gopkg.in/check.v1" ) @@ -26,15 +26,30 @@ type SuiteDotGit struct { var _ = Suite(&SuiteDotGit{}) +func (s *SuiteDotGit) TemporalFilesystem() (fs billy.Filesystem, clean func()) { + fs = osfs.New(os.TempDir()) + path, err := util.TempDir(fs, "", "") + if err != nil { + panic(err) + } + + fs, err = fs.Chroot(path) + if err != nil { + panic(err) + } + + return fs, func() { + util.RemoveAll(fs, path) + } +} + func (s *SuiteDotGit) TestInitialize(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) - err = dir.Initialize() + err := dir.Initialize() c.Assert(err, IsNil) _, err = fs.Stat(fs.Join("objects", "info")) @@ -51,22 +66,18 @@ func (s *SuiteDotGit) TestInitialize(c *C) { } func (s *SuiteDotGit) TestSetRefs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) testSetRefs(c, dir) } func (s *SuiteDotGit) TestSetRefsNorwfs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(&norwfs{fs}) testSetRefs(c, dir) @@ -220,7 +231,7 @@ func (s *SuiteDotGit) TestRemoveRefFromPackedRefs(c *C) { err := dir.RemoveRef(name) c.Assert(err, IsNil) - b, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + b, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(b), Equals, ""+ @@ -255,7 +266,7 @@ func (s *SuiteDotGit) TestRemoveRefFromReferenceFileAndPackedRefs(c *C) { err = dir.RemoveRef(name) c.Assert(err, IsNil) - b, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + b, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(b), Equals, ""+ @@ -274,15 +285,14 @@ func (s *SuiteDotGit) TestRemoveRefNonExistent(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) - before, err := ioutil.ReadFile(packedRefs) + before, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, IsNil) - after, err := ioutil.ReadFile(packedRefs) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(before), Equals, string(after)) @@ -292,17 +302,16 @@ func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) brokenContent := "BROKEN STUFF REALLY BROKEN" - err := ioutil.WriteFile(packedRefs, []byte(brokenContent), os.FileMode(0755)) + err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, NotNil) - after, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(brokenContent, Equals, string(after)) @@ -312,17 +321,16 @@ func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs2(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) brokenContent := strings.Repeat("a", bufio.MaxScanTokenSize*2) - err := ioutil.WriteFile(packedRefs, []byte(brokenContent), os.FileMode(0755)) + err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, NotNil) - after, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(brokenContent, Equals, string(after)) @@ -351,11 +359,9 @@ func (s *SuiteDotGit) TestConfig(c *C) { } func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.ConfigWriter() @@ -383,11 +389,9 @@ func (s *SuiteDotGit) TestIndex(c *C) { } func (s *SuiteDotGit) TestIndexWriteAndIndex(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.IndexWriter() @@ -415,11 +419,9 @@ func (s *SuiteDotGit) TestShallow(c *C) { } func (s *SuiteDotGit) TestShallowWriteAndShallow(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.ShallowWriter() @@ -562,11 +564,9 @@ func (s *SuiteDotGit) TestObjectPackNotFound(c *C) { } func (s *SuiteDotGit) TestNewObject(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) w, err := dir.NewObject() c.Assert(err, IsNil) @@ -627,11 +627,9 @@ func testObjectsWithPrefix(c *C, fs billy.Filesystem, dir *DotGit) { } func (s *SuiteDotGit) TestObjectsNoFolder(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) hash, err := dir.Objects() c.Assert(err, IsNil) @@ -722,14 +720,12 @@ func (s *SuiteDotGit) TestSubmodules(c *C) { } func (s *SuiteDotGit) TestPackRefs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) - err = dir.SetRef(plumbing.NewReferenceFromStrings( + err := dir.SetRef(plumbing.NewReferenceFromStrings( "refs/heads/foo", "e8d3ffab552895c19b9fcf7aa264d277cde33881", ), nil) @@ -794,20 +790,16 @@ func (s *SuiteDotGit) TestPackRefs(c *C) { } func (s *SuiteDotGit) TestAlternates(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) - - // Create a new billy fs. - fs := osfs.New(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() // Create a new dotgit object and initialize. dir := New(fs) - err = dir.Initialize() + err := dir.Initialize() c.Assert(err, IsNil) // Create alternates file. - altpath := filepath.Join("objects", "info", "alternates") + altpath := fs.Join("objects", "info", "alternates") f, err := fs.Create(altpath) c.Assert(err, IsNil) @@ -832,16 +824,17 @@ func (s *SuiteDotGit) TestAlternates(c *C) { // For relative path: // /some/absolute/path/to/dot-git -> /some/absolute/path - pathx := strings.Split(tmp, string(filepath.Separator)) + pathx := strings.Split(fs.Root(), string(filepath.Separator)) pathx = pathx[:len(pathx)-2] // Use string.Join() to avoid malformed absolutepath on windows // C:Users\\User\\... instead of C:\\Users\\appveyor\\... . resolvedPath := strings.Join(pathx, string(filepath.Separator)) // Append the alternate path to the resolvedPath - expectedPath := filepath.Join(string(filepath.Separator), resolvedPath, "rep2", ".git") + expectedPath := fs.Join(string(filepath.Separator), resolvedPath, "rep2", ".git") if runtime.GOOS == "windows" { - expectedPath = filepath.Join(resolvedPath, "rep2", ".git") + expectedPath = fs.Join(resolvedPath, "rep2", ".git") } + c.Assert(dotgits[1].fs.Root(), Equals, expectedPath) } diff --git a/storage/filesystem/dotgit/repository_filesystem_test.go b/storage/filesystem/dotgit/repository_filesystem_test.go index 880ec0d..022bde7 100644 --- a/storage/filesystem/dotgit/repository_filesystem_test.go +++ b/storage/filesystem/dotgit/repository_filesystem_test.go @@ -1,25 +1,16 @@ package dotgit import ( - "io/ioutil" - "log" "os" - "github.com/go-git/go-billy/v5/osfs" - . "gopkg.in/check.v1" ) func (s *SuiteDotGit) TestRepositoryFilesystem(c *C) { - dir, err := ioutil.TempDir("", "repository_filesystem") - if err != nil { - log.Fatal(err) - } - defer os.RemoveAll(dir) - - fs := osfs.New(dir) + fs, clean := s.TemporalFilesystem() + defer clean() - err = fs.MkdirAll("dotGit", 0777) + err := fs.MkdirAll("dotGit", 0777) c.Assert(err, IsNil) dotGitFs, err := fs.Chroot("dotGit") c.Assert(err, IsNil) diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go index 7147aec..a2517cc 100644 --- a/storage/filesystem/dotgit/writers_test.go +++ b/storage/filesystem/dotgit/writers_test.go @@ -3,16 +3,15 @@ package dotgit import ( "fmt" "io" - "io/ioutil" - "log" "os" "strconv" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/idxfile" "github.com/go-git/go-git/v5/plumbing/format/packfile" - "github.com/go-git/go-billy/v5/osfs" fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" ) @@ -20,14 +19,9 @@ import ( func (s *SuiteDotGit) TestNewObjectPack(c *C) { f := fixtures.Basic().One() - dir, err := ioutil.TempDir("", "example") - if err != nil { - log.Fatal(err) - } + fs, clean := s.TemporalFilesystem() + defer clean() - defer os.RemoveAll(dir) - - fs := osfs.New(dir) dot := New(fs) w, err := dot.NewObjectPack() @@ -65,14 +59,9 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { } func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { - dir, err := ioutil.TempDir("", "example") - if err != nil { - log.Fatal(err) - } + fs, clean := s.TemporalFilesystem() + defer clean() - defer os.RemoveAll(dir) - - fs := osfs.New(dir) dot := New(fs) w, err := dot.NewObjectPack() @@ -93,10 +82,10 @@ func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { } func (s *SuiteDotGit) TestSyncedReader(c *C) { - tmpw, err := ioutil.TempFile("", "example") + tmpw, err := util.TempFile(osfs.Default, "", "example") c.Assert(err, IsNil) - tmpr, err := os.Open(tmpw.Name()) + tmpr, err := osfs.Default.Open(tmpw.Name()) c.Assert(err, IsNil) defer func() { @@ -137,14 +126,8 @@ func (s *SuiteDotGit) TestSyncedReader(c *C) { } func (s *SuiteDotGit) TestPackWriterUnusedNotify(c *C) { - dir, err := ioutil.TempDir("", "example") - if err != nil { - c.Assert(err, IsNil) - } - - defer os.RemoveAll(dir) - - fs := osfs.New(dir) + fs, clean := s.TemporalFilesystem() + defer clean() w, err := newPackWrite(fs) c.Assert(err, IsNil) diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 036420f..22f5b0c 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -9,6 +9,8 @@ import ( "path/filepath" "testing" + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" @@ -226,14 +228,14 @@ func (s *FsSuite) TestPackfileIter(c *C) { }) } -func copyFile(c *C, dstDir, dstFilename string, srcFile *os.File) { +func copyFile(c *C, dstDir, dstFilename string, srcFile billy.File) { _, err := srcFile.Seek(0, 0) c.Assert(err, IsNil) - err = os.MkdirAll(dstDir, 0750|os.ModeDir) + err = osfs.Default.MkdirAll(dstDir, 0750|os.ModeDir) c.Assert(err, IsNil) - dst, err := os.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) + dst, err := osfs.Default.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) c.Assert(err, IsNil) defer dst.Close() diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index 20eead0..096c37a 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -1,15 +1,16 @@ package filesystem import ( - "io/ioutil" "testing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/storage/test" + "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" . "gopkg.in/check.v1" ) @@ -18,13 +19,18 @@ func Test(t *testing.T) { TestingT(t) } type StorageSuite struct { test.BaseStorageSuite dir string + fs billy.Filesystem } var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { - s.dir = c.MkDir() - storage := NewStorage(osfs.New(s.dir), cache.NewObjectLRUDefault()) + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") + c.Assert(err, IsNil) + + s.dir = tmp + s.fs = osfs.New(s.dir) + storage := NewStorage(s.fs, cache.NewObjectLRUDefault()) setUpTest(s, c, storage) } @@ -49,7 +55,7 @@ func (s *StorageSuite) TestFilesystem(c *C) { } func (s *StorageSuite) TestNewStorageShouldNotAddAnyContentsToDir(c *C) { - fis, err := ioutil.ReadDir(s.dir) + fis, err := s.fs.ReadDir("/") c.Assert(err, IsNil) c.Assert(fis, HasLen, 0) } @@ -61,9 +67,14 @@ type StorageExclusiveSuite struct { var _ = Suite(&StorageExclusiveSuite{}) func (s *StorageExclusiveSuite) SetUpTest(c *C) { - s.dir = c.MkDir() + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") + c.Assert(err, IsNil) + + s.dir = tmp + s.fs = osfs.New(s.dir) + storage := NewStorageWithOptions( - osfs.New(s.dir), + s.fs, cache.NewObjectLRUDefault(), Options{ExclusiveAccess: true}) diff --git a/submodule_test.go b/submodule_test.go index 418b3ee..4bae544 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -2,7 +2,6 @@ package git import ( "context" - "io/ioutil" "os" "path/filepath" "testing" @@ -24,8 +23,8 @@ var _ = Suite(&SubmoduleSuite{}) func (s *SubmoduleSuite) SetUpTest(c *C) { path := fixtures.ByTag("submodule").One().Worktree().Root() - dir, err := ioutil.TempDir("", "submodule") - c.Assert(err, IsNil) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainClone(filepath.Join(dir, "worktree"), false, &CloneOptions{ URL: path, diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 97b158d..65d4b69 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -2,7 +2,6 @@ package git import ( "bytes" - "io/ioutil" "log" "os" "os/exec" @@ -22,7 +21,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/ProtonMail/go-crypto/openpgp/errors" "github.com/go-git/go-billy/v5/memfs" - "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/util" . "gopkg.in/check.v1" ) @@ -210,15 +208,15 @@ func (s *WorktreeSuite) TestCommitSignBadKey(c *C) { } func (s *WorktreeSuite) TestCommitTreeSort(c *C) { - path, err := ioutil.TempDir(os.TempDir(), "test-commit-tree-sort") - c.Assert(err, IsNil) - fs := osfs.New(path) + fs, clean := s.TemporalFilesystem() + defer clean() + st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) r, err := Init(st, nil) c.Assert(err, IsNil) r, _ = Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ - URL: path, + URL: fs.Root(), }) w, err := r.Worktree() @@ -245,7 +243,7 @@ func (s *WorktreeSuite) TestCommitTreeSort(c *C) { c.Assert(err, IsNil) cmd := exec.Command("git", "fsck") - cmd.Dir = path + cmd.Dir = fs.Root() cmd.Env = os.Environ() buf := &bytes.Buffer{} cmd.Stderr = buf @@ -258,12 +256,11 @@ func (s *WorktreeSuite) TestCommitTreeSort(c *C) { // https://github.com/go-git/go-git/pull/224 func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) { - dir, err := ioutil.TempDir("", "TestJustStoreObjectsNotAlreadyStored") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) // clean up + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(dir) - fsDotgit := osfs.New(filepath.Join(dir, ".git")) // real fs to get modified timestamps + fsDotgit, err := fs.Chroot(".git") // real fs to get modified timestamps + c.Assert(err, IsNil) storage := filesystem.NewStorage(fsDotgit, cache.NewObjectLRUDefault()) r, err := Init(storage, fs) diff --git a/worktree_js.go b/worktree_js.go new file mode 100644 index 0000000..7267d05 --- /dev/null +++ b/worktree_js.go @@ -0,0 +1,26 @@ +// +build js + +package git + +import ( + "syscall" + "time" + + "github.com/go-git/go-git/v5/plumbing/format/index" +) + +func init() { + fillSystemInfo = func(e *index.Entry, sys interface{}) { + if os, ok := sys.(*syscall.Stat_t); ok { + e.CreatedAt = time.Unix(int64(os.Ctime), int64(os.CtimeNsec)) + e.Dev = uint32(os.Dev) + e.Inode = uint32(os.Ino) + e.GID = os.Gid + e.UID = os.Uid + } + } +} + +func isSymlinkWindowsNonAdmin(err error) bool { + return false +} diff --git a/worktree_test.go b/worktree_test.go index 1086735..79cbefd 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -13,7 +13,7 @@ import ( "testing" "time" - "github.com/go-git/go-git-fixtures/v4" + fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" @@ -60,7 +60,9 @@ func (s *WorktreeSuite) TestPullCheckout(c *C) { } func (s *WorktreeSuite) TestPullFastForward(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() server, err := PlainClone(url, false, &CloneOptions{ @@ -68,7 +70,10 @@ func (s *WorktreeSuite) TestPullFastForward(c *C) { }) c.Assert(err, IsNil) - r, err := PlainClone(c.MkDir(), false, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, false, &CloneOptions{ URL: url, }) c.Assert(err, IsNil) @@ -92,7 +97,9 @@ func (s *WorktreeSuite) TestPullFastForward(c *C) { } func (s *WorktreeSuite) TestPullNonFastForward(c *C) { - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() server, err := PlainClone(url, false, &CloneOptions{ @@ -100,7 +107,10 @@ func (s *WorktreeSuite) TestPullNonFastForward(c *C) { }) c.Assert(err, IsNil) - r, err := PlainClone(c.MkDir(), false, &CloneOptions{ + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainClone(dir, false, &CloneOptions{ URL: url, }) c.Assert(err, IsNil) @@ -207,9 +217,8 @@ func (s *WorktreeSuite) TestPullProgressWithRecursion(c *C) { path := fixtures.ByTag("submodule").One().Worktree().Root() - dir, err := ioutil.TempDir("", "plain-clone-submodule") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, _ := PlainInit(dir, false) r.CreateRemote(&config.RemoteConfig{ @@ -380,9 +389,8 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { c.Skip("git doesn't support symlinks by default in windows") } - dir, err := ioutil.TempDir("", "checkout") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -414,7 +422,9 @@ func (s *WorktreeSuite) TestFilenameNormalization(c *C) { c.Skip("windows paths may contain non utf-8 sequences") } - url := c.MkDir() + url, clean := s.TemporalDir() + defer clean() + path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() server, err := PlainClone(url, false, &CloneOptions{ @@ -604,17 +614,15 @@ func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { } func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { - dir, err := ioutil.TempDir("", "checkout") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ r: s.Repository, Filesystem: fs, } - err = w.Checkout(&CheckoutOptions{}) + err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) idx, err := s.Repository.Storer.Index() @@ -1029,17 +1037,15 @@ func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { } func (s *WorktreeSuite) TestStatusModified(c *C) { - dir, err := ioutil.TempDir("", "status") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ r: s.Repository, Filesystem: fs, } - err = w.Checkout(&CheckoutOptions{}) + err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) f, err := fs.Create(".gitignore") @@ -1123,17 +1129,15 @@ func (s *WorktreeSuite) TestStatusUntracked(c *C) { } func (s *WorktreeSuite) TestStatusDeleted(c *C) { - dir, err := ioutil.TempDir("", "status") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ r: s.Repository, Filesystem: fs, } - err = w.Checkout(&CheckoutOptions{}) + err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) err = fs.Remove(".gitignore") @@ -1363,9 +1367,8 @@ func (s *WorktreeSuite) TestAddRemoved(c *C) { } func (s *WorktreeSuite) TestAddSymlink(c *C) { - dir, err := ioutil.TempDir("", "checkout") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() r, err := PlainInit(dir, false) c.Assert(err, IsNil) @@ -2078,7 +2081,11 @@ func (s *WorktreeSuite) TestGrep(c *C) { } path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() - server, err := PlainClone(c.MkDir(), false, &CloneOptions{ + + dir, clean := s.TemporalDir() + defer clean() + + server, err := PlainClone(dir, false, &CloneOptions{ URL: path, }) c.Assert(err, IsNil) @@ -2127,9 +2134,8 @@ func (s *WorktreeSuite) TestGrep(c *C) { } func (s *WorktreeSuite) TestAddAndCommit(c *C) { - dir, err := ioutil.TempDir("", "plain-repo") - c.Assert(err, IsNil) - defer os.RemoveAll(dir) + dir, clean := s.TemporalDir() + defer clean() repo, err := PlainInit(dir, false) c.Assert(err, IsNil) |