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

worktree: add method #361

Merged
merged 1 commit into from
Apr 26, 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
1 change: 1 addition & 0 deletions worktree_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func init() {
fillSystemInfo = func(e *index.Entry, sys interface{}) {
if os, ok := sys.(*syscall.Stat_t); ok {

e.CreatedAt = time.Unix(int64(os.Ctim.Sec), int64(os.Ctim.Nsec))
e.Dev = uint32(os.Dev)
e.Inode = uint32(os.Ino)
Expand Down
114 changes: 110 additions & 4 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package git

import (
"bytes"
"io"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
"gopkg.in/src-d/go-git.v4/utils/merkletrie/filesystem"
"gopkg.in/src-d/go-git.v4/utils/merkletrie/index"
mindex "gopkg.in/src-d/go-git.v4/utils/merkletrie/index"
"gopkg.in/src-d/go-git.v4/utils/merkletrie/noder"
)

// Status returns the working tree status
// Status returns the working tree status.
func (w *Worktree) Status() (Status, error) {
ref, err := w.r.Head()
if err == plumbing.ErrReferenceNotFound {
Expand Down Expand Up @@ -80,7 +84,7 @@ func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) {
return nil, err
}

from := index.NewRootNode(idx)
from := mindex.NewRootNode(idx)
submodules, err := w.getSubmodulesStatus()
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,7 +135,7 @@ func (w *Worktree) diffCommitWithStaging(commit plumbing.Hash, reverse bool) (me
return nil, err
}

to := index.NewRootNode(idx)
to := mindex.NewRootNode(idx)
from := object.NewTreeRootNode(t)

if reverse {
Expand Down Expand Up @@ -159,3 +163,105 @@ func diffTreeIsEquals(a, b noder.Hasher) bool {

return bytes.Equal(hashA, hashB)
}

// Add adds the file contents of a file in the worktree to the index. if the
// file is already stagged in the index no error is returned.
func (w *Worktree) Add(path string) (plumbing.Hash, error) {
s, err := w.Status()
if err != nil {
return plumbing.ZeroHash, err
}

h, err := w.calculateBlobHash(path)
if err != nil {
return h, err
}

if s.File(path).Worktree == Unmodified {
return h, nil
}

if err := w.addOrUpdateFileToIndex(path, h); err != nil {
return h, err
}

return h, err
}

func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err error) {
fi, err := w.fs.Stat(filename)
if err != nil {
return plumbing.ZeroHash, err
}

f, err := w.fs.Open(filename)
if err != nil {
return plumbing.ZeroHash, err
}

defer ioutil.CheckClose(f, &err)

h := plumbing.NewHasher(plumbing.BlobObject, fi.Size())
if _, err := io.Copy(h, f); err != nil {
return plumbing.ZeroHash, err
}

hash = h.Sum()
return
}

func (w *Worktree) addOrUpdateFileToIndex(filename string, h plumbing.Hash) error {
idx, err := w.r.Storer.Index()
if err != nil {
return err
}

_, err = idx.Entry(filename)
if err == index.ErrEntryNotFound {
err = w.doAddFileToIndex(idx, filename)
}

if err != nil {
return err
}

err = w.doUpdateFileToIndex(idx, filename, h)
if err != nil {
return err
}

return w.r.Storer.SetIndex(idx)
}

func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the name of this method to addFileToIndex and make it a function, instead of a method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a function, but since update cannot be a function I rather keep it as a method

idx.Entries = append(idx.Entries, index.Entry{
Name: filename,
})

return nil
}

func (w *Worktree) doUpdateFileToIndex(idx *index.Index, filename string, h plumbing.Hash) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the name of the method to updateFileToIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it need context, and since is a very deep method I prefer having the do prefix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does do mean for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is performing the action with a function is a name close to this one without the do

info, err := w.fs.Stat(filename)
if err != nil {
return err
}

for i, e := range idx.Entries {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the Entry method here instead of looking for it again? (like we did in line 217)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, I need to know the position for overwritten it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is part of the problem with this PR, it is too C style, everything changing other things that are sometimes several levels of indirection away.

I think you should refactor and modify the index and the entries, adding the necessary methods there util the functionality here is trivial and it is performed by the entities responsible for it instead of doing everything here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't if worth do this change, and change the other places where the index is read. When he is the only place that an entry is updated.

if e.Name != filename {
continue
}

e.Hash = h
e.ModifiedAt = info.ModTime()
e.Mode, err = filemode.NewFromOSFileMode(info.Mode())
if err != nil {
return err
}

fillSystemInfo(&e, info.Sys())
idx.Entries[i] = e
}

return nil
}
94 changes: 94 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/src-d/go-git-fixtures"
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-billy.v2"
"gopkg.in/src-d/go-billy.v2/memfs"
"gopkg.in/src-d/go-billy.v2/osfs"
)
Expand Down Expand Up @@ -478,3 +479,96 @@ func (s *WorktreeSuite) TestSubmodules(c *C) {

c.Assert(l, HasLen, 2)
}

func (s *WorktreeSuite) TestAddUntracked(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}

err := w.Checkout(&CheckoutOptions{Force: true})
c.Assert(err, IsNil)

idx, err := w.r.Storer.Index()
c.Assert(err, IsNil)
c.Assert(idx.Entries, HasLen, 9)

err = billy.WriteFile(w.fs, "foo", []byte("FOO"), 0755)
c.Assert(err, IsNil)

hash, err := w.Add("foo")
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5")
c.Assert(err, IsNil)

idx, err = w.r.Storer.Index()
c.Assert(err, IsNil)
c.Assert(idx.Entries, HasLen, 10)

e, err := idx.Entry("foo")
c.Assert(err, IsNil)
c.Assert(e.Hash, Equals, hash)
c.Assert(e.Mode, Equals, filemode.Executable)

status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(status, HasLen, 1)

file := status.File("foo")
c.Assert(file.Staging, Equals, Added)
c.Assert(file.Worktree, Equals, Unmodified)
}

func (s *WorktreeSuite) TestAddModified(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}

err := w.Checkout(&CheckoutOptions{Force: true})
c.Assert(err, IsNil)

idx, err := w.r.Storer.Index()
c.Assert(err, IsNil)
c.Assert(idx.Entries, HasLen, 9)

err = billy.WriteFile(w.fs, "LICENSE", []byte("FOO"), 0644)
c.Assert(err, IsNil)

hash, err := w.Add("LICENSE")
c.Assert(err, IsNil)
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5")

idx, err = w.r.Storer.Index()
c.Assert(err, IsNil)
c.Assert(idx.Entries, HasLen, 9)

e, err := idx.Entry("LICENSE")
c.Assert(err, IsNil)
c.Assert(e.Hash, Equals, hash)
c.Assert(e.Mode, Equals, filemode.Regular)

status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(status, HasLen, 1)

file := status.File("LICENSE")
c.Assert(file.Staging, Equals, Modified)
c.Assert(file.Worktree, Equals, Unmodified)
}

func (s *WorktreeSuite) TestAddUnmodified(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}

err := w.Checkout(&CheckoutOptions{Force: true})
c.Assert(err, IsNil)

hash, err := w.Add("LICENSE")
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f")
c.Assert(err, IsNil)
}