Skip to content

Commit a6ac8f0

Browse files
committed
Cleanup lock files
1 parent 8a53413 commit a6ac8f0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

modules/gitrepo/cleanup.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
"os"
9+
"path/filepath"
10+
"time"
11+
)
12+
13+
var lockFiles = []string{
14+
"config.lock",
15+
"objects/info/commit-graphs/commit-graph-chain.lock",
16+
}
17+
18+
// CleanupRepo cleans up the repository by removing unnecessary lock files.
19+
func CleanupRepo(ctx context.Context, repo Repository) error {
20+
return CleanFixedFileLocks(ctx, repo, time.Now())
21+
}
22+
23+
// CleanFixedFileLocks removes lock files that haven't been modified since the last update.
24+
func CleanFixedFileLocks(ctx context.Context, repo Repository, lastUpdated time.Time) error {
25+
for _, lockFile := range lockFiles {
26+
p := filepath.Join(repoPath(repo), lockFile)
27+
fInfo, err := os.Stat(p)
28+
if err != nil {
29+
if os.IsNotExist(err) {
30+
continue
31+
}
32+
return err
33+
}
34+
35+
if fInfo.ModTime().Before(lastUpdated) {
36+
if err := os.Remove(p); err != nil {
37+
return err
38+
}
39+
}
40+
}
41+
return nil
42+
}

0 commit comments

Comments
 (0)