|
4 | 4 | "context" |
5 | 5 | "io" |
6 | 6 | "os" |
7 | | - "slices" |
8 | 7 |
|
9 | 8 | "github.com/containerd/console" |
10 | 9 | "github.com/containerd/platforms" |
@@ -53,37 +52,52 @@ func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) e |
53 | 52 | return errors.Errorf("no record found for ref %q", ref) |
54 | 53 | } |
55 | 54 |
|
| 55 | + toExport := recs |
| 56 | + if !opts.all && ref == "" { |
| 57 | + latestRef := recs[0].Ref |
| 58 | + recCount := 0 |
| 59 | + for _, rec := range recs { |
| 60 | + if rec.Ref != latestRef { |
| 61 | + break |
| 62 | + } |
| 63 | + recCount++ |
| 64 | + } |
| 65 | + toExport = recs[:recCount] |
| 66 | + } |
56 | 67 | if opts.finalize { |
| 68 | + seen := make(map[string]struct{}, len(toExport)) |
57 | 69 | var finalized bool |
58 | | - for _, rec := range recs { |
59 | | - if rec.Trace == nil { |
60 | | - finalized = true |
61 | | - if err := finalizeRecord(ctx, rec.Ref, nodes); err != nil { |
62 | | - return err |
63 | | - } |
| 70 | + for _, rec := range toExport { |
| 71 | + if rec.node == nil || rec.Trace != nil { |
| 72 | + continue |
| 73 | + } |
| 74 | + key := rec.node.Builder + "\x00" + rec.node.Name + "\x00" + rec.Ref |
| 75 | + if _, ok := seen[key]; ok { |
| 76 | + continue |
| 77 | + } |
| 78 | + seen[key] = struct{}{} |
| 79 | + finalized = true |
| 80 | + if err := finalizeRecord(ctx, rec.Ref, *rec.node); err != nil { |
| 81 | + return err |
64 | 82 | } |
65 | 83 | } |
66 | 84 | if finalized { |
67 | | - recs, err = queryRecords(ctx, ref, nodes, &queryOptions{ |
| 85 | + queryRef := ref |
| 86 | + if !opts.all { |
| 87 | + queryRef = toExport[0].Ref |
| 88 | + } |
| 89 | + recs, err = queryRecords(ctx, queryRef, nodes, &queryOptions{ |
68 | 90 | CompletedOnly: true, |
69 | 91 | }) |
70 | 92 | if err != nil { |
71 | 93 | return err |
72 | 94 | } |
| 95 | + toExport = recs |
73 | 96 | } |
74 | 97 | } |
75 | | - |
76 | | - if ref == "" { |
77 | | - slices.SortFunc(recs, func(a, b historyRecord) int { |
78 | | - return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime()) |
79 | | - }) |
80 | | - } |
81 | | - |
| 98 | + res = append(res, toExport...) |
82 | 99 | if opts.all { |
83 | | - res = append(res, recs...) |
84 | 100 | break |
85 | | - } else { |
86 | | - res = append(res, recs[0]) |
87 | 101 | } |
88 | 102 | } |
89 | 103 |
|
|
0 commit comments