File 0018-compare-export-official-way-to-modify-InodeDelta.Dif.patch of Package go-mtree

From 02df7129871a9ac0c05f3526c3c1ec1d4d6d4635 Mon Sep 17 00:00:00 2001
From: Aleksa Sarai <cyphar@cyphar.com>
Date: Tue, 16 Sep 2025 18:23:15 +1000
Subject: [PATCH 18/25] compare: export official way to modify InodeDelta.Diff

At the moment, filtering out keyword changes from an InodeDelta after
doing Compare is a little complicated and error-prone. The simplest
solution is to allow for callers to access a pointer to the underlying
slice so it can be modified properly.

The filtering logic in the gomtree command-line implicitly depends on
the behaviour of InodeDelta.Diff -- DiffPtr would be a far more
appropriate replacement.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
---
 cmd/gomtree/cmd/validate.go | 22 +++++++++-------------
 compare.go                  | 11 +++++++++++
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/cmd/gomtree/cmd/validate.go b/cmd/gomtree/cmd/validate.go
index ed451b1f53fd..cc45c5ed1783 100644
--- a/cmd/gomtree/cmd/validate.go
+++ b/cmd/gomtree/cmd/validate.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"slices"
 	"strings"
 
 	cli "github.com/urfave/cli/v2"
@@ -463,19 +464,14 @@ loop:
 				}
 
 				// Only filter out the size keyword.
-				// NOTE: This currently takes advantage of the fact the
-				//       diff.Diff() returns the actual slice to diff.keys.
-				keys := diff.Diff()
-				for idx, k := range keys {
-					// Delete the key if it's "size". Unfortunately in Go you
-					// can't delete from a slice without reassigning it. So we
-					// just overwrite it with the last value.
-					if k.Name() == "size" {
-						if len(keys) < 2 {
-							continue loop
-						}
-						keys[idx] = keys[len(keys)-1]
-					}
+				keys := diff.DiffPtr()
+				*keys = slices.DeleteFunc(*keys, func(kd mtree.KeyDelta) bool {
+					return kd.Name() == "size"
+				})
+				// If there are no key deltas left after filtering, the entry
+				// should be filtered out entirely.
+				if len(*keys) == 0 {
+					continue loop
 				}
 			}
 		}
diff --git a/compare.go b/compare.go
index 81aba20dddbd..e3bd2222f16e 100644
--- a/compare.go
+++ b/compare.go
@@ -77,6 +77,17 @@ func (i InodeDelta) Diff() []KeyDelta {
 	return i.keys
 }
 
+// DiffPtr returns a pointer to the internal slice that would be returned by
+// [InodeDelta.Diff]. This is intended to be used by tools which need to filter
+// aspects of [InodeDelta] entries. If the [DifferenceType] of the inode is not
+// [Modified], then DiffPtr returns nil.
+func (i *InodeDelta) DiffPtr() *[]KeyDelta {
+	if i.diff == Modified {
+		return &i.keys
+	}
+	return nil
+}
+
 // Old returns the value of the inode Entry in the "old" DirectoryHierarchy (as
 // determined by the ordering of parameters to Compare).
 func (i InodeDelta) Old() *Entry {
-- 
2.51.0

openSUSE Build Service is sponsored by