Squash commit history for custom project template imports

What does this MR do and why?

Squash commit history for custom project template imports

Contributes to #351032 (closed)

Problem

Custom group and instance-level project templates retain full commit history when used to create new projects. This differs from built-in templates which compress everything into a single commit, causing inconsistent behavior and potential exposure of unwanted commit history.

Solution

Add RepositorySquashRestorer that runs during import of custom template projects. After the repository is restored from the bundle, it squashes the history into 2 commits: the original initial commit plus a squashed commit containing all content from HEAD. This matches built-in template behavior.

Key changes:

  • Add squash_commits and initial_commit methods to Repository model
  • Add initial_commit method to Gitlab::Git::Repository
  • Create RepositorySquashRestorer class in EE
  • Modify EE Importer to include squash restorer for template imports

References

  • Issue: #351032 (closed)
  • Built-in template squashing script: scripts/vendor_template

Screenshots or screen recordings

Before After
Screenshot_2026-03-10_at_16.54.44 Screenshot_2026-03-10_at_16.55.24
Screenshot_2026-03-10_at_16.56.17 Screenshot_2026-03-10_at_16.55.52

How to set up and validate locally

  1. Create a group and configure it as a custom project template group (Admin > Settings > Templates)
  2. Create a project in that group with multiple commits
  3. Create a new project using that template
  4. Verify the new project has only 2 commits:
    • The original initial commit from the template
    • A squashed commit with message "Initialize from template: <project_name>"
  5. Verify all files from the template are present in the new project

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Vasilii Iakliushin

Merge request reports

Loading