github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/lfs/pointer_scanner_gogit.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  //go:build gogit
     7  
     8  package lfs
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"github.com/gitbundle/modules/git"
    15  
    16  	"github.com/go-git/go-git/v5/plumbing/object"
    17  )
    18  
    19  // SearchPointerBlobs scans the whole repository for LFS pointer files
    20  func SearchPointerBlobs(ctx context.Context, repo *git.Repository, pointerChan chan<- PointerBlob, errChan chan<- error) {
    21  	gitRepo := repo.GoGitRepo()
    22  
    23  	err := func() error {
    24  		blobs, err := gitRepo.BlobObjects()
    25  		if err != nil {
    26  			return fmt.Errorf("lfs.SearchPointerBlobs BlobObjects: %w", err)
    27  		}
    28  
    29  		return blobs.ForEach(func(blob *object.Blob) error {
    30  			select {
    31  			case <-ctx.Done():
    32  				return ctx.Err()
    33  			default:
    34  			}
    35  
    36  			if blob.Size > blobSizeCutoff {
    37  				return nil
    38  			}
    39  
    40  			reader, err := blob.Reader()
    41  			if err != nil {
    42  				return fmt.Errorf("lfs.SearchPointerBlobs blob.Reader: %w", err)
    43  			}
    44  			defer reader.Close()
    45  
    46  			pointer, _ := ReadPointer(reader)
    47  			if pointer.IsValid() {
    48  				pointerChan <- PointerBlob{Hash: blob.Hash.String(), Pointer: pointer}
    49  			}
    50  
    51  			return nil
    52  		})
    53  	}()
    54  	if err != nil {
    55  		select {
    56  		case <-ctx.Done():
    57  		default:
    58  			errChan <- err
    59  		}
    60  	}
    61  
    62  	close(pointerChan)
    63  	close(errChan)
    64  }