[PR #236] [MERGED] fix: prevent starred repo name collisions during concurrent mirroring #849

Closed
opened 2026-04-16 02:48:06 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/RayLabsHQ/gitea-mirror/pull/236
Author: @arunavo4
Created: 3/18/2026
Status: Merged
Merged: 3/18/2026
Merged by: @arunavo4

Base: mainHead: fix/95-starred-repo-name-collision


📝 Commits (3)

  • cddb136 fix: prevent starred repo name collisions during concurrent mirroring (#95)
  • 9eac6e9 fix: address review findings for starred repo name collision fix
  • 89c6c9f fix: address P1/P2 review findings for starred repo name collision

📊 Changes

5 files changed (+166 additions, -24 deletions)

View changed files

drizzle/0010_mirrored_location_index.sql (+9 -0)
📝 drizzle/meta/_journal.json (+7 -0)
📝 scripts/validate-migrations.ts (+27 -0)
📝 src/lib/db/schema.ts (+1 -0)
📝 src/lib/gitea.ts (+122 -24)

📄 Description

Summary

Fixes #95 — when multiple starred repos have the same short name but different owners (e.g., alice/dotfiles and bob/dotfiles), only the first one gets mirrored. The second silently fails with a Gitea 409 Conflict.

Root cause

Three collision points: generateUniqueRepoName only checked Gitea (not local DB), concurrent mirroring caused TOCTOU races, and mirroredLocation wasn't cleared on failure — leaving stale claims that confused retries.

Changes

Name collision detection (src/lib/gitea.ts)

  • generateUniqueRepoName now checks both Gitea (HTTP) and local DB (mirroredLocation) for name availability
  • Added isMirroredLocationClaimedInDb helper (fail-closed on DB errors)
  • Added fullName validation guard

Atomic name claiming (drizzle/0010_mirrored_location_index.sql, src/lib/db/schema.ts)

  • Added unique partial index (userId, mirroredLocation) WHERE mirroredLocation != '' — DB enforces that no two repos for the same user can claim the same location
  • Added lookup index for isMirroredLocationClaimedInDb query performance
  • Name claiming only happens at the status="mirroring" DB write (after both idempotency checks), not early in the flow

Smart failure handling (src/lib/gitea.ts)

  • Added migrateSucceeded flag to both mirror functions
  • mirroredLocation only cleared when the Gitea migrate call itself failed (repo doesn't exist in Gitea)
  • Preserved when metadata mirroring fails after successful migrate (repo physically exists, need location for recovery)

Test plan

  • All 171 tests pass (0 failures)
  • Production build succeeds
  • Migration validation passes (new fixture for 0010)
  • Rebased cleanly onto main after PR #235 merge

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/RayLabsHQ/gitea-mirror/pull/236 **Author:** [@arunavo4](https://github.com/arunavo4) **Created:** 3/18/2026 **Status:** ✅ Merged **Merged:** 3/18/2026 **Merged by:** [@arunavo4](https://github.com/arunavo4) **Base:** `main` ← **Head:** `fix/95-starred-repo-name-collision` --- ### 📝 Commits (3) - [`cddb136`](https://github.com/RayLabsHQ/gitea-mirror/commit/cddb136af0fee09613f45601bd3fb326018f727d) fix: prevent starred repo name collisions during concurrent mirroring (#95) - [`9eac6e9`](https://github.com/RayLabsHQ/gitea-mirror/commit/9eac6e998dd21a09d02079bb0f4ab627cdc2e403) fix: address review findings for starred repo name collision fix - [`89c6c9f`](https://github.com/RayLabsHQ/gitea-mirror/commit/89c6c9ff765cd7baf1db294a1ce92844f3a9f56c) fix: address P1/P2 review findings for starred repo name collision ### 📊 Changes **5 files changed** (+166 additions, -24 deletions) <details> <summary>View changed files</summary> ➕ `drizzle/0010_mirrored_location_index.sql` (+9 -0) 📝 `drizzle/meta/_journal.json` (+7 -0) 📝 `scripts/validate-migrations.ts` (+27 -0) 📝 `src/lib/db/schema.ts` (+1 -0) 📝 `src/lib/gitea.ts` (+122 -24) </details> ### 📄 Description ## Summary Fixes #95 — when multiple starred repos have the same short name but different owners (e.g., `alice/dotfiles` and `bob/dotfiles`), only the first one gets mirrored. The second silently fails with a Gitea 409 Conflict. ### Root cause Three collision points: `generateUniqueRepoName` only checked Gitea (not local DB), concurrent mirroring caused TOCTOU races, and `mirroredLocation` wasn't cleared on failure — leaving stale claims that confused retries. ### Changes **Name collision detection** (`src/lib/gitea.ts`) - `generateUniqueRepoName` now checks both Gitea (HTTP) and local DB (`mirroredLocation`) for name availability - Added `isMirroredLocationClaimedInDb` helper (fail-closed on DB errors) - Added `fullName` validation guard **Atomic name claiming** (`drizzle/0010_mirrored_location_index.sql`, `src/lib/db/schema.ts`) - Added unique partial index `(userId, mirroredLocation) WHERE mirroredLocation != ''` — DB enforces that no two repos for the same user can claim the same location - Added lookup index for `isMirroredLocationClaimedInDb` query performance - Name claiming only happens at the `status="mirroring"` DB write (after both idempotency checks), not early in the flow **Smart failure handling** (`src/lib/gitea.ts`) - Added `migrateSucceeded` flag to both mirror functions - `mirroredLocation` only cleared when the Gitea migrate call itself failed (repo doesn't exist in Gitea) - Preserved when metadata mirroring fails after successful migrate (repo physically exists, need location for recovery) ## Test plan - [x] All 171 tests pass (0 failures) - [x] Production build succeeds - [x] Migration validation passes (new fixture for 0010) - [x] Rebased cleanly onto main after PR #235 merge --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-16 02:48:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea-mirror#849