Skip to content

refactor: code deduplication and simplification - #13759

Merged
glours merged 10 commits into
docker:mainfrom
ndeloof:cleanup/code-dedup
May 12, 2026
Merged

refactor: code deduplication and simplification#13759
glours merged 10 commits into
docker:mainfrom
ndeloof:cleanup/code-dedup

Conversation

@ndeloof

Copy link
Copy Markdown
Contributor

Pure refactoring — no functional changes. All existing tests pass.

Changes:

  • Extract withBackend() helper in cmd/compose/ to replace repetitive NewComposeService → backend.Foo boilerplate in pause, unpause, kill, start, stop, restart
  • Extract optionalTimeout() helper to deduplicate timeout-pointer construction in stop/restart
  • Collapse ~18 trivial event-helper one-liners in progress.go to newEvent() calls; keep only functions used ≥3 times or as function values
  • Remove redundant package-level wrapper functions in dependencies.go (leaves, roots, getChildren, filterChildren, filterParents) — replaced with method expressions; replace remove(slice, item) with slices.DeleteFunc
  • Extract removeResource() helper in down.go to deduplicate the remove-emit pattern shared by removeImage and removeVolume
  • Extract forEachContainerConcurrent() in containers.go to deduplicate the errgroup iteration pattern in pause, unpause, kill
  • Collapse three identical blocks in resolveSharedNamespaces (NetworkMode/Ipc/Pid) into a resolve closure
  • Inline containers.forEach (only 1 remaining call site); add mutation comment to sorted(); replace hasConfigHashLabel() with api.ConfigHashLabel directly; add labelFilter() factory for label filters; remove unused Set.Clear and Set.Union

CopilotAI review requested due to automatic review settings April 27, 2026 11:32
@ndeloof
ndeloof requested a review from a team as a code ownerApril 27, 2026 11:32
@ndeloof
ndeloof requested a review from gloursApril 27, 2026 11:32

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors Compose CLI/service code to reduce duplication by extracting shared helpers and inlining/removing trivial wrappers, while keeping behavior the same.

Changes:

  • Added shared helpers in cmd/compose/ (withBackend, optionalTimeout) and pkg/compose/ (forEachContainerConcurrent, removeResource, labelFilter) to deduplicate common patterns.
  • Simplified progress/event emission by collapsing many one-off event helper functions into direct newEvent(...) calls.
  • Removed unused utilities/wrappers (graph traversal wrapper fns; utils.Set methods/tests) and updated call sites/tests accordingly.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
pkg/utils/set_test.goRemoves the Union unit test and its now-unused import.
pkg/utils/set.goRemoves unused Set methods and repositions RemoveAll.
pkg/compose/restart.goSwitches to newEvent(...) for restart progress events.
pkg/compose/remove.goSimplifies iteration over stopped containers; adds cyclo suppression annotation.
pkg/compose/pull.goReplaces pull progress helpers with newEvent(...) calls.
pkg/compose/ps_test.goUpdates label filtering to use api.ConfigHashLabel directly.
pkg/compose/progress.goCollapses many trivial event helper wrappers into newEvent(...); keeps function-value helpers.
pkg/compose/pause.goUses forEachContainerConcurrent to deduplicate pause/unpause concurrency pattern.
pkg/compose/monitor.goUses api.ConfigHashLabel directly (removes wrapper indirection).
pkg/compose/logs_test.goUpdates label filtering to use api.ConfigHashLabel directly.
pkg/compose/kill_test.goUpdates label filtering to use api.ConfigHashLabel directly.
pkg/compose/kill.goUses forEachContainerConcurrent and newEvent(...) for kill progress events.
pkg/compose/filters.goIntroduces labelFilter(...) and removes redundant hasConfigHashLabel().
pkg/compose/down.goExtracts removeResource(...) helper and switches stop events to newEvent(...).
pkg/compose/dependencies.goRemoves redundant wrapper functions via method expressions; replaces custom slice remove with slices.DeleteFunc.
pkg/compose/convergence_test.goUpdates label filtering to use api.ConfigHashLabel directly.
pkg/compose/convergence.goReplaces several progress helpers with newEvent(...); refactors shared-namespace resolution via closure.
pkg/compose/containers.goAdds forEachContainerConcurrent; documents sorted() as in-place mutation; uses api.ConfigHashLabel directly.
cmd/compose/stop.goUses withBackend(...) and optionalTimeout(...) to deduplicate backend/timeout boilerplate.
cmd/compose/start.goUses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/restart.goUses withBackend(...) and optionalTimeout(...) to deduplicate backend/timeout boilerplate.
cmd/compose/pause.goUses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/kill.goUses withBackend(...) to deduplicate backend creation boilerplate.
cmd/compose/backend.goAdds new shared backend/timeout helper functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadpkg/utils/set.go
Comment on lines +54 to 58
func (s Set[T]) RemoveAll(elements ...T) {
for _, e := range elements {
s.Remove(e)
}
}

