Skip to content

Refactor: migrate template helpers from _sync_helpers to RhizaTemplate private methods#383

Merged
tschm merged 2 commits intomainfrom
copilot/refactor-sync-helpers-move-template
Mar 8, 2026
Merged

Refactor: migrate template helpers from _sync_helpers to RhizaTemplate private methods#383
tschm merged 2 commits intomainfrom
copilot/refactor-sync-helpers-move-template

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 8, 2026

_sync_helpers.py contained 8 template-specific functions that operated directly on RhizaTemplate data, creating unnecessary coupling and module bloat. These have been moved to the class as private methods/static methods, eliminating deferred imports from models.py → _sync_helpers.

models.pyRhizaTemplate additions

  • @staticmethod _expand_paths, _excluded_set, _prepare_snapshot — snapshot pipeline helpers
  • @staticmethod _update_sparse_checkout, _get_head_sha — pure git utilities
  • _clone_template_repository(self, ...), _clone_at_sha(self, ...) — instance methods using self.git_url
  • from_project(cls, target, branch)public classmethod replacing the standalone _validate_and_load_template; calls from_yaml internally with full validation
  • clone() / snapshot() now call self._xxx() — no more deferred _sync_helpers imports

_sync_helpers.py — signature changes

_merge_with_base and _sync_merge now accept template: RhizaTemplate instead of separate git_url + include_paths params:

# Before
_sync_merge(..., include_paths=["Makefile"], excludes=set(), git_url="https://...", ...)

# After
_sync_merge(..., template=RhizaTemplate(template_repository="owner/repo", include=["Makefile"]), excludes=set(), ...)

sync.py

Uses RhizaTemplate.from_project(target, branch) (public API) and passes template directly to _sync_merge.

Tests

  • Removed test classes that tested migrated private methods directly (TestExpandPaths, TestExcludedSet, TestPrepareSnapshot, TestGetHeadSha, TestCloneAtSha, TestCloneAtShaErrorPaths, TestCloneTemplateRepository, TestUpdateSparseCheckout, TestValidateAndLoadTemplate)
  • Updated all @patch targets from rhiza.commands._sync_helpers.*rhiza.models.RhizaTemplate.*
  • Updated _merge_with_base / _sync_merge call sites to pass RhizaTemplate instances
  • populate_base side-effect signatures drop the git_url positional arg (now self.git_url)
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor _sync_helpers: Move template-related helpers to RhizaTemplate as private attributes/methods</issue_title>
<issue_description>## Motivation

The module src/rhiza/commands/_sync_helpers.py currently contains many functions that closely operate on template data and would be better organized as private attributes/methods of the RhizaTemplate class (in rhiza/models.py). Migrating them improves encapsulation, reduces module bloat, and brings template logic together with its relevant data.

Candidates for Migration

Suggested functions to move to RhizaTemplate (with a _ prefix for class-private methods):

  • _clone_template_repository(...) — Template repo cloning (already uses git_url, template_branch, include)
  • _update_sparse_checkout(...) — Sparse-checkout update; relates to include paths
  • _clone_at_sha(...) — Clone at a specific commit SHA for template
  • _get_head_sha(...) — Get HEAD SHA of a cloned template repo
  • _prepare_snapshot(...) — Build a snapshot from a clone using include/exclude
  • _expand_paths(...) — Expand include paths relative to clone root
  • _excluded_set(...) — Build exclude set from RhizaTemplate data
  • _validate_and_load_template(...) — Loads and validates from template.yml, suitable for a factory/classmethod

Notes

  • Many of these helpers use the same arguments/data as RhizaTemplate.clone() and RhizaTemplate.snapshot() (which are already class methods).
  • Functions related to general-purpose git operations (diff application, orphan file cleanup) should remain as standalone helpers or move to a dedicated _git_helpers module.
  • Refactoring should preserve testability (existing tests use direct imports).

Linked Areas

  • RhizaTemplate class in src/rhiza/models.py
  • Existing methods: clone(), snapshot()

Acceptance Criteria

  • Template-related helpers migrated as private methods/attributes of RhizaTemplate
  • Refactored helpers are invoked via self instead of standalone calls
  • Module-level function bloat in _sync_helpers.py is reduced
  • Tests updated to import methods as needed

Discussion welcome on boundaries (what should stay as a function vs. a method) and any migration risks.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@tschm tschm marked this pull request as ready for review March 8, 2026 05:56
…lassmethod

Co-authored-by: tschm <2046079+tschm@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor _sync_helpers to move template-related helpers to RhizaTemplate Refactor: migrate template helpers from _sync_helpers to RhizaTemplate private methods Mar 8, 2026
Copilot finished work on behalf of tschm March 8, 2026 06:21
@tschm tschm merged commit cbca0da into main Mar 8, 2026
16 checks passed
@tschm tschm deleted the copilot/refactor-sync-helpers-move-template branch March 8, 2026 06:23
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.

Refactor _sync_helpers: Move template-related helpers to RhizaTemplate as private attributes/methods

2 participants