Skip to content

Resolve merge conflicts per file, check branch releases, and report every merge problem in one run - #269

Draft
Zubair Maalick (zmaalick) wants to merge 7 commits into
MetOffice:mainfrom
zmaalick:merge_conflict_rules
Draft

Zubair Maalick (zmaalick) wants to merge 7 commits into
MetOffice:mainfrom
zmaalick:merge_conflict_rules

Conversation

@zmaalick

@zmaalick Zubair Maalick (zmaalick) commented Dec 1, 2026 •

Copy link
Copy Markdown

PR Summary

Sci/Tech Reviewer:
Code Reviewer:

Makes the dependencies.yaml merge in get_git_sources.py / merge_sources.py easier to debug and harder to break:

  • conflicts are resolved file by file with configurable rules, which fixes the checkout --ours crash;
  • branches that have drifted to a different release are caught before merging;
  • every problem is reported in one run, instead of only the first;
  • a new merge_sources.py --check runs the same merge before a suite is submitted.

Supersedes #270.

Why

When a suite combines many branches, extract_lfric fails in ways that are slow to diagnose:

  • Crash on one-sided files. handle_merge_conflicts() runs git checkout --ours -- <dir> on whole directories. This fails with does not have our version when a conflicted file exists on only one side, which is common for KGO checksum files.
  • Base drift. A branch with a newer main merged into it pulls months of upstream changes into the base, and every later branch then conflicts. For example, u-ed285 had 380 conflicted files from one drifted branch.
  • One problem per run. The task stops at the first problem, so fixing a list with several problems takes one suite submission per problem.
  • Fork tags can't be fetched from the mirrors. The mirrors store a fork's tags as refs/tags/<tag>, but the extract asks for <user>/<tag>, so every tag… entry fails with USE_MIRRORS=true.

Changes

