GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: apply .funcignore rules in Fingerprint and OCI builder · knative/func@0f26d68

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkg
    • buildpacks
      • builder.go
      • builder_test.go
    • functions
      • client.go
      • client_test.go
    • oci
      • builder.go
      • builder_test.go
    • s2i
      • builder.go
      • builder_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkg/buildpacks/builder.go‎

Copy file name to clipboardExpand all lines: pkg/buildpacks/builder.go
+3-18Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"os"
109
"path/filepath"
1110
"regexp"
1211
"runtime"
@@ -142,23 +141,9 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
142141
buildpacks = defaultBuildpacks[f.Runtime]
143142
}
144143

145-
// Reading .funcignore file
146-
var excludes []string
147-
filePath := filepath.Join(f.Root, ".funcignore")
148-
file, err := os.Open(filePath)
149-
if err != nil {
150-
if !os.IsNotExist(err) {
151-
return fmt.Errorf("\nfailed to open file: %s", err)
152-
}
153-
} else {
154-
defer file.Close()
155-
buf := new(bytes.Buffer)
156-
_, err := io.Copy(buf, file)
157-
if err != nil {
158-
return fmt.Errorf("\nfailed to read file: %s", err)
159-
}
160-
excludes = strings.Split(buf.String(), "\n")
161-
}
144+
// Reading .funcignore file (user patterns only — pack handles its own
145+
// build context and needs access to directories like .func/build)
146+
excludes := fn.ParseFuncIgnore(f.Root)
162147
// Pack build options
163148
opts := pack.BuildOptions{
164149
GroupID: -1,
Collapse file

‎pkg/buildpacks/builder_test.go‎

Copy file name to clipboardExpand all lines: pkg/buildpacks/builder_test.go
+23-23Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,45 +145,45 @@ func TestBuild_BuilderImageConfigurable(t *testing.T) {
145145
}
146146
}
147147

