Skip to content

Commit 2111741

Browse files
authored
Add doctor command to write commit-graphs (#20007)
This PR adds a doctor command to write the commit-graphs for the repositories: `gitea doctor --run check-commit-graphs --fix` Signed-off-by: Andrew Thornton <[email protected]>
1 parent 95383b7 commit 2111741

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

modules/doctor/mergebase.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func iteratePRs(ctx context.Context, repo *repo_model.Repository, each func(*rep
3030
}
3131

3232
func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) error {
33+
if err := git.InitOnceWithSync(ctx); err != nil {
34+
return err
35+
}
3336
numRepos := 0
3437
numPRs := 0
3538
numPRsUpdated := 0

modules/doctor/misc.go

+76
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,75 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err
189189
return nil
190190
}
191191

192+
func checkCommitGraph(ctx context.Context, logger log.Logger, autofix bool) error {
193+
if err := git.InitOnceWithSync(ctx); err != nil {
194+
return err
195+
}
196+
197+
numRepos := 0
198+
numNeedUpdate := 0
199+
numWritten := 0
200+
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
201+
numRepos++
202+
203+
commitGraphExists := func() (bool, error) {
204+
// Check commit-graph exists
205+
commitGraphFile := path.Join(repo.RepoPath(), `objects/info/commit-graph`)
206+
isExist, err := util.IsExist(commitGraphFile)
207+
if err != nil {
208+
logger.Error("Unable to check if %s exists. Error: %v", commitGraphFile, err)
209+
return false, err
210+
}
211+
212+
if !isExist {
213+
commitGraphsDir := path.Join(repo.RepoPath(), `objects/info/commit-graphs`)
214+
isExist, err = util.IsExist(commitGraphsDir)
215+
if err != nil {
216+
logger.Error("Unable to check if %s exists. Error: %v", commitGraphsDir, err)
217+
return false, err
218+
}
219+
}
220+
return isExist, nil
221+
}
222+
223+
isExist, err := commitGraphExists()
224+
if err != nil {
225+
return err
226+
}
227+
if !isExist {
228+
numNeedUpdate++
229+
if autofix {
230+
if err := git.WriteCommitGraph(ctx, repo.RepoPath()); err != nil {
231+
logger.Error("Unable to write commit-graph in %s. Error: %v", repo.FullName(), err)
232+
return err
233+
}
234+
isExist, err := commitGraphExists()
235+
if err != nil {
236+
return err
237+
}
238+
if isExist {
239+
numWritten++
240+
logger.Info("Commit-graph written: %s", repo.FullName())
241+
} else {
242+
logger.Warn("No commit-graph written: %s", repo.FullName())
243+
}
244+
}
245+
}
246+
return nil
247+
}); err != nil {
248+
logger.Critical("Unable to checkCommitGraph: %v", err)
249+
return err
250+
}
251+
252+
if autofix {
253+
logger.Info("Wrote commit-graph files for %d of %d repositories.", numWritten, numRepos)
254+
} else {
255+
logger.Info("Checked %d repositories, %d without commit-graphs.", numRepos, numNeedUpdate)
256+
}
257+
258+
return nil
259+
}
260+
192261
func init() {
193262
Register(&Check{
194263
Title: "Check if SCRIPT_TYPE is available",
@@ -225,4 +294,11 @@ func init() {
225294
Run: checkDaemonExport,
226295
Priority: 8,
227296
})
297+
Register(&Check{
298+
Title: "Check commit-graphs",
299+
Name: "check-commit-graphs",
300+
IsDefault: false,
301+
Run: checkCommitGraph,
302+
Priority: 9,
303+
})
228304
}

0 commit comments

Comments
 (0)