From f5861a3515137c4bf823f2f0e49afdb43b5beb6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Fri, 8 May 2026 21:52:54 -0400 Subject: [PATCH] fix: bug where group sort order doesn't update due to duplicate elements in spliced slice --- models/group/group.go | 10 +++++++++- modules/container/filter.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/models/group/group.go b/models/group/group.go index 4ddc1f4d88..115a455bcf 100644 --- a/models/group/group.go +++ b/models/group/group.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -434,12 +435,18 @@ func MoveGroup(ctx context.Context, group *Group, newParent int64, newSortOrder if err = ng.LoadSubgroups(ctx, false); err != nil { return err } - siblings = append(append(ng.Subgroups[0:min(newSortOrder, len(ng.Subgroups))], group), ng.Subgroups[newSortOrder:]...) + filtered := container.Filter(ng.Subgroups, func(e *Group) bool { + return e.ID != group.ID + }) + siblings = append(append(filtered[0:min(newSortOrder, len(ng.Subgroups))], group), filtered[newSortOrder:]...) } else if newParent <= 0 { tmpSiblings, err = FindGroups(ctx, &FindGroupsOptions{ OwnerID: group.OwnerID, ParentGroupID: 0, }) + tmpSiblings = container.Filter(tmpSiblings, func(e *Group) bool { + return group.ID != e.ID + }) tmpSiblings2 := make(RepoGroupList, newSortOrder) copy(tmpSiblings2, tmpSiblings[0:newSortOrder]) tmpSiblings2 = append(tmpSiblings2, group) @@ -463,6 +470,7 @@ func MoveGroup(ctx context.Context, group *Group, newParent int64, newSortOrder group.ParentGroupID = newParent group.SortOrder = newSortOrder for i, gg := range siblings { + log.Info("ITEM %+v", gg) gg.SortOrder = i if _, err = sess.Table(group.TableName()). ID(gg.ID). diff --git a/modules/container/filter.go b/modules/container/filter.go index 3e27552f1e..6dac7c97f2 100644 --- a/modules/container/filter.go +++ b/modules/container/filter.go @@ -20,6 +20,16 @@ func FilterSlice[E any, T comparable](s []E, include func(E) (T, bool)) []T { return slices.Clip(filtered) } +func Filter[E any](s []E, include func(E) bool) []E { + filtered := make([]E, 0, len(s)) // slice will be clipped before returning + for i := range s { + if ok := include(s[i]); ok { + filtered = append(filtered, s[i]) + } + } + return slices.Clip(filtered) +} + func DedupeBy[E any, I comparable](s []E, id func(E) I) []E { filtered := make([]E, 0, len(s)) // slice will be clipped before returning seen := make(map[I]bool, len(s))