148-
// TestBuild_BuilderImageExclude ensures that ignored files are not added to the func
149-
// image
150-
func TestBuild_BuilderImageExclude(t *testing.T) {
148+
// TestBuild_BuilderImageExcludePatterns verifies that all supported
149+
// .funcignore pattern forms are correctly passed to pack's Exclude option.
150+
func TestBuild_BuilderImageExcludePatterns(t *testing.T) {
151151
root, done := Mktemp(t)
152152
defer done()
153153

154154
var (
155-
i = &mockImpl{} // mock underlying implementation
156-
b = NewBuilder( // Func Builder logic
157-
WithName(builders.Pack), WithImpl(i))
158-
f = fn.Function{
159-
Runtime: "go",
160-
Root: root,
161-
Registry: "example.com/alice",
162-
}
155+
i = &mockImpl{}
156+
b = NewBuilder(WithName(builders.Pack), WithImpl(i))
157+
f = fn.Function{Runtime: "go", Root: root, Registry: "example.com/alice"}
163158
err error
164159
)
165160

166-
// Initialize the function to create proper source files
167161
if f, err = fn.New().Init(f); err != nil {
168162
t.Fatal(err)
169163
}
170164

171-
funcIgnoreContent := []byte(`#testing comments
172-
hello.txt`)
173-
expected := []string{"hello.txt"}
174-
175-
//create a .funcignore file containing the details of the files to be ignored
176-
err = os.WriteFile(filepath.Join(f.Root, ".funcignore"), funcIgnoreContent, 0644)
177-
if err != nil {
165+
content := []byte("# comment stripped\nnotes.txt\n*.tmp\n/docs\ndist/\n")
166+
if err = os.WriteFile(filepath.Join(f.Root, ".funcignore"), content, 0644); err != nil {
178167
t.Fatal(err)
179168
}
180169

181170
i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error {
182-
if len(opts.ProjectDescriptor.Build.Exclude) != 2 {
183-
t.Fatalf("expected 2 lines of exclusions , got %v", len(opts.ProjectDescriptor.Build.Exclude))
171+
excludes := opts.ProjectDescriptor.Build.Exclude
172+
// 4 user patterns: notes.txt, *.tmp, /docs, dist/ (comment stripped)
173+
if len(excludes) != 4 {
174+
t.Fatalf("expected 4 exclusions, got %v: %v", len(excludes), excludes)
175+
}
176+
want := map[string]bool{"notes.txt": true, "*.tmp": true, "/docs": true, "dist/": true}
177+
for _, e := range excludes {
178+
if !want[e] {
179+
t.Errorf("unexpected exclusion: %q", e)
180+
}
184181
}
185-
if opts.ProjectDescriptor.Build.Exclude[1] != expected[0] {
186-
t.Fatalf("expected excluded file to be '%v', got '%v'", expected[1], opts.ProjectDescriptor.Build.Exclude[1])
182+
// Verify comment was stripped
183+
for _, e := range excludes {
184+
if len(e) > 0 && e[0] == '#' {
185+
t.Errorf("comment line in excludes: %q", e)
186+
}
187187
}
188188
return nil
189189
}
Collapse file

‎pkg/functions/client.go‎

Copy file name to clipboardExpand all lines: pkg/functions/client.go
+91-11Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,28 +1334,102 @@ func ensureFuncIgnore(root string) error {
13341334
defer file.Close()
13351335

13361336
// Write the desired string to the file
1337-
_, err = file.WriteString(`
1338-
# Use the .funcignore file to exclude files which should not be
1339-
# tracked in the image build. To instruct the system not to track
1340-
# files in the image build, add the regex pattern or file information
1341-
# to this file.
1337+
_, err = file.WriteString(`# Use the .funcignore file to exclude files from the image build.
1338+
# Excluded files do not affect the rebuild fingerprint.
1339+
#
1340+
# Supported patterns:
1341+
# foo - exclude any file or directory named "foo" at any depth
1342+
# *.tmp - glob; exclude matching files at any depth (except S2I: root only)
1343+
# /docs - root-relative; exclude only the top-level "docs" entry
1344+
# build/ - trailing slash stripped, then matched as a basename
1345+
#
1346+
# Lines starting with # are comments and are ignored.
13421347
`)
13431348
if err != nil {
13441349
return err
13451350
}
13461351
return nil
13471352
}
13481353

1354+
// DefaultIgnored contains entries that are always ignored regardless of
1355+
// .funcignore file contents.
1356+
var DefaultIgnored = []string{
1357+
".git",
1358+
".func",
1359+
".funcignore",
1360+
".gitignore",
1361+
}
1362+
1363+
// ParseFuncIgnore reads .funcignore and returns patterns with comments
1364+
// and blank lines stripped.
1365+
func ParseFuncIgnore(root string) []string {
1366+
f, err := os.Open(filepath.Join(root, ".funcignore"))
1367+
if err != nil {
1368+
return nil
1369+
}
1370+
defer f.Close()
1371+
1372+
var patterns []string
1373+
scanner := bufio.NewScanner(f)
1374+
for scanner.Scan() {
1375+
line := strings.TrimSpace(scanner.Text())
1376+
if line == "" || strings.HasPrefix(line, "#") {
1377+
continue
1378+
}
1379+
patterns = append(patterns, line)
1380+
}
1381+
return patterns
1382+
}
1383+
1384+
// IsIgnored reports whether a path should be skipped based on the
1385+
// default ignored entries and user-defined patterns from .funcignore.
1386+
// Four pattern forms are supported:
1387+
//
1388+
// "/foo" — root-relative; matches that exact path and everything beneath it.
1389+
// "foo/" — trailing slash stripped, then matched as basename.
1390+
// "*.tmp" — glob; matched against basename and full relative path (filepath.Match semantics).
1391+
// "foo" — basename; matches any entry named "foo" at any depth.
1392+
//
1393+
// Used by Fingerprint and the OCI builder. Known differences from other builders:
1394+
// - Pack (go-gitignore): supports "**" recursive globs and "!" negation — this does not.
1395+
// - S2I (filepath.Glob): non-recursive — "*.tmp" only matches at root level, this matches at any depth.
1396+
func IsIgnored(relPath string, userPatterns []string) bool {
1397+
base := filepath.Base(relPath)
1398+
for _, d := range DefaultIgnored {
1399+
if base == d {
1400+
return true
1401+
}
1402+
}
1403+
slashRel := filepath.ToSlash(relPath)
1404+
for _, pattern := range userPatterns {
1405+
if strings.HasPrefix(pattern, "/") {
1406+
p := pattern[1:]
1407+
if slashRel == p || strings.HasPrefix(slashRel, p+"/") {
1408+
return true
1409+
}
1410+
continue
1411+
}
1412+
pattern = strings.TrimSuffix(pattern, "/")
1413+
if matched, _ := filepath.Match(pattern, base); matched {
1414+
return true
1415+
}
1416+
if matched, _ := filepath.Match(pattern, relPath); matched {
1417+
return true
1418+
}
1419+
}
1420+
return false
1421+
}
1422+
13491423
// Fingerprint the files at a given path. Returns a hash calculated from the
13501424
// filenames and modification timestamps of the files within the given root.
13511425
// Also returns a logfile consisting of the filenames and modification times
13521426
// which contributed to the hash.
13531427
// Intended to determine if there were appreciable changes to a function's
13541428
// source code, certain directories and files are ignored, such as
1355-
// .git and .func.
1356-
// Future updates will include files explicitly marked as ignored by a
1357-
// .funcignore.
1429+
// .git and .func. User-defined patterns from .funcignore are also respected.
13581430
func Fingerprint(root string) (hash, log string, err error) {
1431+
userPatterns := ParseFuncIgnore(root)
1432+
13591433
h := sha256.New() // Hash builder
13601434
l := bytes.Buffer{} // Log buffer
13611435

@@ -1366,9 +1440,15 @@ func Fingerprint(root string) (hash, log string, err error) {
13661440
if path == root {
13671441
return nil
13681442
}
1369-
// Always ignore .func, .git (TODO: .funcignore)
1370-
if info.IsDir() && (info.Name() == RunDataDir || info.Name() == ".git") {
1371-
return filepath.SkipDir
1443+
relPath, err := filepath.Rel(root, path)
1444+
if err != nil {
1445+
return err
1446+
}
1447+
if IsIgnored(relPath, userPatterns) {
1448+
if info.IsDir() {
1449+
return filepath.SkipDir
1450+
}
1451+
return nil
13721452
}
13731453
fmt.Fprintf(h, "%v:%v:", path, info.ModTime().UnixNano()) // Write to the Hasher
13741454
fmt.Fprintf(&l, "%v:%v\n", path, info.ModTime().UnixNano()) // Write to the Log

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.