Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

worktree: expose underlying filesystem #513

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion submodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 12 additions & 10 deletions worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
8 changes: 4 additions & 4 deletions worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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{})
Expand Down
20 changes: 10 additions & 10 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
Loading