From c7871397d3b477af6a26f548bb186820dc75a031 Mon Sep 17 00:00:00 2001 From: SBPro Date: Tue, 2 Jun 2026 15:02:19 +0100 Subject: [PATCH] fix(import): include tracked ignored files --- CHANGELOG.md | 6 +++ VERSION | 2 +- lib/sync.sh | 2 +- tests/unit/sync.bats | 90 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 tests/unit/sync.bats diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ef85e4..1990108 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ - Bidirectional sync smoke-test marker — forward leg (monorepo → subrepo), 2026-05-28. +## 2.2.1 + +### Fixes + +- Initial import now force-adds copied subrepo files so tracked files that match the copied `.gitignore` are included in the import PR. + ## 2.2.0 ### Changes diff --git a/VERSION b/VERSION index ccbccc3..c043eea 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.0 +2.2.1 diff --git a/lib/sync.sh b/lib/sync.sh index 045a106..866c525 100644 --- a/lib/sync.sh +++ b/lib/sync.sh @@ -500,7 +500,7 @@ initial_import() { mkdir -p "$subfolder" rsync -a --exclude='.git' "${work_dir}/subrepo/" "${subfolder}/" - git add "$subfolder" + git add -f -- "$subfolder" if git diff --cached --quiet; then log "INFO" "No changes — subfolder already matches subrepo" diff --git a/tests/unit/sync.bats b/tests/unit/sync.bats new file mode 100644 index 0000000..bbf7450 --- /dev/null +++ b/tests/unit/sync.bats @@ -0,0 +1,90 @@ +#!/usr/bin/env bats +# tests/unit/sync.bats — Sync/import algorithm tests + +setup() { + export JOSH_SYNC_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)" + source "$JOSH_SYNC_ROOT/lib/core.sh" + source "$JOSH_SYNC_ROOT/lib/sync.sh" + + TEST_ROOT="$(mktemp -d)" + export BOT_NAME="test-bot" + export BOT_EMAIL="test-bot@test.local" + export BOT_TRAILER="Josh-Sync-Origin" + export GITEA_TOKEN="test-token" + export MONOREPO_API="https://gitea.test.local/api/v1/repos/org/mono" + export JOSH_SYNC_TARGET_NAME="crm" + export JOSH_SYNC_TARGETS='[{"name":"crm","subfolder":"apps/crm"}]' + export SYNC_BRANCH_MONO="main" + export SYNC_BRANCH_SUBREPO="main" +} + +teardown() { + rm -rf "$TEST_ROOT" +} + +make_mono_repo() { + local work="${TEST_ROOT}/mono-work" + local bare="${TEST_ROOT}/mono.git" + + git init "$work" >/dev/null + git -C "$work" checkout -b main >/dev/null + git -C "$work" config user.name "Test User" + git -C "$work" config user.email "test@test.local" + printf 'mono\n' > "${work}/README.md" + git -C "$work" add README.md + git -C "$work" commit -m "mono base" >/dev/null + + git init --bare "$bare" >/dev/null + git -C "$work" remote add origin "$bare" + git -C "$work" push origin main >/dev/null + printf '%s\n' "$bare" +} + +make_subrepo_with_tracked_ignored_file() { + local work="${TEST_ROOT}/subrepo-work" + local bare="${TEST_ROOT}/subrepo.git" + + git init "$work" >/dev/null + git -C "$work" checkout -b main >/dev/null + git -C "$work" config user.name "Test User" + git -C "$work" config user.email "test@test.local" + + mkdir -p "${work}/.dependencies" + printf '.dependencies/\n' > "${work}/.gitignore" + printf 'visible\n' > "${work}/visible.txt" + printf 'tracked even though ignored\n' > "${work}/.dependencies/tracked.txt" + + git -C "$work" add .gitignore visible.txt + git -C "$work" add -f .dependencies/tracked.txt + git -C "$work" commit -m "subrepo with tracked ignored file" >/dev/null + + git init --bare "$bare" >/dev/null + git -C "$work" remote add origin "$bare" + git -C "$work" push origin main >/dev/null + printf '%s\n' "$bare" +} + +mono_auth_url() { + printf '%s\n' "$MONO_BARE" +} + +subrepo_auth_url() { + printf '%s\n' "$SUBREPO_BARE" +} + +create_pr() { + printf '%s\n' "$4" > "${TEST_ROOT}/created-pr-head" +} + +@test "initial_import stages files tracked in subrepo even when ignored by copied .gitignore" { + MONO_BARE="$(make_mono_repo)" + SUBREPO_BARE="$(make_subrepo_with_tracked_ignored_file)" + + local result import_branch + result="$(initial_import)" + import_branch="$(cat "${TEST_ROOT}/created-pr-head")" + + [ "$result" = "pr-created" ] + git --git-dir="$MONO_BARE" ls-tree -r --name-only "$import_branch" \ + | grep -q '^apps/crm/.dependencies/tracked.txt$' +}