-
Notifications
You must be signed in to change notification settings - Fork 535
worktree: add method #361
worktree: add method #361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
idx.Entries = append(idx.Entries, index.Entry{ | ||
Name: filename, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (w *Worktree) doUpdateFileToIndex(idx *index.Index, filename string, h plumbing.Hash) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the name of the method to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, I need to know the position for overwritten it. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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