@@ -1353,28 +1353,102 @@ func ensureFuncIgnore(root string) error { |
1353 | 1353 | defer file.Close() |
1354 | 1354 |
|
1355 | 1355 | // 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. |
1361 | 1366 | `) |
1362 | 1367 | if err != nil { |
1363 | 1368 | return err |
1364 | 1369 | } |
1365 | 1370 | return nil |
1366 | 1371 | } |
1367 | 1372 |
|
| 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 | + |
1368 | 1442 | // Fingerprint the files at a given path. Returns a hash calculated from the |
1369 | 1443 | // filenames and modification timestamps of the files within the given root. |
1370 | 1444 | // Also returns a logfile consisting of the filenames and modification times |
1371 | 1445 | // which contributed to the hash. |
1372 | 1446 | // Intended to determine if there were appreciable changes to a function's |
1373 | 1447 | // 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. |
1377 | 1449 | func Fingerprint(root string) (hash, log string, err error) { |
| 1450 | + userPatterns := ParseFuncIgnore(root) |
| 1451 | + |
1378 | 1452 | h := sha256.New() // Hash builder |
1379 | 1453 | l := bytes.Buffer{} // Log buffer |
1380 | 1454 |
|
@@ -1385,9 +1459,15 @@ func Fingerprint(root string) (hash, log string, err error) { |
1385 | 1459 | if path == root { |
1386 | 1460 | return nil |
1387 | 1461 | } |
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 |
1391 | 1471 | } |
1392 | 1472 | fmt.Fprintf(h, "%v:%v:", path, info.ModTime().UnixNano()) // Write to the Hasher |
1393 | 1473 | fmt.Fprintf(&l, "%v:%v\n", path, info.ModTime().UnixNano()) // Write to the Log |
|