@@ -1334,28 +1334,102 @@ func ensureFuncIgnore(root string) error { |
1334 | 1334 | defer file.Close() |
1335 | 1335 |
|
1336 | 1336 | // 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. |
1342 | 1347 | `) |
1343 | 1348 | if err != nil { |
1344 | 1349 | return err |
1345 | 1350 | } |
1346 | 1351 | return nil |
1347 | 1352 | } |
1348 | 1353 |
|
| 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 | + |
1349 | 1423 | // Fingerprint the files at a given path. Returns a hash calculated from the |
1350 | 1424 | // filenames and modification timestamps of the files within the given root. |
1351 | 1425 | // Also returns a logfile consisting of the filenames and modification times |
1352 | 1426 | // which contributed to the hash. |
1353 | 1427 | // Intended to determine if there were appreciable changes to a function's |
1354 | 1428 | // 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. |
1358 | 1430 | func Fingerprint(root string) (hash, log string, err error) { |
| 1431 | + userPatterns := ParseFuncIgnore(root) |
| 1432 | + |
1359 | 1433 | h := sha256.New() // Hash builder |
1360 | 1434 | l := bytes.Buffer{} // Log buffer |
1361 | 1435 |
|
@@ -1366,9 +1440,15 @@ func Fingerprint(root string) (hash, log string, err error) { |
1366 | 1440 | if path == root { |
1367 | 1441 | return nil |
1368 | 1442 | } |
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 |
1372 | 1452 | } |
1373 | 1453 | fmt.Fprintf(h, "%v:%v:", path, info.ModTime().UnixNano()) // Write to the Hasher |
1374 | 1454 | fmt.Fprintf(&l, "%v:%v\n", path, info.ModTime().UnixNano()) // Write to the Log |
|