-
Notifications
You must be signed in to change notification settings - Fork 15
/
package_tree.go
161 lines (139 loc) · 5.3 KB
/
package_tree.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2022 The kpt and Nephio 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 git
import (
"context"
"fmt"
"path"
"time"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/nephio-project/porch/api/porch/v1alpha1"
"k8s.io/klog/v2"
)
// packageList holds a list of packages in the git repository
type packageList struct {
// parent is the gitRepository of which this is part
parent *gitRepository
// commit is the commit at which we scanned for packages
commit *object.Commit
// packages holds the packages we found
packages map[string]*packageListEntry
}
// packageListEntry is a single package found in a git repository
type packageListEntry struct {
// parent is the packageList of which we are part
parent *packageList
// path is the relative path to the root of the package (directory containing the Kptfile)
path string
// treeHash is the git-hash of the git tree corresponding to Path
treeHash plumbing.Hash
}
// buildGitPackageRevision creates a gitPackageRevision for the packageListEntry
// TODO: Can packageListEntry just _be_ a gitPackageRevision?
func (p *packageListEntry) buildGitPackageRevision(ctx context.Context, revision string, workspace v1alpha1.WorkspaceName, ref *plumbing.Reference) (*gitPackageRevision, error) {
repo := p.parent.parent
tasks, err := repo.loadTasks(ctx, p.parent.commit, p.path, workspace)
if err != nil {
return nil, err
}
var updated time.Time
var updatedBy string
// For the published packages on a tag or draft and proposed branches we know that the latest commit
// if specific to the package in question. Thus, we can just take the last commit on the tag/branch.
// If the ref is nil, we consider the package as being final and on the package branch.
if ref != nil && (isTagInLocalRepo(ref.Name()) || isDraftBranchNameInLocal(ref.Name()) || isProposedBranchNameInLocal(ref.Name())) {
updated = p.parent.commit.Author.When
updatedBy = p.parent.commit.Author.Email
} else {
// If we are on the package branch, we can not assume that the last commit
// pertains to the package in question. So we scan the git history to find
// the last commit for the package based on the porch commit tags. We don't
// use the revision here, since we are looking at the package branch while
// the revisions only helps identify the tags.
commit, err := repo.findLatestPackageCommit(ctx, p.parent.commit, p.path)
if err != nil {
return nil, err
}
if commit != nil {
updated = commit.Author.When
updatedBy = commit.Author.Email
}
// If not commit was found with the porch commit tags, we don't really
// know who approved the package or when it happend. We could find this
// by scanning the tree for every commit, but that is a pretty expensive
// operation.
}
// for backwards compatibility with packages that existed before porch supported
// workspaceNames, we populate the workspaceName as the revision number if it is empty
if workspace == "" {
workspace = v1alpha1.WorkspaceName(revision)
}
return &gitPackageRevision{
repo: repo,
path: p.path,
workspaceName: workspace,
revision: revision,
updated: updated,
updatedBy: updatedBy,
ref: ref,
tree: p.treeHash,
commit: p.parent.commit.Hash,
tasks: tasks,
}, nil
}
// DiscoveryPackagesOptions holds the configuration for walking a git tree
type DiscoverPackagesOptions struct {
// FilterPrefix restricts package discovery to a particular subdirectory.
// The subdirectory is not required to exist (we will return an empty list of packages).
FilterPrefix string
// Recurse enables recursive traversal of the git tree.
Recurse bool
}
// discoverPackages is the recursive function we use to traverse the tree and find packages.
// tree is the git-tree we are search, treePath is the repo-relative-path to tree.
func (t *packageList) discoverPackages(tree *object.Tree, treePath string, recurse bool) error {
for _, e := range tree.Entries {
if e.Name == "Kptfile" {
p := path.Join(treePath, e.Name)
if !e.Mode.IsRegular() {
klog.Warningf("skipping %q: Kptfile is not a file", p)
continue
}
// Found a package
t.packages[treePath] = &packageListEntry{
path: treePath,
treeHash: tree.Hash,
parent: t,
}
}
}
if recurse {
for _, e := range tree.Entries {
if e.Mode != filemode.Dir {
continue
}
// This is safe because this function is only called holding the mutex in gitRepository
dirTree, err := t.parent.repo.TreeObject(e.Hash)
if err != nil {
return fmt.Errorf("error getting git tree %v: %w", e.Hash, err)
}
if err := t.discoverPackages(dirTree, path.Join(treePath, e.Name), recurse); err != nil {
return err
}
}
}
return nil
}