github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_base_nogogit.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  // Copyright 2015 The Gogs Authors. All rights reserved.
     7  
     8  //go:build !gogit
     9  
    10  package git
    11  
    12  import (
    13  	"bufio"
    14  	"context"
    15  	"errors"
    16  	"path/filepath"
    17  
    18  	"github.com/gitbundle/modules/log"
    19  )
    20  
    21  // Repository represents a Git repository.
    22  type Repository struct {
    23  	Path string
    24  
    25  	tagCache *ObjectCache
    26  
    27  	gpgSettings *GPGSettings
    28  
    29  	batchCancel context.CancelFunc
    30  	batchReader *bufio.Reader
    31  	batchWriter WriteCloserError
    32  
    33  	checkCancel context.CancelFunc
    34  	checkReader *bufio.Reader
    35  	checkWriter WriteCloserError
    36  
    37  	Ctx context.Context
    38  }
    39  
    40  // openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
    41  func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
    42  	return OpenRepository(DefaultContext, repoPath)
    43  }
    44  
    45  // OpenRepository opens the repository at the given path with the provided context.
    46  func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
    47  	repoPath, err := filepath.Abs(repoPath)
    48  	if err != nil {
    49  		return nil, err
    50  	} else if !isDir(repoPath) {
    51  		return nil, errors.New("no such file or directory")
    52  	}
    53  
    54  	// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
    55  	if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	repo := &Repository{
    60  		Path:     repoPath,
    61  		tagCache: newObjectCache(),
    62  		Ctx:      ctx,
    63  	}
    64  
    65  	repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
    66  	repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
    67  
    68  	return repo, nil
    69  }
    70  
    71  // CatFileBatch obtains a CatFileBatch for this repository
    72  func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
    73  	if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
    74  		log.Debug("Opening temporary cat file batch for: %s", repo.Path)
    75  		return CatFileBatch(ctx, repo.Path)
    76  	}
    77  	return repo.batchWriter, repo.batchReader, func() {}
    78  }
    79  
    80  // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
    81  func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
    82  	if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
    83  		log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
    84  		return CatFileBatchCheck(ctx, repo.Path)
    85  	}
    86  	return repo.checkWriter, repo.checkReader, func() {}
    87  }
    88  
    89  // Close this repository, in particular close the underlying gogitStorage if this is not nil
    90  func (repo *Repository) Close() (err error) {
    91  	if repo == nil {
    92  		return
    93  	}
    94  	if repo.batchCancel != nil {
    95  		repo.batchCancel()
    96  		repo.batchReader = nil
    97  		repo.batchWriter = nil
    98  		repo.batchCancel = nil
    99  	}
   100  	if repo.checkCancel != nil {
   101  		repo.checkCancel()
   102  		repo.checkCancel = nil
   103  		repo.checkReader = nil
   104  		repo.checkWriter = nil
   105  	}
   106  	return
   107  }