From ab590eb89849a0319b8c5a4d7fd980137da7180d Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Wed, 26 Jul 2017 21:46:49 +0200 Subject: worktree: expose underlying filesystem --- repository.go | 2 +- repository_test.go | 2 +- submodule.go | 2 +- submodule_test.go | 2 +- worktree.go | 22 +++++---- worktree_commit.go | 2 +- worktree_commit_test.go | 8 ++-- worktree_status.go | 20 ++++---- worktree_test.go | 120 ++++++++++++++++++++++++------------------------ 9 files changed, 91 insertions(+), 89 deletions(-) diff --git a/repository.go b/repository.go index b80f8d9..00ffaf7 100644 --- a/repository.go +++ b/repository.go @@ -844,7 +844,7 @@ func (r *Repository) Worktree() (*Worktree, error) { return nil, ErrIsBareRepository } - return &Worktree{r: r, fs: r.wt}, nil + return &Worktree{r: r, Filesystem: r.wt}, nil } // ResolveRevision resolves revision to corresponding hash. diff --git a/repository_test.go b/repository_test.go index 7f984f6..558149b 100644 --- a/repository_test.go +++ b/repository_test.go @@ -1138,7 +1138,7 @@ func (s *RepositorySuite) TestWorktree(c *C) { r, _ := Init(memory.NewStorage(), def) w, err := r.Worktree() c.Assert(err, IsNil) - c.Assert(w.fs, Equals, def) + c.Assert(w.Filesystem, Equals, def) } func (s *RepositorySuite) TestWorktreeBare(c *C) { diff --git a/submodule.go b/submodule.go index ac027c2..fbaddfd 100644 --- a/submodule.go +++ b/submodule.go @@ -115,7 +115,7 @@ func (s *Submodule) Repository() (*Repository, error) { } var worktree billy.Filesystem - if worktree, err = s.w.fs.Chroot(s.c.Path); err != nil { + if worktree, err = s.w.Filesystem.Chroot(s.c.Path); err != nil { return nil, err } diff --git a/submodule_test.go b/submodule_test.go index dd797c5..e4f3013 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -128,7 +128,7 @@ func (s *SubmoduleSuite) TestUpdateWithRecursion(c *C) { c.Assert(err, IsNil) - fs := s.Worktree.fs + fs := s.Worktree.Filesystem _, err = fs.Stat(fs.Join("itself", "basic", "LICENSE")) c.Assert(err, IsNil) } diff --git a/worktree.go b/worktree.go index 5768888..b821695 100644 --- a/worktree.go +++ b/worktree.go @@ -29,8 +29,10 @@ var ( // Worktree represents a git worktree. type Worktree struct { - r *Repository - fs billy.Filesystem + // Filesystem underlying filesystem. + Filesystem billy.Filesystem + + r *Repository } // Pull incorporates changes from a remote repository into the current branch. @@ -355,13 +357,13 @@ func (w *Worktree) checkoutChangeSubmodule(name string, return err } - if err := w.fs.MkdirAll(name, mode); err != nil { + if err := w.Filesystem.MkdirAll(name, mode); err != nil { return err } return w.addIndexFromTreeEntry(name, e, idx) case merkletrie.Delete: - if err := rmFileAndDirIfEmpty(w.fs, name); err != nil { + if err := rmFileAndDirIfEmpty(w.Filesystem, name); err != nil { return err } @@ -385,7 +387,7 @@ func (w *Worktree) checkoutChangeRegularFile(name string, // to apply perm changes the file is deleted, billy doesn't implement // chmod - if err := w.fs.Remove(name); err != nil { + if err := w.Filesystem.Remove(name); err != nil { return err } @@ -402,7 +404,7 @@ func (w *Worktree) checkoutChangeRegularFile(name string, return w.addIndexFromFile(name, e.Hash, idx) case merkletrie.Delete: - if err := rmFileAndDirIfEmpty(w.fs, name); err != nil { + if err := rmFileAndDirIfEmpty(w.Filesystem, name); err != nil { return err } @@ -429,7 +431,7 @@ func (w *Worktree) checkoutFile(f *object.File) (err error) { defer ioutil.CheckClose(from, &err) - to, err := w.fs.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm()) + to, err := w.Filesystem.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm()) if err != nil { return } @@ -453,7 +455,7 @@ func (w *Worktree) checkoutFileSymlink(f *object.File) (err error) { return } - err = w.fs.Symlink(string(bytes), f.Name) + err = w.Filesystem.Symlink(string(bytes), f.Name) return } @@ -468,7 +470,7 @@ func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx * } func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *index.Index) error { - fi, err := w.fs.Lstat(name) + fi, err := w.Filesystem.Lstat(name) if err != nil { return err } @@ -577,7 +579,7 @@ func (w *Worktree) newSubmodule(fromModules, fromConfig *config.Submodule) *Subm } func (w *Worktree) readGitmodulesFile() (*config.Modules, error) { - f, err := w.fs.Open(gitmodulesFile) + f, err := w.Filesystem.Open(gitmodulesFile) if err != nil { if os.IsNotExist(err) { return nil, nil diff --git a/worktree_commit.go b/worktree_commit.go index 02a5d03..e5d0a11 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -32,7 +32,7 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error } h := &buildTreeHelper{ - fs: w.fs, + fs: w.Filesystem, s: w.r.Storer, } diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 8f3b7b0..f6744bc 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -54,8 +54,8 @@ func (s *WorktreeSuite) TestCommitParent(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) @@ -78,8 +78,8 @@ func (s *WorktreeSuite) TestCommitAll(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) diff --git a/worktree_status.go b/worktree_status.go index a662516..9b0773e 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -116,7 +116,7 @@ func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) { return nil, err } - to := filesystem.NewRootNode(w.fs, submodules) + to := filesystem.NewRootNode(w.Filesystem, submodules) res, err := merkletrie.DiffTree(from, to, diffTreeIsEquals) if err == nil { res = w.excludeIgnoredChanges(res) @@ -125,7 +125,7 @@ func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) { } func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes { - patterns, err := gitignore.ReadPatterns(w.fs, nil) + patterns, err := gitignore.ReadPatterns(w.Filesystem, nil) if err != nil || len(patterns) == 0 { return changes } @@ -251,7 +251,7 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) { } func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error) { - fi, err := w.fs.Lstat(path) + fi, err := w.Filesystem.Lstat(path) if err != nil { return plumbing.ZeroHash, err } @@ -281,7 +281,7 @@ func (w *Worktree) copyFileToStorage(path string) (hash plumbing.Hash, err error } func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, fi os.FileInfo) (err error) { - src, err := w.fs.Open(path) + src, err := w.Filesystem.Open(path) if err != nil { return err } @@ -296,7 +296,7 @@ func (w *Worktree) fillEncodedObjectFromFile(dst io.Writer, path string, fi os.F } func (w *Worktree) fillEncodedObjectFromSymlink(dst io.Writer, path string, fi os.FileInfo) error { - target, err := w.fs.Readlink(path) + target, err := w.Filesystem.Readlink(path) if err != nil { return err } @@ -337,7 +337,7 @@ func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string, h plumbin } func (w *Worktree) doUpdateFileToIndex(e *index.Entry, filename string, h plumbing.Hash) error { - info, err := w.fs.Lstat(filename) + info, err := w.Filesystem.Lstat(filename) if err != nil { return err } @@ -382,7 +382,7 @@ func (w *Worktree) deleteFromIndex(path string) (plumbing.Hash, error) { } func (w *Worktree) deleteFromFilesystem(path string) error { - err := w.fs.Remove(path) + err := w.Filesystem.Remove(path) if os.IsNotExist(err) { return nil } @@ -393,11 +393,11 @@ func (w *Worktree) deleteFromFilesystem(path string) error { // Move moves or rename a file in the worktree and the index, directories are // not supported. func (w *Worktree) Move(from, to string) (plumbing.Hash, error) { - if _, err := w.fs.Lstat(from); err != nil { + if _, err := w.Filesystem.Lstat(from); err != nil { return plumbing.ZeroHash, err } - if _, err := w.fs.Lstat(to); err == nil { + if _, err := w.Filesystem.Lstat(to); err == nil { return plumbing.ZeroHash, ErrDestinationExists } @@ -406,7 +406,7 @@ func (w *Worktree) Move(from, to string) (plumbing.Hash, error) { return plumbing.ZeroHash, err } - if err := w.fs.Rename(from, to); err != nil { + if err := w.Filesystem.Rename(from, to); err != nil { return hash, err } diff --git a/worktree_test.go b/worktree_test.go index 150176f..4276ea4 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -192,8 +192,8 @@ func (s *RepositorySuite) TestPullAdd(c *C) { func (s *WorktreeSuite) TestCheckout(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) @@ -225,12 +225,12 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { w, err := r.Worktree() c.Assert(err, IsNil) - w.fs.Symlink("not-exists", "bar") + w.Filesystem.Symlink("not-exists", "bar") w.Add("bar") w.Commit("foo", &CommitOptions{Author: defaultSignature()}) r.Storer.SetIndex(&index.Index{Version: 2}) - w.fs = osfs.New(filepath.Join(dir, "worktree-empty")) + w.Filesystem = osfs.New(filepath.Join(dir, "worktree-empty")) err = w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) @@ -239,7 +239,7 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { c.Assert(err, IsNil) c.Assert(status.IsClean(), Equals, true) - target, err := w.fs.Readlink("bar") + target, err := w.Filesystem.Readlink("bar") c.Assert(target, Equals, "not-exists") c.Assert(err, IsNil) } @@ -280,8 +280,8 @@ func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) { func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) @@ -310,8 +310,8 @@ func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err = w.Checkout(&CheckoutOptions{}) @@ -335,8 +335,8 @@ func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { func (s *WorktreeSuite) TestCheckoutBranch(c *C) { w := &Worktree{ - r: s.Repository, - fs: memfs.New(), + r: s.Repository, + Filesystem: memfs.New(), } err := w.Checkout(&CheckoutOptions{ @@ -355,8 +355,8 @@ func (s *WorktreeSuite) TestCheckoutBranch(c *C) { func (s *WorktreeSuite) TestCheckoutCreateWithHash(c *C) { w := &Worktree{ - r: s.Repository, - fs: memfs.New(), + r: s.Repository, + Filesystem: memfs.New(), } err := w.Checkout(&CheckoutOptions{ @@ -378,8 +378,8 @@ func (s *WorktreeSuite) TestCheckoutCreateWithHash(c *C) { func (s *WorktreeSuite) TestCheckoutCreate(c *C) { w := &Worktree{ - r: s.Repository, - fs: memfs.New(), + r: s.Repository, + Filesystem: memfs.New(), } err := w.Checkout(&CheckoutOptions{ @@ -400,8 +400,8 @@ func (s *WorktreeSuite) TestCheckoutCreate(c *C) { func (s *WorktreeSuite) TestCheckoutBranchAndHash(c *C) { w := &Worktree{ - r: s.Repository, - fs: memfs.New(), + r: s.Repository, + Filesystem: memfs.New(), } err := w.Checkout(&CheckoutOptions{ @@ -414,8 +414,8 @@ func (s *WorktreeSuite) TestCheckoutBranchAndHash(c *C) { func (s *WorktreeSuite) TestCheckoutCreateMissingBranch(c *C) { w := &Worktree{ - r: s.Repository, - fs: memfs.New(), + r: s.Repository, + Filesystem: memfs.New(), } err := w.Checkout(&CheckoutOptions{ @@ -497,8 +497,8 @@ func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) { func (s *WorktreeSuite) TestCheckoutWithGitignore(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) @@ -526,8 +526,8 @@ func (s *WorktreeSuite) TestCheckoutWithGitignore(c *C) { func (s *WorktreeSuite) TestStatus(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } status, err := w.Status() @@ -575,8 +575,8 @@ func (s *WorktreeSuite) TestStatusEmptyDirty(c *C) { func (s *WorktreeSuite) TestReset(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") @@ -599,8 +599,8 @@ func (s *WorktreeSuite) TestReset(c *C) { func (s *WorktreeSuite) TestResetMerge(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") @@ -626,8 +626,8 @@ func (s *WorktreeSuite) TestResetMerge(c *C) { func (s *WorktreeSuite) TestResetHard(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") @@ -653,8 +653,8 @@ func (s *WorktreeSuite) TestResetHard(c *C) { func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -672,8 +672,8 @@ func (s *WorktreeSuite) TestStatusModified(c *C) { fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err = w.Checkout(&CheckoutOptions{}) @@ -695,8 +695,8 @@ func (s *WorktreeSuite) TestStatusModified(c *C) { func (s *WorktreeSuite) TestStatusIgnored(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } w.Checkout(&CheckoutOptions{}) @@ -742,14 +742,14 @@ func (s *WorktreeSuite) TestStatusIgnored(c *C) { func (s *WorktreeSuite) TestStatusUntracked(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) c.Assert(err, IsNil) - f, err := w.fs.Create("foo") + f, err := w.Filesystem.Create("foo") c.Assert(err, IsNil) c.Assert(f.Close(), IsNil) @@ -765,8 +765,8 @@ func (s *WorktreeSuite) TestStatusDeleted(c *C) { fs := osfs.New(filepath.Join(dir, "worktree")) w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err = w.Checkout(&CheckoutOptions{}) @@ -812,8 +812,8 @@ func (s *WorktreeSuite) TestSubmodules(c *C) { func (s *WorktreeSuite) TestAddUntracked(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -823,7 +823,7 @@ func (s *WorktreeSuite) TestAddUntracked(c *C) { c.Assert(err, IsNil) c.Assert(idx.Entries, HasLen, 9) - err = util.WriteFile(w.fs, "foo", []byte("FOO"), 0755) + err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) c.Assert(err, IsNil) hash, err := w.Add("foo") @@ -856,8 +856,8 @@ func (s *WorktreeSuite) TestAddUntracked(c *C) { func (s *WorktreeSuite) TestAddModified(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -867,7 +867,7 @@ func (s *WorktreeSuite) TestAddModified(c *C) { c.Assert(err, IsNil) c.Assert(idx.Entries, HasLen, 9) - err = util.WriteFile(w.fs, "LICENSE", []byte("FOO"), 0644) + err = util.WriteFile(w.Filesystem, "LICENSE", []byte("FOO"), 0644) c.Assert(err, IsNil) hash, err := w.Add("LICENSE") @@ -895,8 +895,8 @@ func (s *WorktreeSuite) TestAddModified(c *C) { func (s *WorktreeSuite) TestAddUnmodified(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -938,8 +938,8 @@ func (s *WorktreeSuite) TestAddSymlink(c *C) { func (s *WorktreeSuite) TestRemove(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -958,8 +958,8 @@ func (s *WorktreeSuite) TestRemove(c *C) { func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -973,8 +973,8 @@ func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) { func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -996,8 +996,8 @@ func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { func (s *WorktreeSuite) TestMove(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -1018,8 +1018,8 @@ func (s *WorktreeSuite) TestMove(c *C) { func (s *WorktreeSuite) TestMoveNotExistentEntry(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) @@ -1033,8 +1033,8 @@ func (s *WorktreeSuite) TestMoveNotExistentEntry(c *C) { func (s *WorktreeSuite) TestMoveToExistent(c *C) { fs := memfs.New() w := &Worktree{ - r: s.Repository, - fs: fs, + r: s.Repository, + Filesystem: fs, } err := w.Checkout(&CheckoutOptions{Force: true}) -- cgit