Skip to content

Commit

Permalink
correct nested diff colors and dir sizing (fixes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Oct 31, 2018
1 parent f40600e commit f3f2ec9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .data/Dockerfile
Expand Up @@ -7,3 +7,6 @@ RUN cp /somefile.txt /root/example/somefile3.txt
RUN mv /root/example/somefile3.txt /root/saved.txt
RUN cp /root/saved.txt /root/.saved.txt
RUN rm -rf /root/example/
ADD .data/ /root/.data/
RUN cp /root/saved.txt /tmp/saved.again1.txt
RUN cp /root/saved.txt /root/.data/saved.again2.txt
1 change: 0 additions & 1 deletion .dockerignore
@@ -1,6 +1,5 @@
/.git
/.scripts
/.data
/dist
/ui
/utils
Expand Down
13 changes: 7 additions & 6 deletions filetree/node.go
Expand Up @@ -147,18 +147,19 @@ func (node *FileNode) MetadataString() string {

var sizeBytes int64

if node.Data.FileInfo.TarHeader.FileInfo().IsDir() {

if node.IsLeaf() {
sizeBytes = node.Data.FileInfo.TarHeader.FileInfo().Size()
} else {
sizer := func(curNode *FileNode) error {
if curNode.Data.DiffType != Removed {
// don't include file sizes of children that have been removed (unless the node in question is a removed dir,
// then show the accumulated size of removed files)
if curNode.Data.DiffType != Removed || node.Data.DiffType == Removed {
sizeBytes += curNode.Data.FileInfo.TarHeader.FileInfo().Size()
}
return nil
}

node.VisitDepthChildFirst(sizer, nil)
} else {
sizeBytes = node.Data.FileInfo.TarHeader.FileInfo().Size()
}

size := humanize.Bytes(uint64(sizeBytes))
Expand Down Expand Up @@ -254,7 +255,7 @@ func (node *FileNode) Path() string {
}
node.path = "/" + strings.Join(path, "/")
}
return node.path
return strings.Replace(node.path, "//", "/", -1)
}

// deriveDiffType determines a DiffType to the current FileNode. Note: the DiffType of a node is always the DiffType of
Expand Down
16 changes: 15 additions & 1 deletion filetree/node_test.go
@@ -1,6 +1,7 @@
package filetree

import (
"archive/tar"
"testing"
)

Expand Down Expand Up @@ -87,7 +88,7 @@ func TestPath(t *testing.T) {

actual := node.Path()
if expected != actual {
t.Errorf("Expected Path '%s' got '%s'", expected, actual)
t.Errorf("Expected path '%s' got '%s'", expected, actual)
}
}

Expand Down Expand Up @@ -148,3 +149,16 @@ func TestDiffTypeFromRemovedChildren(t *testing.T) {
}

}

func TestDirSize(t *testing.T) {
tree1 := NewFileTree()
tree1.AddPath("/etc/nginx/public1", FileInfo{TarHeader: tar.Header{Size: 100}})
tree1.AddPath("/etc/nginx/thing1", FileInfo{TarHeader: tar.Header{Size: 200}})
tree1.AddPath("/etc/nginx/public3/thing2", FileInfo{TarHeader: tar.Header{Size: 300}})

node, _ := tree1.GetNode("/etc/nginx")
expected, actual := "---------- 0:0 600 B ", node.MetadataString()
if expected != actual {
t.Errorf("Expected metadata '%s' got '%s'", expected, actual)
}
}
15 changes: 12 additions & 3 deletions filetree/tree.go
Expand Up @@ -245,29 +245,38 @@ func (tree *FileTree) RemovePath(path string) error {
return node.Remove()
}

// Compare marks the FileNodes in the owning tree with DiffType annotations when compared to the given tree.
// Compare marks the FileNodes in the owning (lower) tree with DiffType annotations when compared to the given (upper) tree.
func (tree *FileTree) Compare(upper *FileTree) error {
// always compare relative to the original, unaltered tree.
originalTree := tree.Copy()

graft := func(upperNode *FileNode) error {
if upperNode.IsWhiteout() {
err := tree.markRemoved(upperNode.Path())
if err != nil {
return fmt.Errorf("cannot remove upperNode %s: %v", upperNode.Path(), err.Error())
}
} else {
lowerNode, _ := tree.GetNode(upperNode.Path())
if lowerNode == nil {
// compare against the original tree (to ensure new parents with new children are captured as new instead of modified)
originalLowerNode, _ := originalTree.GetNode(upperNode.Path())

if originalLowerNode == nil {
newNode, err := tree.AddPath(upperNode.Path(), upperNode.Data.FileInfo)
if err != nil {
return fmt.Errorf("cannot add new upperNode %s: %v", upperNode.Path(), err.Error())
}
newNode.AssignDiffType(Added)
} else {
// check the tree for comparison markings
lowerNode, _ := tree.GetNode(upperNode.Path())

diffType := lowerNode.compare(upperNode)
return lowerNode.deriveDiffType(diffType)
}
}
return nil
}
// we must visit from the leaves upwards to ensure that diff types can be derived from and assigned to children
return upper.VisitDepthChildFirst(graft, nil)
}

Expand Down
4 changes: 2 additions & 2 deletions filetree/tree_test.go
Expand Up @@ -288,7 +288,7 @@ func TestCompareWithAdds(t *testing.T) {
lowerTree := NewFileTree()
upperTree := NewFileTree()
lowerPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin"}
upperPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin", "/usr/bin/bash"}
upperPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin", "/usr/bin/bash", "/a/new/path"}

for _, value := range lowerPaths {
lowerTree.AddPath(value, FileInfo{
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestCompareWithAdds(t *testing.T) {
p := n.Path()
if p == "/" {
return nil
} else if stringInSlice(p, []string{"/usr/bin/bash"}) {
} else if stringInSlice(p, []string{"/usr/bin/bash", "/a", "/a/new", "/a/new/path"}) {
if err := AssertDiffType(n, Added); err != nil {
failedAssertions = append(failedAssertions, err)
}
Expand Down
4 changes: 2 additions & 2 deletions ui/filetreeview.go
Expand Up @@ -199,7 +199,7 @@ func (view *FileTreeView) CursorUp() error {
return nil
}

//CursorLeft moves the cursor up until we reach the Parent Node or top of the tree
// CursorLeft moves the cursor up until we reach the Parent Node or top of the tree
func (view *FileTreeView) CursorLeft() error {
var visitor func(*filetree.FileNode) error
var evaluator func(*filetree.FileNode) bool
Expand Down Expand Up @@ -255,7 +255,7 @@ func (view *FileTreeView) CursorLeft() error {
return view.Render()
}

//CursorRight descends into directory expanding it if needed
// CursorRight descends into directory expanding it if needed
func (view *FileTreeView) CursorRight() error {
node := view.getAbsPositionNode()
if node == nil {
Expand Down

0 comments on commit f3f2ec9

Please sign in to comment.