CopilotAIApr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set is an exported type, and this change removes exported methods (Clear, Union). Even if they’re unused internally, this is a compile-time breaking API change for any downstream consumers importing pkg/utils. Consider keeping these methods as thin wrappers (potentially marked deprecated) or moving Set to an internal/ package if it’s not meant to be part of the public API.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored. Both Clear and Union are back as thin wrappers with // Deprecated: godoc comments, preserving API compatibility for any downstream consumers. Fixed in the latest commit.

Comment threadpkg/compose/pull.go Outdated
Comment on lines +177 to +178
resource := "Image " + service.Image
s.events.On(pullingEvent(service.Image))
s.events.On(newEvent("Image "+service.Image, api.Working, api.StatusPulling))

CopilotAIApr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource is computed as "Image "+service.Image, but the progress events re-create the same string literal. Using the resource variable for the newEvent calls would avoid duplication and guarantees the event IDs stay consistent if the formatting ever changes.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — the newEvent calls now use the already-computed resource variable instead of re-constructing the string literal. Fixed in the latest commit.

Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
…pause/kill logic
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloofforce-pushed the cleanup/code-dedup branch from 63752ea to de5e11dCompareApril 27, 2026 11:39
Assisted-By: docker-agent
Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
@codecov

codecovBot commented Apr 27, 2026

Copy link
Copy Markdown

glours
glours previously approved these changes May 12, 2026

@gloursglours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment threadpkg/compose/filters.go Outdated
@@ -45,9 +50,5 @@ func oneOffFilter(b bool) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
returnlabelFilter(api.OneoffLabel, v)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied — oneOffFilter now uses labelFilter(api.OneoffLabel, v). Fixed in the latest commit.

Comment threadpkg/compose/filters.go Outdated

