-
Notifications
You must be signed in to change notification settings - Fork 785
/
buildpack.go
106 lines (95 loc) · 2.99 KB
/
buildpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gitresolver
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// InitBuildPack initialises the build pack URL and git ref returning the packs dir or an error
func InitBuildPack(gitter gits.Gitter, packURL string, packRef string) (string, error) {
u, err := url.Parse(strings.TrimSuffix(packURL, ".git"))
if err != nil {
return "", fmt.Errorf("Failed to parse build pack URL: %s: %s", packURL, err)
}
draftDir, err := util.DraftDir()
if err != nil {
return "", err
}
dir := filepath.Join(draftDir, "packs", u.Host, u.Path)
if err := os.MkdirAll(dir, 0755); err != nil {
return "", fmt.Errorf("Could not create %s: %s", dir, err)
}
err = ensureBranchTracksOrigin(dir, packRef, gitter)
if err != nil {
return "", errors.Wrapf(err, "there was a problem ensuring the branch %s has tracking info", packRef)
}
err = gitter.CloneOrPull(packURL, dir)
if err != nil {
return "", err
}
if packRef != "master" && packRef != "" {
err = gitter.FetchTags(dir)
if err != nil {
return "", errors.Wrapf(err, "fetching tags from %s", packURL)
}
tags, err := gitter.FilterTags(dir, packRef)
if err != nil {
return "", errors.Wrapf(err, "filtering tags for %s", packRef)
}
if len(tags) == 0 {
tags, err = gitter.FilterTags(dir, fmt.Sprintf("v%s", packRef))
if err != nil {
return "", errors.Wrapf(err, "filtering tags for v%s", packRef)
}
}
if len(tags) == 1 {
tag := tags[0]
branchName := fmt.Sprintf("tag-%s", tag)
err = gitter.CreateBranchFrom(dir, branchName, tag)
if err != nil {
return "", errors.Wrapf(err, "creating branch %s from %s", branchName, tag)
}
err = gitter.Checkout(dir, branchName)
if err != nil {
return "", errors.Wrapf(err, "checking out branch %s", branchName)
}
} else {
if len(tags) > 1 {
log.Logger().Debugf("more than one tag matched %s or v%s, ignoring tags", packRef, packRef)
}
err = gitter.CheckoutRemoteBranch(dir, packRef)
if err != nil {
return "", errors.Wrapf(err, "checking out tracking branch %s", packRef)
}
}
}
return filepath.Join(dir, "packs"), nil
}
func ensureBranchTracksOrigin(dir string, packRef string, gitter gits.Gitter) error {
empty, err := util.IsEmpty(dir)
if err != nil {
return errors.Wrapf(err, "there was a problem checking if %s is empty", dir)
}
// The repository is cloned, before the pull, we have to make sure we fetch & checkout <packRef> and we are tracking origin/<packRef>
// This is due to a bug happening on old clones done by the old cloning func
if !empty {
err := gitter.FetchBranch(dir, "origin", packRef)
if err != nil {
return err
}
err = gitter.Checkout(dir, packRef)
if err != nil {
return err
}
err = gitter.SetUpstreamTo(dir, packRef)
if err != nil {
return errors.Wrapf(err, "there was a problem setting upstream to remote branch origin/%s", packRef)
}
}
return nil
}