From a6ac8f06113fc8c8e4859d26e090576458305ef9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 23 Aug 2025 11:24:13 -0700 Subject: [PATCH 1/5] Cleanup lock files --- modules/gitrepo/cleanup.go | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 modules/gitrepo/cleanup.go diff --git a/modules/gitrepo/cleanup.go b/modules/gitrepo/cleanup.go new file mode 100644 index 0000000000..3b8c62dcaf --- /dev/null +++ b/modules/gitrepo/cleanup.go @@ -0,0 +1,42 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +import ( + "context" + "os" + "path/filepath" + "time" +) + +var lockFiles = []string{ + "config.lock", + "objects/info/commit-graphs/commit-graph-chain.lock", +} + +// CleanupRepo cleans up the repository by removing unnecessary lock files. +func CleanupRepo(ctx context.Context, repo Repository) error { + return CleanFixedFileLocks(ctx, repo, time.Now()) +} + +// CleanFixedFileLocks removes lock files that haven't been modified since the last update. +func CleanFixedFileLocks(ctx context.Context, repo Repository, lastUpdated time.Time) error { + for _, lockFile := range lockFiles { + p := filepath.Join(repoPath(repo), lockFile) + fInfo, err := os.Stat(p) + if err != nil { + if os.IsNotExist(err) { + continue + } + return err + } + + if fInfo.ModTime().Before(lastUpdated) { + if err := os.Remove(p); err != nil { + return err + } + } + } + return nil +} From 88be11d9683e73d2497012cff99223ab06396ffc Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 1 Sep 2025 23:09:52 -0700 Subject: [PATCH 2/5] clean up the lock files created 24 hours ago --- modules/gitrepo/cleanup.go | 2 +- services/cron/tasks_extended.go | 11 ++++++++++ services/repository/cleanup.go | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 services/repository/cleanup.go diff --git a/modules/gitrepo/cleanup.go b/modules/gitrepo/cleanup.go index 3b8c62dcaf..90fd61091e 100644 --- a/modules/gitrepo/cleanup.go +++ b/modules/gitrepo/cleanup.go @@ -17,7 +17,7 @@ var lockFiles = []string{ // CleanupRepo cleans up the repository by removing unnecessary lock files. func CleanupRepo(ctx context.Context, repo Repository) error { - return CleanFixedFileLocks(ctx, repo, time.Now()) + return CleanFixedFileLocks(ctx, repo, time.Now().Add(-24*time.Hour)) } // CleanFixedFileLocks removes lock files that haven't been modified since the last update. diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go index 3747111984..a2cdc1b4c6 100644 --- a/services/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -224,6 +224,16 @@ func registerRebuildIssueIndexer() { }) } +func registerCleanupRepoLockFiles() { + RegisterTaskFatal("cleanup_repo_lock_files", &BaseConfig{ + Enabled: false, + RunAtStart: false, + Schedule: "@every 24h", + }, func(ctx context.Context, _ *user_model.User, config Config) error { + return repo_service.CleanupRepo(ctx) + }) +} + func initExtendedTasks() { registerDeleteInactiveUsers() registerDeleteRepositoryArchives() @@ -239,4 +249,5 @@ func initExtendedTasks() { registerDeleteOldSystemNotices() registerGCLFS() registerRebuildIssueIndexer() + registerCleanupRepoLockFiles() } diff --git a/services/repository/cleanup.go b/services/repository/cleanup.go new file mode 100644 index 0000000000..26abbee408 --- /dev/null +++ b/services/repository/cleanup.go @@ -0,0 +1,36 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "context" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/gitrepo" + "code.gitea.io/gitea/modules/log" +) + +func CleanupRepo(ctx context.Context) error { + log.Trace("Doing: CleanupRepo") + + if err := db.Iterate( + ctx, + nil, + func(ctx context.Context, repo *repo_model.Repository) error { + select { + case <-ctx.Done(): + return db.ErrCancelledf("before cleanup repo lock files for %s", repo.FullName()) + default: + } + return gitrepo.CleanupRepo(ctx, repo) + }, + ); err != nil { + log.Trace("Error: CleanupRepo: %v", err) + return err + } + + log.Trace("Finished: CleanupRepo") + return nil +} From c10c1b32dbf1ed338fc16e3ec95699302d1ff452 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 2 Sep 2025 10:08:45 -0700 Subject: [PATCH 3/5] fix test --- options/locale/locale_en-US.ini | 1 + services/repository/cleanup.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 315241a417..1f69c17cfa 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3072,6 +3072,7 @@ dashboard.sync_branch.started = Branches Sync started dashboard.sync_tag.started = Tags Sync started dashboard.rebuild_issue_indexer = Rebuild issue indexer dashboard.sync_repo_licenses = Sync repo licenses +dashboard.cleanup_repo_lock_files = Clean up repository lock files users.user_manage_panel = User Account Management users.new_account = Create User Account diff --git a/services/repository/cleanup.go b/services/repository/cleanup.go index 26abbee408..46af56052f 100644 --- a/services/repository/cleanup.go +++ b/services/repository/cleanup.go @@ -10,6 +10,8 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" + + "xorm.io/builder" ) func CleanupRepo(ctx context.Context) error { @@ -17,7 +19,7 @@ func CleanupRepo(ctx context.Context) error { if err := db.Iterate( ctx, - nil, + builder.Eq{"is_empty": false}, func(ctx context.Context, repo *repo_model.Repository) error { select { case <-ctx.Done(): From 63777a91adbe5a2e70b7c11e830980410f7060e8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 4 Sep 2025 21:25:12 -0700 Subject: [PATCH 4/5] Fix test --- tests/integration/api_admin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/api_admin_test.go b/tests/integration/api_admin_test.go index d28a103e59..726c071ad2 100644 --- a/tests/integration/api_admin_test.go +++ b/tests/integration/api_admin_test.go @@ -308,7 +308,7 @@ func TestAPICron(t *testing.T) { var crons []api.Cron DecodeJSON(t, resp, &crons) - assert.Len(t, crons, 29) + assert.Len(t, crons, 30) }) t.Run("Execute", func(t *testing.T) { From 73af079998d080cdea66415ccbdb4a44a6839e5e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 5 Sep 2025 12:16:29 -0700 Subject: [PATCH 5/5] Fix test --- tests/integration/api_admin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/api_admin_test.go b/tests/integration/api_admin_test.go index 726c071ad2..1cceacefba 100644 --- a/tests/integration/api_admin_test.go +++ b/tests/integration/api_admin_test.go @@ -304,7 +304,7 @@ func TestAPICron(t *testing.T) { AddTokenAuth(token) resp := MakeRequest(t, req, http.StatusOK) - assert.Equal(t, "29", resp.Header().Get("X-Total-Count")) + assert.Equal(t, "30", resp.Header().Get("X-Total-Count")) var crons []api.Cron DecodeJSON(t, resp, &crons)