File: history.go

package info (click to toggle)
golang-github-containers-common 0.56.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,852 kB
  • sloc: makefile: 126; sh: 62
file content (84 lines) | stat: -rw-r--r-- 2,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package libimage

import (
	"context"
	"time"

	"github.com/containers/storage"
)

// ImageHistory contains the history information of an image.
type ImageHistory struct {
	ID        string     `json:"id"`
	Created   *time.Time `json:"created"`
	CreatedBy string     `json:"createdBy"`
	Size      int64      `json:"size"`
	Comment   string     `json:"comment"`
	Tags      []string   `json:"tags"`
}

// History computes the image history of the image including all of its parents.
func (i *Image) History(ctx context.Context) ([]ImageHistory, error) {
	ociImage, err := i.toOCI(ctx)
	if err != nil {
		return nil, err
	}

	layerTree, err := i.runtime.layerTree(nil)
	if err != nil {
		return nil, err
	}

	var allHistory []ImageHistory
	var layer *storage.Layer
	if i.TopLayer() != "" {
		layer, err = i.runtime.store.Layer(i.TopLayer())
		if err != nil {
			return nil, err
		}
	}

	// Iterate in reverse order over the history entries, and lookup the
	// corresponding image ID, size and get the next later if needed.
	numHistories := len(ociImage.History) - 1
	usedIDs := make(map[string]bool) // prevents assigning images IDs more than once
	for x := numHistories; x >= 0; x-- {
		history := ImageHistory{
			ID:        "<missing>", // may be overridden below
			Created:   ociImage.History[x].Created,
			CreatedBy: ociImage.History[x].CreatedBy,
			Comment:   ociImage.History[x].Comment,
		}

		if layer != nil {
			if !ociImage.History[x].EmptyLayer {
				history.Size = layer.UncompressedSize
			}
			// Query the layer tree if it's the top layer of an
			// image.
			node := layerTree.node(layer.ID)
			if len(node.images) > 0 {
				id := node.images[0].ID() // always use the first one
				if _, used := usedIDs[id]; !used {
					history.ID = id
					usedIDs[id] = true
				}
				for i := range node.images {
					history.Tags = append(history.Tags, node.images[i].Names()...)
				}
			}
			if layer.Parent == "" {
				layer = nil
			} else if !ociImage.History[x].EmptyLayer {
				layer, err = i.runtime.store.Layer(layer.Parent)
				if err != nil {
					return nil, err
				}
			}
		}

		allHistory = append(allHistory, history)
	}

	return allHistory, nil
}