code.gitea.io/gitea@v1.22.3/modules/git/repo_base_nogogit.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  //go:build !gogit
     6  
     7  package git
     8  
     9  import (
    10  	"bufio"
    11  	"context"
    12  	"path/filepath"
    13  
    14  	"code.gitea.io/gitea/modules/log"
    15  	"code.gitea.io/gitea/modules/util"
    16  )
    17  
    18  const isGogit = false
    19  
    20  // Repository represents a Git repository.
    21  type Repository struct {
    22  	Path string
    23  
    24  	tagCache *ObjectCache
    25  
    26  	gpgSettings *GPGSettings
    27  
    28  	batchInUse bool
    29  	batch      *Batch
    30  
    31  	checkInUse bool
    32  	check      *Batch
    33  
    34  	Ctx             context.Context
    35  	LastCommitCache *LastCommitCache
    36  
    37  	objectFormat ObjectFormat
    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, util.NewNotExistErrorf("no such file or directory")
    52  	}
    53  
    54  	return &Repository{
    55  		Path:     repoPath,
    56  		tagCache: newObjectCache(),
    57  		Ctx:      ctx,
    58  	}, nil
    59  }
    60  
    61  // CatFileBatch obtains a CatFileBatch for this repository
    62  func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
    63  	if repo.batch == nil {
    64  		var err error
    65  		repo.batch, err = repo.NewBatch(ctx)
    66  		if err != nil {
    67  			return nil, nil, nil, err
    68  		}
    69  	}
    70  
    71  	if !repo.batchInUse {
    72  		repo.batchInUse = true
    73  		return repo.batch.Writer, repo.batch.Reader, func() {
    74  			repo.batchInUse = false
    75  		}, nil
    76  	}
    77  
    78  	log.Debug("Opening temporary cat file batch for: %s", repo.Path)
    79  	tempBatch, err := repo.NewBatch(ctx)
    80  	if err != nil {
    81  		return nil, nil, nil, err
    82  	}
    83  	return tempBatch.Writer, tempBatch.Reader, tempBatch.Close, nil
    84  }
    85  
    86  // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
    87  func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
    88  	if repo.check == nil {
    89  		var err error
    90  		repo.check, err = repo.NewBatchCheck(ctx)
    91  		if err != nil {
    92  			return nil, nil, nil, err
    93  		}
    94  	}
    95  
    96  	if !repo.checkInUse {
    97  		repo.checkInUse = true
    98  		return repo.check.Writer, repo.check.Reader, func() {
    99  			repo.checkInUse = false
   100  		}, nil
   101  	}
   102  
   103  	log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
   104  	tempBatchCheck, err := repo.NewBatchCheck(ctx)
   105  	if err != nil {
   106  		return nil, nil, nil, err
   107  	}
   108  	return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil
   109  }
   110  
   111  func (repo *Repository) Close() error {
   112  	if repo == nil {
   113  		return nil
   114  	}
   115  	if repo.batch != nil {
   116  		repo.batch.Close()
   117  		repo.batch = nil
   118  		repo.batchInUse = false
   119  	}
   120  	if repo.check != nil {
   121  		repo.check.Close()
   122  		repo.check = nil
   123  		repo.checkInUse = false
   124  	}
   125  	repo.LastCommitCache = nil
   126  	repo.tagCache = nil
   127  	return nil
   128  }