drone/gitrpc/internal/gitea/repo_cache.go
2023-08-10 18:42:10 +02:00

36 lines
799 B
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package gitea
import (
"context"
"time"
"github.com/harness/gitness/cache"
gogit "github.com/go-git/go-git/v5"
)
func NewRepoCache() cache.Cache[string, *RepoEntryValue] {
return cache.New[string, *RepoEntryValue](repoGetter{}, 4*time.Hour)
}
type repoGetter struct{}
type RepoEntryValue gogit.Repository
func (repo *RepoEntryValue) Repo() *gogit.Repository {
return (*gogit.Repository)(repo)
}
func (r repoGetter) Find(_ context.Context, path string) (*RepoEntryValue, error) {
repo, err := gogit.PlainOpen(path)
if err != nil {
return nil, err
}
return (*RepoEntryValue)(repo), nil
}