Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mutate): simplify work with layers in MutateTime, add test #1716

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 18 additions & 22 deletions pkg/v1/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,34 +382,30 @@ func Time(img v1.Image, t time.Time) (v1.Image, error) {
}

addendums := make([]Addendum, max(len(ocf.History), len(layers)))
var historyIdx, addendumIdx int
for layerIdx := 0; layerIdx < len(layers); addendumIdx, layerIdx = addendumIdx+1, layerIdx+1 {
newLayer, err := layerTime(layers[layerIdx], t)
if err != nil {
return nil, fmt.Errorf("setting layer times: %w", err)

for historyIdx, history := range ocf.History {
addendums[historyIdx].History = history
}

var addendumIdx int
for _, layer := range layers {
// search next non-empty layer
for addendumIdx < len(ocf.History) && addendums[addendumIdx].History.EmptyLayer {
addendumIdx++
}

// try to search for the history entry that corresponds to this layer
for ; historyIdx < len(ocf.History); historyIdx++ {
addendums[addendumIdx].History = ocf.History[historyIdx]
// if it's an EmptyLayer, do not set the Layer and have the Addendum with just the History
// and move on to the next History entry
if ocf.History[historyIdx].EmptyLayer {
addendumIdx++
continue
}
// otherwise, we can exit from the cycle
historyIdx++
// we hadn't found any non-empty layer
if addendumIdx >= len(ocf.History) {
break
}
if addendumIdx < len(addendums) {
addendums[addendumIdx].Layer = newLayer

// for found non-empty layer we set layer time from layer
newLayer, err := layerTime(layer, t)
if err != nil {
return nil, fmt.Errorf("setting layer times: %w", err)
}
}

// add all leftover History entries
for ; historyIdx < len(ocf.History); historyIdx, addendumIdx = historyIdx+1, addendumIdx+1 {
addendums[addendumIdx].History = ocf.History[historyIdx]
addendums[addendumIdx].Layer = newLayer
}

newImage, err = Append(newImage, addendums...)
Expand Down
4 changes: 4 additions & 0 deletions pkg/v1/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ func TestMutateTime(t *testing.T) {
name: "image with empty_layer history entries",
source: sourceImagePath(t, "testdata/source_image_with_empty_layer_history.tar"),
},
{
name: "image with empty_layer all history entries",
source: sourceImagePath(t, "testdata/source_image_all_empty.tar"),
},
} {
t.Run(tc.name, func(t *testing.T) {
want := time.Time{}
Expand Down
Binary file not shown.