func hasConfigHashLabel() string {
return api.ConfigHashLabel
return labelFilter(api.ContainerNumberLabel, fmt.Sprintf("%d", index))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
returnlabelFilter(api.ContainerNumberLabel, fmt.Sprintf("%d", index))
returnlabelFilter(api.ContainerNumberLabel, strconv.Itoa(index))

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied — containerNumberFilter now uses labelFilter(api.ContainerNumberLabel, strconv.Itoa(index)). Fixed in the latest commit.

Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
@glours
glours enabled auto-merge (rebase) May 12, 2026 09:27
@glours
glours merged commit 659b269 into docker:mainMay 12, 2026
41 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request May 21, 2026
This MR contains the following updates:
| Package | Update | Change |
|---|---|---|
| [docker/compose](https://github.com/docker/compose) | patch | `v5.1.3` → `v5.1.4` |
MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).
**Proposed changes to behavior should be submitted there as MRs.**
---
### Release Notes
<details>
<summary>docker/compose (docker/compose)</summary>
### [`v5.1.4`](https://github.com/docker/compose/releases/tag/v5.1.4)
[Compare Source](docker/compose@v5.1.3...v5.1.4)
#### What's Changed
##### ✨ Improvements
- feat: add stop lifecycle hook for external providers by [@&#8203;glours](https://github.com/glours) in [#&#8203;13779](docker/compose#13779)
##### 🐛 Fixes
- fix: route OCI artifact pulls through Docker Desktop HTTP proxy by [@&#8203;glours](https://github.com/glours) in [#&#8203;13770](docker/compose#13770)
- fix: restore stoppingEvent/stoppedEvent helpers for plugin stop hook by [@&#8203;glours](https://github.com/glours) in [#&#8203;13794](docker/compose#13794)
- fix(publish): flag literal inline environment values by [@&#8203;glours](https://github.com/glours) in [#&#8203;13760](docker/compose#13760)
##### 🔧 Internal
- ci: remove unused e2e job from merge workflow by [@&#8203;glours](https://github.com/glours) in [#&#8203;13740](docker/compose#13740)
- chore: update cagent-action to `v1.4.4` by [@&#8203;derekmisler](https://github.com/derekmisler) in [#&#8203;13745](docker/compose#13745)
- Change verb tense in Docker Compose reference documentation by [@&#8203;ryanjbonnell](https://github.com/ryanjbonnell) in [#&#8203;13773](docker/compose#13773)
- pkg/compose: go fix by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13782](docker/compose#13782)
- refactor: code deduplication and simplification by [@&#8203;ndeloof](https://github.com/ndeloof) in [#&#8203;13759](docker/compose#13759)
- fix: make e2e tests pass reliably locally with Docker Desktop by [@&#8203;glours](https://github.com/glours) in [#&#8203;13741](docker/compose#13741)
- refactor: drop Desktop beta-settings check; gate hint on LogsTab flag by [@&#8203;glours](https://github.com/glours) in [#&#8203;13755](docker/compose#13755)
##### ⚙️ Dependencies
- build(deps): bump github.com/mattn/go-shellwords from `1.0.12` to `1.0.13` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13731](docker/compose#13731)
- build(deps): bump github.com/docker/cli from `29.4.0+incompatible` to `29.4.2+incompatible` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13768](docker/compose#13768)
- build(deps): bump github.com/moby/moby/client from `0.4.0` to `0.4.1` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13752](docker/compose#13752)
- build(deps): bump github.com/docker/cli from `29.4.2+incompatible` to `29.4.3+incompatible` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13776](docker/compose#13776)
- build(deps): bump google.golang.org/grpc from `1.80.0` to `1.81.0` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13775](docker/compose#13775)
- build(deps): update to `go 1.26.3` by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13783](docker/compose#13783)
- build(deps): bump google.golang.org/grpc from `1.81.0` to `1.81.1` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13791](docker/compose#13791)
- build(deps): bump github.com/compose-spec/compose-go/v2 from `2.10.2` to `2.11.0` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13798](docker/compose#13798)
- build(deps): bump github.com/docker/cli from `29.4.3+incompatible` to `29.5.1+incompatible` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13796](docker/compose#13796)
- build(deps): bump golang.org/x/sys from `0.42.0` to `0.44.0` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13788](docker/compose#13788)
#### New Contributors
- [@&#8203;ryanjbonnell](https://github.com/ryanjbonnell) made their first contribution in [#&#8203;13773](docker/compose#13773)
**Full Changelog**: <docker/compose@v5.1.3...v5.1.4>
</details>
---
### Configuration
📅 **Schedule**: (UTC)
- Branch creation
- At any time (no schedule defined)
- Automerge
- At any time (no schedule defined)
🚦 **Automerge**: Enabled.
♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.
🔕 **Ignore**: Close this MR and you won't be reminded about this update again.
---
- [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box
---
This MR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xODYuNCIsInVwZGF0ZWRJblZlciI6IjQzLjE4Ni40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiLCJhdXRvbWF0aW9uOmJvdC1hdXRob3JlZCIsImRlcGVuZGVuY3ktdHlwZTo6cGF0Y2giXX0=-->
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@ndeloof@glours