Per-file conflict rules (CONFLICT_RULES)

  • Comma-separated pattern=strategy rules; the first match wins.
    • Strategies: ours, theirs, union (keep lines from both sides) and fail.
    • Patterns with *, ? or [ are globs on the whole path. Plain patterns match a file or everything under a directory.
  • Each conflicted file is resolved on its own. If the chosen side has no version of a file, the file is removed instead of crashing.
  • CONFLICT_IGNORES still works: it is applied as ours, after CONFLICT_RULES.
  • The default is dependencies.yaml=ours,rose-stem=ours,CONTRIBUTORS.md=union. CONTRIBUTORS.md was previously ours.

Release check (RELEASE_CHECK)

For dependencies listed in RELEASE_CHECK (default lfric_apps; set it empty to disable), each branch's release (the newest YYYY.MM.N tag it contains) is compared with the first entry's:

Branch release vs base Result
Same merged
Newer (e.g. newer main merged in) rejected: it would bring newer upstream changes into the base
Older, different YYYY.MM rejected
Older patch, same YYYY.MM (e.g. 2026.03.1 vs 2026.03.2) merged, with a warning recommending a rebase

Release tags are fetched once per dependency.

Every problem reported in one run

  • clone_and_merge() fetches every branch and checks its release before merging anything.
  • Branches that can't be fetched, are rejected by the release check, or have unresolved conflicts are left out, and the rest are still merged. Conflicted merges are aborted. Each later branch is checked against the branches that merged.
  • All problems are raised together as a MergeProblemsError. merge_sources.py lists the problems for every dependency, then the warnings, then a one-line summary, and exits with status 1.
  • Conflict reports show the following for each file:
    • the type of conflict;
    • the earlier merge that touched the file;
    • options for resolving it.

Checking before running a suite: merge_sources.py --check [--pin FILE]

  • --check merges into a scratch directory, deleted afterwards unless -p/--path is given, using the same code as the extract task. It exits 0 if every source merges and 1 otherwise. -m (use the mirrors) and --tokens work as usual.
  • --pin FILE, written only when every source merges, is a copy of dependencies.yaml with each ref replaced by the commit merged. A suite that uses it merges exactly what was checked.

Fetching fork tags from the mirrors

  • With --mirrors, if <user>/<ref> is not in the mirror, the ref is fetched as refs/tags/<ref>.
  • If neither is found, the error says both were tried.

Optional shared rerere cache (CONFLICT_RERERE_CACHE)

  • Turns on git rerere, with rr-cache linked to $CONFLICT_RERERE_CACHE/<dependency>, so a resolution committed once is reused by later extracts.
  • A merge left in progress with every conflict resolved (for example by rerere) is now committed instead of raising SubprocessRunError.

Fixes and clean-up

  • SubprocessRunError was created with the command and return code swapped.
  • The temporary local remote is now always removed, including after a failed merge or an interrupted run.
  • merge_source() now returns the commit it merged.
  • Removed the unused get_unmerged().

Notes for reviewers

  • Behaviour change: conflicted merges are aborted, so the extract can carry on to the remaining branches. The half-merged tree is no longer left in share/source. The report lists the files and the earlier merge involved.
  • Release check default. It is on for lfric_apps. Feedback welcome on whether this should be opt-in, or only enforced when the first ref is a release tag. Suites whose first entry is main may otherwise see branches rejected.
  • rerere. Recorded resolutions are applied without review and don't appear in dependencies.yaml. This is opt-in, and I'm happy to split it into a separate PR.
  • Mirror tags aren't stored per user, so two forks with the same tag name would clash in the mirror. This was already the case before this PR.
  • "Earlier change" can show a branch's internal commits. When the first merge onto the release tag is a fast-forward, the "earlier change" lines show commits inside that branch, not the dependencies.yaml entry. Merging with --no-ff would fix this, but it changes the extract's history, so it isn't included here. Views welcome.
  • Setting CONFLICT_RULES replaces the defaults, the same way CONFLICT_IGNORES already does.
  • A first entry synced from a local working copy is not pinned, because pinning it would drop uncommitted changes.

Code Quality Checklist

  • I have performed a self-review of my own code
  • My code follows the project's style guidelines
  • Comments have been included that aid understanding and enhance the readability of the code
  • My changes generate no new warnings
  • All automated checks in the CI pipeline have completed successfully

Testing

  • This change has been tested appropriately (please describe)

Security Considerations

  • I have reviewed my changes for potential security issues
  • Sensitive data is properly handled (if applicable)
  • Authentication and authorisation are properly implemented (if applicable)

AI Assistance and Attribution

  • Some of the content of this change has been produced with the assistance of Generative AI tool name (e.g., Met Office Github Copilot Enterprise, Github Copilot Personal, ChatGPT GPT-4, etc) and I have followed the Simulation Systems AI policy (including attribution labels)

Sci/Tech Review

  • I understand this area of code and the changes being added
  • The proposed changes correspond to the pull request description
  • Documentation is sufficient (do documentation papers need updating)
  • Sufficient testing has been completed

(Please alert the code reviewer via a tag when you have approved the SR)

Code Review

  • All dependencies have been resolved
  • Related Issues have been properly linked and addressed
  • Code quality standards have been met
  • Tests are adequate and have passed
  • Security considerations have been addressed
  • Performance impact is acceptable

@github-actions github-actions Bot added the cla-required The CLA has not yet been signed by the author of this PR - added by GA label Dec 1, 2026
@zmaalick
Zubair Maalick (zmaalick) marked this pull request as draft December 1, 2026 14:28
@github-actions github-actions Bot added cla-signed The CLA has been signed as part of this PR - added by GA and removed cla-required The CLA has not yet been signed by the author of this PR - added by GA labels Dec 1, 2026
@zmaalick Zubair Maalick (zmaalick) changed the title Resolve merge conflicts per file with configurable rules and shared r… Resolve merge conflicts per file, check branch releases, and report every merge problem in one run Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The CLA has been signed as part of this PR - added by GA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant