code.gitea.io/gitea@v1.19.3/modules/doctor/lfs.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package doctor
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	"code.gitea.io/gitea/modules/log"
    12  	"code.gitea.io/gitea/modules/setting"
    13  	"code.gitea.io/gitea/services/repository"
    14  )
    15  
    16  func init() {
    17  	Register(&Check{
    18  		Title:                      "Garbage collect LFS",
    19  		Name:                       "gc-lfs",
    20  		IsDefault:                  false,
    21  		Run:                        garbageCollectLFSCheck,
    22  		AbortIfFailed:              false,
    23  		SkipDatabaseInitialization: false,
    24  		Priority:                   1,
    25  	})
    26  }
    27  
    28  func garbageCollectLFSCheck(ctx context.Context, logger log.Logger, autofix bool) error {
    29  	if !setting.LFS.StartServer {
    30  		return fmt.Errorf("LFS support is disabled")
    31  	}
    32  
    33  	if err := repository.GarbageCollectLFSMetaObjects(ctx, repository.GarbageCollectLFSMetaObjectsOptions{
    34  		Logger:  logger,
    35  		AutoFix: autofix,
    36  		// Only attempt to garbage collect lfs meta objects older than a week as the order of git lfs upload
    37  		// and git object upload is not necessarily guaranteed. It's possible to imagine a situation whereby
    38  		// an LFS object is uploaded but the git branch is not uploaded immediately, or there are some rapid
    39  		// changes in new branches that might lead to lfs objects becoming temporarily unassociated with git
    40  		// objects.
    41  		//
    42  		// It is likely that a week is potentially excessive but it should definitely be enough that any
    43  		// unassociated LFS object is genuinely unassociated.
    44  		OlderThan: time.Now().Add(-24 * time.Hour * 7),
    45  		// We don't set the UpdatedLessRecentlyThan because we want to do a full GC
    46  	}); err != nil {
    47  		return err
    48  	}
    49  
    50  	return checkStorage(&checkStorageOptions{LFS: true})(ctx, logger, autofix)
    51  }