Skip to content

fix(git): regenerate missing packfile index to recover orphaned packs#38388

Draft
bircni wants to merge 1 commit into
go-gitea:mainfrom
bircni:orphaned-package
Draft

fix(git): regenerate missing packfile index to recover orphaned packs#38388
bircni wants to merge 1 commit into
go-gitea:mainfrom
bircni:orphaned-package

Conversation

@bircni

@bircni bircni commented Jul 9, 2026

Copy link
Copy Markdown
Member

Fixes #38359.

On Windows, the official Gitea binary is built with the gogit tag, so git objects are read through go-git instead of the git CLI. go-git opens repositories with KeepDescriptors: true (modules/git/repo_base_gogit.go), which keeps every .pack file descriptor open for the lifetime of the cached repository.

When a subsequent git push triggers auto-gc / repack -d, git consolidates packfiles and unlinks the redundant ones. On Windows an open handle blocks deletion — but go-git only holds the .pack open (it opens .idx files transiently and closes them). So git succeeds in deleting the .idx while failing to unlink the still-locked .pack, leaving an orphan packfile with no index.

The next time a fresh go-git storage opens that repository, ObjectStorage.requireIndex() scans for *.pack files and tries to load each .idx. The orphan's missing index returns ErrPackfileNotFound ("packfile not found"), and because requireIndex aborts on the first failure, every object read then fails with a 500 Internal Server Error — on the web UI, the API, and actions (GetBranchCommit, ListBranches, CreateCommitStatusForRunJobs, etc.), exactly as seen in the reported logs.

The reporter's manual workaround (regenerating the missing .idx) is git index-pack under the hood, so this is safe to automate — the packfile still contains all objects, only the index is gone.

@bircni bircni requested a review from lunny July 9, 2026 21:44
@bircni bircni added the backport/v1.27 This PR should be backported to Gitea 1.27 label Jul 9, 2026
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 9, 2026
// pack without its ".idx" makes the whole repository unreadable with "packfile not found".
// This happens on Windows (issue #38359): the gogit storage keeps ".pack" descriptors open,
// so git's repack cleanup can delete the ".idx" while failing to unlink the locked ".pack".
repairOrphanPackIndexes(ctx, gitDirPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean it will be checked for every repository where it is used?

@wxiaoguang

Copy link
Copy Markdown
Contributor

I don't think it's right to keep adding more patches.

There are far more "git repo maintenance" related problems. For example: stale ".lock" files.

Ideally, we need a "maintenance" zone on the repo setting page, let repo owner "fix" the repo in wrong state.

@bircni bircni marked this pull request as draft July 10, 2026 21:10
@bircni bircni removed the backport/v1.27 This PR should be backported to Gitea 1.27 label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repository broken with "500 Internal Server Error" after git push: "packfile not found" due to missing .idx files

4 participants