mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2026-02-02 09:25:08 +01:00
Merge remote-tracking branch 'origin/master' into git-clone-optim
# Conflicts: # pkg/git.go
This commit is contained in:
commit
926830713e
13 changed files with 363 additions and 146 deletions
48
pkg/git.go
48
pkg/git.go
|
|
@ -33,6 +33,7 @@ import (
|
|||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GitPackage struct {
|
||||
|
|
@ -146,13 +147,21 @@ func gzipUntar(dst string, r io.Reader, subDir string) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVersion string, err error) {
|
||||
func (p *GitPackage) Install(ctx context.Context, name, dir, version string) (string, error) {
|
||||
destPath := path.Join(dir, name)
|
||||
|
||||
tmpDir, err := ioutil.TempDir(filepath.Join(dir, ".tmp"), fmt.Sprintf("jsonnetpkg-%s-%s", name, version))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to create tmp dir")
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Optimization for GitHub sources: download a tarball archive of the requested
|
||||
// version instead of cloning the entire repository. Resolves the version to a
|
||||
// commit SHA using the GitHub API.
|
||||
if strings.HasPrefix(p.Source.Remote, "https://github.com/") {
|
||||
archiveUrl := fmt.Sprintf("%s/archive/%s.tar.gz", p.Source.Remote, version)
|
||||
archiveFilepath := fmt.Sprintf("%s.tar.gz", dir)
|
||||
archiveFilepath := fmt.Sprintf("%s.tar.gz", tmpDir)
|
||||
|
||||
defer os.Remove(archiveFilepath)
|
||||
commitSha, err := downloadGitHubArchive(archiveFilepath, archiveUrl)
|
||||
|
|
@ -160,7 +169,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
return "", err
|
||||
}
|
||||
r, err := os.Open(archiveFilepath)
|
||||
err = gzipUntar(dir, r, p.Source.Subdir)
|
||||
err = gzipUntar(tmpDir, r, p.Source.Subdir)
|
||||
return commitSha, nil
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +177,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -178,7 +187,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -189,7 +198,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
// Fall back to normal fetch (all revisions)
|
||||
|
|
@ -197,7 +206,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -211,20 +220,20 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
glob := []byte(p.Source.Subdir + "/*\n")
|
||||
ioutil.WriteFile(dir+"/.git/info/sparse-checkout", glob, 0644)
|
||||
ioutil.WriteFile(tmpDir+"/.git/info/sparse-checkout", glob, 0644)
|
||||
}
|
||||
|
||||
cmd = exec.CommandContext(ctx, "git", "-c", "advice.detachedHead=false", "checkout", version)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -233,7 +242,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
b := bytes.NewBuffer(nil)
|
||||
cmd = exec.CommandContext(ctx, "git", "rev-parse", "HEAD")
|
||||
cmd.Stdout = b
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -241,10 +250,25 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
|
||||
commitHash := strings.TrimSpace(b.String())
|
||||
|
||||
err = os.RemoveAll(path.Join(dir, ".git"))
|
||||
err = os.RemoveAll(path.Join(tmpDir, ".git"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(path.Dir(destPath), os.ModePerm)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to create parent path")
|
||||
}
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
|
||||
err = os.Rename(path.Join(tmpDir, p.Source.Subdir), destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to move package")
|
||||
}
|
||||
|
||||
return commitHash, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,5 +19,5 @@ import (
|
|||
)
|
||||
|
||||
type Interface interface {
|
||||
Install(ctx context.Context, dir, version string) (lockVersion string, err error)
|
||||
Install(ctx context.Context, name, dir, version string) (lockVersion string, err error)
|
||||
}
|
||||
|
|
|
|||
56
pkg/local.go
Normal file
56
pkg/local.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2018 jsonnet-bundler authors
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// http://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.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type LocalPackage struct {
|
||||
Source *spec.LocalSource
|
||||
}
|
||||
|
||||
func NewLocalPackage(source *spec.LocalSource) Interface {
|
||||
return &LocalPackage{
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *LocalPackage) Install(ctx context.Context, name, dir, version string) (lockVersion string, err error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get current working directory: %v", err)
|
||||
}
|
||||
|
||||
destPath := filepath.Join(dir, name)
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
|
||||
err = os.Symlink(filepath.Join(wd, p.Source.Directory), filepath.Join(wd, destPath))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create symlink for local dependency: %v", err)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
|
@ -18,19 +18,17 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
JsonnetFile = "jsonnetfile.json"
|
||||
JsonnetLockFile = "jsonnetfile.lock.json"
|
||||
VersionMismatch = errors.New("multiple colliding versions specified")
|
||||
)
|
||||
|
||||
|
|
@ -43,20 +41,16 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create general tmp dir")
|
||||
}
|
||||
tmpDir, err := ioutil.TempDir(tmp, fmt.Sprintf("jsonnetpkg-%s-%s", dep.Name, dep.Version))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create tmp dir")
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
subdir := ""
|
||||
var p Interface
|
||||
if dep.Source.GitSource != nil {
|
||||
p = NewGitPackage(dep.Source.GitSource)
|
||||
subdir = dep.Source.GitSource.Subdir
|
||||
}
|
||||
if dep.Source.LocalSource != nil {
|
||||
p = NewLocalPackage(dep.Source.LocalSource)
|
||||
}
|
||||
|
||||
lockVersion, err := p.Install(ctx, tmpDir, dep.Version)
|
||||
lockVersion, err := p.Install(ctx, dep.Name, dir, dep.Version)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to install package")
|
||||
}
|
||||
|
|
@ -65,20 +59,6 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
|
||||
destPath := path.Join(dir, dep.Name)
|
||||
|
||||
err = os.MkdirAll(path.Dir(destPath), os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create parent path")
|
||||
}
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
err = os.Rename(path.Join(tmpDir, subdir), destPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to move package")
|
||||
}
|
||||
|
||||
lockfile.Dependencies, err = insertDependency(lockfile.Dependencies, spec.Dependency{
|
||||
Name: dep.Name,
|
||||
Source: dep.Source,
|
||||
|
|
@ -101,7 +81,7 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
return nil, err
|
||||
}
|
||||
depsDeps, err := LoadJsonnetfile(filepath)
|
||||
// It is ok for depedencies not to have a JsonnetFile, it just means
|
||||
// It is ok for dependencies not to have a JsonnetFile, it just means
|
||||
// they do not have transitive dependencies of their own.
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
|
|
@ -161,18 +141,18 @@ func FileExists(path string) (bool, error) {
|
|||
}
|
||||
|
||||
func ChooseJsonnetFile(dir string) (string, bool, error) {
|
||||
lockfile := path.Join(dir, JsonnetLockFile)
|
||||
jsonnetfile := path.Join(dir, JsonnetFile)
|
||||
filename := lockfile
|
||||
lockfilePath := path.Join(dir, jsonnetfile.LockFile)
|
||||
jsonnetfilePath := path.Join(dir, jsonnetfile.File)
|
||||
filename := lockfilePath
|
||||
isLock := true
|
||||
|
||||
lockExists, err := FileExists(filepath.Join(dir, JsonnetLockFile))
|
||||
lockExists, err := FileExists(filepath.Join(dir, jsonnetfile.LockFile))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if !lockExists {
|
||||
filename = jsonnetfile
|
||||
filename = jsonnetfilePath
|
||||
isLock = false
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
|
@ -110,7 +111,7 @@ func TestLoadJsonnetfile(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, JsonnetFile)
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(`{}`), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
|
@ -128,7 +129,7 @@ func TestLoadJsonnetfile(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, JsonnetFile)
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(jsonnetfileContent), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue