remove reverse iteration for layer display and processing

This commit is contained in:
Alex Goodman 2019-10-07 12:41:57 -04:00
parent a310732060
commit 2712c1017e
No known key found for this signature in database
GPG key ID: 98AF011C5C78EB7E
4 changed files with 26 additions and 32 deletions

View file

@ -147,16 +147,13 @@ func (img *ImageArchive) ToImage() (*image.Image, error) {
}
// build the layers array
layers := make([]*image.Layer, len(trees))
layers := make([]*image.Layer, 0)
// note that the resolver config stores images in reverse chronological order, so iterate backwards through layers
// as you iterate chronologically through history (ignoring history items that have no layer contents)
// Note: history is not required metadata in a docker image!
tarPathIdx := 0
histIdx := 0
for layerIdx := len(trees) - 1; layerIdx >= 0; layerIdx-- {
tree := trees[(len(trees)-1)-layerIdx]
for idx, tree := range trees {
// ignore empty layers, we are only observing layers with content
historyObj := historyEntry{
CreatedBy: "(missing)",
@ -176,14 +173,13 @@ func (img *ImageArchive) ToImage() (*image.Image, error) {
dockerLayer := layer{
history: historyObj,
index: tarPathIdx,
tree: trees[layerIdx],
index: idx,
tree: tree,
}
layers[layerIdx] = dockerLayer.ToLayer()
tarPathIdx++
layers = append(layers, dockerLayer.ToLayer())
}
return &image.Image{
Trees: trees,
Layers: layers,

View file

@ -99,29 +99,25 @@ func processLayer(name, rootDir string) (*filetree.FileTree, error) {
func (img *ImageDirectoryRef) ToImage() (*image.Image, error) {
trees := make([]*filetree.FileTree, 0)
layers := make([]*image.Layer, 0)
// build the content tree
// todo: this isn't needed!
for _, id := range img.layerOrder {
tr, exists := img.treeMap[id]
if exists {
trees = append(trees, tr)
continue
}
return nil, fmt.Errorf("could not find '%s' in parsed trees", id)
}
layers := make([]*image.Layer, len(trees))
// note that the resolver config stores images in reverse chronological order, so iterate backwards through layers
// as you iterate chronologically through history (ignoring history items that have no layer contents)
// Note: history is not required metadata in an OCI image!
for layerIdx, id := range img.layerOrder {
tr, exists := img.treeMap[id]
if !exists {
return nil, fmt.Errorf("could not find '%s' in parsed trees", id)
}
trees = append(trees, tr)
// note that the resolver config stores images in reverse chronological order, so iterate backwards through layers
// as you iterate chronologically through history (ignoring history items that have no layer contents)
// Note: history is not required metadata in an OCI image!
podmanLayer := layer{
obj: img.layerMap[id],
index: layerIdx,
tree: trees[layerIdx],
}
layers[layerIdx] = podmanLayer.ToLayer()
layers = append(layers, podmanLayer.ToLayer())
}
return &image.Image{

View file

@ -1,6 +1,7 @@
package podman
import (
"context"
podmanImage "github.com/containers/libpod/libpod/image"
"github.com/wagoodman/dive/dive/filetree"
"github.com/wagoodman/dive/dive/image"
@ -16,9 +17,12 @@ type layer struct {
// ShortId returns the truncated id of the current layer.
func (l *layer) Command() string {
if len(l.obj.ImageData.History) > 0 {
hist := l.obj.ImageData.History
return strings.TrimPrefix(hist[len(hist)-1].CreatedBy, "/bin/sh -c ")
history, err := l.obj.History(context.TODO())
if err != nil {
return "error: " + err.Error()
}
if len(history) > 0 {
return strings.TrimPrefix(history[0].CreatedBy, "/bin/sh -c ")
}
return "unknown"
}

View file

@ -291,9 +291,7 @@ func (controller *LayerController) Render() error {
// update contents
controller.view.Clear()
for revIdx := len(controller.Layers) - 1; revIdx >= 0; revIdx-- {
layer := controller.Layers[revIdx]
idx := (len(controller.Layers) - 1) - revIdx
for idx, layer := range controller.Layers {
layerStr := layer.String()
compareBar := controller.renderCompareBar(idx)