|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + gogit "github.com/go-git/go-git/v6" |
| 10 | + "github.com/go-git/go-git/v6/plumbing" |
| 11 | + "github.com/go-git/go-git/v6/plumbing/protocol/packp" |
| 12 | + "github.com/go-git/go-git/v6/plumbing/transport" |
| 13 | + "github.com/go-git/go-git/v6/storage/memory" |
| 14 | + |
| 15 | + corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1" |
| 16 | +) |
| 17 | + |
| 18 | +const commitSHARegex = `^[a-f0-9]{40}$` |
| 19 | + |
| 20 | +var commitSHAValidator = regexp.MustCompile(commitSHARegex) |
| 21 | + |
| 22 | +func (r *remoteGitResolver) ResolveByCloning(auth transport.AuthMethod, sourceConfig corev1alpha1.SourceConfig) (corev1alpha1.ResolvedSourceConfig, error) { |
| 23 | + // git clone |
| 24 | + repository, err := gogit.Clone(memory.NewStorage(), nil, &gogit.CloneOptions{ |
| 25 | + URL: sourceConfig.Git.URL, |
| 26 | + Auth: auth, |
| 27 | + RemoteName: defaultRemote, |
| 28 | + ReferenceName: plumbing.ReferenceName(sourceConfig.Git.Revision), |
| 29 | + Depth: 1, |
| 30 | + Bare: true, |
| 31 | + Filter: packp.FilterBlobNone(), |
| 32 | + }) |
| 33 | + |
| 34 | + var resolvedRef *plumbing.Reference |
| 35 | + var hash plumbing.Hash |
| 36 | + var kind corev1alpha1.GitSourceKind |
| 37 | + errs := []error{} |
| 38 | + |
| 39 | + for _, resolver := range resolvers { |
| 40 | + resolvedRef, hash, kind, err = resolver(repository, sourceConfig.Git.Revision) |
| 41 | + if err != nil { |
| 42 | + errs = append(errs, err) |
| 43 | + } |
| 44 | + if resolvedRef != nil { |
| 45 | + break |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if resolvedRef == nil { |
| 50 | + return corev1alpha1.ResolvedSourceConfig{ |
| 51 | + Git: &corev1alpha1.ResolvedGitSource{ |
| 52 | + URL: sourceConfig.Git.URL, |
| 53 | + Revision: sourceConfig.Git.Revision, |
| 54 | + Type: corev1alpha1.Unknown, |
| 55 | + SubPath: sourceConfig.SubPath, |
| 56 | + InitializeSubmodules: sourceConfig.Git.InitializeSubmodules, |
| 57 | + }, |
| 58 | + }, fmt.Errorf("revision \"%s\": unable to fetch references for repository: %w", sourceConfig.Git.Revision, errors.Join(errs...)) |
| 59 | + } |
| 60 | + |
| 61 | + return corev1alpha1.ResolvedSourceConfig{ |
| 62 | + Git: &corev1alpha1.ResolvedGitSource{ |
| 63 | + URL: sourceConfig.Git.URL, |
| 64 | + Revision: hash.String(), |
| 65 | + Tree: hashOfSubpath(sourceConfig.SubPath, hash, repository), |
| 66 | + Type: kind, |
| 67 | + SubPath: sourceConfig.SubPath, |
| 68 | + InitializeSubmodules: sourceConfig.Git.InitializeSubmodules, |
| 69 | + }, |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func hashOfSubpath(subPath string, hash plumbing.Hash, repository *gogit.Repository) string { |
| 74 | + path := strings.Trim(subPath, "/") |
| 75 | + if path == "" { |
| 76 | + return "" |
| 77 | + } |
| 78 | + |
| 79 | + commit, err := repository.CommitObject(hash) |
| 80 | + if err != nil { |
| 81 | + return "" |
| 82 | + } |
| 83 | + |
| 84 | + treeObject, err := commit.Tree() |
| 85 | + if err != nil { |
| 86 | + return "" |
| 87 | + } |
| 88 | + |
| 89 | + entry, err := treeObject.FindEntry(path) |
| 90 | + if err != nil { |
| 91 | + return "" |
| 92 | + } |
| 93 | + |
| 94 | + return entry.Hash.String() |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +type resolverFunc func(repository *gogit.Repository, revision string) (*plumbing.Reference, plumbing.Hash, corev1alpha1.GitSourceKind, error) |
| 99 | + |
| 100 | +var resolvers = []resolverFunc{resolveBranch, resolveTag, resolveRevision, looksLikeACommit} |
| 101 | + |
| 102 | +func resolveBranch(repository *gogit.Repository, branch string) (*plumbing.Reference, plumbing.Hash, corev1alpha1.GitSourceKind, error) { |
| 103 | + resolvedBranch, err := repository.Branch(branch) |
| 104 | + if err != nil { |
| 105 | + return nil, plumbing.Hash{}, corev1alpha1.Unknown, err |
| 106 | + } |
| 107 | + |
| 108 | + resolvedRef := plumbing.NewSymbolicReference(plumbing.ReferenceName(branch), plumbing.ReferenceName(resolvedBranch.Merge)) |
| 109 | + h, err := repository.ResolveRevision(plumbing.Revision(resolvedBranch.Merge)) |
| 110 | + if err != nil { |
| 111 | + return nil, plumbing.Hash{}, corev1alpha1.Unknown, err |
| 112 | + } |
| 113 | + |
| 114 | + return resolvedRef, *h, corev1alpha1.Branch, nil |
| 115 | +} |
| 116 | + |
| 117 | +func resolveTag(repository *gogit.Repository, tag string) (*plumbing.Reference, plumbing.Hash, corev1alpha1.GitSourceKind, error) { |
| 118 | + resolvedTag, err := repository.Tag(tag) |
| 119 | + if err != nil { |
| 120 | + return nil, plumbing.Hash{}, corev1alpha1.Unknown, err |
| 121 | + } |
| 122 | + |
| 123 | + return resolvedTag, resolvedTag.Hash(), corev1alpha1.Tag, nil |
| 124 | +} |
| 125 | + |
| 126 | +func resolveRevision(repository *gogit.Repository, revision string) (*plumbing.Reference, plumbing.Hash, corev1alpha1.GitSourceKind, error) { |
| 127 | + h := plumbing.NewHash(revision) |
| 128 | + _, err := repository.Object(plumbing.AnyObject, h) |
| 129 | + if err != nil { |
| 130 | + return nil, plumbing.Hash{}, corev1alpha1.Unknown, err |
| 131 | + } |
| 132 | + |
| 133 | + return plumbing.NewHashReference(plumbing.ReferenceName(revision), h), h, corev1alpha1.Commit, nil |
| 134 | +} |
| 135 | + |
| 136 | +func looksLikeACommit(_ *gogit.Repository, revision string) (*plumbing.Reference, plumbing.Hash, corev1alpha1.GitSourceKind, error) { |
| 137 | + if !commitSHAValidator.MatchString(revision) { |
| 138 | + return nil, plumbing.Hash{}, corev1alpha1.Unknown, nil |
| 139 | + } |
| 140 | + |
| 141 | + hash := plumbing.NewHash(revision) |
| 142 | + |
| 143 | + return plumbing.NewHashReference(plumbing.ReferenceName(revision), hash), hash, corev1alpha1.Commit, nil |
| 144 | +} |
0 commit comments