Skip to content

fix(backports/apply): scope cherry-pick to target package, ignore other-package conflicts - #20541

Merged
mrodm merged 1 commit into
elastic:mainfrom
mrodm:fix_bug_multiple_packages_pr
Aug 5, 2026
Merged

fix(backports/apply): scope cherry-pick to target package, ignore other-package conflicts#20541
mrodm merged 1 commit into
elastic:mainfrom
mrodm:fix_bug_multiple_packages_pr

Conversation

@mrodm

@mrodm mrodm commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Proposed commit message

fix(backports/apply): scope cherry-pick to target package, ignore other-package conflicts

WHAT: Added `resetNonPackageChanges(relPkgDir string)` in `apply.go`. After
every cherry-pick (and after changelog.yml is restored), this function iterates
over `git status --porcelain` and resets every file whose path falls outside the
target package directory back to HEAD:

- `git checkout HEAD -- <file>` resolves both regular (UU) conflicts and clean
  staged changes.
- If the file does not exist in HEAD (modify/delete DU conflict, i.e. a package
  removed from the backport branch), falls back to `git rm --force -- <file>`.

Errors from either command are non-fatal — `conflictingFiles()` is the
authoritative gate that determines whether anything real remains unresolved.

WHY: When a commit on `main` touches more than one package and is cherry-picked
onto a backport branch that does not include all of those packages, `git
cherry-pick` produces conflicts in the absent packages (modify/delete DU) or UU
conflicts when the branch evolved those files independently. The `apply` command
was surfacing these as `status: conflict`, blocking the backport even when the
target package's changes had applied cleanly — the contributor had no way to
resolve something that was never their concern.

Three integration tests added to `apply_integration_test.go`:

1. `TestApplyIntegration_IgnoresConflictsInOtherPackages` — other package absent
   from branch; DU conflict resolved via `git rm` → `status == "success"`.
2. `TestApplyIntegration_IgnoresRegularConflictInOtherPackage` — both packages
   present; branch changed other-package description independently (UU conflict);
   `git checkout HEAD` restores branch version → `status == "success"`.
3. `TestApplyIntegration_DiscardsCleanChangesInOtherPackage` — both packages
   present; cherry-pick applies cleanly to other package; clean change discarded
   → `status == "success"`, other package stays at branch version.

Author's Checklist

  • go test -run TestApplyIntegration ./dev/backports/apply/ — all 14 integration tests pass (11 pre-existing + 3 new)
  • No changes outside dev/backports/apply/ — root module build unaffected

How to test this PR locally

# Run the integration tests for the apply package
go test -v -run TestApplyIntegration ./dev/backports/apply/

# The three new scenarios specifically:
go test -v -run 'TestApplyIntegration_IgnoresConflictsInOtherPackages|TestApplyIntegration_IgnoresRegularConflictInOtherPackage|TestApplyIntegration_DiscardsCleanChangesInOtherPackage' ./dev/backports/apply/

Real-world examples:

  • mrodm/integrations#66 — failure case: backport of a commit touching multiple packages incorrectly reported status: conflict before this fix
  • mrodm/integrations#68 — success case: same scenario after this fix, backport completes cleanly

Related issues


This PR was generated with the assistance of Claude (claude-sonnet-4-6).

…er-package conflicts

When a commit on main touches multiple packages and is cherry-picked onto a
backport branch that does not include all of those packages, git cherry-pick
produces modify/delete (DU) conflicts for absent packages, or UU conflicts
when the branch evolved those files independently. The apply command was
surfacing these as status: conflict, blocking the backport even when the
target package's changes had applied cleanly.

Added resetNonPackageChanges() which, after every cherry-pick, iterates
git status --porcelain and resets every file outside the target package
directory back to HEAD. Files present in HEAD are restored via
git checkout HEAD; files absent from HEAD (modify/delete conflicts) are
removed via git rm --force. Errors are non-fatal — conflictingFiles() is
the authoritative gate for genuine remaining conflicts.

Three integration tests added:
- TestApplyIntegration_IgnoresConflictsInOtherPackages: DU conflict in
  absent package resolved via git rm → status: success
- TestApplyIntegration_IgnoresRegularConflictInOtherPackage: UU conflict
  in present package reset to branch version → status: success
- TestApplyIntegration_DiscardsCleanChangesInOtherPackage: clean change
  in other package discarded → status: success

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mrodm mrodm self-assigned this Aug 5, 2026
return nil, fmt.Errorf("computing relative package dir: %w", err)
}
relPkgDir = filepath.ToSlash(relPkgDir)
a.resetNonPackageChanges(relPkgDir)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If a Pull Request contains changes in two different packages and it is selected to backport to one of those specific packages, this function removes the files that are not related to the selected package.

For instance, a pull request updates kubernetes and security_detection_engine packages. The user selects to backport just the kubernetes package. Then this process ensures that the changes about security_detection_engine are not part of the backport process.

@mrodm
mrodm marked this pull request as ready for review August 5, 2026 10:27
@mrodm
mrodm requested a review from a team as a code owner August 5, 2026 10:27
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

💚 Build Succeeded

cc @mrodm

@mergify

mergify Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@mrodm
mrodm merged commit fb032cc into elastic:main Aug 5, 2026
10 checks passed
Sign up for free to 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.

backport: automate backport PR creation from checklist and fix changelog links

2 participants