cmd/pkg: Extract 'extra-sources' and adjust hash calculation

If the build.yml specifies 'extra-sources', ie sources
outside the package directory, calculate the hash based on
the tree hash of all source directories and the package
directory.

Note, this requires the source directories to be under
git revision control.

Also clean up the src and dst of the path and stash the
result in the Pkg structure.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2018-07-24 15:25:15 +01:00
parent 75149c56c2
commit ba4d1c79b0

View File

@ -1,11 +1,13 @@
package pkglib package pkglib
import ( import (
"crypto/sha1"
"flag" "flag"
"fmt" "fmt"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -177,6 +179,37 @@ func NewFromCLI(fs *flag.FlagSet, args ...string) (Pkg, error) {
} }
}) })
var srcHashes string
sources := []pkgSource{{src: pkgPath, dst: "/"}}
for _, source := range pi.ExtraSources {
tmp := strings.Split(source, ":")
if len(tmp) != 2 {
return Pkg{}, fmt.Errorf("Bad source format in %s", source)
}
srcPath := filepath.Clean(tmp[0]) // Should work with windows paths
dstPath := path.Clean(tmp[1]) // 'path' here because this should be a Unix path
if !filepath.IsAbs(srcPath) {
srcPath = filepath.Join(pkgPath, srcPath)
}
g, err := newGit(srcPath)
if err != nil {
return Pkg{}, err
}
if g == nil {
return Pkg{}, fmt.Errorf("Source %s not in a git repository", srcPath)
}
h, err := g.treeHash(srcPath, hashCommit)
if err != nil {
return Pkg{}, err
}
srcHashes += h
sources = append(sources, pkgSource{src: srcPath, dst: dstPath})
}
git, err := newGit(pkgPath) git, err := newGit(pkgPath)
if err != nil { if err != nil {
return Pkg{}, err return Pkg{}, err
@ -195,6 +228,11 @@ func NewFromCLI(fs *flag.FlagSet, args ...string) (Pkg, error) {
return Pkg{}, err return Pkg{}, err
} }
if srcHashes != "" {
hash += srcHashes
hash = fmt.Sprintf("%x", sha1.Sum([]byte(hash)))
}
if dirty { if dirty {
hash += "-dirty" hash += "-dirty"
} }
@ -207,7 +245,7 @@ func NewFromCLI(fs *flag.FlagSet, args ...string) (Pkg, error) {
hash: hash, hash: hash,
commitHash: hashCommit, commitHash: hashCommit,
arches: pi.Arches, arches: pi.Arches,
sources: pi.Sources, sources: sources,
gitRepo: pi.GitRepo, gitRepo: pi.GitRepo,
network: pi.Network, network: pi.Network,
trust: !pi.DisableContentTrust, trust: !pi.DisableContentTrust,