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 (#3789) · knative/func@f43bf4c

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
@@ -1353,28 +1353,102 @@ func ensureFuncIgnore(root string) error {
13531353
defer file.Close()
13541354

13551355
// Write the desired string to the file
1356-
_, err = file.WriteString(`
1357-
# Use the .funcignore file to exclude files which should not be
1358-
# tracked in the image build. To instruct the system not to track
1359-
# files in the image build, add the regex pattern or file information
1360-
# to this file.
1356+
_, err = file.WriteString(`# Use the .funcignore file to exclude files from the image build.
1357+
# Excluded files do not affect the rebuild fingerprint.
1358+
#
1359+
# Supported patterns:
1360+
# foo - exclude any file or directory named "foo" at any depth
1361+
# *.tmp - glob; exclude matching files at any depth (except S2I: root only)
1362+
# /docs - root-relative; exclude only the top-level "docs" entry
1363+
# build/ - trailing slash stripped, then matched as a basename
1364+
#
1365+
# Lines starting with # are comments and are ignored.
13611366
`)
13621367
if err != nil {
13631368
return err
13641369
}
13651370
return nil
13661371
}
13671372

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

@@ -1385,9 +1459,15 @@ func Fingerprint(root string) (hash, log string, err error) {
13851459
if path == root {
13861460
return nil
13871461
}
1388-
// Always ignore .func, .git (TODO: .funcignore)
1389-
if info.IsDir() && (info.Name() == RunDataDir || info.Name() == ".git") {
1390-
return filepath.SkipDir
1462+
relPath, err := filepath.Rel(root, path)
1463+
if err != nil {
1464+
return err
1465+
}
1466+
if IsIgnored(relPath, userPatterns) {
1467+
if info.IsDir() {
1468+
return filepath.SkipDir
1469+
}
1470+
return nil
13911471
}
13921472
fmt.Fprintf(h, "%v:%v:", path, info.ModTime().UnixNano()) // Write to the Hasher
13931473
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.