2020-04-01 09:55:42 -05:00
|
|
|
/*
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-06 18:48:43 -06:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-04-06 15:56:39 -05:00
|
|
|
"github.com/go-git/go-billy/v5"
|
|
|
|
"github.com/go-git/go-billy/v5/osfs"
|
|
|
|
"github.com/go-git/go-git/v5"
|
2020-07-27 19:37:04 -05:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2020-04-06 15:56:39 -05:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/cache"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
|
|
"github.com/go-git/go-git/v5/storage"
|
|
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
2019-12-06 18:48:43 -06:00
|
|
|
|
|
|
|
"opendev.org/airship/airshipctl/pkg/log"
|
Split document model, add entrypoints for repos
Add NewBundleByPath function, that would return bundle built from
the specified path argument
Add CurrentContextEntryPoint method of the config
object, that would allow easily get kustomize root path based on
clusterType and phase. You can also leave phase arg empty string,
which would try to return bundle for all phases
Introduce changes to config pakage objects:
- Manifest:
SubPath: this is relative path to the root of the repository that
contains directories with sites (SiteNames)
PrimaryRepositoryName: this is a string that must correspond to a key
of the Repositories map of manifest object, which is used to derive
primary repository
Repositories object is a map, map keys correspond to names of the
directories where `document pull` command will download repositories
defined in manifest prepended by manifest.TargetPath.
Introduce new config method CurrentContextEntryPoint(), method takes
TargetPath, PrimaryRepo.URL, SubPath, and clusterType and phase
constructs a path to the entry point out of which the DocumentBundle
should be build, and returns it to the caller. After that caller can
build a bundle out of it, the bundle will contain documents relevant to
particular cluster-type and phase.
All objects that depend on bundle interface are updated to use the
CurrentContextEntryPoint() method of the config object
Relates-To: #99
Closes: #99
Change-Id: I99320c4cb626841d46f4c298b583e9af90b1dce4
2020-03-06 00:48:50 +00:00
|
|
|
"opendev.org/airship/airshipctl/pkg/util"
|
2019-12-06 18:48:43 -06:00
|
|
|
)
|
|
|
|
|
2020-05-13 10:41:52 -05:00
|
|
|
// OptionsBuilder interface provides specification for a repository implementation
|
2019-12-06 18:48:43 -06:00
|
|
|
type OptionsBuilder interface {
|
|
|
|
ToAuth() (transport.AuthMethod, error)
|
|
|
|
ToCloneOptions(auth transport.AuthMethod) *git.CloneOptions
|
2020-09-17 18:33:18 -05:00
|
|
|
ToCheckoutOptions() *git.CheckoutOptions
|
2019-12-06 18:48:43 -06:00
|
|
|
ToFetchOptions(auth transport.AuthMethod) *git.FetchOptions
|
|
|
|
URL() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repository container holds Filesystem, spec and open repository object
|
|
|
|
// Abstracts git repository and allows for easy cloning, checkout and update of git repos
|
|
|
|
type Repository struct {
|
|
|
|
Driver Adapter
|
|
|
|
OptionsBuilder
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepository create repository object, with real filesystem on disk
|
|
|
|
// basePath is used to calculate final path where to clone/open the repository
|
|
|
|
func NewRepository(basePath string, builder OptionsBuilder) (*Repository, error) {
|
Split document model, add entrypoints for repos
Add NewBundleByPath function, that would return bundle built from
the specified path argument
Add CurrentContextEntryPoint method of the config
object, that would allow easily get kustomize root path based on
clusterType and phase. You can also leave phase arg empty string,
which would try to return bundle for all phases
Introduce changes to config pakage objects:
- Manifest:
SubPath: this is relative path to the root of the repository that
contains directories with sites (SiteNames)
PrimaryRepositoryName: this is a string that must correspond to a key
of the Repositories map of manifest object, which is used to derive
primary repository
Repositories object is a map, map keys correspond to names of the
directories where `document pull` command will download repositories
defined in manifest prepended by manifest.TargetPath.
Introduce new config method CurrentContextEntryPoint(), method takes
TargetPath, PrimaryRepo.URL, SubPath, and clusterType and phase
constructs a path to the entry point out of which the DocumentBundle
should be build, and returns it to the caller. After that caller can
build a bundle out of it, the bundle will contain documents relevant to
particular cluster-type and phase.
All objects that depend on bundle interface are updated to use the
CurrentContextEntryPoint() method of the config object
Relates-To: #99
Closes: #99
Change-Id: I99320c4cb626841d46f4c298b583e9af90b1dce4
2020-03-06 00:48:50 +00:00
|
|
|
dirName := util.GitDirNameFromURL(builder.URL())
|
2019-12-06 18:48:43 -06:00
|
|
|
if dirName == "" {
|
2020-08-28 12:24:24 -07:00
|
|
|
return nil, ErrParseURL{URL: builder.URL()}
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
|
|
|
fs := osfs.New(filepath.Join(basePath, dirName))
|
|
|
|
|
|
|
|
s, err := storerFromFs(fs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This can create
|
|
|
|
return &Repository{
|
|
|
|
Name: dirName,
|
|
|
|
Driver: NewGitDriver(fs, s),
|
|
|
|
OptionsBuilder: builder,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func storerFromFs(fs billy.Filesystem) (storage.Storer, error) {
|
|
|
|
dot, err := fs.Chroot(".git")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return filesystem.NewStorage(dot, cache.NewObjectLRUDefault()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update fetches new refs, and checkout according to checkout options
|
2020-09-17 18:33:18 -05:00
|
|
|
func (repo *Repository) Update() error {
|
2019-12-06 18:48:43 -06:00
|
|
|
log.Debugf("Updating repository %s", repo.Name)
|
|
|
|
if !repo.Driver.IsOpen() {
|
2020-08-28 12:24:24 -07:00
|
|
|
return ErrNoOpenRepo{}
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
|
|
|
auth, err := repo.ToAuth()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = repo.Driver.Fetch(repo.ToFetchOptions(auth))
|
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
2020-02-28 09:30:26 -05:00
|
|
|
return fmt.Errorf("failed to fetch refs for repository %v: %w", repo.Name, err)
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
2020-09-17 18:33:18 -05:00
|
|
|
return repo.Checkout()
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checkout git repository, ToCheckoutOptions method will be used go get CheckoutOptions
|
2020-09-17 18:33:18 -05:00
|
|
|
func (repo *Repository) Checkout() error {
|
2019-12-06 18:48:43 -06:00
|
|
|
if !repo.Driver.IsOpen() {
|
2020-08-28 12:24:24 -07:00
|
|
|
return ErrNoOpenRepo{}
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
2020-09-17 18:33:18 -05:00
|
|
|
co := repo.ToCheckoutOptions()
|
2020-07-27 19:37:04 -05:00
|
|
|
var branchHash string
|
|
|
|
if co.Hash == plumbing.ZeroHash {
|
|
|
|
branchHash = fmt.Sprintf("branch %s", co.Branch.String())
|
|
|
|
} else {
|
|
|
|
branchHash = fmt.Sprintf("commit hash %s", co.Hash.String())
|
|
|
|
}
|
|
|
|
log.Debugf("Attempting to checkout the repository %s from %s", repo.Name, branchHash)
|
2019-12-06 18:48:43 -06:00
|
|
|
tree, err := repo.Driver.Worktree()
|
|
|
|
if err != nil {
|
2020-02-28 09:30:26 -05:00
|
|
|
return fmt.Errorf("could not get worktree from the repo, %w", err)
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
|
|
|
return tree.Checkout(co)
|
|
|
|
}
|
|
|
|
|
2021-07-28 13:05:23 -05:00
|
|
|
// Fetch fetches remote refs
|
|
|
|
func (repo *Repository) Fetch() error {
|
|
|
|
if !repo.Driver.IsOpen() {
|
|
|
|
return ErrNoOpenRepo{}
|
|
|
|
}
|
|
|
|
auth, err := repo.ToAuth()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to build auth options for repository %v: %w", repo.Name, err)
|
|
|
|
}
|
|
|
|
fo := repo.ToFetchOptions(auth)
|
|
|
|
return repo.Driver.Fetch(fo)
|
|
|
|
}
|
|
|
|
|
2019-12-06 18:48:43 -06:00
|
|
|
// Open the repository
|
|
|
|
func (repo *Repository) Open() error {
|
|
|
|
log.Debugf("Attempting to open repository %s", repo.Name)
|
|
|
|
return repo.Driver.Open()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone given repository
|
|
|
|
func (repo *Repository) Clone() error {
|
2020-07-27 19:37:04 -05:00
|
|
|
log.Debugf("Attempting to clone the repository %s from %s", repo.Name, repo.URL())
|
2019-12-06 18:48:43 -06:00
|
|
|
auth, err := repo.ToAuth()
|
|
|
|
if err != nil {
|
2020-02-28 09:30:26 -05:00
|
|
|
return fmt.Errorf("failed to build auth options for repository %v: %w", repo.Name, err)
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return repo.Driver.Clone(repo.ToCloneOptions(auth))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download will clone and checkout repository based on auth and checkout fields of the Repository object
|
|
|
|
// If repository is already cloned, it will be opened and checked out to configured hash,branch,tag etc...
|
|
|
|
// no remotes will be modified in this case, also no refs will be updated.
|
|
|
|
// enforce parameter is used to simulate git reset --hard option.
|
|
|
|
// If you want to enforce state of the repository, please delete current git repository before downloading.
|
2020-09-17 18:33:18 -05:00
|
|
|
func (repo *Repository) Download(noCheckout bool) error {
|
2019-12-06 18:48:43 -06:00
|
|
|
log.Debugf("Attempting to download the repository %s", repo.Name)
|
|
|
|
|
|
|
|
if !repo.Driver.IsOpen() {
|
|
|
|
err := repo.Clone()
|
|
|
|
if err == git.ErrRepositoryAlreadyExists {
|
|
|
|
openErr := repo.Open()
|
|
|
|
if openErr != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 18:33:18 -05:00
|
|
|
if noCheckout {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-28 13:05:23 -05:00
|
|
|
|
|
|
|
err := repo.Fetch()
|
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
|
|
|
return fmt.Errorf("failed to fetch refs for repository %v: %w", repo.Name, err)
|
|
|
|
}
|
|
|
|
|
2020-09-17 18:33:18 -05:00
|
|
|
return repo.Checkout()
|
2019-12-06 18:48:43 -06:00
|
|
|
}
|